1 /* xsoldier, a shoot 'em up game with "not shooting" bonus
2 * Copyright (C) 1997 Yuusuke HASHIMOTO <s945750@educ.info.kanagawa-u.ac.jp>
3 * Copyright (C) 2002 Oohara Yuuma <oohara@libra.interq.or.jp>
4 *
5 * This is a copyleft program. See the file LICENSE for details.
6 */
7 /* $Id: input.c,v 1.17 2006/09/16 09:20:59 oohara Exp $ */
8
9 #include <config.h>
10
11 #ifdef HAVE_LIBSDL
12 #include <SDL.h>
13 #else /* HAVE_LIBSDL */
14 #include <X11/Xlib.h>
15 #endif /* HAVE_LIBSDL */
16
17 /*
18 #include <X11/Xutil.h>
19 */
20
21 #include "image.h"
22 #include "xsoldier.h"
23 #include "extern.h"
24 #include "graphic.h"
25 #include "input.h"
26 #include "key.h"
27 #include "manage.h"
28
29 #ifdef JSTK
30 #include "joystick.h"
31 #endif /* JSTK */
32
33
34 static void SetKeyMask(KeySym key);
35 static void UnsetKeyMask(KeySym key);
36
input_init(void)37 int input_init(void)
38 {
39 keymask = 0;
40 #ifdef JSTK
41 /* initialize joystick */
42 initJS();
43 #endif /* JSTK */
44
45 #ifdef HAVE_LIBSDL
46 upKey = SDL_GetKeyName(SDLK_UP);
47 downKey = SDL_GetKeyName(SDLK_DOWN);
48 leftKey = SDL_GetKeyName(SDLK_LEFT);
49 rightKey = SDL_GetKeyName(SDLK_RIGHT);
50 shotKey = SDL_GetKeyName(SDLK_LSHIFT);
51 spdupKey = SDL_GetKeyName(SDLK_a);
52 spdwnKey = SDL_GetKeyName(SDLK_s);
53 pauseKey = SDL_GetKeyName(SDLK_p);
54 quitKey = SDL_GetKeyName(SDLK_q);
55 #ifdef DEBUG
56 weaponchangeKey = SDL_GetKeyName(SDLK_w);
57 clearenemyshotKey = SDL_GetKeyName(SDLK_c);
58 #endif /* DEBUG */
59
60 #else /* not HAVE_LIBSDL */
61 upKey = XKeysymToString(UpKey);
62 downKey = XKeysymToString(DownKey);
63 leftKey = XKeysymToString(LeftKey);
64 rightKey = XKeysymToString(RightKey);
65 shotKey = XKeysymToString(ShotKey);
66 spdupKey = XKeysymToString(SpeedUPKey);
67 spdwnKey = XKeysymToString(SpeedDOWNKey);
68 pauseKey = XKeysymToString(PauseKey);
69 quitKey = XKeysymToString(QuitKey);
70 #ifdef DEBUG
71 weaponchangeKey = XKeysymToString(WeaponChangeKey);
72 clearenemyshotKey = XKeysymToString(ClearEnemyShotKey);
73 #endif /* DEBUG */
74
75 #endif /* not HAVE_LIBSDL */
76 return 0;
77 }
78
79
80 /* return 0 if the game is over, 1 if not */
event_handle(void)81 int event_handle(void)
82 {
83 #ifdef HAVE_LIBSDL
84 while (SDL_PollEvent(&event) > 0)
85 {
86 if (event.type == SDL_QUIT)
87 {
88 manage->program_should_quit = True;
89 return 0;
90 }
91 else if (event.type == SDL_VIDEOEXPOSE)
92 {
93 redraw_window();
94 }
95 else if (event.type == SDL_ACTIVEEVENT)
96 {
97 if (event.active.gain == 1)
98 /* the mouse cursor entered the window */
99 ;
100 else
101 {
102 if (manage->player[0]->Data.used==False && player->Ships==0)
103 return 0;
104 else
105 {
106 keymask = 0;
107 keymask |= Pause;
108 }
109 }
110 }
111 else if (event.type == SDL_KEYDOWN)
112 {
113 if (manage->player[0]->Data.used==False && player->Ships==0)
114 /* key press after the game is over */
115 return 0;
116 else
117 SetKeyMask(event.key.keysym.sym);
118
119 if (keymask&Quit)
120 return 0;
121 }
122 else if (event.type == SDL_KEYUP)
123 UnsetKeyMask(event.key.keysym.sym);
124
125 }
126
127
128 #else /* not HAVE_LIBSDL */
129 while(XPending(dpy))
130 {
131 XNextEvent(dpy,&event);
132 switch(event.type)
133 {
134 case Expose:
135 if (event.xexpose.count == 0)
136 XCopyArea(dpy,WorkPixmap,win,BackGC,0,0,FieldW,FieldH,0,0);
137 break;
138 case EnterNotify:
139 XAutoRepeatOff(dpy);
140 break;
141 case LeaveNotify:
142 XAutoRepeatOn(dpy);
143 if (manage->player[0]->Data.used==False && player->Ships==0)
144 return 0;
145 else
146 {
147 keymask = 0;
148 keymask |= Pause;
149 }
150 break;
151 case KeyPress:
152 if (manage->player[0]->Data.used==False && player->Ships==0)
153 /* key press after the game is over */
154 return 0;
155 else
156 SetKeyMask(XLookupKeysym(&(event.xkey),0));
157
158 if (keymask&Quit)
159 return 0;
160 break;
161 case KeyRelease:
162 UnsetKeyMask(XLookupKeysym(&(event.xkey),0));
163 break;
164 }
165 }
166 #endif /* not HAVE_LIBSDL */
167
168
169 #ifdef JSTK
170 readJS();
171 if ((manage->player[0]->Data.used==False && player->Ships==0) && (joymask&Shot || joymask&SpeedUP))
172 /* key press after the game is over */
173 return 0;
174 #endif
175 return 1;
176 }
177
178 /* return
179 * 1 if the game starts
180 * -1 if the program quits
181 * 0 otherwise
182 */
event_handle_opening(void)183 int event_handle_opening(void)
184 {
185 #ifdef HAVE_LIBSDL
186 while (SDL_PollEvent(&event) > 0)
187 {
188 if (event.type == SDL_QUIT)
189 {
190 manage->program_should_quit = True;
191 return -1;
192 }
193 else if (event.type == SDL_VIDEOEXPOSE)
194 {
195 ;
196 }
197 else if (event.type == SDL_ACTIVEEVENT)
198 {
199 if (event.active.gain == 1)
200 /* the mouse cursor entered the window */
201 ;
202 else
203 ;
204 }
205 else if (event.type == SDL_KEYDOWN)
206 {
207 if (event.key.keysym.sym == SDLK_SPACE)
208 return 1;
209 if (event.key.keysym.sym == SDLK_q)
210 return -1;
211 }
212 }
213
214 #else /* not HAVE_LIBSDL */
215 while(XPending(dpy))
216 {
217 XNextEvent(dpy,&event);
218 switch(event.type)
219 {
220 case Expose:
221 break;
222 case EnterNotify:
223 XAutoRepeatOff(dpy);
224 break;
225 case LeaveNotify:
226 XAutoRepeatOn(dpy);
227 break;
228 case KeyPress:
229 if (XLookupKeysym(&(event.xkey),0) == XK_space)
230 {
231 return 1;
232 }
233 if (XLookupKeysym(&(event.xkey),0) == QuitKey)
234 {
235 return -1;
236 }
237 break;
238 }
239 }
240
241 #endif /* not HAVE_LIBSDL */
242
243 #ifdef JSTK
244 readJS();
245 if ((joymask&Shot) || (joymask&SpeedUP))
246 {
247 return 1;
248 }
249 #endif
250 return 0;
251 }
252
253 /* return 0 if the ending is over, 1 if not */
event_handle_ending(void)254 int event_handle_ending(void)
255 {
256 #ifdef HAVE_LIBSDL
257 while (SDL_PollEvent(&event) > 0)
258 {
259 if (event.type == SDL_QUIT)
260 {
261 manage->program_should_quit = True;
262 return 0;
263 }
264 else if (event.type == SDL_VIDEOEXPOSE)
265 {
266 ;
267 }
268 else if (event.type == SDL_ACTIVEEVENT)
269 {
270 if (event.active.gain == 1)
271 /* the mouse cursor entered the window */
272 ;
273 else
274 ;
275 }
276 else if (event.type == SDL_KEYDOWN)
277 {
278 if (event.key.keysym.sym == SDLK_SPACE)
279 return 0;
280 }
281 }
282
283 #else /* not HAVE_LIBSDL */
284
285 while(XPending(dpy))
286 {
287 XNextEvent(dpy,&event);
288 switch(event.type)
289 {
290 case EnterNotify:
291 XAutoRepeatOff(dpy);
292 break;
293 case LeaveNotify:
294 XAutoRepeatOn(dpy);
295 break;
296 case KeyPress:
297 if (XLookupKeysym(&(event.xkey),0) == XK_space)
298 {
299 return 0;
300 }
301 }
302 }
303
304 #endif /* not HAVE_LIBSDL */
305
306
307 #ifdef JSTK
308 readJS();
309 if ((joymask&Shot) || (joymask&SpeedUP))
310 {
311 return 0;
312 }
313 #endif
314 return 1;
315 }
316
SetKeyMask(KeySym key)317 static void SetKeyMask(KeySym key)
318 {
319 if (key == UpKey)
320 keymask |= Up;
321 else if (key == DownKey)
322 keymask |= Down;
323 else if (key == LeftKey)
324 keymask |= Left;
325 else if (key == RightKey)
326 keymask |= Right;
327 else if (key == ShotKey)
328 keymask |= Shot;
329 else if (key == SpeedUPKey)
330 keymask |= SpeedUP;
331 else if (key == SpeedDOWNKey)
332 keymask |= SpeedDOWN;
333 else if (key == PauseKey)
334 /* toggle */
335 keymask ^= Pause;
336 else if (key == QuitKey)
337 keymask |= Quit;
338 #ifdef DEBUG
339 else if (key == XK_w)
340 {
341 if (manage->player[0]->Data.Cnt[5] == 3)
342 manage->player[0]->Data.Cnt[5] = 0;
343 manage->player[0]->Data.Cnt[5]++;
344 }
345 else if (key == XK_c)
346 ClearEnemyShotManage(manage);
347 #endif
348 }
349
UnsetKeyMask(KeySym key)350 static void UnsetKeyMask(KeySym key)
351 {
352 if (key == UpKey)
353 keymask &= ~Up;
354 else if (key == DownKey)
355 keymask &= ~Down;
356 else if (key == LeftKey)
357 keymask &= ~Left;
358 else if (key == RightKey)
359 keymask &= ~Right;
360 else if (key == ShotKey)
361 keymask &= ~Shot;
362 else if (key == SpeedUPKey)
363 keymask &= ~SpeedUP;
364 else if (key == SpeedDOWNKey)
365 keymask &= ~SpeedDOWN;
366 else if (key == QuitKey)
367 keymask &= ~Quit;
368 }
369