Make engine_event_get() return a pointer

This commit is contained in:
Fierelier 2023-05-14 21:41:46 +02:00
parent cecb06903c
commit 32fef09e83
4 changed files with 23 additions and 22 deletions

9
main.c
View File

@ -5,8 +5,8 @@
#include "modules/engine/frontend/generic/textures.c" #include "modules/engine/frontend/generic/textures.c"
#include "modules/engine/addon/lua.c" #include "modules/engine/addon/lua.c"
void handleEvent(struct ENGINE_EVENT event) { void handleEvent(struct ENGINE_EVENT * event) {
if (event.type == ENGINE_EVENT_TYPE_EXIT) { if (event->type == ENGINE_EVENT_TYPE_EXIT) {
exit(0); exit(0);
} }
} }
@ -39,11 +39,12 @@ int main(int argc, char **argv) {
engine_luaInit(); engine_luaInit();
while (1) { while (1) {
struct ENGINE_EVENT event = engine_event_get(); struct ENGINE_EVENT * event = engine_event_get();
if (event.type != ENGINE_EVENT_TYPE_NONE) { if (event->type != ENGINE_EVENT_TYPE_NONE) {
handleEvent(event); handleEvent(event);
} else { } else {
tick(); tick();
} }
engine_free(event);
} }
} }

View File

@ -26,10 +26,9 @@ type = "void"
arguments = ["long long"] arguments = ["long long"]
# LOGIC # LOGIC
# TODO: Make this into a pointer [engine_event_get]
#[engine_event_get] type = "struct ENGINE_EVENT *"
#type = "struct ENGINE_EVENT" arguments = []
#arguments = []
# TEXTURES # TEXTURES
[engine_texture_create] [engine_texture_create]

View File

@ -78,21 +78,21 @@ SDL_Window * engine_sdl_window;
int engine_width = 0; int engine_width = 0;
int engine_height = 0; int engine_height = 0;
struct ENGINE_EVENT engine_event_get() { struct ENGINE_EVENT * engine_event_get() {
struct ENGINE_EVENT event; struct ENGINE_EVENT * event = engine_malloc(NULL,sizeof(struct ENGINE_EVENT));
SDL_Event sdlevent; SDL_Event sdlevent;
if (!SDL_PollEvent(&sdlevent)) { if (!SDL_PollEvent(&sdlevent)) {
struct ENGINE_EVENT_NONE data; struct ENGINE_EVENT_NONE data;
event.type = ENGINE_EVENT_TYPE_NONE; event->type = ENGINE_EVENT_TYPE_NONE;
event.data = &data; event->data = &data;
return event; return event;
} }
if (sdlevent.type == SDL_QUIT) { if (sdlevent.type == SDL_QUIT) {
struct ENGINE_EVENT_EXIT data; struct ENGINE_EVENT_EXIT data;
event.type = ENGINE_EVENT_TYPE_EXIT; event->type = ENGINE_EVENT_TYPE_EXIT;
event.data = &data; event->data = &data;
return event; return event;
} }
@ -101,15 +101,15 @@ struct ENGINE_EVENT engine_event_get() {
struct ENGINE_EVENT_INPUTKB data; struct ENGINE_EVENT_INPUTKB data;
data.pressed = (sdlevent.type == SDL_KEYDOWN); data.pressed = (sdlevent.type == SDL_KEYDOWN);
data.key = sdlevent.key.keysym.scancode; data.key = sdlevent.key.keysym.scancode;
event.type = ENGINE_EVENT_TYPE_INPUTKB; event->type = ENGINE_EVENT_TYPE_INPUTKB;
event.data = &data; event->data = &data;
return event; return event;
} }
unknown: unknown:
struct ENGINE_EVENT_UNKNOWN data; struct ENGINE_EVENT_UNKNOWN data;
event.type = ENGINE_EVENT_TYPE_UNKNOWN; event->type = ENGINE_EVENT_TYPE_UNKNOWN;
event.data = &data; event->data = &data;
return event; return event;
} }

9
test.c
View File

@ -5,8 +5,8 @@
#include "modules/engine/frontend/generic/textures.c" #include "modules/engine/frontend/generic/textures.c"
//#include "modules/engine/frontend/sdl/textures.c" // TODO //#include "modules/engine/frontend/sdl/textures.c" // TODO
void handleEvent(struct ENGINE_EVENT event) { void handleEvent(struct ENGINE_EVENT * event) {
if (event.type == ENGINE_EVENT_TYPE_EXIT) { if (event->type == ENGINE_EVENT_TYPE_EXIT) {
exit(0); exit(0);
} }
} }
@ -134,11 +134,12 @@ int main(int argc, char **argv) {
engine_window_init(256,256,"Game"); engine_window_init(256,256,"Game");
while (1) { while (1) {
struct ENGINE_EVENT event = engine_event_get(); struct ENGINE_EVENT * event = engine_event_get();
if (event.type != ENGINE_EVENT_TYPE_NONE) { if (event->type != ENGINE_EVENT_TYPE_NONE) {
handleEvent(event); handleEvent(event);
} else { } else {
tick(); tick();
} }
engine_free(event);
} }
} }