diff --git a/engine/main.cpp b/engine/main.cpp index 00157d0..b39746a 100644 --- a/engine/main.cpp +++ b/engine/main.cpp @@ -1,7 +1,11 @@  #include #include + #include +#include +#include +namespace fs = std::filesystem; extern "C" { #include @@ -11,13 +15,63 @@ extern "C" { #include "input.h" - bool isRunning = false; lua_State* luaState; +// PLACEHOLDER +std::unordered_map loadedLuaFiles; +void loadLuaScripts(lua_State* L, const std::string& path) { + for (const auto& entry : fs::directory_iterator(path)) { + if (entry.path().extension() == ".lua") { + std::string filePath = entry.path().string(); + auto lastWrite = fs::last_write_time(entry); + + if (loadedLuaFiles.find(filePath) == loadedLuaFiles.end() || loadedLuaFiles[filePath] != lastWrite){ + loadedLuaFiles[filePath] = lastWrite; + + std::cout << "Loading Lua file: " << filePath << std::endl; + if (luaL_dofile(L, filePath.c_str()) != LUA_OK) { + std::cerr << "Lua error in " << filePath << ": " << lua_tostring(L, -1) << std::endl; + lua_pop(L, 1); + } + } + } + } +} + +int closeGame(lua_State* L) { + isRunning = false; + + return 0; +} + void createLuaTables() { lua_newtable(luaState); + + // Add Close() to engine table + lua_pushcfunction(luaState, closeGame); + lua_setfield(luaState, -2, "Close"); + lua_setglobal(luaState, "engine"); + + // PLACEHOLDER + // Add global KEY_* constants + for (int scancode = 0; scancode < SDL_NUM_SCANCODES; ++scancode) { + const char* name = SDL_GetScancodeName(static_cast(scancode)); + if (name && name[0] != '\0') { + std::string keyName = "KEY_"; + + for (int i = 0; name[i]; ++i) { + if (name[i] == ' ') + keyName += '_'; + else + keyName += std::toupper(name[i]); + } + + lua_pushinteger(luaState, scancode); + lua_setglobal(luaState, keyName.c_str()); + } + } } int main(int argc, char* argv[]) { @@ -54,20 +108,18 @@ int main(int argc, char* argv[]) { std::cout << "GLAD initialized!\n"; + // Create Lua State luaState = luaL_newstate(); luaL_openlibs(luaState); createLuaTables(); std::cout << "Lua State created!\n"; - if (luaL_dofile(luaState, "test.lua") != LUA_OK) { - const char* error = lua_tostring(luaState, -1); - printf("Lua error: %s\n", error); - } - std::cout << "Running game loop...\n"; isRunning = true; SDL_Event event; while (isRunning) { + loadLuaScripts(luaState, "lua"); + while (SDL_PollEvent(&event)) { SDL_KeyboardEvent* key; key = &event.key; @@ -97,6 +149,8 @@ int main(int argc, char* argv[]) { SDL_GL_SwapWindow(window); } + std::cout << "Shutting down..\n"; + lua_close(luaState); SDL_GL_DeleteContext(glContext); diff --git a/lua/test copy.lua b/lua/test copy.lua new file mode 100644 index 0000000..0b7ae96 --- /dev/null +++ b/lua/test copy.lua @@ -0,0 +1,6 @@ + +function engine.KeyPressed(key) + if (key == KEY_ESCAPE) then + engine.Close() + end +end \ No newline at end of file diff --git a/test.lua b/lua/test.lua similarity index 87% rename from test.lua rename to lua/test.lua index 4c617be..cba44cb 100644 --- a/test.lua +++ b/lua/test.lua @@ -5,4 +5,6 @@ end function engine.KeyPressed(key) print("Pressed key: " .. key) -end \ No newline at end of file +end + +GLOBAL_TEST = 5 \ No newline at end of file