From 625f202156bdf8d06e40e1f6915f27ddcca7d712 Mon Sep 17 00:00:00 2001 From: Sebastian Krzyszkowiak Date: Tue, 8 Nov 2016 22:11:10 +0100 Subject: [PATCH] add ability to define game-specific global event handler --- src/libsuperderpy.c | 23 ++++++++++++++++++----- src/libsuperderpy.h | 4 +++- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/libsuperderpy.c b/src/libsuperderpy.c index 3885131..0039267 100644 --- a/src/libsuperderpy.c +++ b/src/libsuperderpy.c @@ -42,7 +42,7 @@ SYMBOL_EXPORT struct Game* libsuperderpy_init(int argc, char** argv, const char* name, struct Viewport viewport) { - struct Game *game = malloc(sizeof(struct Game)); + struct Game *game = calloc(1, sizeof(struct Game)); game->name = name; game->viewport_config = viewport; @@ -73,6 +73,8 @@ SYMBOL_EXPORT struct Game* libsuperderpy_init(int argc, char** argv, const char* game->_priv.garbage = NULL; + game->eventHandler = NULL; + game->config.fullscreen = atoi(GetConfigOptionDefault(game, "SuperDerpy", "fullscreen", "1")); game->config.music = atoi(GetConfigOptionDefault(game, "SuperDerpy", "music", "10")); game->config.voice = atoi(GetConfigOptionDefault(game, "SuperDerpy", "voice", "10")); @@ -124,10 +126,14 @@ SYMBOL_EXPORT struct Game* libsuperderpy_init(int argc, char** argv, const char* return NULL; } - game->_priv.touch = al_install_touch_input(); + game->touch = false; + + if (!atoi(GetConfigOptionDefault(game, "SuperDerpy", "disableTouch", "0"))) { + game->touch = al_install_touch_input(); + } #ifdef LIBSUPERDERPY_MOUSE_EMULATION - if (game->_priv.touch) { + if (game->touch) { al_set_mouse_emulation_mode(ALLEGRO_MOUSE_EMULATION_TRANSPARENT); } #endif @@ -209,7 +215,7 @@ SYMBOL_EXPORT int libsuperderpy_run(struct Game *game) { al_register_event_source(game->_priv.event_queue, al_get_display_event_source(game->display)); al_register_event_source(game->_priv.event_queue, al_get_mouse_event_source()); al_register_event_source(game->_priv.event_queue, al_get_keyboard_event_source()); - if (game->_priv.touch) { + if (game->touch) { al_register_event_source(game->_priv.event_queue, al_get_touch_input_event_source()); #ifdef LIBSUPERDERPY_MOUSE_EMULATION al_register_event_source(game->_priv.event_queue, al_get_touch_input_mouse_emulation_event_source()); @@ -258,6 +264,8 @@ SYMBOL_EXPORT int libsuperderpy_run(struct Game *game) { game->_priv.draw = true; while(1) { + ClearGarbage(game); + // TODO: split mainloop to functions to make it readable ALLEGRO_EVENT ev; if (game->_priv.draw && ((redraw && al_is_event_queue_empty(game->_priv.event_queue)) || (game->_priv.gamestate_scheduled))) { @@ -390,6 +398,12 @@ SYMBOL_EXPORT int libsuperderpy_run(struct Game *game) { al_wait_for_event(game->_priv.event_queue, &ev); + if (game->eventHandler) { + if ((*game->eventHandler)(game, &ev)) { + continue; + } + } + if ((ev.type == ALLEGRO_EVENT_TIMER) && (ev.timer.source == game->_priv.timer)) { LogicGamestates(game); redraw = true; @@ -464,7 +478,6 @@ SYMBOL_EXPORT int libsuperderpy_run(struct Game *game) { } EventGamestates(game, &ev); } - ClearGarbage(game); } return 0; diff --git a/src/libsuperderpy.h b/src/libsuperderpy.h index 2508c38..4ecef77 100644 --- a/src/libsuperderpy.h +++ b/src/libsuperderpy.h @@ -122,7 +122,6 @@ struct Game { struct libsuperderpy_list *garbage; bool draw; - bool touch; #ifdef ALLEGRO_MACOSX char cwd[MAXPATHLEN]; @@ -132,6 +131,7 @@ struct Game { bool shuttingdown; /*!< If true then shut down of the game is pending. */ bool restart; /*!< If true then restart of the game is pending. */ + bool touch; bool show_loading_on_launch; @@ -139,6 +139,8 @@ struct Game { ALLEGRO_EVENT_SOURCE event_source; + bool (*eventHandler)(struct Game *game, ALLEGRO_EVENT *ev); + LIBSUPERDERPY_DATA_TYPE *data; };