1 /***************************************************************************
2 event.c - description
3 -------------------
4 begin : Wed Mar 20 2002
5 copyright : (C) 2001 by Michael Speck
6 email : kulkanie@gmx.net
7 ***************************************************************************/
8
9 /***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
18 #include "lgeneral.h"
19 #include "event.h"
20
21 int buttonup = 0, buttondown = 0;
22 int motion = 0;
23 int motion_x = 50, motion_y = 50;
24 int keystate[SDLK_LAST];
25 int buttonstate[10];
26 int sdl_quit = 0;
27
28 /*
29 ====================================================================
30 Locals
31 ====================================================================
32 */
33
34 /*
35 ====================================================================
36 Event filter that blocks all events. Used to clear the SDL
37 event queue.
38 ====================================================================
39 */
all_filter(const SDL_Event * event)40 int all_filter( const SDL_Event *event )
41 {
42 return 0;
43 }
44
45 /*
46 ====================================================================
47 Returns whether any mouse button is pressed
48 ====================================================================
49 */
event_is_button_pressed()50 static int event_is_button_pressed()
51 {
52 return (SDL_GetMouseState(0,0)!=0);
53 }
event_is_key_pressed()54 static int event_is_key_pressed()
55 {
56 int i;
57 Uint8 *keystate=SDL_GetKeyState(0);
58 for (i=0;i<SDLK_LAST;i++)
59 {
60 if (i==SDLK_NUMLOCK||i==SDLK_CAPSLOCK||i==SDLK_SCROLLOCK) continue;
61 if (keystate[i]) return 1;
62 }
63 return 0;
64 }
65
66 /*
67 ====================================================================
68 Event filter. As long as this is active no KEY or MOUSE events
69 will be available by SDL_PollEvent().
70 ====================================================================
71 */
event_filter(const SDL_Event * event)72 int event_filter( const SDL_Event *event )
73 {
74 switch ( event->type ) {
75 case SDL_MOUSEMOTION:
76 motion_x = event->motion.x;
77 motion_y = event->motion.y;
78 buttonstate[event->motion.state] = 1;
79 motion = 1;
80 return 0;
81 case SDL_MOUSEBUTTONUP:
82 buttonstate[event->button.button] = 0;
83 buttonup = event->button.button;
84 return 0;
85 case SDL_MOUSEBUTTONDOWN:
86 buttonstate[event->button.button] = 1;
87 buttondown = event->button.button;
88 return 0;
89 case SDL_KEYUP:
90 keystate[event->key.keysym.sym] = 0;
91 return 0;
92 case SDL_KEYDOWN:
93 keystate[event->key.keysym.sym] = 1;
94 return 1;
95 case SDL_QUIT:
96 sdl_quit = 1;
97 return 0;
98 }
99 return 1;
100 }
101
102 /*
103 ====================================================================
104 Publics
105 ====================================================================
106 */
107
108 /*
109 ====================================================================
110 Enable/Disable event filtering of mouse and keyboard.
111 ====================================================================
112 */
event_enable_filter(void)113 void event_enable_filter( void )
114 {
115 SDL_SetEventFilter( event_filter );
116 event_clear();
117 }
event_disable_filter(void)118 void event_disable_filter( void )
119 {
120 SDL_SetEventFilter( 0 );
121 }
event_clear(void)122 void event_clear( void )
123 {
124 memset( keystate, 0, sizeof( int ) * SDLK_LAST );
125 memset( buttonstate, 0, sizeof( int ) * 10 );
126 buttonup = buttondown = motion = 0;
127 }
128
129 /*
130 ====================================================================
131 Check if an motion event occured since last call.
132 ====================================================================
133 */
event_get_motion(int * x,int * y)134 int event_get_motion( int *x, int *y )
135 {
136 if ( motion ) {
137 *x = motion_x;
138 *y = motion_y;
139 motion = 0;
140 return 1;
141 }
142 return 0;
143 }
144 /*
145 ====================================================================
146 Check if a button was pressed and return its value.
147 ====================================================================
148 */
event_get_buttondown(int * button,int * x,int * y)149 int event_get_buttondown( int *button, int *x, int *y )
150 {
151 if ( buttondown ) {
152 *button = buttondown;
153 *x = motion_x;
154 *y = motion_y;
155 buttondown = 0;
156 return 1;
157 }
158 return 0;
159 }
event_get_buttonup(int * button,int * x,int * y)160 int event_get_buttonup( int *button, int *x, int *y )
161 {
162 if ( buttonup ) {
163 *button = buttonup;
164 *x = motion_x;
165 *y = motion_y;
166 buttonup = 0;
167 return 1;
168 }
169 return 0;
170 }
171
172 /*
173 ====================================================================
174 Get current cursor position.
175 ====================================================================
176 */
event_get_cursor_pos(int * x,int * y)177 void event_get_cursor_pos( int *x, int *y )
178 {
179 *x = motion_x; *y = motion_y;
180 }
181
182 /*
183 ====================================================================
184 Check if 'button' button is currently set.
185 ====================================================================
186 */
event_check_button(int button)187 int event_check_button( int button )
188 {
189 return buttonstate[button];
190 }
191
192 /*
193 ====================================================================
194 Check if 'key' is currently set.
195 ====================================================================
196 */
event_check_key(int key)197 int event_check_key( int key )
198 {
199 return keystate[key];
200 }
201
202 /*
203 ====================================================================
204 Check if SDL_QUIT was received.
205 ====================================================================
206 */
event_check_quit()207 int event_check_quit()
208 {
209 return sdl_quit;
210 }
211
212 /*
213 ====================================================================
214 Clear the SDL event key (keydown events)
215 ====================================================================
216 */
event_clear_sdl_queue()217 void event_clear_sdl_queue()
218 {
219 SDL_Event event;
220 SDL_SetEventFilter( all_filter );
221 while ( SDL_PollEvent( &event ) );
222 SDL_SetEventFilter( event_filter );
223 }
224
225 /*
226 ====================================================================
227 Wait until neither a key nor a button is pressed.
228 ====================================================================
229 */
event_wait_until_no_input()230 void event_wait_until_no_input()
231 {
232 SDL_PumpEvents();
233 while (event_is_button_pressed()||event_is_key_pressed())
234 {
235 SDL_Delay(20);
236 SDL_PumpEvents();
237 }
238 event_clear();
239 }
240