From 8c7dfa2167ca737e33947b98ed6c077351f6a2b6 Mon Sep 17 00:00:00 2001 From: Arslaan Pathan Date: Thu, 12 Jun 2025 20:22:40 +1200 Subject: Button is work, very very nice --- src/main.cpp | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) (limited to 'src/main.cpp') diff --git a/src/main.cpp b/src/main.cpp index e4dadc0..ee291a7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -230,6 +230,18 @@ int main() { SDL_SetWindowTitle(window, "Showdown of the Sticks"); SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND); + 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) { + std::cerr << "Failed to load font: " << TTF_GetError() << std::endl; + return 1; + } + + if (!(IMG_Init(IMG_INIT_PNG) & IMG_INIT_PNG)) { std::cerr << "Failed to initialize SDL_image: " << IMG_GetError() << std::endl; return 1; @@ -265,7 +277,7 @@ int main() { } else { lua_pop(L, 1); } - + SDL_Delay(25); } } } @@ -302,6 +314,25 @@ int main() { for (const auto& button: buttonList) { SDL_SetRenderDrawColor(renderer, 252, 210, 77, 255); SDL_RenderFillRect(renderer, &button.rect); + if (globalFont) { + SDL_Color white = {255, 255, 255, 255}; + SDL_Surface* textSurface = TTF_RenderText_Blended(globalFont, button.text.c_str(), white); + if (textSurface) { + SDL_Texture* textTexture = SDL_CreateTextureFromSurface(renderer, textSurface); + SDL_Rect textRect; + textRect.w = textSurface->w; + textRect.h = textSurface->h; + textRect.x = button.rect.x + (button.rect.w - textRect.w) / 2; + textRect.y = button.rect.y + (button.rect.h - textRect.h) / 2; + + SDL_FreeSurface(textSurface); + + if (textTexture) { + SDL_RenderCopy(renderer, textTexture, nullptr, &textRect); + SDL_DestroyTexture(textTexture); + } + } + } } -- cgit v1.2.3