diff options
| author | Arslaan Pathan <[email protected]> | 2025-06-14 20:02:12 +1200 |
|---|---|---|
| committer | Arslaan Pathan <[email protected]> | 2025-06-14 20:02:12 +1200 |
| commit | 37127e7d25d003b2526f06fa894736abe80b8454 (patch) | |
| tree | 439cdf2cc9fe1b869a3fab8db52e5a0c1eef6997 | |
| parent | ed38ddd48d0393ba4296ac4ba34e92d791be0f3e (diff) | |
| download | showdownofthesticks-37127e7d25d003b2526f06fa894736abe80b8454.tar.xz showdownofthesticks-37127e7d25d003b2526f06fa894736abe80b8454.zip | |
queueTextForRender, starting character select, new character portraits - at least placeholders, the art is shit right now
| -rw-r--r-- | assets/characters/Cobalt Phantom/portrait.png | bin | 0 -> 1833 bytes | |||
| -rw-r--r-- | assets/characters/Crimson Reaper/portrait.png | bin | 0 -> 1833 bytes | |||
| -rw-r--r-- | assets/characters/Emerald Venom/portrait.png | bin | 0 -> 1832 bytes | |||
| -rw-r--r-- | assets/characters/Golden Radiance/portrait.png | bin | 0 -> 1829 bytes | |||
| -rw-r--r-- | assets/scripts/character-select.lua | 24 | ||||
| -rw-r--r-- | assets/scripts/local2P.lua | 3 | ||||
| -rw-r--r-- | assets/scripts/mainMenu.lua | 14 | ||||
| -rw-r--r-- | src/main.cpp | 191 |
8 files changed, 169 insertions, 63 deletions
diff --git a/assets/characters/Cobalt Phantom/portrait.png b/assets/characters/Cobalt Phantom/portrait.png Binary files differnew file mode 100644 index 0000000..b143d32 --- /dev/null +++ b/assets/characters/Cobalt Phantom/portrait.png diff --git a/assets/characters/Crimson Reaper/portrait.png b/assets/characters/Crimson Reaper/portrait.png Binary files differnew file mode 100644 index 0000000..9bad599 --- /dev/null +++ b/assets/characters/Crimson Reaper/portrait.png diff --git a/assets/characters/Emerald Venom/portrait.png b/assets/characters/Emerald Venom/portrait.png Binary files differnew file mode 100644 index 0000000..99beaaa --- /dev/null +++ b/assets/characters/Emerald Venom/portrait.png diff --git a/assets/characters/Golden Radiance/portrait.png b/assets/characters/Golden Radiance/portrait.png Binary files differnew file mode 100644 index 0000000..3d44464 --- /dev/null +++ b/assets/characters/Golden Radiance/portrait.png diff --git a/assets/scripts/character-select.lua b/assets/scripts/character-select.lua new file mode 100644 index 0000000..c3f7e52 --- /dev/null +++ b/assets/scripts/character-select.lua @@ -0,0 +1,24 @@ +---@diagnostic disable: undefined-global + +characters = {"Cobalt Phantom", "Emerald Venom", "Golden Radiance", "Crimson Reaper"} +player1Character = "" +player2Character = "" + +function Setup() + print(characterSelectType) +end + +function Update() + local fontFile = "assets/fonts/OpenSans-Bold.ttf" + local fontSize = 50 + local text = "Character Select" + + local textWidth = getTextWidth(fontFile, fontSize, text) + local x = (WIDTH - textWidth) // 2 + local y = 20 + + queueTextForRender(text, fontFile, x, y, fontSize, 255, 255, 255, 255) + + + +end
\ No newline at end of file diff --git a/assets/scripts/local2P.lua b/assets/scripts/local2P.lua index 5f5c233..635251b 100644 --- a/assets/scripts/local2P.lua +++ b/assets/scripts/local2P.lua @@ -1,6 +1,7 @@ ---@diagnostic disable: undefined-global function Setup() + setRenderPlayer(true) end function Update() -end
\ No newline at end of file +end diff --git a/assets/scripts/mainMenu.lua b/assets/scripts/mainMenu.lua index 11c583a..7ed16cc 100644 --- a/assets/scripts/mainMenu.lua +++ b/assets/scripts/mainMenu.lua @@ -5,22 +5,27 @@ buttonHeight = 50; buttonWidth = 110; bigButtonWidth = 150; bigButtonHeight = buttonHeight; +characterSelectType = "" function Setup() - setRenderPlayer(false) setBgImage("assets/backgrounds/city-background-1.png") end function Local2PButton() - reloadWithScript("assets/scripts/local2P.lua") + characterSelectType = "Local2P" + dofile("assets/scripts/character-select.lua") + Setup() end function MultiplayerButton() - -- Add C functions to reset Lua state and re-run Setup with different scripts + characterSelectType = "Multiplayer" + dofile("assets/scripts/multiplayer.lua") + Setup() end function SettingsButton() - -- Add C functions to reset Lua state and re-run Setup with different scripts + dofile("assets/scripts/settings.lua") + Setup() end function Update() @@ -28,7 +33,6 @@ function Update() queueButtonForRender("Local 2P", WIDTH // 2 - buttonWidth // 2, HEIGHT // 2 - buttonHeight // 2, buttonWidth, buttonHeight, "Local2PButton"); queueButtonForRender("Multiplayer", WIDTH // 2 - bigButtonWidth // 2, HEIGHT // 2 - bigButtonHeight // 2 + 55, bigButtonWidth, bigButtonHeight, "MultiplayerButton"); queueButtonForRender("Settings", WIDTH // 2 - buttonWidth // 2, HEIGHT // 2 - buttonHeight // 2 + 55 + 55, buttonWidth, buttonHeight, "SettingsButton"); - -- if keys[getKeycodeByName("RIGHT")] or keys[getKeycodeByName("D")] then -- movePlayer(movementSpeed, 0) -- end 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); |
