diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.cpp | 42 |
1 files changed, 39 insertions, 3 deletions
diff --git a/src/main.cpp b/src/main.cpp index 191da02..e4dadc0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,6 +5,7 @@ #include <iostream> #include <fstream> #include <vector> +#include <string> #include <unistd.h> #include <map> // Prevents SDL from shitting the Windows cross-compiler and causing a "WinMain" error @@ -39,6 +40,7 @@ void fix_working_directory() { SDL_Window* window = nullptr; SDL_Renderer* renderer = nullptr; SDL_Event event; +TTF_Font* globalFont = nullptr; struct QueuedTexture { SDL_Texture* texture; @@ -47,13 +49,16 @@ struct QueuedTexture { }; struct QueuedButton { - SDL_Rect* rect; - const char* text; + SDL_Rect rect; + std::string text; + std::string callback; }; std::vector<QueuedTexture> textureList = {}; 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; @@ -84,6 +89,11 @@ int l_queue_button_for_render(lua_State* L) { 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); + + SDL_Rect rect = {x, y, w, h}; + buttonList.push_back({rect, text, callback}); + return 0; } bool lua_checkboolean(lua_State* L, int index) { @@ -197,6 +207,7 @@ int main() { 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); + lua_register(L, "queueButtonForRender", l_queue_button_for_render); char cwd[1024]; luaL_dofile(L, "assets/scripts/mainMenu.lua"); @@ -238,6 +249,26 @@ int main() { if (event.type == SDL_KEYUP) { keys[event.key.keysym.scancode] = false; } + + if (event.type == SDL_MOUSEBUTTONDOWN) { + int x = event.button.x; + int y = event.button.y; + 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) { + 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); + } + + } + } + } } push_keys_to_lua(L, keys); @@ -266,7 +297,12 @@ int main() { SDL_QueryTexture(tex.texture, nullptr, nullptr, &dst.w, &dst.h); SDL_RenderCopy(renderer, tex.texture, nullptr, &dst); } - textureList.clear(); // clear after rendering this frame + textureList.clear(); + + for (const auto& button: buttonList) { + SDL_SetRenderDrawColor(renderer, 252, 210, 77, 255); + SDL_RenderFillRect(renderer, &button.rect); + } SDL_RenderPresent(renderer); |
