Fleshing out TextBox
This commit is contained in:
@@ -104,6 +104,7 @@ SDL_Texture* Image::Create(SDL_Renderer* renderer, Uint16 w, Uint16 h) {
|
|||||||
SDL_TEXTUREACCESS_TARGET,
|
SDL_TEXTUREACCESS_TARGET,
|
||||||
w, h);
|
w, h);
|
||||||
|
|
||||||
|
//check
|
||||||
if (!texture) {
|
if (!texture) {
|
||||||
std::ostringstream msg;
|
std::ostringstream msg;
|
||||||
msg << "Failed to create a texture; " << SDL_GetError();
|
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;
|
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;
|
return texture;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -40,6 +40,7 @@ public:
|
|||||||
|
|
||||||
SDL_Texture* Load(SDL_Renderer* renderer, std::string fname);
|
SDL_Texture* Load(SDL_Renderer* renderer, std::string fname);
|
||||||
SDL_Texture* Create(SDL_Renderer* renderer, Uint16 w, Uint16 h);
|
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* SetTexture(SDL_Texture*);
|
||||||
SDL_Texture* GetTexture() const;
|
SDL_Texture* GetTexture() const;
|
||||||
virtual void Free();
|
virtual void Free();
|
||||||
|
|||||||
@@ -19,11 +19,11 @@
|
|||||||
* 3. This notice may not be removed or altered from any source
|
* 3. This notice may not be removed or altered from any source
|
||||||
* distribution.
|
* distribution.
|
||||||
*/
|
*/
|
||||||
#include "text_util.hpp"
|
#include "text_box.hpp"
|
||||||
|
|
||||||
#include <stdexcept>
|
#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)
|
//make the surface (from SDL_ttf)
|
||||||
SDL_Surface* surface = TTF_RenderText_Solid(font, str.c_str(), color);
|
SDL_Surface* surface = TTF_RenderText_Solid(font, str.c_str(), color);
|
||||||
if (!surface) {
|
if (!surface) {
|
||||||
@@ -32,38 +32,39 @@ SDL_Texture* renderPlainText(SDL_Renderer* renderer, TTF_Font* font, std::string
|
|||||||
|
|
||||||
//convert to texture
|
//convert to texture
|
||||||
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface);
|
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface);
|
||||||
if (!texture) {
|
|
||||||
SDL_FreeSurface(surface);
|
|
||||||
throw(std::runtime_error("Failed to create a TTF texture"));
|
|
||||||
}
|
|
||||||
|
|
||||||
//cleanup
|
//cleanup
|
||||||
SDL_FreeSurface(surface);
|
SDL_FreeSurface(surface);
|
||||||
|
|
||||||
|
//check
|
||||||
|
if (!texture) {
|
||||||
|
throw(std::runtime_error("Failed to create a TTF texture"));
|
||||||
|
}
|
||||||
|
|
||||||
//NOTE: free the texture yourself
|
//NOTE: free the texture yourself
|
||||||
return texture;
|
return texture;
|
||||||
}
|
}
|
||||||
|
|
||||||
TextBox::TextBox() {
|
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
TextBox::~TextBox() {
|
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
void TextBox::DrawTo(SDL_Renderer* renderer) {
|
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() {
|
void TextBox::ClearText() {
|
||||||
//
|
image.Free();
|
||||||
}
|
}
|
||||||
@@ -29,12 +29,12 @@
|
|||||||
|
|
||||||
constexpr SDL_Color COLOR_WHITE = {255, 255, 255, 255};
|
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 {
|
class TextBox {
|
||||||
public:
|
public:
|
||||||
TextBox();
|
TextBox() = default;
|
||||||
~TextBox();
|
~TextBox() = default;
|
||||||
|
|
||||||
void DrawTo(SDL_Renderer*);
|
void DrawTo(SDL_Renderer*);
|
||||||
|
|
||||||
@@ -61,6 +61,10 @@ ExampleScene::ExampleScene(lua_State* L) {
|
|||||||
button.SetX(400);
|
button.SetX(400);
|
||||||
button.SetY(200);
|
button.SetY(200);
|
||||||
// {140, 62, 54, 255}
|
// {140, 62, 54, 255}
|
||||||
|
|
||||||
|
//DEBUG: testing TextBox
|
||||||
|
textBox.SetY(500);
|
||||||
|
textBox.SetText(GetRenderer(), font, "Button Text", {255, 255, 255, 255});
|
||||||
}
|
}
|
||||||
|
|
||||||
ExampleScene::~ExampleScene() {
|
ExampleScene::~ExampleScene() {
|
||||||
@@ -91,13 +95,7 @@ void ExampleScene::RenderFrame(SDL_Renderer* renderer) {
|
|||||||
|
|
||||||
//DEBUG: testing UI
|
//DEBUG: testing UI
|
||||||
button.DrawTo(renderer);
|
button.DrawTo(renderer);
|
||||||
|
textBox.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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//-------------------------
|
//-------------------------
|
||||||
|
|||||||
@@ -24,7 +24,7 @@
|
|||||||
#include "base_scene.hpp"
|
#include "base_scene.hpp"
|
||||||
#include "button.hpp"
|
#include "button.hpp"
|
||||||
#include "region_pager_lua.hpp"
|
#include "region_pager_lua.hpp"
|
||||||
#include "text_util.hpp"
|
#include "text_box.hpp"
|
||||||
#include "tile_sheet.hpp"
|
#include "tile_sheet.hpp"
|
||||||
|
|
||||||
#include "lua.hpp"
|
#include "lua.hpp"
|
||||||
@@ -67,4 +67,5 @@ private:
|
|||||||
TTF_Font* font = nullptr;
|
TTF_Font* font = nullptr;
|
||||||
Image buttonBG;
|
Image buttonBG;
|
||||||
Button button;
|
Button button;
|
||||||
|
TextBox textBox;
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user