Rev 7 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1 | pmbaty | 1 | /* |
2 | * src/sysevt.c |
||
3 | * |
||
4 | * Copyright (C) 1998-2002 BigOrno (bigorno@bigorno.net). All rights reserved. |
||
5 | * |
||
6 | * The use and distribution terms for this software are contained in the file |
||
7 | * named README, which can be found in the root of this distribution. By |
||
8 | * using this software in any fashion, you are agreeing to be bound by the |
||
9 | * terms of this license. |
||
10 | * |
||
11 | * You must not remove this notice, or any other, from this software. |
||
12 | */ |
||
13 | |||
14 | #include <SDL.h> |
||
15 | |||
16 | #include "system.h" |
||
17 | #include "game.h" |
||
18 | |||
19 | #include "control.h" |
||
20 | #include "draw.h" |
||
21 | |||
22 | #define SYSJOY_RANGE 3280 |
||
23 | |||
24 | static SDL_Event event; |
||
25 | extern U8 want_filter; |
||
26 | extern U8 want_fullscreen; |
||
10 | pmbaty | 27 | extern U8 enable_endkey; |
7 | pmbaty | 28 | extern U8 recreate_screen; |
1 | pmbaty | 29 | |
30 | |||
31 | /* |
||
32 | * Process an event |
||
33 | */ |
||
34 | static void processEvent (void) |
||
35 | { |
||
36 | static U8 alt_pressed = FALSE; |
||
37 | |||
7 | pmbaty | 38 | U16 key = event.key.keysym.scancode; |
1 | pmbaty | 39 | |
40 | switch (event.type) |
||
41 | { |
||
42 | case SDL_KEYDOWN: |
||
7 | pmbaty | 43 | if (key == SDL_SCANCODE_LALT) |
1 | pmbaty | 44 | alt_pressed = TRUE; |
7 | pmbaty | 45 | else if (alt_pressed && key == SDL_SCANCODE_RETURN) |
1 | pmbaty | 46 | { |
47 | want_fullscreen ^= TRUE; |
||
7 | pmbaty | 48 | recreate_screen = TRUE; |
1 | pmbaty | 49 | } |
7 | pmbaty | 50 | else if (alt_pressed && key == SDL_SCANCODE_F4) |
1 | pmbaty | 51 | { |
52 | control_status |= CONTROL_EXIT; |
||
53 | control_last = CONTROL_EXIT; |
||
54 | } |
||
7 | pmbaty | 55 | else if (key == SDL_SCANCODE_O || key == SDL_SCANCODE_UP) |
1 | pmbaty | 56 | { |
57 | control_status |= CONTROL_UP; |
||
58 | control_last = CONTROL_UP; |
||
59 | } |
||
7 | pmbaty | 60 | else if (key == SDL_SCANCODE_K || key == SDL_SCANCODE_DOWN) |
1 | pmbaty | 61 | { |
62 | control_status |= CONTROL_DOWN; |
||
63 | control_last = CONTROL_DOWN; |
||
64 | } |
||
7 | pmbaty | 65 | else if (key == SDL_SCANCODE_Z || key == SDL_SCANCODE_LEFT) |
1 | pmbaty | 66 | { |
67 | control_status |= CONTROL_LEFT; |
||
68 | control_last = CONTROL_LEFT; |
||
69 | } |
||
7 | pmbaty | 70 | else if (key == SDL_SCANCODE_X || key == SDL_SCANCODE_RIGHT) |
1 | pmbaty | 71 | { |
72 | control_status |= CONTROL_RIGHT; |
||
73 | control_last = CONTROL_RIGHT; |
||
74 | } |
||
10 | pmbaty | 75 | else if ((key == SDL_SCANCODE_PAUSE) || (key == SDL_SCANCODE_P)) |
1 | pmbaty | 76 | { |
77 | control_status |= CONTROL_PAUSE; |
||
78 | control_last = CONTROL_PAUSE; |
||
79 | } |
||
10 | pmbaty | 80 | else if ((key == SDL_SCANCODE_END) && enable_endkey) |
1 | pmbaty | 81 | { |
82 | control_status |= CONTROL_END; |
||
83 | control_last = CONTROL_END; |
||
84 | } |
||
7 | pmbaty | 85 | else if (key == SDL_SCANCODE_ESCAPE) |
1 | pmbaty | 86 | { |
87 | control_status |= CONTROL_EXIT; |
||
88 | control_last = CONTROL_EXIT; |
||
89 | } |
||
7 | pmbaty | 90 | else if (key == SDL_SCANCODE_SPACE) |
1 | pmbaty | 91 | { |
92 | control_status |= CONTROL_FIRE; |
||
93 | control_last = CONTROL_FIRE; |
||
94 | } |
||
7 | pmbaty | 95 | else if (key == SDL_SCANCODE_F4) |
1 | pmbaty | 96 | syssnd_toggleMute(); |
7 | pmbaty | 97 | else if (key == SDL_SCANCODE_F5) |
1 | pmbaty | 98 | syssnd_vol(-1); |
7 | pmbaty | 99 | else if (key == SDL_SCANCODE_F6) |
1 | pmbaty | 100 | syssnd_vol(+1); |
7 | pmbaty | 101 | else if (key == SDL_SCANCODE_F7) |
1 | pmbaty | 102 | want_filter ^= TRUE; |
7 | pmbaty | 103 | else if (key == SDL_SCANCODE_F10) |
1 | pmbaty | 104 | { |
105 | control_status &= ~CONTROL_EXIT; |
||
106 | control_last = CONTROL_EXIT; |
||
107 | } |
||
108 | break; |
||
109 | case SDL_KEYUP: |
||
7 | pmbaty | 110 | if (key == SDL_SCANCODE_LALT) |
1 | pmbaty | 111 | alt_pressed = FALSE; |
7 | pmbaty | 112 | else if (key == SDL_SCANCODE_O || key == SDL_SCANCODE_UP) |
1 | pmbaty | 113 | { |
114 | control_status &= ~CONTROL_UP; |
||
115 | control_last = CONTROL_UP; |
||
116 | } |
||
7 | pmbaty | 117 | else if (key == SDL_SCANCODE_K || key == SDL_SCANCODE_DOWN) |
1 | pmbaty | 118 | { |
119 | control_status &= ~CONTROL_DOWN; |
||
120 | control_last = CONTROL_DOWN; |
||
121 | } |
||
7 | pmbaty | 122 | else if (key == SDL_SCANCODE_Z || key == SDL_SCANCODE_LEFT) |
1 | pmbaty | 123 | { |
124 | control_status &= ~CONTROL_LEFT; |
||
125 | control_last = CONTROL_LEFT; |
||
126 | } |
||
7 | pmbaty | 127 | else if (key == SDL_SCANCODE_X || key == SDL_SCANCODE_RIGHT) |
1 | pmbaty | 128 | { |
129 | control_status &= ~CONTROL_RIGHT; |
||
130 | control_last = CONTROL_RIGHT; |
||
131 | } |
||
10 | pmbaty | 132 | else if ((key == SDL_SCANCODE_PAUSE) || (key == SDL_SCANCODE_P)) |
1 | pmbaty | 133 | { |
134 | control_status &= ~CONTROL_PAUSE; |
||
135 | control_last = CONTROL_PAUSE; |
||
136 | } |
||
10 | pmbaty | 137 | else if ((key == SDL_SCANCODE_END) && enable_endkey) |
1 | pmbaty | 138 | { |
139 | control_status &= ~CONTROL_END; |
||
140 | control_last = CONTROL_END; |
||
141 | } |
||
7 | pmbaty | 142 | else if (key == SDL_SCANCODE_ESCAPE) |
1 | pmbaty | 143 | { |
144 | control_status &= ~CONTROL_EXIT; |
||
145 | control_last = CONTROL_EXIT; |
||
146 | } |
||
7 | pmbaty | 147 | else if (key == SDL_SCANCODE_SPACE) |
1 | pmbaty | 148 | { |
149 | control_status &= ~CONTROL_FIRE; |
||
150 | control_last = CONTROL_FIRE; |
||
151 | } |
||
152 | break; |
||
153 | case SDL_QUIT: |
||
154 | /* player tries to close the window -- this is the same as pressing ESC */ |
||
155 | control_status |= CONTROL_EXIT; |
||
156 | control_last = CONTROL_EXIT; |
||
157 | break; |
||
158 | case SDL_JOYAXISMOTION: |
||
159 | if (event.jaxis.axis == 0) |
||
160 | { |
||
161 | /* left-right */ |
||
162 | if (event.jaxis.value < -SYSJOY_RANGE) |
||
163 | { /* left */ |
||
164 | control_status |= CONTROL_LEFT; |
||
165 | control_status &= ~CONTROL_RIGHT; |
||
166 | } |
||
167 | else if (event.jaxis.value > SYSJOY_RANGE) |
||
168 | { /* right */ |
||
169 | control_status |= CONTROL_RIGHT; |
||
170 | control_status &= ~CONTROL_LEFT; |
||
171 | } |
||
172 | else |
||
173 | { /* center */ |
||
174 | control_status &= ~CONTROL_RIGHT; |
||
175 | control_status &= ~CONTROL_LEFT; |
||
176 | } |
||
177 | } |
||
178 | if (event.jaxis.axis == 1) |
||
179 | { |
||
180 | /* up-down */ |
||
181 | if (event.jaxis.value < -SYSJOY_RANGE) |
||
182 | { /* up */ |
||
183 | control_status |= CONTROL_UP; |
||
184 | control_status &= ~CONTROL_DOWN; |
||
185 | } |
||
186 | else if (event.jaxis.value > SYSJOY_RANGE) |
||
187 | { /* down */ |
||
188 | control_status |= CONTROL_DOWN; |
||
189 | control_status &= ~CONTROL_UP; |
||
190 | } |
||
191 | else |
||
192 | { /* center */ |
||
193 | control_status &= ~CONTROL_DOWN; |
||
194 | control_status &= ~CONTROL_UP; |
||
195 | } |
||
196 | } |
||
197 | break; |
||
198 | case SDL_JOYBUTTONDOWN: |
||
199 | control_status |= CONTROL_FIRE; |
||
200 | break; |
||
201 | case SDL_JOYBUTTONUP: |
||
202 | control_status &= ~CONTROL_FIRE; |
||
203 | break; |
||
204 | default: |
||
205 | break; |
||
206 | } |
||
207 | } |
||
208 | |||
209 | |||
210 | /* |
||
211 | * Process events, if any, then return |
||
212 | */ |
||
213 | void sysevt_poll (void) |
||
214 | { |
||
215 | while (SDL_PollEvent (&event)) |
||
216 | processEvent (); |
||
217 | } |
||
218 | |||
219 | |||
220 | /* |
||
221 | * Wait for an event, then process it and return |
||
222 | */ |
||
223 | void sysevt_wait (void) |
||
224 | { |
||
225 | SDL_WaitEvent (&event); |
||
226 | processEvent (); |
||
227 | } |