diff options
Diffstat (limited to 'src/main.cpp')
| -rw-r--r-- | src/main.cpp | 191 |
1 files changed, 134 insertions, 57 deletions
diff --git a/src/main.cpp b/src/main.cpp index 4a1574e..34de048 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,5 +1,5 @@ -// TOTL Studios (TM) -// Showdown of the Sticks - developed by Arslaan Pathan ([email protected]) +// (C) Copyright 2025 Arslaan Pathan - [email protected] +// Showdown of the Sticks #include "lua/lua.hpp" #include <iostream> @@ -50,15 +50,17 @@ lua_State *L; // Colors SDL_Color veryDarkGrey = {30, 30, 47, 255}; +const char *globalFontFile = "assets/fonts/OpenSans-Regular.ttf"; + // 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 +int l_queue_text_for_render(lua_State *L); +int l_get_text_width(lua_State *L); struct QueuedTexture { @@ -74,16 +76,27 @@ struct QueuedButton std::string callback; }; +struct QueuedText +{ + std::string text; + std::string font_file; + int x; + int y; + int font_size; + SDL_Color color = {255, 255, 255, 255}; +}; + std::vector<QueuedTexture> textureList = {}; std::map<std::string, SDL_Texture *> textureCache; std::vector<QueuedButton> buttonList = {}; +std::vector<QueuedText> textList = {}; +std::map<std::pair<std::string, int>, TTF_Font *> textCache; + 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 dx = luaL_checkinteger(L, 1); @@ -122,6 +135,22 @@ int l_queue_button_for_render(lua_State *L) return 0; } +int l_queue_text_for_render(lua_State *L) +{ + std::string text = (std::string)luaL_checkstring(L, 1); + std::string font_file = (std::string)luaL_checkstring(L, 2); + int x = luaL_checkinteger(L, 3); + int y = luaL_checkinteger(L, 4); + int font_size = luaL_checkinteger(L, 5); + Uint8 r = luaL_checkinteger(L, 6); + Uint8 g = luaL_checkinteger(L, 7); + Uint8 b = luaL_checkinteger(L, 8); + Uint8 a = luaL_checkinteger(L, 9); + SDL_Color color = {r, g, b, a}; + textList.push_back({text, font_file, x, y, font_size, color}); + return 0; +} + bool lua_checkboolean(lua_State *L, int index) { if (!lua_isboolean(L, index)) @@ -132,12 +161,6 @@ bool lua_checkboolean(lua_State *L, int index) 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(); @@ -214,6 +237,44 @@ int l_set_background_image(lua_State *L) return 0; } +int l_get_text_width(lua_State *L) +{ + const char *font_file = luaL_checkstring(L, 1); + int font_size = luaL_checkinteger(L, 2); + const char *text = luaL_checkstring(L, 3); + + std::pair<std::string, int> key = {font_file, font_size}; + + TTF_Font *font; + auto it = textCache.find(key); + if (it != textCache.end()) + { + font = it->second; + } + else + { + font = TTF_OpenFont(font_file, font_size); + if (!font) + { + std::cerr << "Failed to load font: " << TTF_GetError() << std::endl; + lua_pushinteger(L, -1); + return 1; + } + textCache[key] = font; + } + if (!font) + { + lua_pushinteger(L, -1); + return 1; + } + + int w = 0; + TTF_SizeText(font, text, &w, nullptr); + + lua_pushinteger(L, w); + return 1; +} + void push_keys_to_lua(lua_State *L, bool keys[SDL_NUM_SCANCODES]) { lua_newtable(L); // create a new table on the stack @@ -248,39 +309,11 @@ 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); - 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); - 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; + lua_register(L, "queueTextForRender", l_queue_text_for_render); + lua_register(L, "getTextWidth", l_get_text_width); } int main() @@ -309,6 +342,12 @@ int main() SDL_CreateWindowAndRenderer(WIDTH, HEIGHT, 0, &window, &renderer); SDL_SetWindowTitle(window, "Showdown of the Sticks"); SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND); + SDL_RenderSetLogicalSize(renderer, WIDTH, HEIGHT); + SDL_RenderSetIntegerScale(renderer, SDL_FALSE); + SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "best"); + + SDL_SetWindowResizable(window, SDL_TRUE); + SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN_DESKTOP); if (TTF_Init() == -1) { @@ -316,7 +355,7 @@ int main() return 1; } - globalFont = TTF_OpenFont("assets/fonts/OpenSans-Regular.ttf", 24); + globalFont = TTF_OpenFont(globalFontFile, 24); if (!globalFont) { std::cerr << "Failed to load font: " << TTF_GetError() << std::endl; @@ -333,6 +372,11 @@ int main() while (running) { + push_keys_to_lua(L, keys); + lua_setglobal(L, "keys"); + + call_lua_function(L, "Update"); + while (SDL_PollEvent(&event)) { if (event.type == SDL_QUIT) @@ -353,13 +397,14 @@ int main() { int x = event.button.x; int y = event.button.y; - for (const auto& button : buttonList) + std::cout << "Mouse clicked at: " << x << ", " << y << std::endl; + 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) { + std::cout << "Clicked button: " << button.text << " (Callback: " << button.callback << ")" << std::endl; lua_getglobal(L, button.callback.c_str()); - if (lua_isfunction(L, -1)) { @@ -381,11 +426,6 @@ int main() } } - push_keys_to_lua(L, keys); - lua_setglobal(L, "keys"); - - call_lua_function(L, "Update"); - // Clear screen SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); SDL_RenderClear(renderer); @@ -393,13 +433,6 @@ int main() // Background rendering SDL_RenderCopy(renderer, background_texture, nullptr, nullptr); - // Player rendering - if (renderPlayer) - { - SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255); - SDL_RenderFillRect(renderer, &rect); - } - // UI rendering for (const auto &tex : textureList) { @@ -439,6 +472,47 @@ int main() } buttonList.clear(); + for (const auto &text : textList) + { + std::pair<std::string, int> key = {text.font_file, text.font_size}; + + TTF_Font *font; + auto it = textCache.find(key); + if (it != textCache.end()) + { + font = it->second; + } + else + { + font = TTF_OpenFont(text.font_file.c_str(), text.font_size); + if (!font) + { + std::cerr << "Failed to load font: " << TTF_GetError() << std::endl; + break; + } + textCache[key] = font; + } + SDL_Surface *textSurface = TTF_RenderText_Blended(font, text.text.c_str(), text.color); + if (textSurface) + { + SDL_Texture *textTexture = SDL_CreateTextureFromSurface(renderer, textSurface); + SDL_Rect textRect; + textRect.w = textSurface->w; + textRect.h = textSurface->h; + textRect.x = text.x; + textRect.y = text.y; + + SDL_FreeSurface(textSurface); + + if (textTexture) + { + SDL_RenderCopy(renderer, textTexture, nullptr, &textRect); + SDL_DestroyTexture(textTexture); + } + } + } + textList.clear(); + SDL_RenderPresent(renderer); SDL_Delay(10); } @@ -452,6 +526,9 @@ int main() { SDL_DestroyTexture(tex); } + for (auto &[_, fnt] : textCache) { + TTF_CloseFont(fnt); + } textureCache.clear(); SDL_DestroyRenderer(renderer); |
