Skip to content

Commit

Permalink
Added support for event filter (needed to ignore SDL_TEXTINPUT from k…
Browse files Browse the repository at this point in the history
…eyboard), fix SDL_PollEvent call (it was called once per frame), some cleanup.
  • Loading branch information
sergiobenrocha2 committed Mar 23, 2015
1 parent 7d32099 commit a9db70e
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 31 deletions.
50 changes: 29 additions & 21 deletions event.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ static const int BALL_THRESHOLD = 10;
static const int MOUSE_THRESHOLD = 10;
static int num_joysticks = 0;
static SDL_Joystick *joysticks[MAX_JOYSTICKS];
static int event_num = EVENT_NONE;
static struct event events[NUM_EVENTS];

int event_init( void ) {
Expand Down Expand Up @@ -98,11 +97,21 @@ void event_flush( void ) {
while( SDL_PollEvent( &sdl_event ) );
}

int event_poll( void ) {
int event_filter( void* data, SDL_Event* sdl_event ) {
if( sdl_event->type == SDL_TEXTINPUT )
return 0;
return 1;
}

void event_set_filter( void ) {
SDL_SetEventFilter( event_filter, NULL );
}

int event_poll( int event_num ) {
SDL_Event sdl_event;
int i;

if( SDL_PollEvent( &sdl_event ) ) {
while( SDL_PollEvent( &sdl_event ) ) {
event_num = EVENT_NONE;
if( sdl_event.type == SDL_QUIT ) {
event_num = EVENT_QUIT;
Expand All @@ -112,48 +121,48 @@ int event_poll( void ) {
switch( sdl_event.type ) {
case SDL_KEYDOWN:
if( events[i].device_type == DEV_KEYBOARD
&& sdl_event.key.keysym.sym == events[i].value )
&& events[i].value == sdl_event.key.keysym.sym )
event_num = i;

break;

case SDL_JOYAXISMOTION:
if( events[i].device_type == DEV_JOYSTICK
&& events[i].control_type == CTRL_AXIS
&& sdl_event.jaxis.which == events[i].device_id
&& sdl_event.jaxis.axis == events[i].control_id ) {
if( sdl_event.jaxis.value < -AXIS_THRESHOLD && events[i].value < 0 )
&& events[i].device_id == sdl_event.jaxis.which
&& events[i].control_id == sdl_event.jaxis.axis ) {
if( events[i].value < 0 && sdl_event.jaxis.value < -AXIS_THRESHOLD )
event_num = i;

else if ( sdl_event.jaxis.value > AXIS_THRESHOLD && events[i].value > 0 )
else if ( events[i].value > 0 && sdl_event.jaxis.value > AXIS_THRESHOLD )
event_num = i;
}
break;

case SDL_JOYBUTTONDOWN:
if( events[i].device_type == DEV_JOYSTICK
&& events[i].control_type == CTRL_BUTTON
&& sdl_event.jbutton.which == events[i].device_id
&& sdl_event.jbutton.button == events[i].value )
&& events[i].device_id == sdl_event.jbutton.which
&& events[i].value == sdl_event.jbutton.button )
event_num = i;

break;

case SDL_JOYHATMOTION:
if( events[i].device_type == DEV_JOYSTICK
&& events[i].control_type == CTRL_HAT
&& sdl_event.jhat.which == events[i].device_id
&& sdl_event.jhat.hat == events[i].control_id
&& sdl_hat_dir_value( sdl_event.jhat.value ) == events[i].value )
&& events[i].device_id == sdl_event.jhat.which
&& events[i].control_id == sdl_event.jhat.hat
&& events[i].value == sdl_hat_dir_value( sdl_event.jhat.value ) )
event_num = i;

break;

case SDL_JOYBALLMOTION:
if( events[i].device_type == DEV_JOYSTICK
&& events[i].control_type == CTRL_BALL
&& sdl_event.jball.which == events[i].device_id
&& sdl_event.jball.ball == events[i].control_id ) {
&& events[i].device_id == sdl_event.jball.which
&& events[i].control_id == sdl_event.jball.ball ) {
if( events[i].value == DIR_DOWN && sdl_event.jball.yrel > BALL_THRESHOLD )
event_num = i;

Expand All @@ -171,16 +180,16 @@ int event_poll( void ) {
case SDL_MOUSEBUTTONDOWN:
if( events[i].device_type == DEV_MOUSE
&& events[i].control_type == CTRL_BUTTON
&& sdl_event.button.which == events[i].device_id
&& sdl_event.button.button == events[i].value )
&& events[i].device_id == sdl_event.button.which
&& events[i].value == sdl_event.button.button )
event_num = i;

break;

case SDL_MOUSEMOTION:
if( events[i].device_type == DEV_MOUSE
&& events[i].control_type == CTRL_AXIS
&& sdl_event.motion.which == events[i].device_id ) {
&& events[i].device_id == sdl_event.motion.which ) {
if( events[i].value == DIR_DOWN && sdl_event.motion.yrel > MOUSE_THRESHOLD )
event_num = i;

Expand All @@ -196,7 +205,7 @@ int event_poll( void ) {
}
}
}

// Returns the last event happened if there is no new event in the queue
return event_num;
}

Expand Down Expand Up @@ -324,4 +333,3 @@ const char *event_name( int event_n ) {
else
return event_str[event_n];
}

3 changes: 2 additions & 1 deletion include/event.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ int event_set( int id, struct event *event );
struct event *event_get( int id );

void event_flush( void );
int event_poll( void );
void event_set_filter( void );
int event_poll( int event_num );
int event_probe( int timeout, struct event *event );
int event_process( int e );
int event_id( char *name );
Expand Down
20 changes: 11 additions & 9 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ int main( int argc, char *arvg[] ) {
const struct config *config_m = config_get();
int quit = 0;
int config_status = 0;
int event;
int event = EVENT_SELECT;


// Initialise pseudo random generator
srand( time( NULL ) );

#ifdef __WIN32__
freopen( "cabrio.out", "w", stdout );
freopen( "cabrio.err", "w", stderr );
Expand All @@ -70,7 +70,7 @@ int main( int argc, char *arvg[] ) {

if( ogl_init() != 0 )
bail();

/* Clear the screen as soon as we can. This avoids graphics
* glitches which can occur with some SDL implementations. */
ogl_clear();
Expand Down Expand Up @@ -118,7 +118,7 @@ int main( int argc, char *arvg[] ) {

if( snap_init() != 0 )
bail();

if( game_list_create() != 0 )
bail();

Expand All @@ -130,15 +130,19 @@ int main( int argc, char *arvg[] ) {

sound_init();
video_init();

event_flush();

if( !config_m->iface.theme.menu.auto_hide )
menu_show();

focus_set( FOCUS_GAMESEL );

while( !quit ) {
if( event == EVENT_SELECT ) {
event_set_filter();
event = EVENT_NONE;
}
ogl_clear();
bg_draw();
snap_draw();
Expand All @@ -150,7 +154,7 @@ int main( int argc, char *arvg[] ) {
sdl_swap();
if (Mix_PlayingMusic() != 1 && config_m->iface.theme_sound && reader_running == 0)
playmusic();
if (( event = event_poll() )) {
if (( event = event_poll( event ) )) {
if( supress_wait == 0 ) {
if( event == EVENT_QUIT ) {
quit = 1;
Expand All @@ -163,10 +167,8 @@ int main( int argc, char *arvg[] ) {
}
if( supress_wait > 0 )
supress_wait--;

sdl_frame_delay( config_m->iface.frame_rate );
}
clean_up();
return 0;
}

0 comments on commit a9db70e

Please sign in to comment.