Bring hunt in from OpenBSD. The best multi-player terminal game ever!
[games.git] / games / hunt / hunt / display.c
1 /*
2  * Display abstraction.
3  * David Leonard <d@openbsd.org>, 1999. Public domain.
4  *
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.1 2008/09/02 21:50:20 dillon Exp $
7  */
8
9 #define USE_CURSES
10
11 #include <sys/cdefs.h>
12 #include "display.h"
13
14 #if !defined(USE_CURSES)
15
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"
25
26 static struct termios saved_tty;
27
28 char    screen[SCREEN_HEIGHT][SCREEN_WIDTH2];
29 char    blanks[SCREEN_WIDTH];
30 int     cur_row, cur_col;
31
32 /*
33  * tstp:
34  *      Handle stop and start signals
35  */
36 static void
37 tstp(dummy)
38         int dummy;
39 {
40         int     y, x;
41
42         y = cur_row;
43         x = cur_col;
44         mvcur(cur_row, cur_col, HEIGHT, 0);
45         cur_row = HEIGHT;
46         cur_col = 0;
47         _puts(VE);
48         _puts(TE);
49         (void) fflush(stdout);
50         tcsetattr(0, TCSADRAIN, &__orig_termios);
51         (void) kill(getpid(), SIGSTOP);
52         (void) signal(SIGTSTP, tstp);
53         tcsetattr(0, TCSADRAIN, &saved_tty);
54         _puts(TI);
55         _puts(VS);
56         cur_row = y;
57         cur_col = x;
58         _puts(tgoto(CM, cur_row, cur_col));
59         display_redraw_screen();
60         (void) fflush(stdout);
61 }
62
63 /*
64  * display_open:
65  *      open the display
66  */
67 void
68 display_open()
69 {
70         char *term;
71
72         if (!isatty(0) || (term = getenv("TERM")) == NULL)
73                 errx(1, "no terminal type");
74
75         gettmode();
76         (void) setterm(term);
77         (void) noecho();  
78         (void) cbreak();
79         tcgetattr(0, &saved_tty);
80         _puts(TI);
81         _puts(VS);
82 #ifdef SIGTSTP
83         (void) signal(SIGTSTP, tstp);
84 #endif
85 }
86
87 /*
88  * display_beep:
89  *      beep
90  */
91 void
92 display_beep()
93 {
94         (void) putchar('\a');
95 }
96
97 /*
98  * display_refresh:
99  *      sync the display
100  */
101 void
102 display_refresh()
103 {
104         (void) fflush(stdout);
105 }
106
107 /*
108  * display_clear_eol:
109  *      clear to end of line, without moving cursor
110  */
111 void
112 display_clear_eol()
113 {
114         if (CE != NULL)
115                 tputs(CE, 1, __cputchar);
116         else {
117                 fwrite(blanks, sizeof (char), SCREEN_WIDTH - cur_col, stdout);
118                 if (COLS != SCREEN_WIDTH)
119                         mvcur(cur_row, SCREEN_WIDTH, cur_row, cur_col);
120                 else if (AM)
121                         mvcur(cur_row + 1, 0, cur_row, cur_col);
122                 else
123                         mvcur(cur_row, SCREEN_WIDTH - 1, cur_row, cur_col);
124         }
125         memcpy(&screen[cur_row][cur_col], blanks, SCREEN_WIDTH - cur_col);
126 }
127
128 /*
129  * display_putchar:
130  *      put one character on the screen, move the cursor right one,
131  *      with wraparound
132  */
133 void
134 display_put_ch(ch)
135         char ch;
136 {
137         if (!isprint(ch)) {
138                 fprintf(stderr, "r,c,ch: %d,%d,%d", cur_row, cur_col, ch);
139                 return;
140         }
141         screen[cur_row][cur_col] = ch;
142         putchar(ch);
143         if (++cur_col >= COLS) {
144                 if (!AM || XN) 
145                         putchar('\n');
146                 cur_col = 0;
147                 if (++cur_row >= LINES)
148                         cur_row = LINES;
149         }
150 }
151
152 /*
153  * display_put_str:
154  *      put a string of characters on the screen
155  */
156 void
157 display_put_str(const char *s)
158 {
159         for( ; *s; s++)
160                 display_put_ch(*s);
161 }
162
163 /*
164  * display_clear_the_screen:
165  *      clear the screen; move cursor to top left
166  */
167 void
168 display_clear_the_screen()
169 {
170         int     i;
171
172         if (blanks[0] == '\0')   
173                 for (i = 0; i < SCREEN_WIDTH; i++)
174                         blanks[i] = ' ';
175   
176         if (CL != NULL) {
177                 tputs(CL, LINES, __cputchar);
178                 for (i = 0; i < SCREEN_HEIGHT; i++)
179                         memcpy(screen[i], blanks, SCREEN_WIDTH);
180         } else {
181                 for (i = 0; i < SCREEN_HEIGHT; i++) {
182                         mvcur(cur_row, cur_col, i, 0);
183                         cur_row = i;
184                         cur_col = 0;
185                         display_clear_eol();
186                 }
187                 mvcur(cur_row, cur_col, 0, 0);
188         }
189         cur_row = cur_col = 0;
190 }
191
192 /*
193  * display_move:
194  *      move the cursor
195  */
196 void
197 display_move(y, x)
198         int y, x;
199 {
200         mvcur(cur_row, cur_col, y, x);
201         cur_row = y;
202         cur_col = x;
203 }
204
205 /*
206  * display_getyx:
207  *      locate the cursor
208  */
209 void
210 display_getyx(yp, xp)
211         int *yp, *xp;
212 {
213         *xp = cur_col;
214         *yp = cur_row;
215 }
216
217 /*
218  * display_end:
219  *      close the display
220  */
221 void
222 display_end()
223 {
224         tcsetattr(0, TCSADRAIN, &__orig_termios);
225         _puts(VE);
226         _puts(TE);
227 }
228
229 /*
230  * display_atyx:
231  *      return a character from the screen
232  */
233 char
234 display_atyx(y, x)
235         int y, x;
236 {
237         return screen[y][x];
238 }
239
240 /*
241  * display_redraw_screen:
242  *      redraw the screen
243  */
244 void
245 display_redraw_screen()
246 {
247         int i;
248
249         mvcur(cur_row, cur_col, 0, 0);
250         for (i = 0; i < SCREEN_HEIGHT - 1; i++) {
251                 fwrite(screen[i], sizeof (char), SCREEN_WIDTH, stdout);
252                 if (COLS > SCREEN_WIDTH || (COLS == SCREEN_WIDTH && !AM))
253                         putchar('\n');
254         }
255         fwrite(screen[SCREEN_HEIGHT - 1], sizeof (char), SCREEN_WIDTH - 1,
256                 stdout);
257         mvcur(SCREEN_HEIGHT - 1, SCREEN_WIDTH - 1, cur_row, cur_col);
258 }
259
260 #else /* CURSES */ /* --------------------------------------------------- */
261
262 #include <curses.h>
263 #include "hunt.h"
264
265 void
266 display_open()
267 {
268         initscr();
269         (void) noecho(); 
270         (void) cbreak();  
271 }
272
273 void
274 display_beep()
275 {
276         beep();
277 }
278
279 void
280 display_refresh()
281 {
282         refresh();
283 }
284
285 void
286 display_clear_eol()
287 {
288         clrtoeol();
289 }
290
291 void
292 display_put_ch(c)
293         char c;
294 {
295         addch(c);
296 }
297
298 void
299 display_put_str(const char *s)
300 {
301         addstr(s);
302 }
303
304 void
305 display_clear_the_screen()
306 {
307         clear();
308         move(0, 0);
309         display_refresh();
310 }
311
312 void
313 display_move(y, x)
314         int y, x;
315 {
316         move(y, x);
317 }
318
319 void
320 display_getyx(yp, xp)
321         int *yp, *xp;
322 {
323         getyx(stdscr, *yp, *xp);
324 }
325
326 void
327 display_end()
328 {
329         endwin();
330 }
331
332 char
333 display_atyx(y, x)
334         int y, x;
335 {
336         int oy, ox;
337         char c;
338
339         display_getyx(&oy, &ox);
340         c = mvwinch(stdscr, y, x) & 0x7f;
341         display_move(oy, ox);
342         return (c);
343 }
344
345 void
346 display_redraw_screen()
347 {
348         clearok(stdscr, TRUE);
349         touchwin(stdscr);
350 }
351
352 int
353 display_iserasechar(ch)
354         char ch;
355 {
356         return ch == erasechar();
357 }
358
359 int
360 display_iskillchar(ch)
361         char ch;
362 {
363         return ch == killchar();
364 }
365
366 #endif