2006-03-20 17:12:09 +00:00
|
|
|
/* $Id: event.c,v 1.1.1.1 2006/03/17 19:53:40 zicodxx Exp $ */
|
|
|
|
/*
|
|
|
|
*
|
|
|
|
* SDL Event related stuff
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2009-05-21 12:16:39 +00:00
|
|
|
#include "event.h"
|
2008-11-17 23:38:43 +00:00
|
|
|
#include "key.h"
|
2009-05-21 12:16:39 +00:00
|
|
|
#include "window.h"
|
2010-07-16 11:07:42 +00:00
|
|
|
#include "timer.h"
|
2006-03-20 17:12:09 +00:00
|
|
|
|
2007-04-09 02:41:45 +00:00
|
|
|
#include <SDL/SDL.h>
|
2006-03-20 17:12:09 +00:00
|
|
|
|
2010-07-16 11:07:42 +00:00
|
|
|
extern void key_handler(SDL_KeyboardEvent *event, fix time);
|
|
|
|
extern void mouse_button_handler(SDL_MouseButtonEvent *mbe, fix time);
|
|
|
|
extern void mouse_motion_handler(SDL_MouseMotionEvent *mme, fix time);
|
|
|
|
extern void joy_button_handler(SDL_JoyButtonEvent *jbe, fix time);
|
|
|
|
extern void joy_hat_handler(SDL_JoyHatEvent *jhe, fix time);
|
|
|
|
extern void joy_axis_handler(SDL_JoyAxisEvent *jae, fix time);
|
2010-07-26 18:10:21 +00:00
|
|
|
extern void mouse_update_cursor_and_grab(fix time);
|
2006-03-20 17:12:09 +00:00
|
|
|
|
|
|
|
static int initialised=0;
|
|
|
|
|
|
|
|
void event_poll()
|
|
|
|
{
|
|
|
|
SDL_Event event;
|
2008-11-17 23:38:43 +00:00
|
|
|
int clean_uniframe=1;
|
2010-07-16 11:07:42 +00:00
|
|
|
fix time = timer_get_fixed_seconds();
|
2010-07-25 00:49:33 +00:00
|
|
|
window *wind = window_get_front();
|
2006-03-20 17:12:09 +00:00
|
|
|
|
2010-07-25 00:49:33 +00:00
|
|
|
// If the front window changes, exit this loop, otherwise unintended behavior can occur
|
|
|
|
// like pressing 'Return' really fast at 'Difficulty Level' causing multiple games to be started
|
|
|
|
while ((wind == window_get_front()) && SDL_PollEvent(&event))
|
|
|
|
{
|
2006-03-20 17:12:09 +00:00
|
|
|
switch(event.type) {
|
2008-11-17 23:38:43 +00:00
|
|
|
case SDL_KEYDOWN:
|
|
|
|
case SDL_KEYUP:
|
|
|
|
if (clean_uniframe)
|
|
|
|
memset(unicode_frame_buffer,'\0',sizeof(unsigned char)*KEY_BUFFER_SIZE);
|
|
|
|
clean_uniframe=0;
|
2010-07-16 11:07:42 +00:00
|
|
|
key_handler((SDL_KeyboardEvent *)&event, time);
|
2008-11-17 23:38:43 +00:00
|
|
|
break;
|
|
|
|
case SDL_MOUSEBUTTONDOWN:
|
|
|
|
case SDL_MOUSEBUTTONUP:
|
2010-07-16 11:07:42 +00:00
|
|
|
mouse_button_handler((SDL_MouseButtonEvent *)&event, time);
|
2008-11-17 23:38:43 +00:00
|
|
|
break;
|
|
|
|
case SDL_MOUSEMOTION:
|
2010-07-16 11:07:42 +00:00
|
|
|
mouse_motion_handler((SDL_MouseMotionEvent *)&event, time);
|
2008-11-17 23:38:43 +00:00
|
|
|
break;
|
|
|
|
case SDL_JOYBUTTONDOWN:
|
|
|
|
case SDL_JOYBUTTONUP:
|
2010-07-16 11:07:42 +00:00
|
|
|
joy_button_handler((SDL_JoyButtonEvent *)&event, time);
|
2008-11-17 23:38:43 +00:00
|
|
|
break;
|
|
|
|
case SDL_JOYAXISMOTION:
|
2010-07-16 11:07:42 +00:00
|
|
|
joy_axis_handler((SDL_JoyAxisEvent *)&event, time);
|
2008-11-17 23:38:43 +00:00
|
|
|
break;
|
|
|
|
case SDL_JOYHATMOTION:
|
2010-07-16 11:07:42 +00:00
|
|
|
joy_hat_handler((SDL_JoyHatEvent *)&event, time);
|
2008-11-17 23:38:43 +00:00
|
|
|
break;
|
|
|
|
case SDL_JOYBALLMOTION:
|
|
|
|
break;
|
|
|
|
case SDL_QUIT: {
|
2010-04-04 01:31:48 +00:00
|
|
|
d_event event = { EVENT_QUIT };
|
|
|
|
call_default_handler(&event);
|
2008-11-17 23:38:43 +00:00
|
|
|
} break;
|
2006-03-20 17:12:09 +00:00
|
|
|
}
|
|
|
|
}
|
2010-07-16 11:07:42 +00:00
|
|
|
|
2010-07-26 18:10:21 +00:00
|
|
|
mouse_update_cursor_and_grab(time);
|
2006-03-20 17:12:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int event_init()
|
|
|
|
{
|
|
|
|
// We should now be active and responding to events.
|
|
|
|
initialised = 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2009-05-21 12:16:39 +00:00
|
|
|
|
2010-04-02 05:01:08 +00:00
|
|
|
int (*default_handler)(d_event *event) = NULL;
|
|
|
|
|
|
|
|
void set_default_handler(int (*handler)(d_event *event))
|
|
|
|
{
|
|
|
|
default_handler = handler;
|
|
|
|
}
|
|
|
|
|
|
|
|
int call_default_handler(d_event *event)
|
|
|
|
{
|
|
|
|
if (default_handler)
|
|
|
|
return (*default_handler)(event);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-05-21 12:16:39 +00:00
|
|
|
// Process the first event in queue, sending to the appropriate handler
|
|
|
|
// This is the new object-oriented system
|
2010-01-30 03:24:19 +00:00
|
|
|
// Uses the old system for now, but this may change
|
2009-05-21 12:16:39 +00:00
|
|
|
void event_process(void)
|
|
|
|
{
|
|
|
|
d_event event;
|
|
|
|
window *wind;
|
|
|
|
|
2010-01-30 03:24:19 +00:00
|
|
|
event_poll(); // send input events first
|
|
|
|
|
2010-04-02 05:01:08 +00:00
|
|
|
event.type = EVENT_IDLE;
|
|
|
|
if ((wind = window_get_front()))
|
|
|
|
{
|
|
|
|
if (!window_send_event(wind, &event))
|
|
|
|
call_default_handler(&event);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
call_default_handler(&event);
|
|
|
|
|
2010-01-20 05:10:32 +00:00
|
|
|
event.type = EVENT_WINDOW_DRAW; // then draw all visible windows
|
2009-05-21 12:16:39 +00:00
|
|
|
for (wind = window_get_first(); wind != NULL; wind = window_get_next(wind))
|
|
|
|
if (window_is_visible(wind))
|
|
|
|
window_send_event(wind, &event);
|
2009-12-26 01:08:57 +00:00
|
|
|
|
|
|
|
gr_flip();
|
2009-05-21 12:16:39 +00:00
|
|
|
}
|