1 | /* |
---|
2 | * ttyvaders Textmode shoot'em up |
---|
3 | * Copyright (c) 2002 Sam Hocevar <sam@zoy.org> |
---|
4 | * All Rights Reserved |
---|
5 | * |
---|
6 | * $Id: graphics.c,v 1.6 2002/12/23 16:21:38 sam Exp $ |
---|
7 | * |
---|
8 | * This program is free software; you can redistribute it and/or modify |
---|
9 | * it under the terms of the GNU General Public License as published by |
---|
10 | * the Free Software Foundation; either version 2 of the License, or |
---|
11 | * (at your option) any later version. |
---|
12 | * |
---|
13 | * This program is distributed in the hope that it will be useful, |
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
16 | * GNU General Public License for more details. |
---|
17 | * |
---|
18 | * You should have received a copy of the GNU General Public License |
---|
19 | * along with this program; if not, write to the Free Software |
---|
20 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
21 | */ |
---|
22 | |
---|
23 | #include "config.h" |
---|
24 | |
---|
25 | #include <stdlib.h> |
---|
26 | #include <unistd.h> |
---|
27 | |
---|
28 | #include "common.h" |
---|
29 | |
---|
30 | int init_graphics( void ) |
---|
31 | { |
---|
32 | #ifdef USE_SLANG |
---|
33 | /* Initialize slang library */ |
---|
34 | SLsig_block_signals(); |
---|
35 | SLtt_get_terminfo(); |
---|
36 | |
---|
37 | if( SLkp_init() == -1 ) |
---|
38 | { |
---|
39 | SLsig_unblock_signals(); |
---|
40 | return 1; |
---|
41 | } |
---|
42 | |
---|
43 | SLang_init_tty (-1, 0, 1); |
---|
44 | |
---|
45 | if( SLsmg_init_smg() == -1 ) |
---|
46 | { |
---|
47 | SLsig_unblock_signals(); |
---|
48 | return 1; |
---|
49 | } |
---|
50 | |
---|
51 | SLsig_unblock_signals(); |
---|
52 | |
---|
53 | SLsmg_cls(); |
---|
54 | SLsmg_refresh(); |
---|
55 | #elif USE_NCURSES |
---|
56 | /* Initialize ncurses library */ |
---|
57 | initscr(); |
---|
58 | keypad(stdscr, TRUE); |
---|
59 | nonl(); |
---|
60 | cbreak(); |
---|
61 | noecho(); |
---|
62 | nodelay(stdscr, TRUE); |
---|
63 | #else |
---|
64 | /* Dummy driver */ |
---|
65 | #endif |
---|
66 | |
---|
67 | return 0; |
---|
68 | } |
---|
69 | |
---|
70 | void init_game( game *g ) |
---|
71 | { |
---|
72 | #ifdef USE_SLANG |
---|
73 | static char * const colors[] = |
---|
74 | { |
---|
75 | "black", "green", "yellow", "white", |
---|
76 | "red", "gray", "lightgray", "blue", "cyan", "magenta", NULL |
---|
77 | }; |
---|
78 | |
---|
79 | int i; |
---|
80 | |
---|
81 | for( i = 0; colors[i] ; i++ ) |
---|
82 | { |
---|
83 | SLtt_set_color( i+1, NULL, colors[i], "black" ); |
---|
84 | } |
---|
85 | |
---|
86 | g->w = SLtt_Screen_Cols; |
---|
87 | g->h = SLtt_Screen_Rows; |
---|
88 | #elif USE_NCURSES |
---|
89 | start_color(); |
---|
90 | |
---|
91 | init_pair( BLACK, COLOR_BLACK, COLOR_BLACK ); |
---|
92 | init_pair( GREEN, COLOR_GREEN, COLOR_BLACK ); |
---|
93 | init_pair( YELLOW, COLOR_YELLOW, COLOR_BLACK ); |
---|
94 | init_pair( WHITE, COLOR_WHITE, COLOR_BLACK ); |
---|
95 | init_pair( RED, COLOR_RED, COLOR_BLACK ); |
---|
96 | init_pair( GRAY, COLOR_WHITE, COLOR_BLACK ); // XXX |
---|
97 | init_pair( LIGHTGRAY, COLOR_WHITE, COLOR_BLACK ); // XXX |
---|
98 | init_pair( BLUE, COLOR_BLUE, COLOR_BLACK ); |
---|
99 | init_pair( CYAN, COLOR_CYAN, COLOR_BLACK ); |
---|
100 | init_pair( MAGENTA, COLOR_MAGENTA, COLOR_BLACK ); |
---|
101 | |
---|
102 | g->w = COLS; |
---|
103 | g->h = LINES; |
---|
104 | #else |
---|
105 | /* Use dummy driver */ |
---|
106 | g->w = 80; |
---|
107 | g->h = 25; |
---|
108 | #endif |
---|
109 | } |
---|
110 | |
---|
111 | char get_key( void ) |
---|
112 | { |
---|
113 | #ifdef USE_SLANG |
---|
114 | if( SLang_input_pending (0) ) |
---|
115 | { |
---|
116 | return SLang_getkey(); |
---|
117 | } |
---|
118 | #elif USE_NCURSES |
---|
119 | char key; |
---|
120 | |
---|
121 | if( ( key = getch() ) != ERR ) |
---|
122 | { |
---|
123 | return key; |
---|
124 | } |
---|
125 | #else |
---|
126 | /* Use dummy driver */ |
---|
127 | char key = GET_RAND(0,256); |
---|
128 | |
---|
129 | if( key != 'q' && key != 'p' && key != '\t' ) |
---|
130 | { |
---|
131 | return key; |
---|
132 | } |
---|
133 | #endif |
---|
134 | |
---|
135 | return 0; |
---|
136 | } |
---|
137 | |
---|
138 | void gfx_delay( void ) |
---|
139 | { |
---|
140 | #ifdef USE_SLANG |
---|
141 | usleep(40000); |
---|
142 | #elif USE_NCURSES |
---|
143 | usleep(40000); |
---|
144 | #else |
---|
145 | /* Use dummy driver */ |
---|
146 | #endif |
---|
147 | } |
---|
148 | |
---|
149 | void clear_graphics( void ) |
---|
150 | { |
---|
151 | #ifdef USE_SLANG |
---|
152 | SLsmg_cls(); |
---|
153 | #elif USE_NCURSES |
---|
154 | clear(); |
---|
155 | #else |
---|
156 | /* Use dummy driver */ |
---|
157 | #endif |
---|
158 | } |
---|
159 | |
---|
160 | void refresh_graphics( void ) |
---|
161 | { |
---|
162 | gfx_goto( 0, 0 ); |
---|
163 | #ifdef USE_SLANG |
---|
164 | SLsmg_refresh(); |
---|
165 | #elif USE_NCURSES |
---|
166 | refresh(); |
---|
167 | #else |
---|
168 | /* Use dummy driver */ |
---|
169 | #endif |
---|
170 | } |
---|
171 | |
---|
172 | void end_graphics( void ) |
---|
173 | { |
---|
174 | #ifdef USE_SLANG |
---|
175 | SLang_reset_tty(); |
---|
176 | SLsmg_reset_smg(); |
---|
177 | #elif USE_NCURSES |
---|
178 | endwin(); |
---|
179 | #else |
---|
180 | /* Use dummy driver */ |
---|
181 | #endif |
---|
182 | } |
---|
183 | |
---|