diff --git a/src/common/text_util.cpp b/src/common/text_util.cpp new file mode 100644 index 0000000..054aa3c --- /dev/null +++ b/src/common/text_util.cpp @@ -0,0 +1,69 @@ +/* Copyright: (c) Kayne Ruse 2013-2015 + * + * 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_util.hpp" + +#include + +SDL_Texture* renderPlainText(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); + if (!texture) { + SDL_FreeSurface(surface); + throw(std::runtime_error("Failed to create a TTF texture")); + } + + //cleanup + SDL_FreeSurface(surface); + + //NOTE: free the texture yourself + return texture; +} + +TextBox::TextBox() { + // +} + +TextBox::~TextBox() { + // +} + +void TextBox::DrawTo(SDL_Renderer* renderer) { + // +} + +void TextBox::SetText(SDL_Renderer*, std::string) { + // +} + +void TextBox::AddText(SDL_Renderer*, std::string) { + // +} + +void TextBox::ClearText() { + // +} diff --git a/src/common/text_util.hpp b/src/common/text_util.hpp new file mode 100644 index 0000000..5e20ff1 --- /dev/null +++ b/src/common/text_util.hpp @@ -0,0 +1,54 @@ +/* Copyright: (c) Kayne Ruse 2013-2015 + * + * 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 "image.hpp" +#include "SDL2/SDL.h" +#include "SDL2/SDL_ttf.h" + +#include + +constexpr SDL_Color COLOR_WHITE = {255, 255, 255, 255}; + +SDL_Texture* renderPlainText(SDL_Renderer*, TTF_Font*, std::string, SDL_Color color); + +class TextBox { +public: + TextBox(); + ~TextBox(); + + void DrawTo(SDL_Renderer*); + + void SetText(SDL_Renderer*, TTF_Font*, std::string, SDL_Color color); + void AddText(SDL_Renderer*, TTF_Font*, std::string, SDL_Color color); + void ClearText(); + + //position + int SetX(int x) { return posX = x; } + int SetY(int y) { return posY = y; } + int SetX() { return posX; } + int SetY() { return posY; } + +protected: + Image image; + int posX = 0, posY = 0; +}; \ No newline at end of file diff --git a/src/example_scene.cpp b/src/example_scene.cpp index 165b867..7162dc7 100644 --- a/src/example_scene.cpp +++ b/src/example_scene.cpp @@ -91,6 +91,13 @@ 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); } //------------------------- diff --git a/src/example_scene.hpp b/src/example_scene.hpp index 757ae95..c950901 100644 --- a/src/example_scene.hpp +++ b/src/example_scene.hpp @@ -24,6 +24,7 @@ #include "base_scene.hpp" #include "button.hpp" #include "region_pager_lua.hpp" +#include "text_util.hpp" #include "tile_sheet.hpp" #include "lua.hpp"