diff --git a/assets/scripts/main.lua b/assets/scripts/main.lua index 00fe491..033c83d 100644 --- a/assets/scripts/main.lua +++ b/assets/scripts/main.lua @@ -13,7 +13,6 @@ void handleEvent(struct ENGINE_EVENT * event) { frame = 0 frameSec = 0 lastSec = 0 -iters = 0 function tick() frame = frame + 1 frameSec = frameSec + 1 @@ -22,22 +21,31 @@ function tick() print("FPS: " ..tostring(frameSec)) lastSec = t frameSec = 0 - iters = iters + 1 - if iters > 4 then os.exit() end end engine_window_present() end +function handleEvent(event,eventData) + if eventData[1] == ENGINE_EVENT_TYPE_EXIT then + os.exit() + end + + if eventData[1] == ENGINE_EVENT_TYPE_INPUT_KB then + for _,val in pairs(eventData) do + print(tostring(val)) + end + end +end + while true do event = engine_event_get() - --[[ - if (event->type != ENGINE_EVENT_TYPE_NONE) { - handleEvent(event); - } else { - tick(); - } - ]]-- - engine_free(event) -- We can't actually do anything with the event yet (no Lua implementation) - tick() + eventData = {engine_lua_event_get_data(event)} + if eventData[1] ~= ENGINE_EVENT_TYPE_NONE then + handleEvent(event,eventData) + else + tick() + end + + engine_event_free(event) engine_texture_render_2d(texture,math.random(-8,102),math.random(-8,72)) end diff --git a/lua_translate b/lua_translate index d276cbe..bf9cd89 100755 --- a/lua_translate +++ b/lua_translate @@ -20,6 +20,7 @@ functionBlacklist = [ "engine_malloc" ] +statics = toml.loads(open("modules/engine/STATICS.toml").read()) functions = toml.loads(open("modules/engine/FUNCTIONS.toml").read()) ofile = open("modules/engine/lua.c","w") @@ -27,9 +28,9 @@ ofile.write('''\ #include #include #include +lua_State * engine_lua_state; #include "lua_manual.c" -lua_State * engine_lua_state; ''') for func in functions: @@ -70,6 +71,10 @@ void engine_lua_init() { ''') +for static in statics: + ofile.write('\tlua_pushinteger(engine_lua_state,' +static+ ');\n') + ofile.write('\tlua_setglobal(engine_lua_state,"' +static+ '");\n') + for func in functions: if func in functionBlacklist: continue funcnew = "engine_luaf_" +func.replace("engine_","",1) diff --git a/modules/engine/FUNCTIONS.toml b/modules/engine/FUNCTIONS.toml index 519c882..8bbdcc6 100644 --- a/modules/engine/FUNCTIONS.toml +++ b/modules/engine/FUNCTIONS.toml @@ -57,6 +57,12 @@ arguments = [] argNames = [] description = "Get an event. If the type is ENGINE_EVENT_TYPE_NONE, there are no more events. Needs to be freed with engine_free()." +[engine_event_free] +type = "void" +arguments = ["struct ENGINE_EVENT *"] +argNames = ["event"] +description = "Free the event instance from memory." + # TEXTURES [engine_texture_create] type = "struct ENGINE_TEXTURE *" diff --git a/modules/engine/STATICS.toml b/modules/engine/STATICS.toml new file mode 100644 index 0000000..ad18d54 --- /dev/null +++ b/modules/engine/STATICS.toml @@ -0,0 +1,19 @@ +[ENGINE_EVENT_TYPE_UNKNOWN] +type = "char" +value = 0 +description = "Unknown event (not implemented)." + +[ENGINE_EVENT_TYPE_NONE] +type = "char" +value = 1 +description = "Signifies that no more events are in the queue." + +[ENGINE_EVENT_TYPE_EXIT] +type = "char" +value = 2 +description = "User has requested the application to exit." + +[ENGINE_EVENT_TYPE_INPUT_KB] +type = "char" +value = 3 +description = "A keyboard input has occured." diff --git a/modules/engine/frontend/sdl/main.c b/modules/engine/frontend/sdl/main.c index 7347d25..c49d8fc 100644 --- a/modules/engine/frontend/sdl/main.c +++ b/modules/engine/frontend/sdl/main.c @@ -83,33 +83,33 @@ struct ENGINE_EVENT * engine_event_get() { SDL_Event sdlevent; if (!SDL_PollEvent(&sdlevent)) { - struct ENGINE_EVENT_NONE data; + struct ENGINE_EVENT_NONE * data = engine_malloc(NULL,sizeof(struct ENGINE_EVENT_NONE)); event->type = ENGINE_EVENT_TYPE_NONE; - event->data = &data; + event->data = data; return event; } if (sdlevent.type == SDL_QUIT) { - struct ENGINE_EVENT_EXIT data; + struct ENGINE_EVENT_EXIT * data = engine_malloc(NULL,sizeof(struct ENGINE_EVENT_EXIT));; event->type = ENGINE_EVENT_TYPE_EXIT; - event->data = &data; + event->data = data; return event; } if (sdlevent.type == SDL_KEYDOWN || sdlevent.type == SDL_KEYUP) { if (sdlevent.key.repeat != 0) { goto unknown; } - 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; + struct ENGINE_EVENT_INPUT_KB * data = engine_malloc(NULL,sizeof(struct ENGINE_EVENT_INPUT_KB)); + data->pressed = (sdlevent.type == SDL_KEYDOWN); + data->key = sdlevent.key.keysym.scancode; + event->type = ENGINE_EVENT_TYPE_INPUT_KB; + event->data = data; return event; } unknown: - struct ENGINE_EVENT_UNKNOWN data; + struct ENGINE_EVENT_UNKNOWN * data = engine_malloc(NULL,sizeof(struct ENGINE_EVENT_INPUT_KB)); event->type = ENGINE_EVENT_TYPE_UNKNOWN; - event->data = &data; + event->data = data; return event; } diff --git a/modules/engine/lua.c b/modules/engine/lua.c index 6e0dc86..9497c41 100644 --- a/modules/engine/lua.c +++ b/modules/engine/lua.c @@ -1,9 +1,9 @@ #include #include #include +lua_State * engine_lua_state; #include "lua_manual.c" -lua_State * engine_lua_state; int engine_luaf_free(lua_State *L) { void * ptr = lua_touserdata(L,1); engine_free(ptr); @@ -57,6 +57,12 @@ int engine_luaf_event_get(lua_State *L) { return 1; } +int engine_luaf_event_free(lua_State *L) { + struct ENGINE_EVENT * event = lua_touserdata(L,1); + engine_event_free(event); + return 0; +} + int engine_luaf_texture_create(lua_State *L) { int width = luaL_checkinteger(L,1); int height = luaL_checkinteger(L,2); @@ -107,6 +113,14 @@ void engine_lua_init() { engine_lua_state = luaL_newstate(); luaL_openlibs(engine_lua_state); + lua_pushinteger(engine_lua_state,ENGINE_EVENT_TYPE_UNKNOWN); + lua_setglobal(engine_lua_state,"ENGINE_EVENT_TYPE_UNKNOWN"); + lua_pushinteger(engine_lua_state,ENGINE_EVENT_TYPE_NONE); + lua_setglobal(engine_lua_state,"ENGINE_EVENT_TYPE_NONE"); + lua_pushinteger(engine_lua_state,ENGINE_EVENT_TYPE_EXIT); + lua_setglobal(engine_lua_state,"ENGINE_EVENT_TYPE_EXIT"); + lua_pushinteger(engine_lua_state,ENGINE_EVENT_TYPE_INPUT_KB); + lua_setglobal(engine_lua_state,"ENGINE_EVENT_TYPE_INPUT_KB"); lua_pushcfunction(engine_lua_state,engine_luaf_free); lua_setglobal (engine_lua_state,"engine_free"); lua_pushcfunction(engine_lua_state,engine_luaf_window_init); @@ -123,6 +137,8 @@ void engine_lua_init() { lua_setglobal (engine_lua_state,"engine_time_sleep"); lua_pushcfunction(engine_lua_state,engine_luaf_event_get); lua_setglobal (engine_lua_state,"engine_event_get"); + lua_pushcfunction(engine_lua_state,engine_luaf_event_free); + lua_setglobal (engine_lua_state,"engine_event_free"); lua_pushcfunction(engine_lua_state,engine_luaf_texture_create); lua_setglobal (engine_lua_state,"engine_texture_create"); lua_pushcfunction(engine_lua_state,engine_luaf_texture_color_set); diff --git a/modules/engine/lua_manual.c b/modules/engine/lua_manual.c index 292307b..befc5b9 100644 --- a/modules/engine/lua_manual.c +++ b/modules/engine/lua_manual.c @@ -1,3 +1,24 @@ -void engine_lua_init_manual() { +int engine_luaf_lua_event_get_data(lua_State *L) { + struct ENGINE_EVENT * event = lua_touserdata(L,1); + char type = event->type; + if (type == ENGINE_EVENT_TYPE_UNKNOWN) { goto simple; } + if (type == ENGINE_EVENT_TYPE_NONE) { goto simple; } + if (type == ENGINE_EVENT_TYPE_EXIT) { goto simple; } + if (type == ENGINE_EVENT_TYPE_INPUT_KB) { + struct ENGINE_EVENT_INPUT_KB * data = event->data; + lua_pushinteger(L,type); + lua_pushinteger(L,data->key); + lua_pushinteger(L,data->pressed); + return 3; + } + type = ENGINE_EVENT_TYPE_UNKNOWN; + simple: + lua_pushinteger(L,type); + return 1; +} + +void engine_lua_init_manual() { + lua_pushcfunction(engine_lua_state,engine_luaf_lua_event_get_data); + lua_setglobal (engine_lua_state,"engine_lua_event_get_data"); } diff --git a/modules/engine/main.c b/modules/engine/main.c index 0b81d92..2d3703a 100644 --- a/modules/engine/main.c +++ b/modules/engine/main.c @@ -5,8 +5,8 @@ static char ENGINE_EVENT_TYPE_NONE = 1; struct ENGINE_EVENT_NONE { }; static char ENGINE_EVENT_TYPE_EXIT = 2; struct ENGINE_EVENT_EXIT { }; -static char ENGINE_EVENT_TYPE_INPUTKB = 3; -struct ENGINE_EVENT_INPUTKB { char key; char pressed; }; +static char ENGINE_EVENT_TYPE_INPUT_KB = 3; +struct ENGINE_EVENT_INPUT_KB { char key; char pressed; }; void * engine_malloc(void * pnt,size_t size) { void * mem = realloc(pnt,size); @@ -20,3 +20,8 @@ void * engine_malloc(void * pnt,size_t size) { void engine_free(void *pnt) { free(pnt); } + +void engine_event_free(struct ENGINE_EVENT * event) { + engine_free(event->data); + engine_free(event); +}