From aa49d54dc5e0e2a86f2973984df67ddae056d59a Mon Sep 17 00:00:00 2001 From: Arslaan Pathan Date: Thu, 12 Jun 2025 19:32:32 +1200 Subject: Start working on button logic and make banner work --- src/main.cpp | 150 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 145 insertions(+), 5 deletions(-) (limited to 'src/main.cpp') diff --git a/src/main.cpp b/src/main.cpp index f5c399d..191da02 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,6 +3,10 @@ #include "lua/lua.hpp" #include +#include +#include +#include +#include // Prevents SDL from shitting the Windows cross-compiler and causing a "WinMain" error // At least that's what I think - I'm not sure what this does but if I remove it the code won't work // If it ain't broke don't fix it @@ -11,6 +15,22 @@ #include #include #include +#include +#ifdef __APPLE__ +#include // macOS only +#endif + +void fix_working_directory() { + #ifdef __APPLE__ + char path[1024]; + uint32_t size = sizeof(path); + 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 +} #define WIDTH 1280 #define HEIGHT 720 @@ -20,10 +40,26 @@ SDL_Window* window = nullptr; SDL_Renderer* renderer = nullptr; SDL_Event event; +struct QueuedTexture { + SDL_Texture* texture; + int x; + int y; +}; + +struct QueuedButton { + SDL_Rect* rect; + const char* text; +}; + +std::vector textureList = {}; +std::map textureCache; + SDL_Rect rect = {WIDTH/2, HEIGHT/2, 50, 50}; SDL_Texture* background_texture = nullptr; -int l_move_rect(lua_State* L) { +bool renderPlayer = false; + +int l_move_player(lua_State* L) { int dx = luaL_checkinteger(L, 1); int dy = luaL_checkinteger(L, 2); rect.x += dx; @@ -42,8 +78,70 @@ int l_keycode_from_string(lua_State* L) { return 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); +} + +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) { + renderPlayer = lua_checkboolean(L, 1); + return 0; +} + +int l_queue_texture_for_render(lua_State* L) { + fix_working_directory(); + const char* path = luaL_checkstring(L, 1); + int x = luaL_checkinteger(L, 2); + int y = luaL_checkinteger(L, 3); + + SDL_Texture* texture = nullptr; + + // Check cache first + auto it = textureCache.find(path); + if (it != textureCache.end()) { + texture = it->second; + } else { + SDL_Surface* surface = IMG_Load(path); + if (!surface) { + std::cerr << "Failed to load image in queueTextureForRender: " << IMG_GetError() << std::endl; + return 0; + } + + texture = SDL_CreateTextureFromSurface(renderer, surface); + SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND); + SDL_FreeSurface(surface); + + if (!texture) { + std::cerr << "Failed to create texture in queueTextureForRender: " << SDL_GetError() << std::endl; + return 0; + } + + textureCache[path] = texture; // Cache it + } + + textureList.push_back({texture, x, y}); + return 0; +} + +int l_unqueue_all_textures(lua_State* L) { + textureList.clear(); + + return 0; +} 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) { @@ -56,6 +154,7 @@ int l_set_background_image(lua_State* L) { } background_texture = SDL_CreateTextureFromSurface(renderer, temp_surface); + SDL_SetTextureBlendMode(background_texture, SDL_BLENDMODE_BLEND); SDL_FreeSurface(temp_surface); if (!background_texture) { @@ -89,24 +188,41 @@ void call_lua_function(lua_State* L, const char* func_name) { int main() { + fix_working_directory(); lua_State* L = luaL_newstate(); luaL_openlibs(L); - lua_register(L, "movePlayer", l_move_rect); + lua_register(L, "movePlayer", l_move_player); lua_register(L, "getKeycodeByName", l_keycode_from_string); lua_register(L, "setBgImage", l_set_background_image); + lua_register(L, "setRenderPlayer", l_set_do_render_player); + lua_register(L, "queueTextureForRender", l_queue_texture_for_render); + lua_register(L, "unqueueAllTextures", l_unqueue_all_textures); + char cwd[1024]; + luaL_dofile(L, "assets/scripts/mainMenu.lua"); + bool keys[SDL_NUM_SCANCODES] = {false}; bool running = true; push_keys_to_lua(L, keys); lua_setglobal(L, "keys"); + lua_pushinteger(L, WIDTH); + lua_setglobal(L, "WIDTH"); + + lua_pushinteger(L, HEIGHT); + lua_setglobal(L, "HEIGHT"); + SDL_Init(SDL_INIT_EVERYTHING); SDL_CreateWindowAndRenderer(WIDTH, HEIGHT, 0, &window, &renderer); SDL_SetWindowTitle(window, "Showdown of the Sticks"); + SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND); - IMG_Init(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"); @@ -129,13 +245,29 @@ int main() { call_lua_function(L, "Update"); + // Clear screen SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); SDL_RenderClear(renderer); + // Background rendering SDL_RenderCopy(renderer, background_texture, nullptr, nullptr); - SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255); - SDL_RenderFillRect(renderer, &rect); + // Player rendering + if (renderPlayer) { + SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255); + SDL_RenderFillRect(renderer, &rect); + } + + // UI rendering + for (const auto& tex : textureList) { + SDL_Rect dst; + dst.x = tex.x; + dst.y = tex.y; + SDL_QueryTexture(tex.texture, nullptr, nullptr, &dst.w, &dst.h); + SDL_RenderCopy(renderer, tex.texture, nullptr, &dst); + } + textureList.clear(); // clear after rendering this frame + SDL_RenderPresent(renderer); SDL_Delay(10); @@ -145,8 +277,16 @@ int main() { if (background_texture) { SDL_DestroyTexture(background_texture); } + for (auto& [_, tex] : textureCache) { + SDL_DestroyTexture(tex); + } + textureCache.clear(); + SDL_DestroyRenderer(renderer); SDL_DestroyWindow(window); + IMG_Quit(); + TTF_Quit(); + Mix_Quit(); SDL_Quit(); return 0; -- cgit v1.2.3