From 36ba3fa1328b55520b6beea30fb266807ffedaf5 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Sun, 17 Apr 2016 02:10:08 +1000 Subject: [PATCH] Updated GUI library --- client/scenes/disconnected_screen.cpp | 10 ++- client/scenes/lobby_menu.cpp | 22 +++-- client/scenes/main_menu.cpp | 26 +++--- client/scenes/options_menu.cpp | 10 ++- client/scenes/world.cpp | 10 ++- common/graphics/button.cpp | 40 ++++----- common/graphics/button.hpp | 17 ++-- common/graphics/makefile | 2 +- common/graphics/render_text_texture.cpp | 46 ++++++++++ common/graphics/render_text_texture.hpp | 29 +++++++ common/graphics/text_box.cpp | 31 +++++-- common/graphics/text_box.hpp | 10 ++- common/graphics/text_field.cpp | 109 ++++++++++++++++++++++++ common/graphics/text_field.hpp | 65 ++++++++++++++ common/graphics/text_line.cpp | 58 +++++++------ common/graphics/text_line.hpp | 20 +++-- 16 files changed, 408 insertions(+), 97 deletions(-) create mode 100644 common/graphics/render_text_texture.cpp create mode 100644 common/graphics/render_text_texture.hpp create mode 100644 common/graphics/text_field.cpp create mode 100644 common/graphics/text_field.hpp diff --git a/client/scenes/disconnected_screen.cpp b/client/scenes/disconnected_screen.cpp index f8d3340..ab9b400 100644 --- a/client/scenes/disconnected_screen.cpp +++ b/client/scenes/disconnected_screen.cpp @@ -28,6 +28,8 @@ #include #include +constexpr SDL_Color WHITE = {255, 255, 255, 255}; + //------------------------- //Public access members //------------------------- @@ -49,14 +51,16 @@ DisconnectedScreen::DisconnectedScreen() { //setup the button backButton.SetBackgroundTexture(GetRenderer(), image.GetTexture()); - backButton.SetText(GetRenderer(), font, "Back", COLOR_WHITE); + backButton.SetText(GetRenderer(), font, WHITE, "Back"); //set the button positions backButton.SetX(50); backButton.SetY(50); //set the disconnection message text - textLine.SetText(GetRenderer(), font, config["client.disconnectMessage"], {255, 255, 255, 255}); + textLine.SetX(50); + textLine.SetY(30); + textLine.SetText(GetRenderer(), font, WHITE, config["client.disconnectMessage"]); //full reset UDPNetworkUtility::GetSingleton().Unbind(Channels::SERVER); @@ -92,7 +96,7 @@ void DisconnectedScreen::FrameEnd() { void DisconnectedScreen::RenderFrame(SDL_Renderer* renderer) { backButton.DrawTo(renderer); - textLine.DrawTo(renderer, 50, 30); + textLine.DrawTo(renderer); } //------------------------- diff --git a/client/scenes/lobby_menu.cpp b/client/scenes/lobby_menu.cpp index 347bd34..3954c26 100644 --- a/client/scenes/lobby_menu.cpp +++ b/client/scenes/lobby_menu.cpp @@ -27,6 +27,8 @@ #include #include +constexpr SDL_Color WHITE = {255, 255, 255, 255}; + //------------------------- //Public access members //------------------------- @@ -52,11 +54,11 @@ LobbyMenu::LobbyMenu(int* const argClientIndex, int* const argAccountIndex): //setup the buttons searchButton.SetBackgroundTexture(GetRenderer(), buttonImage.GetTexture()); - searchButton.SetText(GetRenderer(), font, "Search", COLOR_WHITE); + searchButton.SetText(GetRenderer(), font, WHITE, "Search"); joinButton.SetBackgroundTexture(GetRenderer(), buttonImage.GetTexture()); - joinButton.SetText(GetRenderer(), font, "Join", COLOR_WHITE); + joinButton.SetText(GetRenderer(), font, WHITE, "Join"); backButton.SetBackgroundTexture(GetRenderer(), buttonImage.GetTexture()); - backButton.SetText(GetRenderer(), font, "Back", COLOR_WHITE); + backButton.SetText(GetRenderer(), font, WHITE, "Back"); //set the button positions (assumed) searchButton.SetX(50); @@ -67,6 +69,7 @@ LobbyMenu::LobbyMenu(int* const argClientIndex, int* const argAccountIndex): backButton.SetY(90); //pseudo-list selection + //TODO: move this into the UI library? boundingBox = {300, 50, 200, 12}; //hacked together a highlight box @@ -121,8 +124,13 @@ void LobbyMenu::RenderFrame(SDL_Renderer* renderer) { } //draw the server's info - serverVector[i].nameImage.DrawTo(renderer, boundingBox.x, boundingBox.y + boundingBox.h * i); - serverVector[i].playerCountImage.DrawTo(renderer, boundingBox.x+276, boundingBox.y + boundingBox.h * i); + serverVector[i].nameImage.SetX(boundingBox.x); + serverVector[i].nameImage.SetY(boundingBox.y + boundingBox.h * i); + serverVector[i].nameImage.DrawTo(renderer); + + serverVector[i].playerCountImage.SetX(boundingBox.x+276); + serverVector[i].playerCountImage.SetY(boundingBox.y + boundingBox.h * i); + serverVector[i].playerCountImage.DrawTo(renderer); } } @@ -248,8 +256,8 @@ void LobbyMenu::HandleBroadcastResponse(ServerPacket* const argPacket) { }; //text graphics - serverVector.back().nameImage.SetText(GetRenderer(), font, newServer.name, color); - serverVector.back().playerCountImage.SetText(GetRenderer(), font, itoa_base10(newServer.playerCount), color); + serverVector.back().nameImage.SetText(GetRenderer(), font, color, newServer.name); + serverVector.back().playerCountImage.SetText(GetRenderer(), font, color, itoa_base10(newServer.playerCount)); } void LobbyMenu::HandleJoinResponse(ClientPacket* const argPacket) { diff --git a/client/scenes/main_menu.cpp b/client/scenes/main_menu.cpp index d7fcf45..c36569b 100644 --- a/client/scenes/main_menu.cpp +++ b/client/scenes/main_menu.cpp @@ -26,6 +26,8 @@ #include #include +constexpr SDL_Color WHITE = {255, 255, 255, 255}; + //------------------------- //Public access members //------------------------- @@ -46,11 +48,11 @@ MainMenu::MainMenu() { //setup the buttons startButton.SetBackgroundTexture(GetRenderer(), buttonImage.GetTexture()); - startButton.SetText(GetRenderer(), font, "Start", COLOR_WHITE); + startButton.SetText(GetRenderer(), font, WHITE, "Start"); optionsButton.SetBackgroundTexture(GetRenderer(), buttonImage.GetTexture()); - optionsButton.SetText(GetRenderer(), font, "Options", COLOR_WHITE); + optionsButton.SetText(GetRenderer(), font, WHITE, "Options"); quitButton.SetBackgroundTexture(GetRenderer(), buttonImage.GetTexture()); - quitButton.SetText(GetRenderer(), font, "Quit", COLOR_WHITE); + quitButton.SetText(GetRenderer(), font, WHITE, "Quit"); //set the button positions startButton.SetX(50); @@ -61,9 +63,15 @@ MainMenu::MainMenu() { quitButton.SetY(50 + 20 * 2); //text box - textBox.PushLine(GetRenderer(), font, "Thanks for playing!", {255, 255, 255, 255}); - textBox.PushLine(GetRenderer(), font, "You can get the latest version at: ", {255, 255, 255, 255}); - textBox.PushLine(GetRenderer(), font, "krgamestudios.com", {255, 255, 255, 255}); //TODO: (9) click to open the website/update + int h = -1; + SDL_RenderGetLogicalSize(GetRenderer(), nullptr, &h); + + textBox.SetX(50); + textBox.SetY(h-100); + + textBox.PushLine(GetRenderer(), font, WHITE, "Thanks for playing!"); + textBox.PushLine(GetRenderer(), font, WHITE, "You can get the latest version at: "); + textBox.PushLine(GetRenderer(), font, WHITE, "krgamestudios.com"); //TODO: (9) click to open the website/update //debug // @@ -93,11 +101,7 @@ void MainMenu::RenderFrame(SDL_Renderer* renderer) { startButton.DrawTo(renderer); optionsButton.DrawTo(renderer); quitButton.DrawTo(renderer); - - int h = -1; - SDL_RenderGetLogicalSize(GetRenderer(), nullptr, &h); - - textBox.DrawTo(renderer, 50, h-50, -12); + textBox.DrawTo(renderer); } //------------------------- diff --git a/client/scenes/options_menu.cpp b/client/scenes/options_menu.cpp index f243393..13230f4 100644 --- a/client/scenes/options_menu.cpp +++ b/client/scenes/options_menu.cpp @@ -26,6 +26,8 @@ #include #include +constexpr SDL_Color WHITE = {255, 255, 255, 255}; + //------------------------- //Public access members //------------------------- @@ -46,14 +48,16 @@ OptionsMenu::OptionsMenu() { //setup the button backButton.SetBackgroundTexture(GetRenderer(), buttonImage.GetTexture()); - backButton.SetText(GetRenderer(), font, "Back", COLOR_WHITE); + backButton.SetText(GetRenderer(), font, WHITE, "Back"); //set the button positions backButton.SetX(50); backButton.SetY(50); //text line - textLine.SetText(GetRenderer(), font, "This code is fucking hard to refactor.", {255, 255, 255, 255}); + textLine.SetX(50); + textLine.SetY(30); + textLine.SetText(GetRenderer(), font, WHITE, "Am I making any progress?"); } OptionsMenu::~OptionsMenu() { @@ -78,7 +82,7 @@ void OptionsMenu::FrameEnd() { void OptionsMenu::RenderFrame(SDL_Renderer* renderer) { backButton.DrawTo(renderer); - textLine.DrawTo(renderer, 50, 30); + textLine.DrawTo(renderer); } //------------------------- diff --git a/client/scenes/world.cpp b/client/scenes/world.cpp index 6a8a7b0..4c5aa0f 100644 --- a/client/scenes/world.cpp +++ b/client/scenes/world.cpp @@ -34,6 +34,8 @@ #include #include +constexpr SDL_Color WHITE = {255, 255, 255, 255}; + //------------------------- //static functions //------------------------- @@ -72,9 +74,9 @@ World::World(int* const argClientIndex, int* const argAccountIndex): //setup the buttons disconnectButton.SetBackgroundTexture(GetRenderer(), buttonImage.GetTexture()); - disconnectButton.SetText(GetRenderer(), font, "Disconnect", COLOR_WHITE); + disconnectButton.SetText(GetRenderer(), font, WHITE, "Disconnect"); shutdownButton.SetBackgroundTexture(GetRenderer(), buttonImage.GetTexture()); - shutdownButton.SetText(GetRenderer(), font, "Shutdown", COLOR_WHITE); + shutdownButton.SetText(GetRenderer(), font, WHITE, "Shutdown"); //set the button positions disconnectButton.SetX(50); @@ -261,12 +263,12 @@ void World::RenderFrame(SDL_Renderer* renderer) { shutdownButton.DrawTo(renderer); //FPS - fpsTextLine.DrawTo(renderer, 0, 0); + fpsTextLine.DrawTo(renderer); int fpsRet = fps.Calculate(); if (fpsRet != -1) { std::ostringstream msg; msg << "FPS: " << fpsRet; - fpsTextLine.SetText(renderer, font, msg.str(), {255, 255, 255, 255}); + fpsTextLine.SetText(renderer, font, WHITE, msg.str()); } } diff --git a/common/graphics/button.cpp b/common/graphics/button.cpp index 4091bee..d58fed8 100644 --- a/common/graphics/button.cpp +++ b/common/graphics/button.cpp @@ -21,6 +21,8 @@ */ #include "button.hpp" +#include "render_text_texture.hpp" + #include void Button::DrawTo(SDL_Renderer* renderer) { @@ -51,19 +53,9 @@ void Button::SetBackgroundTexture(SDL_Renderer* renderer, SDL_Texture* texture) image.SetClipH(image.GetClipH() / 3); } -void Button::SetText(SDL_Renderer* renderer, TTF_Font* font, std::string s, SDL_Color color) { - //make the surface (from SDL_ttf) - SDL_Surface* surf = TTF_RenderText_Solid(font, s.c_str(), color); - if (!surf) { - throw(std::runtime_error("Failed to create a TTF surface")); - } - +void Button::SetText(SDL_Renderer* renderer, TTF_Font* font, SDL_Color color, std::string s) { //convert to texture - SDL_Texture* text = SDL_CreateTextureFromSurface(renderer, surf); - SDL_FreeSurface(surf); - if (!text) { - throw(std::runtime_error("Failed to create a TTF texture")); - } + SDL_Texture* text = renderTextTexture(renderer, font, color, s); //get the dimensions & rects int x, y, w, h; @@ -87,14 +79,6 @@ void Button::SetText(SDL_Renderer* renderer, TTF_Font* font, std::string s, SDL_ SDL_DestroyTexture(text); } -void Button::SetX(int x) { - posX = x; -} - -void Button::SetY(int y) { - posY = y; -} - Button::State Button::MouseMotion(SDL_MouseMotionEvent const& event) { //if out of bounds, exit if (!CheckBounds(event.x, event.y)) { @@ -156,6 +140,22 @@ Button::State Button::GetState() { return state; } +int Button::SetX(int i) { + return posX = i; +} + +int Button::SetY(int i) { + return posY = i; +} + +int Button::GetX() const { + return posX; +} + +int Button::GetY() const { + return posY; +} + bool Button::CheckBounds(int x, int y) { //return if true (x, y) is within bounds, otherwise return false return !( diff --git a/common/graphics/button.hpp b/common/graphics/button.hpp index d0f032e..2d9da37 100644 --- a/common/graphics/button.hpp +++ b/common/graphics/button.hpp @@ -27,11 +27,6 @@ #include -constexpr SDL_Color COLOR_WHITE = {255, 255, 255, 255}; -constexpr SDL_Color COLOR_RED = {255, 0, 0, 255}; -constexpr SDL_Color COLOR_ORANGE = {255, 127, 0, 255}; -constexpr SDL_Color COLOR_BLUE = {0, 0, 255, 255}; - class Button { public: enum State { @@ -46,9 +41,7 @@ public: //setup void SetBackgroundTexture(SDL_Renderer*, SDL_Texture*); - void SetText(SDL_Renderer*, TTF_Font*, std::string, SDL_Color); - void SetX(int x); - void SetY(int y); + void SetText(SDL_Renderer*, TTF_Font*, SDL_Color, std::string); //capture input State MouseMotion(SDL_MouseMotionEvent const&); @@ -59,10 +52,16 @@ public: void SetState(State); //TODO: idle, busy or disabled State GetState(); + //accessors & mutators + int SetX(int x); + int SetY(int y); + int GetX() const; + int GetY() const; + protected: bool CheckBounds(int x, int y); Image image; - int posX = 0, posY = 0; State state = State::IDLE; + int posX = 0, posY = 0; }; diff --git a/common/graphics/makefile b/common/graphics/makefile index e6bcb85..9363f70 100644 --- a/common/graphics/makefile +++ b/common/graphics/makefile @@ -1,5 +1,5 @@ #config -INCLUDES+=. +INCLUDES+=. ../utilities LIBS+= CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES)) diff --git a/common/graphics/render_text_texture.cpp b/common/graphics/render_text_texture.cpp new file mode 100644 index 0000000..bd02f48 --- /dev/null +++ b/common/graphics/render_text_texture.cpp @@ -0,0 +1,46 @@ +/* Copyright: (c) Kayne Ruse 2013-2016 + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. +*/ +#include "render_text_texture.hpp" + +#include + +SDL_Texture* renderTextTexture(SDL_Renderer* renderer, TTF_Font* font, SDL_Color color, std::string str) { + //make the surface (from SDL_ttf) + SDL_Surface* surface = TTF_RenderText_Solid(font, str.c_str(), color); + if (!surface) { + throw(std::runtime_error("Failed to create a TTF surface")); + } + + //convert to texture + SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface); + + //cleanup + SDL_FreeSurface(surface); + + //check + if (!texture) { + throw(std::runtime_error("Failed to create a TTF texture")); + } + + //NOTE: free the texture yourself + return texture; +} \ No newline at end of file diff --git a/common/graphics/render_text_texture.hpp b/common/graphics/render_text_texture.hpp new file mode 100644 index 0000000..1aca1ed --- /dev/null +++ b/common/graphics/render_text_texture.hpp @@ -0,0 +1,29 @@ +/* Copyright: (c) Kayne Ruse 2013-2016 + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. +*/ +#pragma once + +#include "SDL2/SDL.h" +#include "SDL2/SDL_ttf.h" + +#include + +SDL_Texture* renderTextTexture(SDL_Renderer*, TTF_Font*, SDL_Color color, std::string); \ No newline at end of file diff --git a/common/graphics/text_box.cpp b/common/graphics/text_box.cpp index 56e7531..f597d73 100644 --- a/common/graphics/text_box.cpp +++ b/common/graphics/text_box.cpp @@ -31,15 +31,18 @@ TextBox::~TextBox() { // } -void TextBox::DrawTo(SDL_Renderer* renderer, int posX, int posY, int pointSize) { +void TextBox::DrawTo(SDL_Renderer* renderer) { + int renderY = posY; for (std::list::iterator it = lineList.begin(); it != lineList.end(); it++) { - it->DrawTo(renderer, posX, posY); - posY += pointSize; + it->SetX(posX); + it->SetY(renderY); + it->DrawTo(renderer); + renderY += it->GetPointHeight(); } } -void TextBox::PushLine(SDL_Renderer* renderer, TTF_Font* font, std::string str, SDL_Color color) { - lineList.emplace_front(renderer, font, str, color); +void TextBox::PushLine(SDL_Renderer* renderer, TTF_Font* font, SDL_Color color, std::string str) { + lineList.emplace_back(renderer, font, color, str, 0, 0); } void TextBox::PopLine(int num) { @@ -47,10 +50,26 @@ void TextBox::PopLine(int num) { num < lineList.size() ? num : lineList.size(); for (int i = 0; i < num; ++i) { - lineList.pop_back(); + lineList.pop_front(); } } void TextBox::ClearLines() { lineList.clear(); } + +int TextBox::SetX(int i) { + return posX = i; +} + +int TextBox::SetY(int i) { + return posY = i; +} + +int TextBox::GetX() const { + return posX; +} + +int TextBox::GetY() const { + return posY; +} diff --git a/common/graphics/text_box.hpp b/common/graphics/text_box.hpp index 66c34ba..0e96bc8 100644 --- a/common/graphics/text_box.hpp +++ b/common/graphics/text_box.hpp @@ -34,12 +34,18 @@ public: TextBox(); ~TextBox(); - void DrawTo(SDL_Renderer*, int posX, int posY, int pointSize); + void DrawTo(SDL_Renderer*); - void PushLine(SDL_Renderer*, TTF_Font*, std::string, SDL_Color color); + void PushLine(SDL_Renderer*, TTF_Font*, SDL_Color color, std::string); void PopLine(int num = 1); void ClearLines(); + int SetX(int i); + int SetY(int i); + int GetX() const; + int GetY() const; + private: std::list lineList; + int posX = 0, posY = 0; }; \ No newline at end of file diff --git a/common/graphics/text_field.cpp b/common/graphics/text_field.cpp new file mode 100644 index 0000000..c9056be --- /dev/null +++ b/common/graphics/text_field.cpp @@ -0,0 +1,109 @@ +/* Copyright: (c) Kayne Ruse 2013-2016 + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. +*/ +#include "text_field.hpp" + +#include "render_text_texture.hpp" + +TextField::TextField() { + // +} + +TextField::~TextField() { + SDL_DestroyTexture(texture); +} + +void TextField::DrawTo(SDL_Renderer* renderer) { + if (!texture) { + return; + } + SDL_Rect dclip = {posX, posY, 0, 0}; + SDL_QueryTexture(texture, nullptr, nullptr, &dclip.w, &dclip.h); + SDL_RenderCopy(renderer, texture, nullptr, &dclip); +} + +std::string TextField::PushText(SDL_Renderer* renderer, TTF_Font* font, SDL_Color color, std::string s) { + text += s; + return SetText(renderer, font, color, text); +} + +std::string TextField::SetText(SDL_Renderer* renderer, TTF_Font* font, SDL_Color color, std::string s) { + text = s; + SDL_DestroyTexture(texture); + if (text.size()) { + texture = renderTextTexture(renderer, font, color, text); + } + else { + texture = nullptr; + } + return text; +} + +std::string TextField::PopChars(SDL_Renderer* renderer, TTF_Font* font, SDL_Color color, int i) { + if (text.size() > 0) { + text.erase(text.size() - i); + } + return SetText(renderer, font, color, text); +} + +std::string TextField::GetText() { + return text; +} + +bool TextField::MouseButtonDown(SDL_MouseButtonEvent const& event) { + BoundingBox cursorBox = {event.x, event.y, 0, 0}; + BoundingBox fieldBox = bounds; + fieldBox.x += posX; + fieldBox.y += posY; + return focus = fieldBox.CheckOverlap(cursorBox); +} + +BoundingBox TextField::SetBounds(BoundingBox b) { + return bounds = b; +} + +BoundingBox TextField::GetBounds() { + return bounds; +} + +bool TextField::SetFocus(bool b) { + return focus = b; +} + +bool TextField::GetFocus() { + return focus; +} + +int TextField::SetX(int i) { + return posX = i; +} + +int TextField::SetY(int i) { + return posY = i; +} + +int TextField::GetX() const { + return posX; +} + +int TextField::GetY() const { + return posY; +} diff --git a/common/graphics/text_field.hpp b/common/graphics/text_field.hpp new file mode 100644 index 0000000..5bdbc21 --- /dev/null +++ b/common/graphics/text_field.hpp @@ -0,0 +1,65 @@ +/* Copyright: (c) Kayne Ruse 2013-2016 + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. +*/ +#pragma once + +#include "bounding_box.hpp" + +#include "SDL2/SDL.h" +#include "SDL2/SDL_ttf.h" + +#include + +class TextField { +public: + TextField(); + ~TextField(); + + void DrawTo(SDL_Renderer*); + + //input + std::string PushText(SDL_Renderer*, TTF_Font*, SDL_Color color, std::string); + std::string SetText(SDL_Renderer*, TTF_Font*, SDL_Color color, std::string); + std::string PopChars(SDL_Renderer*, TTF_Font*, SDL_Color color, int i); + + std::string GetText(); + + bool MouseButtonDown(SDL_MouseButtonEvent const& event); + + BoundingBox SetBounds(BoundingBox b); + BoundingBox GetBounds(); + + bool SetFocus(bool b); + bool GetFocus(); + + //accessors & mutators + int SetX(int i); + int SetY(int i); + int GetX() const; + int GetY() const; + +private: + SDL_Texture* texture = nullptr; + std::string text; + BoundingBox bounds; + bool focus = false; + int posX = 0, posY = 0; +}; \ No newline at end of file diff --git a/common/graphics/text_line.cpp b/common/graphics/text_line.cpp index 9c3c0ab..865f07e 100644 --- a/common/graphics/text_line.cpp +++ b/common/graphics/text_line.cpp @@ -21,50 +21,58 @@ */ #include "text_line.hpp" +#include "render_text_texture.hpp" + #include -SDL_Texture* renderTextTexture(SDL_Renderer* renderer, TTF_Font* font, std::string str, SDL_Color color) { - //make the surface (from SDL_ttf) - SDL_Surface* surface = TTF_RenderText_Solid(font, str.c_str(), color); - if (!surface) { - throw(std::runtime_error("Failed to create a TTF surface")); - } - - //convert to texture - SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface); - - //cleanup - SDL_FreeSurface(surface); - - //check - if (!texture) { - throw(std::runtime_error("Failed to create a TTF texture")); - } - - //NOTE: free the texture yourself - return texture; -} - TextLine::TextLine() { // } +TextLine::TextLine(SDL_Renderer* renderer, TTF_Font* font, SDL_Color color, std::string str, int x, int y) { + SetText(renderer, font, color, str); + posX = x; + posY = y; +} + TextLine::~TextLine() { ClearText(); } -void TextLine::DrawTo(SDL_Renderer* renderer, int posX, int posY) { +void TextLine::DrawTo(SDL_Renderer* renderer) { SDL_Rect dclip = {posX, posY, 0, 0}; SDL_QueryTexture(texture, nullptr, nullptr, &dclip.w, &dclip.h); SDL_RenderCopy(renderer, texture, nullptr, &dclip); } -void TextLine::SetText(SDL_Renderer* renderer, TTF_Font* font, std::string str, SDL_Color color) { +void TextLine::SetText(SDL_Renderer* renderer, TTF_Font* font, SDL_Color color, std::string str) { //just use the above global function SDL_DestroyTexture(texture); - texture = renderTextTexture(renderer, font, str, color); + texture = renderTextTexture(renderer, font, color, str); + pointHeight = TTF_FontHeight(font); } void TextLine::ClearText() { SDL_DestroyTexture(texture); + pointHeight = 0; +} + +int TextLine::SetX(int i) { + return posX = i; +} + +int TextLine::SetY(int i) { + return posY = i; +} + +int TextLine::GetX() const { + return posX; +} + +int TextLine::GetY() const { + return posY; +} + +int TextLine::GetPointHeight() { + return pointHeight; } \ No newline at end of file diff --git a/common/graphics/text_line.hpp b/common/graphics/text_line.hpp index d43d876..ffa6661 100644 --- a/common/graphics/text_line.hpp +++ b/common/graphics/text_line.hpp @@ -26,20 +26,28 @@ #include -SDL_Texture* renderTextTexture(SDL_Renderer*, TTF_Font*, std::string, SDL_Color color); - class TextLine { public: TextLine(); - TextLine(SDL_Renderer* r, TTF_Font* f, std::string s, SDL_Color c) - { SetText(r, f, s, c); } + TextLine(SDL_Renderer*, TTF_Font*, SDL_Color, std::string, int x, int y); virtual ~TextLine(); - void DrawTo(SDL_Renderer*, int posX, int posY); + void DrawTo(SDL_Renderer*); - void SetText(SDL_Renderer*, TTF_Font*, std::string, SDL_Color color); + void SetText(SDL_Renderer*, TTF_Font*, SDL_Color, std::string); void ClearText(); + //accessors & mutators + int SetX(int i); + int SetY(int i); + int GetX() const; + int GetY() const; + + //utility + int GetPointHeight(); + protected: SDL_Texture* texture = nullptr; + int posX = 0, posY = 0; + int pointHeight = 0; //internal use for TextBox }; \ No newline at end of file