From 74ad38fa95d0b2ee504b04e8762cd284e0f5d784 Mon Sep 17 00:00:00 2001 From: janwas Date: Thu, 24 Jun 2004 14:05:39 +0000 Subject: [PATCH] better interface (constants for event handler return value, instead of "what does it do again" bool) This was SVN commit r597. --- source/lib/input.cpp | 27 +++++++++++++++++++++------ source/lib/input.h | 23 ++++++++++++++++------- 2 files changed, 37 insertions(+), 13 deletions(-) diff --git a/source/lib/input.cpp b/source/lib/input.cpp index 5b997f94fe..53033abe86 100755 --- a/source/lib/input.cpp +++ b/source/lib/input.cpp @@ -21,6 +21,8 @@ #include "precompiled.h" #include "input.h" +#include "sdl.h" +#include "lib.h" #include #include @@ -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); } } diff --git a/source/lib/input.h b/source/lib/input.h index 246e9dd9c1..e7c51660c0 100755 --- a/source/lib/input.h +++ b/source/lib/input.h @@ -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();