From e5914a3f54a81cf5c529487cedc2481920cf19f3 Mon Sep 17 00:00:00 2001 From: Fierelier Date: Tue, 16 May 2023 12:29:12 +0200 Subject: [PATCH] Restructure --- assets/scripts/main.lua | 51 ------ assets/textures/fier1.rgba | Bin 256 -> 0 bytes assets/textures/fier2.rgba | Bin 256 -> 0 bytes lua_translate | 2 +- main.c | 15 -- mods/main/script/main.lua | 61 ++++++++ .../fier0.rgba => mods/main/texture/fier.rgba | Bin run | 2 +- {modules => src}/engine/FUNCTIONS.toml | 0 {modules => src}/engine/STATICS.toml | 0 .../engine/frontend/generic/textures.c | 0 {modules => src}/engine/frontend/sdl/main.c | 2 +- {modules => src}/engine/lua.c | 2 +- {modules => src}/engine/lua_manual.c | 0 {modules => src}/engine/main.c | 0 {modules => src}/engine/textures.c | 0 src/main.c | 16 ++ test.c | 145 ------------------ 18 files changed, 81 insertions(+), 215 deletions(-) delete mode 100644 assets/scripts/main.lua delete mode 100644 assets/textures/fier1.rgba delete mode 100644 assets/textures/fier2.rgba delete mode 100644 main.c create mode 100644 mods/main/script/main.lua rename assets/textures/fier0.rgba => mods/main/texture/fier.rgba (100%) rename {modules => src}/engine/FUNCTIONS.toml (100%) rename {modules => src}/engine/STATICS.toml (100%) rename {modules => src}/engine/frontend/generic/textures.c (100%) rename {modules => src}/engine/frontend/sdl/main.c (99%) rename {modules => src}/engine/lua.c (98%) rename {modules => src}/engine/lua_manual.c (100%) rename {modules => src}/engine/main.c (100%) rename {modules => src}/engine/textures.c (100%) create mode 100644 src/main.c delete mode 100644 test.c diff --git a/assets/scripts/main.lua b/assets/scripts/main.lua deleted file mode 100644 index 033c83d..0000000 --- a/assets/scripts/main.lua +++ /dev/null @@ -1,51 +0,0 @@ -engine_window_init(96,64,"Game") -texture = engine_texture_create(8,8) -engine_texture_from_file(texture,"assets/textures/fier0.rgba") - ---[[ -void handleEvent(struct ENGINE_EVENT * event) { - if (event->type == ENGINE_EVENT_TYPE_EXIT) { - exit(0); - } -} -]]-- - -frame = 0 -frameSec = 0 -lastSec = 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 - 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() - 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/assets/textures/fier1.rgba b/assets/textures/fier1.rgba deleted file mode 100644 index daf854ce467a2c134ea3b38020702e868569353b..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 256 zcmezW|3AZjAa;$f`|lK6`yWJu_~;lU$H2e -#include -#include "modules/engine/main.c" -#include "modules/engine/textures.c" -#include "modules/engine/frontend/sdl/main.c" -#include "modules/engine/frontend/generic/textures.c" -#include "modules/engine/lua.c" - -int main(int argc, char **argv) { - for (int i = 0; i < argc; ++i) { - printf("argv[%d]: %s\n", i, argv[i]); - } - - engine_lua_init(); -} diff --git a/mods/main/script/main.lua b/mods/main/script/main.lua new file mode 100644 index 0000000..4428de7 --- /dev/null +++ b/mods/main/script/main.lua @@ -0,0 +1,61 @@ +function main() + math.randomseed(os.time()) + engine_window_init(96,64,"Game") + texture = engine_texture_create(8,8) + engine_texture_from_file(texture,bp("texture/fier.rgba")) + + while true do + event = engine_event_get() + 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) + end +end + +lastFrame = 0 +function tick() + engine_texture_render_2d(texture,math.random(0,88),math.random(0,56)) + engine_window_present() + trackFps() + local curFrame = engine_time_get() + local wait = 16 - (curFrame - lastFrame) + if wait > 0 then engine_time_sleep(wait) end + lastFrame = engine_time_get() +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 + +local frameSec = 0 +local lastSec = 0 +function trackFps() + frameSec = frameSec + 1 + t = engine_time_get() + if t - lastSec >= 1000 then + print("FPS: " ..tostring(frameSec)) + lastSec = t + frameSec = 0 + end +end + +function bp(pth) + return basepath .. "/mods/main/" ..pth +end + +basepath = (debug.getinfo(1, "S").source:sub(2):match("(.*[/\\])") or "./"):sub(1,-2) .. "/../../.." -- Lazy, fix +package.path = basepath.. "/mods/main/script/?.lua;" ..basepath.. "/mods/main/script/?/main.lua;" ..package.path +main() diff --git a/assets/textures/fier0.rgba b/mods/main/texture/fier.rgba similarity index 100% rename from assets/textures/fier0.rgba rename to mods/main/texture/fier.rgba diff --git a/run b/run index e848ed9..03605cc 100755 --- a/run +++ b/run @@ -1,5 +1,5 @@ #!/usr/bin/env bash set -e ./lua_translate -gcc main.c -g -lSDL2 -llua5.3 -o engine -O3 -Werror -Wall +gcc src/main.c -g -lSDL2 -llua5.3 -o engine -O3 -Werror -Wall exec ./engine diff --git a/modules/engine/FUNCTIONS.toml b/src/engine/FUNCTIONS.toml similarity index 100% rename from modules/engine/FUNCTIONS.toml rename to src/engine/FUNCTIONS.toml diff --git a/modules/engine/STATICS.toml b/src/engine/STATICS.toml similarity index 100% rename from modules/engine/STATICS.toml rename to src/engine/STATICS.toml diff --git a/modules/engine/frontend/generic/textures.c b/src/engine/frontend/generic/textures.c similarity index 100% rename from modules/engine/frontend/generic/textures.c rename to src/engine/frontend/generic/textures.c diff --git a/modules/engine/frontend/sdl/main.c b/src/engine/frontend/sdl/main.c similarity index 99% rename from modules/engine/frontend/sdl/main.c rename to src/engine/frontend/sdl/main.c index c49d8fc..9f93548 100644 --- a/modules/engine/frontend/sdl/main.c +++ b/src/engine/frontend/sdl/main.c @@ -28,7 +28,7 @@ // INTEGER UPSCALING // 2 makes the surface scaled 2x, 3 = 3x, etc. // TODO: Fix ENGINE_SDL_TRANSPARENCY 2 -#define ENGINE_SDL_SCALE 5 +#define ENGINE_SDL_SCALE 2 // SKIP: COLOR // Skip setting colors every X requests. Set to 2 to skip every second request, 3 for every third, etc. diff --git a/modules/engine/lua.c b/src/engine/lua.c similarity index 98% rename from modules/engine/lua.c rename to src/engine/lua.c index 9497c41..59bdd32 100644 --- a/modules/engine/lua.c +++ b/src/engine/lua.c @@ -152,6 +152,6 @@ void engine_lua_init() { lua_pushcfunction(engine_lua_state,engine_luaf_texture_from_file); lua_setglobal (engine_lua_state,"engine_texture_from_file"); engine_lua_init_manual(); - luaL_loadfile(engine_lua_state,"assets/scripts/main.lua"); + luaL_loadfile(engine_lua_state,"mods/main/script/main.lua"); lua_call(engine_lua_state,0,0); } \ No newline at end of file diff --git a/modules/engine/lua_manual.c b/src/engine/lua_manual.c similarity index 100% rename from modules/engine/lua_manual.c rename to src/engine/lua_manual.c diff --git a/modules/engine/main.c b/src/engine/main.c similarity index 100% rename from modules/engine/main.c rename to src/engine/main.c diff --git a/modules/engine/textures.c b/src/engine/textures.c similarity index 100% rename from modules/engine/textures.c rename to src/engine/textures.c diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..ed318ef --- /dev/null +++ b/src/main.c @@ -0,0 +1,16 @@ +#include +#include +#include "engine/main.c" +#include "engine/textures.c" +#include "engine/frontend/sdl/main.c" +#include "engine/frontend/generic/textures.c" +#include "engine/lua.c" + +int main(int argc, char **argv) { + for (int i = 0; i < argc; ++i) { + printf("argv[%d]: %s\n", i, argv[i]); + } + + engine_lua_init(); + return 0; +} diff --git a/test.c b/test.c deleted file mode 100644 index dcc0066..0000000 --- a/test.c +++ /dev/null @@ -1,145 +0,0 @@ -#include -#include -#include "modules/engine/main.c" -#include "modules/engine/frontend/sdl/main.c" -#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) { - exit(0); - } -} - -int frame = 0; -int frameSec = 0; -Uint32 lastSec = 0; -char tr, tg, tb; - -struct ENGINE_TEXTURE * texture; -void tick() { - int y = 0; - while (y < engine_height) { - int x = 0; - while (x < engine_width) { - /*tr = (char)(x + y) + ((char)frame * 1); - tg = (char)(x + y) + ((char)frame * 3); - tb = (char)(x + y) + ((char)frame * 5);*/ - - /*tr = (char)(x + y) * (char)frame * 1; - tg = (char)(x + y) * (char)frame * 3; - tb = (char)(x + y) * (char)frame * 5;*/ - - tr = (char)((x * y) + 64) + frame; - tg = (char)((x * y) + 128) + frame; - tb = (char)((x * y) + 192) + frame; - - /*int part = engine_width / 3; - if (x < part * 1) { - tr = 255; - tg = 0; - tb = 0; - } else if (x < part * 2) { - tr = 0; - tg = 255; - tb = 0; - } else { - tr = 0; - tg = 0; - tb = 255; - }*/ - - /*tr = 0; - tg = 255 - ((char)((x * y) + 128) + frame); - tb = 0;*/ - - /*tr = (char)(((float)x / (float)engine_width) * 255); - tg = 255 - tr; - tb = (char)(((float)y / (float)engine_height) * 255);*/ - engine_surface_color_set(tr,tg,tb,255); - engine_surface_draw_pixel(x,y); - ++x; - } - ++y; - } - - //y = 0; - //while (y < engine_height) { - // int x = 0; - // while (x < engine_width) { - // tr = (char)(x + y) + ((char)frame * 1); - // tg = (char)(x + y) + ((char)frame * 3); - // tb = (char)(x + y) + ((char)frame * 5); - - /*tr = (char)(x + y) * (char)frame * 1; - tg = (char)(x + y) * (char)frame * 3; - tb = (char)(x + y) * (char)frame * 5;*/ - - /*tr = (char)((x * y) + 64) + frame; - tg = (char)((x * y) + 128) + frame; - tb = (char)((x * y) + 192) + frame;*/ - - /*int part = engine_width / 3; - if (x < part * 1) { - tr = 255; - tg = 0; - tb = 0; - } else if (x < part * 2) { - tr = 0; - tg = 255; - tb = 0; - } else { - tr = 0; - tg = 0; - tb = 255; - }*/ - - /*tr = 0; - tg = 255 - ((char)((x * y) + 128) + frame); - tb = 0;*/ - - /*tr = (char)(((float)x / (float)engine_width) * 255); - tg = 255 - tr; - tb = (char)(((float)y / (float)engine_height) * 255);*/ - // engine_surface_color_set(tr,tg,tb,128); - // engine_surface_draw_pixel(x,y); - // ++x; - // } - // ++y; - //} - - - engine_texture_render_2d(texture,5,5); - - frame += 1; - frameSec += 1; - Uint32 tick = SDL_GetTicks(); - if (tick - lastSec >= 1000) { - printf("FPS: %d\n",frameSec); - lastSec = tick; - frameSec = 0; - } - engine_window_present(); - //engine_time_sleep(33); -} - -int main(int argc, char **argv) { - texture = engine_texture_create(8,8); - engine_texture_from_file(texture,"assets/textures/fier.rgba"); - - for (int i = 0; i < argc; ++i) { - printf("argv[%d]: %s\n", i, argv[i]); - } - - engine_window_init(256,256,"Game"); - - while (1) { - struct ENGINE_EVENT * event = engine_event_get(); - if (event->type != ENGINE_EVENT_TYPE_NONE) { - handleEvent(event); - } else { - tick(); - } - engine_free(event); - } -}