Fleshing out TextBox

This commit is contained in:
2015-08-13 21:45:20 +10:00
parent 2dec2a46fc
commit f81032b718
6 changed files with 58 additions and 31 deletions
+26
View File
@@ -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;
}
+1
View File
@@ -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();
@@ -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 <stdexcept>
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();
}
@@ -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*);
+5 -7
View File
@@ -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);
}
//-------------------------
+2 -1
View File
@@ -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;
};