games: Remove (void) casts.
[dragonfly.git] / games / hunt / hunt / display.c
blob735384983814df768344205ae56abb62f1a95637
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 */
8 #define USE_CURSES
10 #include "display.h"
12 #if !defined(USE_CURSES)
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include <signal.h>
17 #include <stdio.h>
18 #include <termios.h>
19 #define _USE_OLD_CURSES_
20 #include <curses.h>
21 #include <err.h>
22 #include "hunt.h"
24 static struct termios saved_tty;
26 char screen[SCREEN_HEIGHT][SCREEN_WIDTH2];
27 char blanks[SCREEN_WIDTH];
28 int cur_row, cur_col;
31 * tstp:
32 * Handle stop and start signals
34 static void
35 tstp(int dummy)
37 int y, x;
39 y = cur_row;
40 x = cur_col;
41 mvcur(cur_row, cur_col, HEIGHT, 0);
42 cur_row = HEIGHT;
43 cur_col = 0;
44 _puts(VE);
45 _puts(TE);
46 fflush(stdout);
47 tcsetattr(0, TCSADRAIN, &__orig_termios);
48 kill(getpid(), SIGSTOP);
49 signal(SIGTSTP, tstp);
50 tcsetattr(0, TCSADRAIN, &saved_tty);
51 _puts(TI);
52 _puts(VS);
53 cur_row = y;
54 cur_col = x;
55 _puts(tgoto(CM, cur_row, cur_col));
56 display_redraw_screen();
57 fflush(stdout);
61 * display_open:
62 * open the display
64 void
65 display_open(void)
67 char *term;
69 if (!isatty(0) || (term = getenv("TERM")) == NULL)
70 errx(1, "no terminal type");
72 gettmode();
73 setterm(term);
74 noecho();
75 cbreak();
76 tcgetattr(0, &saved_tty);
77 _puts(TI);
78 _puts(VS);
79 #ifdef SIGTSTP
80 signal(SIGTSTP, tstp);
81 #endif
85 * display_beep:
86 * beep
88 void
89 display_beep(void)
91 putchar('\a');
95 * display_refresh:
96 * sync the display
98 void
99 display_refresh(void)
101 fflush(stdout);
105 * display_clear_eol:
106 * clear to end of line, without moving cursor
108 void
109 display_clear_eol(void)
111 if (CE != NULL)
112 tputs(CE, 1, __cputchar);
113 else {
114 fwrite(blanks, sizeof (char), SCREEN_WIDTH - cur_col, stdout);
115 if (COLS != SCREEN_WIDTH)
116 mvcur(cur_row, SCREEN_WIDTH, cur_row, cur_col);
117 else if (AM)
118 mvcur(cur_row + 1, 0, cur_row, cur_col);
119 else
120 mvcur(cur_row, SCREEN_WIDTH - 1, cur_row, cur_col);
122 memcpy(&screen[cur_row][cur_col], blanks, SCREEN_WIDTH - cur_col);
126 * display_putchar:
127 * put one character on the screen, move the cursor right one,
128 * with wraparound
130 void
131 display_put_ch(char ch)
133 if (!isprint(ch)) {
134 fprintf(stderr, "r,c,ch: %d,%d,%d", cur_row, cur_col, ch);
135 return;
137 screen[cur_row][cur_col] = ch;
138 putchar(ch);
139 if (++cur_col >= COLS) {
140 if (!AM || XN)
141 putchar('\n');
142 cur_col = 0;
143 if (++cur_row >= LINES)
144 cur_row = LINES;
149 * display_put_str:
150 * put a string of characters on the screen
152 void
153 display_put_str(const char *s)
155 for( ; *s; s++)
156 display_put_ch(*s);
160 * display_clear_the_screen:
161 * clear the screen; move cursor to top left
163 void
164 display_clear_the_screen(void)
166 int i;
168 if (blanks[0] == '\0')
169 for (i = 0; i < SCREEN_WIDTH; i++)
170 blanks[i] = ' ';
172 if (CL != NULL) {
173 tputs(CL, LINES, __cputchar);
174 for (i = 0; i < SCREEN_HEIGHT; i++)
175 memcpy(screen[i], blanks, SCREEN_WIDTH);
176 } else {
177 for (i = 0; i < SCREEN_HEIGHT; i++) {
178 mvcur(cur_row, cur_col, i, 0);
179 cur_row = i;
180 cur_col = 0;
181 display_clear_eol();
183 mvcur(cur_row, cur_col, 0, 0);
185 cur_row = cur_col = 0;
189 * display_move:
190 * move the cursor
192 void
193 display_move(int y, int x)
195 mvcur(cur_row, cur_col, y, x);
196 cur_row = y;
197 cur_col = x;
201 * display_getyx:
202 * locate the cursor
204 void
205 display_getyx(int *yp, int *xp)
207 *xp = cur_col;
208 *yp = cur_row;
212 * display_end:
213 * close the display
215 void
216 display_end(void)
218 tcsetattr(0, TCSADRAIN, &__orig_termios);
219 _puts(VE);
220 _puts(TE);
224 * display_atyx:
225 * return a character from the screen
227 char
228 display_atyx(int y, int x)
230 return screen[y][x];
234 * display_redraw_screen:
235 * redraw the screen
237 void
238 display_redraw_screen(void)
240 int i;
242 mvcur(cur_row, cur_col, 0, 0);
243 for (i = 0; i < SCREEN_HEIGHT - 1; i++) {
244 fwrite(screen[i], sizeof (char), SCREEN_WIDTH, stdout);
245 if (COLS > SCREEN_WIDTH || (COLS == SCREEN_WIDTH && !AM))
246 putchar('\n');
248 fwrite(screen[SCREEN_HEIGHT - 1], sizeof (char), SCREEN_WIDTH - 1,
249 stdout);
250 mvcur(SCREEN_HEIGHT - 1, SCREEN_WIDTH - 1, cur_row, cur_col);
253 #else /* CURSES */ /* --------------------------------------------------- */
255 #include <curses.h>
256 #include "hunt.h"
258 void
259 display_open(void)
261 initscr();
262 noecho();
263 cbreak();
266 void
267 display_beep(void)
269 beep();
272 void
273 display_refresh(void)
275 refresh();
278 void
279 display_clear_eol(void)
281 clrtoeol();
284 void
285 display_put_ch(char c)
287 addch(c);
290 void
291 display_put_str(const char *s)
293 addstr(s);
296 void
297 display_clear_the_screen(void)
299 clear();
300 move(0, 0);
301 display_refresh();
304 void
305 display_move(int y, int x)
307 move(y, x);
310 void
311 display_getyx(int *yp, int *xp)
313 getyx(stdscr, *yp, *xp);
316 void
317 display_end(void)
319 endwin();
322 char
323 display_atyx(int y, int x)
325 int oy, ox;
326 char c;
328 display_getyx(&oy, &ox);
329 c = mvwinch(stdscr, y, x) & 0x7f;
330 display_move(oy, ox);
331 return (c);
334 void
335 display_redraw_screen(void)
337 clearok(stdscr, TRUE);
338 touchwin(stdscr);
342 display_iserasechar(char ch)
344 return ch == erasechar();
348 display_iskillchar(char ch)
350 return ch == killchar();
353 #endif