xref: /netbsd/games/dab/ttyscrn.cc (revision 97d5545f)
1 /*	$NetBSD: ttyscrn.cc,v 1.6 2021/12/05 09:22:45 rillig Exp $	*/
2 
3 /*-
4  * Copyright (c) 2003 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Christos Zoulas.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * Curses screen implementation for dots
34  */
35 
36 #include "defs.h"
37 RCSID("$NetBSD: ttyscrn.cc,v 1.6 2021/12/05 09:22:45 rillig Exp $")
38 
39 #include <stdio.h>
40 #include <curses.h>
41 #include <sys/ioctl.h>
42 
43 #include "player.h"
44 #include "ttyscrn.h"
45 
clean(void)46 void TTYSCRN::clean(void)
47 {
48     clear();
49 }
50 
moveto(size_t y,size_t x)51 void TTYSCRN::moveto(size_t y, size_t x)
52 {
53     move(y + TTYSCRN::offsy, x + TTYSCRN::offsx);
54 }
55 
addsym(const int sym)56 void TTYSCRN::addsym(const int sym)
57 {
58     addch(sym);
59 }
60 
addedge(const int sym)61 void TTYSCRN::addedge(const int sym)
62 {
63     int nsym;
64 #ifdef A_ALTCHARSET
65     if (_acs) {
66 	switch (sym) {
67 	case GS_HLINE:
68 	    nsym = ACS_HLINE;
69 	    break;
70 	case GS_VLINE:
71 	    nsym = ACS_VLINE;
72 	    break;
73 	case GS_ULCORNER:
74 	    nsym = ACS_ULCORNER;
75 	    break;
76 	case GS_URCORNER:
77 	    nsym = ACS_URCORNER;
78 	    break;
79 	case GS_LLCORNER:
80 	    nsym = ACS_LLCORNER;
81 	    break;
82 	case GS_LRCORNER:
83 	    nsym = ACS_LRCORNER;
84 	    break;
85 	case GS_LTEE:
86 	    nsym = ACS_LTEE;
87 	    break;
88 	case GS_RTEE:
89 	    nsym = ACS_RTEE;
90 	    break;
91 	case GS_TTEE:
92 	    nsym = ACS_TTEE;
93 	    break;
94 	case GS_BTEE:
95 	    nsym = ACS_BTEE;
96 	    break;
97 	case GS_PLUS:
98 	    nsym = ACS_PLUS;
99 	    break;
100 	case ' ':
101 	    addsym(' ');
102 	    return;
103 	default:
104 	    ::abort();
105 	}
106 	attron(A_ALTCHARSET);
107 	addch(nsym);
108 	attroff(A_ALTCHARSET);
109 	return;
110     }
111 #endif
112     switch (sym) {
113     case GS_HLINE:
114 	nsym = '-';
115 	break;
116     case GS_VLINE:
117 	nsym = '|';
118 	break;
119     case GS_ULCORNER:
120 	nsym = '.';
121 	break;
122     case GS_URCORNER:
123 	nsym = '.';
124 	break;
125     case GS_LLCORNER:
126 	nsym = '.';
127 	break;
128     case GS_LRCORNER:
129 	nsym = '.';
130 	break;
131     case GS_LTEE:
132 	nsym = '.';
133 	break;
134     case GS_RTEE:
135 	nsym = '.';
136 	break;
137     case GS_TTEE:
138 	nsym = '.';
139 	break;
140     case GS_BTEE:
141 	nsym = '.';
142 	break;
143     case GS_PLUS:
144 	nsym = '+';
145 	break;
146     case ' ':
147 	addsym(' ');
148 	return;
149     default:
150 	::abort();
151     }
152     addsym(nsym);
153 }
154 
redraw(void)155 void TTYSCRN::redraw(void)
156 {
157     refresh();
158     doupdate();
159 }
160 
bell(void)161 void TTYSCRN::bell(void)
162 {
163     putc('\007', stdout);
164 }
165 
getinput(void)166 int TTYSCRN::getinput(void)
167 {
168     return getch();
169 }
170 
score(size_t s,const PLAYER & p)171 void TTYSCRN::score(size_t s, const PLAYER& p)
172 {
173     mvwprintw(stdscr, _sy + s + TTYSCRN::offsscore, _sx, "S %c:%5zd", p.getWho(),
174 	      p.getScore());
175 }
176 
total(size_t s,const PLAYER & p)177 void TTYSCRN::total(size_t s, const PLAYER& p)
178 {
179     mvwprintw(stdscr, _sy + s + TTYSCRN::offstotal, _sx, "T %c:%5zd", p.getWho(),
180 	      p.getTotal());
181 }
182 
games(size_t s,const PLAYER & p)183 void TTYSCRN::games(size_t s, const PLAYER& p)
184 {
185     mvwprintw(stdscr, _sy + s + TTYSCRN::offsgames, _sx, "G %c:%5zd", p.getWho(),
186 	      p.getGames());
187 }
188 
ties(const PLAYER & p)189 void TTYSCRN::ties(const PLAYER& p)
190 {
191     mvwprintw(stdscr, _sy + TTYSCRN::offsties, _sx, "G =:%5zd", p.getTies());
192 }
193 
create(int acs,size_t * y,size_t * x)194 TTYSCRN* TTYSCRN::create(int acs, size_t *y, size_t *x)
195 {
196     int tx, ty;
197 
198     initscr();
199 
200     tx = getmaxx(stdscr);
201     ty = getmaxy(stdscr);
202 
203     if (tx == ERR || ty == ERR
204 	|| static_cast<size_t>(tx) < *x * 2 + TTYSCRN::offsx + 14
205 	|| static_cast<size_t>(ty) < *y * 2 + TTYSCRN::offsy) {
206 	endwin();
207 	return NULL;
208     }
209     if (*x == 0)
210 	*x = (tx - 14 - TTYSCRN::offsx) / 2;
211     if (*y == 0)
212 	*y = (ty - TTYSCRN::offsy) / 2;
213     cbreak();
214     noecho();
215 
216 
217     TTYSCRN* that = new TTYSCRN;
218 
219     that->_tx = tx;
220     that->_ty = ty;
221     that->_sx = tx - 12;
222     that->_sy = TTYSCRN::offsy;
223     that->_acs = acs;
224 
225     return that;
226 }
227 
~TTYSCRN(void)228 TTYSCRN::~TTYSCRN(void)
229 {
230     nocbreak();
231     echo();
232     endwin();
233 }
234