diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.cpp | 264 |
1 files changed, 188 insertions, 76 deletions
diff --git a/src/main.cpp b/src/main.cpp index 6e79ff1..4a1574e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -21,53 +21,71 @@ #include <mach-o/dyld.h> // macOS only #endif -void fix_working_directory() { - #ifdef __APPLE__ +void fix_working_directory() +{ +#ifdef __APPLE__ char path[1024]; uint32_t size = sizeof(path); - if (_NSGetExecutablePath(path, &size) == 0) { + if (_NSGetExecutablePath(path, &size) == 0) + { std::filesystem::path exePath = std::filesystem::canonical(path); std::filesystem::path resPath = exePath.parent_path().parent_path() / "Resources"; std::filesystem::current_path(resPath); } - #endif +#endif } #define WIDTH 1280 #define HEIGHT 720 #define KEYSYM event.key.keysym.sym -SDL_Window* window = nullptr; -SDL_Renderer* renderer = nullptr; +SDL_Window *window = nullptr; +SDL_Renderer *renderer = nullptr; SDL_Event event; -TTF_Font* globalFont = nullptr; +TTF_Font *globalFont = nullptr; + +// Lua +lua_State *L; // Colors SDL_Color veryDarkGrey = {30, 30, 47, 255}; -struct QueuedTexture { - SDL_Texture* texture; +// Forward declarations +int l_move_player(lua_State *L); +int l_keycode_from_string(lua_State *L); +int l_set_background_image(lua_State *L); +int l_set_do_render_player(lua_State *L); +int l_queue_texture_for_render(lua_State *L); +int l_unqueue_all_textures(lua_State *L); +int l_queue_button_for_render(lua_State *L); +int l_reload_script(lua_State *L); // This is the reload function + +struct QueuedTexture +{ + SDL_Texture *texture; int x; int y; }; -struct QueuedButton { +struct QueuedButton +{ SDL_Rect rect; std::string text; std::string callback; }; std::vector<QueuedTexture> textureList = {}; -std::map<std::string, SDL_Texture*> textureCache; +std::map<std::string, SDL_Texture *> textureCache; std::vector<QueuedButton> buttonList = {}; -SDL_Rect rect = {WIDTH/2, HEIGHT/2, 50, 50}; -SDL_Texture* background_texture = nullptr; +SDL_Rect rect = {WIDTH / 2, HEIGHT / 2, 50, 50}; +SDL_Texture *background_texture = nullptr; bool renderPlayer = false; -int l_move_player(lua_State* L) { +int l_move_player(lua_State *L) +{ int dx = luaL_checkinteger(L, 1); int dy = luaL_checkinteger(L, 2); rect.x += dx; @@ -75,58 +93,71 @@ int l_move_player(lua_State* L) { return 0; } -int l_keycode_from_string(lua_State* L) { - const char* keystr = luaL_checkstring(L, 1); +int l_keycode_from_string(lua_State *L) +{ + const char *keystr = luaL_checkstring(L, 1); SDL_Scancode sc = SDL_GetScancodeFromName(keystr); - if (sc == SDL_SCANCODE_UNKNOWN) { + if (sc == SDL_SCANCODE_UNKNOWN) + { lua_pushinteger(L, -1); - } else { + } + else + { lua_pushinteger(L, sc + 1); } return 1; } -int l_queue_button_for_render(lua_State* L) { - const char* text = luaL_checkstring(L, 1); +int l_queue_button_for_render(lua_State *L) +{ + const char *text = luaL_checkstring(L, 1); int x = luaL_checkinteger(L, 2); int y = luaL_checkinteger(L, 3); int w = luaL_checkinteger(L, 4); int h = luaL_checkinteger(L, 5); - const char* callback = luaL_checkstring(L, 6); + const char *callback = luaL_checkstring(L, 6); SDL_Rect rect = {x, y, w, h}; buttonList.push_back({rect, text, callback}); return 0; } -bool lua_checkboolean(lua_State* L, int index) { - if (!lua_isboolean(L, index)) { +bool lua_checkboolean(lua_State *L, int index) +{ + if (!lua_isboolean(L, index)) + { luaL_error(L, "Expected boolean at argument %d", index); return false; } return lua_toboolean(L, index); } -int l_set_do_render_player(lua_State* L) { +int l_set_do_render_player(lua_State *L) +{ renderPlayer = lua_checkboolean(L, 1); return 0; } -int l_queue_texture_for_render(lua_State* L) { +int l_queue_texture_for_render(lua_State *L) +{ fix_working_directory(); - const char* path = luaL_checkstring(L, 1); + const char *path = luaL_checkstring(L, 1); int x = luaL_checkinteger(L, 2); int y = luaL_checkinteger(L, 3); - SDL_Texture* texture = nullptr; + SDL_Texture *texture = nullptr; // Check cache first auto it = textureCache.find(path); - if (it != textureCache.end()) { + if (it != textureCache.end()) + { texture = it->second; - } else { - SDL_Surface* surface = IMG_Load(path); - if (!surface) { + } + else + { + SDL_Surface *surface = IMG_Load(path); + if (!surface) + { std::cerr << "Failed to load image in queueTextureForRender: " << IMG_GetError() << std::endl; return 0; } @@ -135,7 +166,8 @@ int l_queue_texture_for_render(lua_State* L) { SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND); SDL_FreeSurface(surface); - if (!texture) { + if (!texture) + { std::cerr << "Failed to create texture in queueTextureForRender: " << SDL_GetError() << std::endl; return 0; } @@ -147,22 +179,26 @@ int l_queue_texture_for_render(lua_State* L) { return 0; } -int l_unqueue_all_textures(lua_State* L) { +int l_unqueue_all_textures(lua_State *L) +{ textureList.clear(); return 0; } -int l_set_background_image(lua_State* L) { +int l_set_background_image(lua_State *L) +{ fix_working_directory(); - const char* bgImagePath = luaL_checkstring(L, 1); - SDL_Surface* temp_surface = IMG_Load(bgImagePath); - if (!temp_surface) { + const char *bgImagePath = luaL_checkstring(L, 1); + SDL_Surface *temp_surface = IMG_Load(bgImagePath); + if (!temp_surface) + { std::cerr << "Failed to load image: " << IMG_GetError() << std::endl; return 0; } - if (background_texture) { + if (background_texture) + { SDL_DestroyTexture(background_texture); } @@ -170,40 +206,45 @@ int l_set_background_image(lua_State* L) { SDL_SetTextureBlendMode(background_texture, SDL_BLENDMODE_BLEND); SDL_FreeSurface(temp_surface); - if (!background_texture) { + if (!background_texture) + { std::cerr << "Failed to create texture: " << SDL_GetError() << std::endl; } return 0; } -void push_keys_to_lua(lua_State* L, bool keys[SDL_NUM_SCANCODES]) { - lua_newtable(L); // create a new table on the stack - - for (int i = 0; i < SDL_NUM_SCANCODES; i++) { +void push_keys_to_lua(lua_State *L, bool keys[SDL_NUM_SCANCODES]) +{ + lua_newtable(L); // create a new table on the stack + + for (int i = 0; i < SDL_NUM_SCANCODES; i++) + { lua_pushinteger(L, i + 1); lua_pushboolean(L, keys[i]); lua_settable(L, -3); } } -void call_lua_function(lua_State* L, const char* func_name) { +void call_lua_function(lua_State *L, const char *func_name) +{ lua_getglobal(L, func_name); - if (lua_isfunction(L, -1)) { - if (lua_pcall(L, 0, 0, 0) != LUA_OK) { + if (lua_isfunction(L, -1)) + { + if (lua_pcall(L, 0, 0, 0) != LUA_OK) + { std::cerr << "Lua Error in " << func_name << ": " << lua_tostring(L, -1) << std::endl; lua_pop(L, 1); } - } else { + } + else + { lua_pop(L, 1); } } - -int main() { - fix_working_directory(); - lua_State* L = luaL_newstate(); - luaL_openlibs(L); +void expose_c_functions() +{ lua_register(L, "movePlayer", l_move_player); lua_register(L, "getKeycodeByName", l_keycode_from_string); lua_register(L, "setBgImage", l_set_background_image); @@ -211,11 +252,47 @@ int main() { lua_register(L, "queueTextureForRender", l_queue_texture_for_render); lua_register(L, "unqueueAllTextures", l_unqueue_all_textures); lua_register(L, "queueButtonForRender", l_queue_button_for_render); + lua_register(L, "reloadWithScript", l_reload_script); +} + +void reload_lua_script(const char *script_path) +{ + if (L) + { + lua_close(L); + } + + L = luaL_newstate(); + luaL_openlibs(L); + + expose_c_functions(); + + if (luaL_dofile(L, script_path) != LUA_OK) + { + fprintf(stderr, "Lua error: %s\n", lua_tostring(L, -1)); + lua_pop(L, 1); + } + + call_lua_function(L, "Setup"); +} + +int l_reload_script(lua_State *L) +{ + const char *script = luaL_checkstring(L, 1); + reload_lua_script(script); + return 0; +} + +int main() +{ + fix_working_directory(); + L = luaL_newstate(); + expose_c_functions(); + luaL_openlibs(L); char cwd[1024]; luaL_dofile(L, "assets/scripts/mainMenu.lua"); - bool keys[SDL_NUM_SCANCODES] = {false}; bool running = true; @@ -233,45 +310,72 @@ int main() { SDL_SetWindowTitle(window, "Showdown of the Sticks"); SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND); - if (TTF_Init() == -1) { + if (TTF_Init() == -1) + { std::cerr << "TTF_Init failed: " << TTF_GetError() << std::endl; return 1; } globalFont = TTF_OpenFont("assets/fonts/OpenSans-Regular.ttf", 24); - if (!globalFont) { + if (!globalFont) + { std::cerr << "Failed to load font: " << TTF_GetError() << std::endl; return 1; } - - if (!(IMG_Init(IMG_INIT_PNG) & IMG_INIT_PNG)) { + if (!(IMG_Init(IMG_INIT_PNG) & IMG_INIT_PNG)) + { std::cerr << "Failed to initialize SDL_image: " << IMG_GetError() << std::endl; return 1; } call_lua_function(L, "Setup"); - while (running) { - while (SDL_PollEvent(&event)) { - if (event.type == SDL_QUIT) { + while (running) + { + while (SDL_PollEvent(&event)) + { + if (event.type == SDL_QUIT) + { running = false; } - if (event.type == SDL_KEYDOWN) { + if (event.type == SDL_KEYDOWN) + { keys[event.key.keysym.scancode] = true; } - if (event.type == SDL_KEYUP) { + if (event.type == SDL_KEYUP) + { keys[event.key.keysym.scancode] = false; } - if (event.type == SDL_MOUSEBUTTONDOWN) { + if (event.type == SDL_MOUSEBUTTONDOWN) + { int x = event.button.x; int y = event.button.y; - for (const auto& button: buttonList) { + for (const auto& button : buttonList) + { if (x > button.rect.x && x < button.rect.x + button.rect.w && - y > button.rect.y && y < button.rect.y + button.rect.h) { - call_lua_function(L, button.callback.c_str()); + y > button.rect.y && y < button.rect.y + button.rect.h) + { + lua_getglobal(L, button.callback.c_str()); + + if (lua_isfunction(L, -1)) + { + + if (lua_pcall(L, 0, 0, 0) != LUA_OK) + { + + std::cerr << "Lua Error in button callback: " << lua_tostring(L, -1) << std::endl; + + lua_pop(L, 1); + } + } + else + { + + lua_pop(L, 1); + } } } } @@ -290,13 +394,15 @@ int main() { SDL_RenderCopy(renderer, background_texture, nullptr, nullptr); // Player rendering - if (renderPlayer) { + if (renderPlayer) + { SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255); SDL_RenderFillRect(renderer, &rect); } // UI rendering - for (const auto& tex : textureList) { + for (const auto &tex : textureList) + { SDL_Rect dst; dst.x = tex.x; dst.y = tex.y; @@ -305,13 +411,16 @@ int main() { } textureList.clear(); - for (const auto& button: buttonList) { + for (const auto &button : buttonList) + { SDL_SetRenderDrawColor(renderer, 252, 210, 77, 255); SDL_RenderFillRect(renderer, &button.rect); - if (globalFont) { - SDL_Surface* textSurface = TTF_RenderText_Blended(globalFont, button.text.c_str(), veryDarkGrey); - if (textSurface) { - SDL_Texture* textTexture = SDL_CreateTextureFromSurface(renderer, textSurface); + if (globalFont) + { + SDL_Surface *textSurface = TTF_RenderText_Blended(globalFont, button.text.c_str(), veryDarkGrey); + if (textSurface) + { + SDL_Texture *textTexture = SDL_CreateTextureFromSurface(renderer, textSurface); SDL_Rect textRect; textRect.w = textSurface->w; textRect.h = textSurface->h; @@ -320,7 +429,8 @@ int main() { SDL_FreeSurface(textSurface); - if (textTexture) { + if (textTexture) + { SDL_RenderCopy(renderer, textTexture, nullptr, &textRect); SDL_DestroyTexture(textTexture); } @@ -334,10 +444,12 @@ int main() { } lua_close(L); - if (background_texture) { + if (background_texture) + { SDL_DestroyTexture(background_texture); } - for (auto& [_, tex] : textureCache) { + for (auto &[_, tex] : textureCache) + { SDL_DestroyTexture(tex); } textureCache.clear(); |
