games: Massive style(9) cleanup commit. Reduces differences to NetBSD.
[dragonfly.git] / games / hunt / hunt / display.c
blob34d610eb4b09b413092fc9f28e06c484c882b84b
1 /*-
2 * Display abstraction.
3 * David Leonard <d@openbsd.org>, 1999. Public domain.
5 * $OpenBSD: display.c,v 1.4 2002/02/19 19:39:36 millert Exp $
6 * $DragonFly: src/games/hunt/hunt/display.c,v 1.2 2008/09/04 16:12:51 swildner Exp $
7 */
9 #define USE_CURSES
11 #include <sys/cdefs.h>
12 #include "display.h"
14 #if !defined(USE_CURSES)
16 #include <stdlib.h>
17 #include <unistd.h>
18 #include <signal.h>
19 #include <stdio.h>
20 #include <termios.h>
21 #define _USE_OLD_CURSES_
22 #include <curses.h>
23 #include <err.h>
24 #include "hunt.h"
26 static struct termios saved_tty;
28 char screen[SCREEN_HEIGHT][SCREEN_WIDTH2];
29 char blanks[SCREEN_WIDTH];
30 int cur_row, cur_col;
33 * tstp:
34 * Handle stop and start signals
36 static void
37 tstp(int dummy)
39 int y, x;
41 y = cur_row;
42 x = cur_col;
43 mvcur(cur_row, cur_col, HEIGHT, 0);
44 cur_row = HEIGHT;
45 cur_col = 0;
46 _puts(VE);
47 _puts(TE);
48 (void) fflush(stdout);
49 tcsetattr(0, TCSADRAIN, &__orig_termios);
50 (void) kill(getpid(), SIGSTOP);
51 (void) signal(SIGTSTP, tstp);
52 tcsetattr(0, TCSADRAIN, &saved_tty);
53 _puts(TI);
54 _puts(VS);
55 cur_row = y;
56 cur_col = x;
57 _puts(tgoto(CM, cur_row, cur_col));
58 display_redraw_screen();
59 (void) fflush(stdout);
63 * display_open:
64 * open the display
66 void
67 display_open(void)
69 char *term;
71 if (!isatty(0) || (term = getenv("TERM")) == NULL)
72 errx(1, "no terminal type");
74 gettmode();
75 (void) setterm(term);
76 (void) noecho();
77 (void) cbreak();
78 tcgetattr(0, &saved_tty);
79 _puts(TI);
80 _puts(VS);
81 #ifdef SIGTSTP
82 (void) signal(SIGTSTP, tstp);
83 #endif
87 * display_beep:
88 * beep
90 void
91 display_beep(void)
93 (void) putchar('\a');
97 * display_refresh:
98 * sync the display
100 void
101 display_refresh(void)
103 (void) fflush(stdout);
107 * display_clear_eol:
108 * clear to end of line, without moving cursor
110 void
111 display_clear_eol(void)
113 if (CE != NULL)
114 tputs(CE, 1, __cputchar);
115 else {
116 fwrite(blanks, sizeof (char), SCREEN_WIDTH - cur_col, stdout);
117 if (COLS != SCREEN_WIDTH)
118 mvcur(cur_row, SCREEN_WIDTH, cur_row, cur_col);
119 else if (AM)
120 mvcur(cur_row + 1, 0, cur_row, cur_col);
121 else
122 mvcur(cur_row, SCREEN_WIDTH - 1, cur_row, cur_col);
124 memcpy(&screen[cur_row][cur_col], blanks, SCREEN_WIDTH - cur_col);
128 * display_putchar:
129 * put one character on the screen, move the cursor right one,
130 * with wraparound
132 void
133 display_put_ch(char ch)
135 if (!isprint(ch)) {
136 fprintf(stderr, "r,c,ch: %d,%d,%d", cur_row, cur_col, ch);
137 return;
139 screen[cur_row][cur_col] = ch;
140 putchar(ch);
141 if (++cur_col >= COLS) {
142 if (!AM || XN)
143 putchar('\n');
144 cur_col = 0;
145 if (++cur_row >= LINES)
146 cur_row = LINES;
151 * display_put_str:
152 * put a string of characters on the screen
154 void
155 display_put_str(const char *s)
157 for( ; *s; s++)
158 display_put_ch(*s);
162 * display_clear_the_screen:
163 * clear the screen; move cursor to top left
165 void
166 display_clear_the_screen(void)
168 int i;
170 if (blanks[0] == '\0')
171 for (i = 0; i < SCREEN_WIDTH; i++)
172 blanks[i] = ' ';
174 if (CL != NULL) {
175 tputs(CL, LINES, __cputchar);
176 for (i = 0; i < SCREEN_HEIGHT; i++)
177 memcpy(screen[i], blanks, SCREEN_WIDTH);
178 } else {
179 for (i = 0; i < SCREEN_HEIGHT; i++) {
180 mvcur(cur_row, cur_col, i, 0);
181 cur_row = i;
182 cur_col = 0;
183 display_clear_eol();
185 mvcur(cur_row, cur_col, 0, 0);
187 cur_row = cur_col = 0;
191 * display_move:
192 * move the cursor
194 void
195 display_move(int y, int x)
197 mvcur(cur_row, cur_col, y, x);
198 cur_row = y;
199 cur_col = x;
203 * display_getyx:
204 * locate the cursor
206 void
207 display_getyx(int *yp, int *xp)
209 *xp = cur_col;
210 *yp = cur_row;
214 * display_end:
215 * close the display
217 void
218 display_end(void)
220 tcsetattr(0, TCSADRAIN, &__orig_termios);
221 _puts(VE);
222 _puts(TE);
226 * display_atyx:
227 * return a character from the screen
229 char
230 display_atyx(int y, int x)
232 return screen[y][x];
236 * display_redraw_screen:
237 * redraw the screen
239 void
240 display_redraw_screen(void)
242 int i;
244 mvcur(cur_row, cur_col, 0, 0);
245 for (i = 0; i < SCREEN_HEIGHT - 1; i++) {
246 fwrite(screen[i], sizeof (char), SCREEN_WIDTH, stdout);
247 if (COLS > SCREEN_WIDTH || (COLS == SCREEN_WIDTH && !AM))
248 putchar('\n');
250 fwrite(screen[SCREEN_HEIGHT - 1], sizeof (char), SCREEN_WIDTH - 1,
251 stdout);
252 mvcur(SCREEN_HEIGHT - 1, SCREEN_WIDTH - 1, cur_row, cur_col);
255 #else /* CURSES */ /* --------------------------------------------------- */
257 #include <curses.h>
258 #include "hunt.h"
260 void
261 display_open(void)
263 initscr();
264 (void) noecho();
265 (void) cbreak();
268 void
269 display_beep(void)
271 beep();
274 void
275 display_refresh(void)
277 refresh();
280 void
281 display_clear_eol(void)
283 clrtoeol();
286 void
287 display_put_ch(char c)
289 addch(c);
292 void
293 display_put_str(const char *s)
295 addstr(s);
298 void
299 display_clear_the_screen(void)
301 clear();
302 move(0, 0);
303 display_refresh();
306 void
307 display_move(int y, int x)
309 move(y, x);
312 void
313 display_getyx(int *yp, int *xp)
315 getyx(stdscr, *yp, *xp);
318 void
319 display_end(void)
321 endwin();
324 char
325 display_atyx(int y, int x)
327 int oy, ox;
328 char c;
330 display_getyx(&oy, &ox);
331 c = mvwinch(stdscr, y, x) & 0x7f;
332 display_move(oy, ox);
333 return (c);
336 void
337 display_redraw_screen(void)
339 clearok(stdscr, TRUE);
340 touchwin(stdscr);
344 display_iserasechar(char ch)
346 return ch == erasechar();
350 display_iskillchar(char ch)
352 return ch == killchar();
355 #endif