1
0
forked from 0ad/0ad

better interface (constants for event handler return value, instead of "what does it do again" bool)

This was SVN commit r597.
This commit is contained in:
janwas 2004-06-24 14:05:39 +00:00
parent ce8f5d3520
commit 74ad38fa95
2 changed files with 37 additions and 13 deletions

View File

@ -21,6 +21,8 @@
#include "precompiled.h"
#include "input.h"
#include "sdl.h"
#include "lib.h"
#include <stdio.h>
#include <stdlib.h>
@ -28,14 +30,17 @@
#define MAX_HANDLERS 4
static IN_HANDLER handler_stack[MAX_HANDLERS];
static EventHandler handler_stack[MAX_HANDLERS];
static int handler_stack_top = 0;
int in_add_handler(IN_HANDLER handler)
int _in_add_handler(EventHandler handler)
{
if(handler_stack_top >= MAX_HANDLERS || !handler)
{
debug_warn("in_add_handler");
return -1;
}
handler_stack[handler_stack_top++] = handler;
@ -44,11 +49,21 @@ int in_add_handler(IN_HANDLER handler)
/* send event to each handler (newest first) until one returns true */
static void dispatch_event(const SDL_Event& event)
static void dispatch_event(const SDL_Event* event)
{
for(int i = handler_stack_top-1; i >= 0; i--)
if(handler_stack[i](event))
{
int ret = handler_stack[i](event);
// .. done, return
if(ret == EV_HANDLED)
return;
// .. next handler
else if(ret == EV_PASS)
continue;
// .. invalid return value
else
debug_warn("dispatch_event: invalid handler return value");
}
}
@ -145,7 +160,7 @@ exit(0x73c07d);
}
next_event_time += time_adjust;
dispatch_event(event);
dispatch_event(&event);
}
/* get new events */
@ -161,6 +176,6 @@ exit(0x73c07d);
if(event.type == SDL_KEYDOWN)
in_stop();
dispatch_event(event);
dispatch_event(&event);
}
}

View File

@ -22,25 +22,34 @@
#define __INPUT_H__
#include "sdl.h"
#include "types.h"
#ifdef __cplusplus
extern "C" {
#endif
extern u32 game_ticks;
// event handler return value defs (int).
// don't require an enum type - simplifies user function decl;
// the dispatcher makes sure each return value is correct.
enum
{
// pass the event to the next handler in the chain
EV_PASS = 4,
// we've handled it; no other handlers will receive this event.
EV_HANDLED = 2
};
// declare functions to take SDL_Event*; in_add_handler converts to void*
// (avoids header dependency on SDL)
typedef int (*EventHandler)(const void* sdl_event);
typedef bool (*IN_HANDLER)(const SDL_Event& event);
/*
* register an input handler, which will receive all subsequent events first.
* events are passed to other handlers if handler returns false.
*/
extern int in_add_handler(IN_HANDLER handler);
extern int _in_add_handler(EventHandler handler);
#define in_add_handler(h) _in_add_handler((EventHandler)h)
extern void in_get_events();