From f5dcf6ccd38de279dd54ba665aa4097681ff5683 Mon Sep 17 00:00:00 2001 From: Fierelier Date: Sun, 14 May 2023 22:48:25 +0200 Subject: [PATCH] Port game-specific logic to Lua --- assets/scripts/main.lua | 40 ++++++++++++++++++++++++++++++++++++++-- main.c | 36 ------------------------------------ 2 files changed, 38 insertions(+), 38 deletions(-) diff --git a/assets/scripts/main.lua b/assets/scripts/main.lua index c402243..732bbf0 100644 --- a/assets/scripts/main.lua +++ b/assets/scripts/main.lua @@ -1,7 +1,43 @@ +engine_window_init(96,64,"Game") texture = engine_texture_create(8,32) engine_texture_from_file(texture,"assets/textures/fier0.rgba") -a = 0 -function engine_onFrame() +--[[ +void handleEvent(struct ENGINE_EVENT * event) { + if (event->type == ENGINE_EVENT_TYPE_EXIT) { + exit(0); + } +} +]]-- + +frame = 0 +frameSec = 0 +lastSec = 0 +iters = 0 +function tick() + frame = frame + 1 + frameSec = frameSec + 1 + t = engine_time_get() + if t - lastSec >= 1000 then + print("FPS: " ..tostring(frameSec)) + lastSec = t + frameSec = 0 + iters = iters + 1 + if iters > 4 then os.exit() end + end + engine_window_present() +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() engine_texture_render_2d(texture,math.random(-8,102),math.random(-8,72)) end diff --git a/main.c b/main.c index b83d3e6..559d6c6 100644 --- a/main.c +++ b/main.c @@ -5,46 +5,10 @@ #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) { - exit(0); - } -} - -int frame = 0; -int frameSec = 0; -Uint32 lastSec = 0; - -void tick() { - frame += 1; - frameSec += 1; - unsigned long tick = engine_time_get(); - if (tick - lastSec >= 1000) { - printf("FPS: %d\n",frameSec); - lastSec = tick; - frameSec = 0; - } - luaL_loadstring(engine_lua_state,"engine_onFrame()"); - lua_call(engine_lua_state,0,0); - engine_window_present(); - //engine_time_sleep(33); -} - int main(int argc, char **argv) { for (int i = 0; i < argc; ++i) { printf("argv[%d]: %s\n", i, argv[i]); } - engine_window_init(96,64,"Game"); engine_luaInit(); - - while (1) { - struct ENGINE_EVENT * event = engine_event_get(); - if (event->type != ENGINE_EVENT_TYPE_NONE) { - handleEvent(event); - } else { - tick(); - } - engine_free(event); - } }