From 32fef09e83ec30e4a70730932be30c0162ea75c3 Mon Sep 17 00:00:00 2001 From: Fierelier Date: Sun, 14 May 2023 21:41:46 +0200 Subject: [PATCH] Make engine_event_get() return a pointer --- main.c | 9 +++++---- modules/engine/FUNCTIONS.toml | 7 +++---- modules/engine/frontend/sdl/main.c | 20 ++++++++++---------- test.c | 9 +++++---- 4 files changed, 23 insertions(+), 22 deletions(-) diff --git a/main.c b/main.c index e8ee720..b83d3e6 100644 --- a/main.c +++ b/main.c @@ -5,8 +5,8 @@ #include "modules/engine/frontend/generic/textures.c" #include "modules/engine/addon/lua.c" -void handleEvent(struct ENGINE_EVENT event) { - if (event.type == ENGINE_EVENT_TYPE_EXIT) { +void handleEvent(struct ENGINE_EVENT * event) { + if (event->type == ENGINE_EVENT_TYPE_EXIT) { exit(0); } } @@ -39,11 +39,12 @@ int main(int argc, char **argv) { engine_luaInit(); while (1) { - struct ENGINE_EVENT event = engine_event_get(); - if (event.type != ENGINE_EVENT_TYPE_NONE) { + struct ENGINE_EVENT * event = engine_event_get(); + if (event->type != ENGINE_EVENT_TYPE_NONE) { handleEvent(event); } else { tick(); } + engine_free(event); } } diff --git a/modules/engine/FUNCTIONS.toml b/modules/engine/FUNCTIONS.toml index b0874f7..d224438 100644 --- a/modules/engine/FUNCTIONS.toml +++ b/modules/engine/FUNCTIONS.toml @@ -26,10 +26,9 @@ type = "void" arguments = ["long long"] # LOGIC -# TODO: Make this into a pointer -#[engine_event_get] -#type = "struct ENGINE_EVENT" -#arguments = [] +[engine_event_get] +type = "struct ENGINE_EVENT *" +arguments = [] # TEXTURES [engine_texture_create] diff --git a/modules/engine/frontend/sdl/main.c b/modules/engine/frontend/sdl/main.c index 062646e..7347d25 100644 --- a/modules/engine/frontend/sdl/main.c +++ b/modules/engine/frontend/sdl/main.c @@ -78,21 +78,21 @@ SDL_Window * engine_sdl_window; int engine_width = 0; int engine_height = 0; -struct ENGINE_EVENT engine_event_get() { - struct ENGINE_EVENT event; +struct ENGINE_EVENT * engine_event_get() { + struct ENGINE_EVENT * event = engine_malloc(NULL,sizeof(struct ENGINE_EVENT)); SDL_Event sdlevent; if (!SDL_PollEvent(&sdlevent)) { struct ENGINE_EVENT_NONE data; - event.type = ENGINE_EVENT_TYPE_NONE; - event.data = &data; + event->type = ENGINE_EVENT_TYPE_NONE; + event->data = &data; return event; } if (sdlevent.type == SDL_QUIT) { struct ENGINE_EVENT_EXIT data; - event.type = ENGINE_EVENT_TYPE_EXIT; - event.data = &data; + event->type = ENGINE_EVENT_TYPE_EXIT; + event->data = &data; return event; } @@ -101,15 +101,15 @@ struct ENGINE_EVENT engine_event_get() { struct ENGINE_EVENT_INPUTKB data; data.pressed = (sdlevent.type == SDL_KEYDOWN); data.key = sdlevent.key.keysym.scancode; - event.type = ENGINE_EVENT_TYPE_INPUTKB; - event.data = &data; + event->type = ENGINE_EVENT_TYPE_INPUTKB; + event->data = &data; return event; } unknown: struct ENGINE_EVENT_UNKNOWN data; - event.type = ENGINE_EVENT_TYPE_UNKNOWN; - event.data = &data; + event->type = ENGINE_EVENT_TYPE_UNKNOWN; + event->data = &data; return event; } diff --git a/test.c b/test.c index 4aa22f3..dcc0066 100644 --- a/test.c +++ b/test.c @@ -5,8 +5,8 @@ #include "modules/engine/frontend/generic/textures.c" //#include "modules/engine/frontend/sdl/textures.c" // TODO -void handleEvent(struct ENGINE_EVENT event) { - if (event.type == ENGINE_EVENT_TYPE_EXIT) { +void handleEvent(struct ENGINE_EVENT * event) { + if (event->type == ENGINE_EVENT_TYPE_EXIT) { exit(0); } } @@ -134,11 +134,12 @@ int main(int argc, char **argv) { engine_window_init(256,256,"Game"); while (1) { - struct ENGINE_EVENT event = engine_event_get(); - if (event.type != ENGINE_EVENT_TYPE_NONE) { + struct ENGINE_EVENT * event = engine_event_get(); + if (event->type != ENGINE_EVENT_TYPE_NONE) { handleEvent(event); } else { tick(); } + engine_free(event); } }