mirror of
https://gitlab.com/dosowisko.net/libsuperderpy.git
synced 2025-02-12 16:14:23 +01:00
add ability to define game-specific global event handler
This commit is contained in:
parent
5619fc993b
commit
625f202156
2 changed files with 21 additions and 6 deletions
|
@ -42,7 +42,7 @@
|
||||||
|
|
||||||
SYMBOL_EXPORT struct Game* libsuperderpy_init(int argc, char** argv, const char* name, struct Viewport viewport) {
|
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->name = name;
|
||||||
game->viewport_config = viewport;
|
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->_priv.garbage = NULL;
|
||||||
|
|
||||||
|
game->eventHandler = NULL;
|
||||||
|
|
||||||
game->config.fullscreen = atoi(GetConfigOptionDefault(game, "SuperDerpy", "fullscreen", "1"));
|
game->config.fullscreen = atoi(GetConfigOptionDefault(game, "SuperDerpy", "fullscreen", "1"));
|
||||||
game->config.music = atoi(GetConfigOptionDefault(game, "SuperDerpy", "music", "10"));
|
game->config.music = atoi(GetConfigOptionDefault(game, "SuperDerpy", "music", "10"));
|
||||||
game->config.voice = atoi(GetConfigOptionDefault(game, "SuperDerpy", "voice", "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;
|
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
|
#ifdef LIBSUPERDERPY_MOUSE_EMULATION
|
||||||
if (game->_priv.touch) {
|
if (game->touch) {
|
||||||
al_set_mouse_emulation_mode(ALLEGRO_MOUSE_EMULATION_TRANSPARENT);
|
al_set_mouse_emulation_mode(ALLEGRO_MOUSE_EMULATION_TRANSPARENT);
|
||||||
}
|
}
|
||||||
#endif
|
#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_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_mouse_event_source());
|
||||||
al_register_event_source(game->_priv.event_queue, al_get_keyboard_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());
|
al_register_event_source(game->_priv.event_queue, al_get_touch_input_event_source());
|
||||||
#ifdef LIBSUPERDERPY_MOUSE_EMULATION
|
#ifdef LIBSUPERDERPY_MOUSE_EMULATION
|
||||||
al_register_event_source(game->_priv.event_queue, al_get_touch_input_mouse_emulation_event_source());
|
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;
|
game->_priv.draw = true;
|
||||||
|
|
||||||
while(1) {
|
while(1) {
|
||||||
|
ClearGarbage(game);
|
||||||
|
|
||||||
// TODO: split mainloop to functions to make it readable
|
// TODO: split mainloop to functions to make it readable
|
||||||
ALLEGRO_EVENT ev;
|
ALLEGRO_EVENT ev;
|
||||||
if (game->_priv.draw && ((redraw && al_is_event_queue_empty(game->_priv.event_queue)) || (game->_priv.gamestate_scheduled))) {
|
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);
|
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)) {
|
if ((ev.type == ALLEGRO_EVENT_TIMER) && (ev.timer.source == game->_priv.timer)) {
|
||||||
LogicGamestates(game);
|
LogicGamestates(game);
|
||||||
redraw = true;
|
redraw = true;
|
||||||
|
@ -464,7 +478,6 @@ SYMBOL_EXPORT int libsuperderpy_run(struct Game *game) {
|
||||||
}
|
}
|
||||||
EventGamestates(game, &ev);
|
EventGamestates(game, &ev);
|
||||||
}
|
}
|
||||||
ClearGarbage(game);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
@ -122,7 +122,6 @@ struct Game {
|
||||||
struct libsuperderpy_list *garbage;
|
struct libsuperderpy_list *garbage;
|
||||||
|
|
||||||
bool draw;
|
bool draw;
|
||||||
bool touch;
|
|
||||||
|
|
||||||
#ifdef ALLEGRO_MACOSX
|
#ifdef ALLEGRO_MACOSX
|
||||||
char cwd[MAXPATHLEN];
|
char cwd[MAXPATHLEN];
|
||||||
|
@ -132,6 +131,7 @@ struct Game {
|
||||||
|
|
||||||
bool shuttingdown; /*!< If true then shut down of the game is pending. */
|
bool shuttingdown; /*!< If true then shut down of the game is pending. */
|
||||||
bool restart; /*!< If true then restart of the game is pending. */
|
bool restart; /*!< If true then restart of the game is pending. */
|
||||||
|
bool touch;
|
||||||
|
|
||||||
bool show_loading_on_launch;
|
bool show_loading_on_launch;
|
||||||
|
|
||||||
|
@ -139,6 +139,8 @@ struct Game {
|
||||||
|
|
||||||
ALLEGRO_EVENT_SOURCE event_source;
|
ALLEGRO_EVENT_SOURCE event_source;
|
||||||
|
|
||||||
|
bool (*eventHandler)(struct Game *game, ALLEGRO_EVENT *ev);
|
||||||
|
|
||||||
LIBSUPERDERPY_DATA_TYPE *data;
|
LIBSUPERDERPY_DATA_TYPE *data;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue