Text is rendered without a black box
This makes it look nicer, IMO. textBox doesn't use Image, since TextBox is more specialized.
This commit is contained in:
+15
-11
@@ -45,20 +45,24 @@ SDL_Texture* renderTextTexture(SDL_Renderer* renderer, TTF_Font* font, std::stri
|
|||||||
return texture;
|
return texture;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TextBox::TextBox() {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
TextBox::~TextBox() {
|
||||||
|
ClearText();
|
||||||
|
}
|
||||||
|
|
||||||
void TextBox::DrawTo(SDL_Renderer* renderer) {
|
void TextBox::DrawTo(SDL_Renderer* renderer) {
|
||||||
image.DrawTo(renderer, posX, posY);
|
SDL_Rect dclip = {posX, posY, 0, 0};
|
||||||
|
SDL_QueryTexture(texture, nullptr, nullptr, &dclip.w, &dclip.h);
|
||||||
|
SDL_RenderCopy(renderer, texture, nullptr, &dclip);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TextBox::SetText(SDL_Renderer* renderer, TTF_Font* font, std::string str, SDL_Color color) {
|
void TextBox::SetText(SDL_Renderer* renderer, TTF_Font* font, std::string str, SDL_Color color) {
|
||||||
//make the new texture
|
//just use the above global function
|
||||||
SDL_Texture* newText = renderTextTexture(renderer, font, str, color);
|
SDL_DestroyTexture(texture);
|
||||||
|
texture = renderTextTexture(renderer, font, str, color);
|
||||||
//copy the texture
|
|
||||||
image.Free();
|
|
||||||
image.CopyTexture(renderer, newText);
|
|
||||||
|
|
||||||
//cleanup
|
|
||||||
SDL_DestroyTexture(newText);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void TextBox::AddText(SDL_Renderer* renderer, TTF_Font* font, std::string str, SDL_Color color) {
|
void TextBox::AddText(SDL_Renderer* renderer, TTF_Font* font, std::string str, SDL_Color color) {
|
||||||
@@ -66,5 +70,5 @@ void TextBox::AddText(SDL_Renderer* renderer, TTF_Font* font, std::string str, S
|
|||||||
}
|
}
|
||||||
|
|
||||||
void TextBox::ClearText() {
|
void TextBox::ClearText() {
|
||||||
image.Free();
|
SDL_DestroyTexture(texture);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,7 +21,6 @@
|
|||||||
*/
|
*/
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "image.hpp"
|
|
||||||
#include "SDL2/SDL.h"
|
#include "SDL2/SDL.h"
|
||||||
#include "SDL2/SDL_ttf.h"
|
#include "SDL2/SDL_ttf.h"
|
||||||
|
|
||||||
@@ -33,8 +32,8 @@ SDL_Texture* renderTextTexture(SDL_Renderer*, TTF_Font*, std::string, SDL_Color
|
|||||||
|
|
||||||
class TextBox {
|
class TextBox {
|
||||||
public:
|
public:
|
||||||
TextBox() = default;
|
TextBox();
|
||||||
~TextBox() = default;
|
~TextBox();
|
||||||
|
|
||||||
void DrawTo(SDL_Renderer*);
|
void DrawTo(SDL_Renderer*);
|
||||||
|
|
||||||
@@ -49,6 +48,6 @@ public:
|
|||||||
int SetY() { return posY; }
|
int SetY() { return posY; }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
Image image;
|
SDL_Texture* texture = nullptr;
|
||||||
int posX = 0, posY = 0;
|
int posX = 0, posY = 0;
|
||||||
};
|
};
|
||||||
Reference in New Issue
Block a user