git repos / saland

commit a69fc5dd

sago007 · 2018-03-04 15:23
a69fc5dd109648e735de6df99cda1ea998de191b patch · browse files
parent c8a83270734480fe146b89877d9b8df4ff84415c

It is now possible to set font and size

Changed files

M src/sago/SagoTextField.cpp before
M src/sago/SagoTextField.hpp before
M src/saland.cpp before
M src/saland/globals.hpp before
diff --git a/src/sago/SagoTextField.cpp b/src/sago/SagoTextField.cpp index 79bccec..5871e1f 100644 --- a/src/sago/SagoTextField.cpp +++ b/src/sago/SagoTextField.cpp
@@ -23,6 +23,7 @@ SOFTWARE.
*/
#include "SagoTextField.hpp"
+#include <iostream>
namespace sago {
@@ -31,6 +32,8 @@ struct SagoTextField::SagoTextFieldData {
SDL_Surface* textSurface = nullptr;
SDL_Texture* texture = nullptr;
std::string fontName = "freeserif";
+ SDL_Color color = { 255, 255, 255, 0 };
+ int fontSize = 16;
};
SagoTextField::SagoTextField() {
@@ -38,6 +41,7 @@ SagoTextField::SagoTextField() {
}
SagoTextField::~SagoTextField() {
+ ClearCache();
delete data;
}
@@ -49,20 +53,27 @@ void SagoTextField::SetText(std::string text) {
this->text = text;
}
+void SagoTextField::SetColor(const SDL_Color& color) {
+ data->color = color;
+}
+
+void SagoTextField::SetFont(const std::string& fontName) {
+ data->fontName = fontName;
+}
+
+void SagoTextField::SetFontSize(int fontSize) {
+ data->fontSize = fontSize;
+}
+
std::string SagoTextField::GetText() const {
return text;
}
-void SagoTextField::Draw(SDL_Renderer* target, int x, int y) const {
- SDL_Color color = { 255, 255, 255, 0 };
- TTF_Font *font = data->tex->getFontPtr(data->fontName, 30);
- data->textSurface = TTF_RenderText_Blended (font, text.c_str(), color);
- data->texture = SDL_CreateTextureFromSurface(target, data->textSurface);
- int texW = 0;
- int texH = 0;
- SDL_QueryTexture(data->texture, NULL, NULL, &texW, &texH);
- SDL_Rect dstrect = { x, y, texW, texH };
- SDL_RenderCopy(target, data->texture, NULL, &dstrect);
+void SagoTextField::ClearCache() {
+ if (!data->tex) {
+ std::cerr << "FATAL: DataHolder not set!\n";
+ abort();
+ }
if (data->texture) {
SDL_DestroyTexture(data->texture);
data->texture = nullptr;
@@ -73,4 +84,29 @@ void SagoTextField::Draw(SDL_Renderer* target, int x, int y) const {
}
}
+void SagoTextField::UpdateCache(SDL_Renderer* target) {
+ ClearCache();
+ TTF_Font *font = data->tex->getFontPtr(data->fontName, data->fontSize);
+ data->textSurface = TTF_RenderText_Blended (font, text.c_str(), data->color);
+ data->texture = SDL_CreateTextureFromSurface(target, data->textSurface);
+ renderedText = text;
+}
+
+void SagoTextField::Draw(SDL_Renderer* target, int x, int y) {
+ if (text.empty()) {
+ return;
+ }
+ if (text != renderedText) {
+ UpdateCache(target);
+ }
+ if (!data->texture) {
+ return;
+ }
+ int texW = 0;
+ int texH = 0;
+ SDL_QueryTexture(data->texture, NULL, NULL, &texW, &texH);
+ SDL_Rect dstrect = { x, y, texW, texH };
+ SDL_RenderCopy(target, data->texture, NULL, &dstrect);
+}
+
} //namespace sago
\ No newline at end of file
diff --git a/src/sago/SagoTextField.hpp b/src/sago/SagoTextField.hpp index e03a3de..bbb2395 100644 --- a/src/sago/SagoTextField.hpp +++ b/src/sago/SagoTextField.hpp
@@ -35,8 +35,13 @@ public:
virtual ~SagoTextField();
void SetHolder(SagoDataHolder* holder);
void SetText(std::string text);
+ void SetColor(const SDL_Color& color);
+ void SetFont(const std::string& fontName);
+ void SetFontSize(int fontSize);
std::string GetText() const;
- void Draw(SDL_Renderer* target, int x, int y) const;
+ void Draw(SDL_Renderer* target, int x, int y);
+ void UpdateCache(SDL_Renderer* target);
+ void ClearCache();
private:
std::string text = "";
std::string renderedText = "";
diff --git a/src/saland.cpp b/src/saland.cpp index 45e551e..7cbf0db 100644 --- a/src/saland.cpp +++ b/src/saland.cpp
@@ -58,21 +58,22 @@ https://github.com/sago007/saland
GlobalData globalData;
-static void NFont_Write(SDL_Renderer* target, int x, int y, const char* text) {
- globalData.nf_standard_font.draw(target, x, y, "%s", text);
-}
-
class TitleScreen : public sago::GameStateInterface {
+public:
+ TitleScreen() {
+ textField.SetHolder(globalData.dataHolder);
+ textField.SetFontSize(30);
+ }
+
virtual bool IsActive() override {
return isActive;
}
virtual void Draw(SDL_Renderer* target) override {
- sago::SagoTextField textField;
- textField.SetHolder(globalData.dataHolder);
+
+
textField.SetText("Saland Adventures");
- textField.Draw(target, 10, 100);
- NFont_Write(target, 10, 10, "Saland Adventures");
+ textField.Draw(target, 10, 10);
circleRGBA(target,
150, 150, 75,
0, 0, 255, 255);
@@ -100,6 +101,7 @@ class TitleScreen : public sago::GameStateInterface {
}
private:
bool isActive = true;
+ sago::SagoTextField textField;
};
void RunGameState(sago::GameStateInterface& state ) {
@@ -146,7 +148,6 @@ void runGame() {
globalData.screen = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED);
sago::SagoDataHolder holder(globalData.screen);
globalData.spriteHolder.reset(new sago::SagoSpriteHolder(holder));
- globalData.nf_standard_font.load(globalData.screen, holder.getFontPtr("freeserif", 30),NFont::Color(255,255,255));
globalData.dataHolder = &holder;
TitleScreen ts;
diff --git a/src/saland/globals.hpp b/src/saland/globals.hpp index bd2604f..93dafe1 100644 --- a/src/saland/globals.hpp +++ b/src/saland/globals.hpp
@@ -24,12 +24,10 @@ https://github.com/sago007/saland
#ifndef GLOBALS_HPP
#define GLOBALS_HPP
-#include "../Libs/NFont.h"
#include "../sago/SagoSpriteHolder.hpp"
#include <memory>
struct GlobalData {
- NFont nf_standard_font;
bool isShuttingDown = false;
SDL_Renderer* screen = nullptr;
std::unique_ptr<sago::SagoSpriteHolder> spriteHolder;