diff --git a/src/common/image.cpp b/src/common/image.cpp index f04c2e4..f18e521 100644 --- a/src/common/image.cpp +++ b/src/common/image.cpp @@ -104,6 +104,7 @@ SDL_Texture* Image::Create(SDL_Renderer* renderer, Uint16 w, Uint16 h) { SDL_TEXTUREACCESS_TARGET, w, h); + //check if (!texture) { std::ostringstream msg; msg << "Failed to create a texture; " << SDL_GetError(); @@ -121,6 +122,31 @@ SDL_Texture* Image::Create(SDL_Renderer* renderer, Uint16 w, Uint16 h) { } local = true; + //blank (black) texture + SDL_SetRenderTarget(renderer, texture); + SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0); + SDL_RenderFillRect(renderer, nullptr); + SDL_SetRenderTarget(renderer, nullptr); + + return texture; +} + +SDL_Texture* Image::CopyTexture(SDL_Renderer* renderer, SDL_Texture* ptr) { + Free(); + int w = 0, h = 0; + + //get the info + SDL_QueryTexture(ptr, nullptr, nullptr, &w, &h); + + //create a texture of (w, h) size (also sets the metadata) + Create(renderer, w, h); + + //copy the argument texture to the local texture + SDL_SetRenderTarget(renderer, texture); + SDL_RenderCopy(renderer, ptr, nullptr, nullptr); + SDL_SetRenderTarget(renderer, nullptr); + + //return the local texture return texture; } diff --git a/src/common/image.hpp b/src/common/image.hpp index f6685e4..09e7c33 100644 --- a/src/common/image.hpp +++ b/src/common/image.hpp @@ -40,6 +40,7 @@ public: SDL_Texture* Load(SDL_Renderer* renderer, std::string fname); SDL_Texture* Create(SDL_Renderer* renderer, Uint16 w, Uint16 h); + SDL_Texture* CopyTexture(SDL_Renderer* renderer, SDL_Texture* ptr); SDL_Texture* SetTexture(SDL_Texture*); SDL_Texture* GetTexture() const; virtual void Free(); diff --git a/src/common/text_util.cpp b/src/common/text_box.cpp similarity index 70% rename from src/common/text_util.cpp rename to src/common/text_box.cpp index 054aa3c..aeb15dd 100644 --- a/src/common/text_util.cpp +++ b/src/common/text_box.cpp @@ -19,11 +19,11 @@ * 3. This notice may not be removed or altered from any source * distribution. */ -#include "text_util.hpp" +#include "text_box.hpp" #include -SDL_Texture* renderPlainText(SDL_Renderer* renderer, TTF_Font* font, std::string str, SDL_Color color) { +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) { @@ -32,38 +32,39 @@ SDL_Texture* renderPlainText(SDL_Renderer* renderer, TTF_Font* font, std::string //convert to texture SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface); - if (!texture) { - SDL_FreeSurface(surface); - throw(std::runtime_error("Failed to create a TTF texture")); - } //cleanup SDL_FreeSurface(surface); + //check + if (!texture) { + throw(std::runtime_error("Failed to create a TTF texture")); + } + //NOTE: free the texture yourself return texture; } -TextBox::TextBox() { - // -} - -TextBox::~TextBox() { - // -} - void TextBox::DrawTo(SDL_Renderer* renderer) { - // + image.DrawTo(renderer, posX, posY); } -void TextBox::SetText(SDL_Renderer*, std::string) { - // +void TextBox::SetText(SDL_Renderer* renderer, TTF_Font* font, std::string str, SDL_Color color) { + //make the new texture + SDL_Texture* newText = renderTextTexture(renderer, font, str, color); + + //copy the texture + image.Free(); + image.CopyTexture(renderer, newText); + + //cleanup + SDL_DestroyTexture(newText); } -void TextBox::AddText(SDL_Renderer*, std::string) { - // +void TextBox::AddText(SDL_Renderer* renderer, TTF_Font* font, std::string str, SDL_Color color) { + //TODO: empty } void TextBox::ClearText() { - // + image.Free(); } diff --git a/src/common/text_util.hpp b/src/common/text_box.hpp similarity index 91% rename from src/common/text_util.hpp rename to src/common/text_box.hpp index 5e20ff1..376ed4c 100644 --- a/src/common/text_util.hpp +++ b/src/common/text_box.hpp @@ -29,12 +29,12 @@ constexpr SDL_Color COLOR_WHITE = {255, 255, 255, 255}; -SDL_Texture* renderPlainText(SDL_Renderer*, TTF_Font*, std::string, SDL_Color color); +SDL_Texture* renderTextTexture(SDL_Renderer*, TTF_Font*, std::string, SDL_Color color); class TextBox { public: - TextBox(); - ~TextBox(); + TextBox() = default; + ~TextBox() = default; void DrawTo(SDL_Renderer*); diff --git a/src/example_scene.cpp b/src/example_scene.cpp index 7162dc7..17172fd 100644 --- a/src/example_scene.cpp +++ b/src/example_scene.cpp @@ -61,6 +61,10 @@ ExampleScene::ExampleScene(lua_State* L) { button.SetX(400); button.SetY(200); // {140, 62, 54, 255} + + //DEBUG: testing TextBox + textBox.SetY(500); + textBox.SetText(GetRenderer(), font, "Button Text", {255, 255, 255, 255}); } ExampleScene::~ExampleScene() { @@ -91,13 +95,7 @@ void ExampleScene::RenderFrame(SDL_Renderer* renderer) { //DEBUG: testing UI button.DrawTo(renderer); - - SDL_Texture* tex = renderPlainText(renderer, font, "STRING\nTEXT", {255, 255, 255, 255}); - int w = 0, h = 0; - SDL_QueryTexture(tex, nullptr, nullptr, &w, &h); - SDL_Rect d = {0, 0, w, h}; - SDL_RenderCopy(renderer, tex, nullptr, &d); - SDL_DestroyTexture(tex); + textBox.DrawTo(renderer); } //------------------------- diff --git a/src/example_scene.hpp b/src/example_scene.hpp index c950901..1feea6f 100644 --- a/src/example_scene.hpp +++ b/src/example_scene.hpp @@ -24,7 +24,7 @@ #include "base_scene.hpp" #include "button.hpp" #include "region_pager_lua.hpp" -#include "text_util.hpp" +#include "text_box.hpp" #include "tile_sheet.hpp" #include "lua.hpp" @@ -67,4 +67,5 @@ private: TTF_Font* font = nullptr; Image buttonBG; Button button; + TextBox textBox; };