commit 188f5e46
Added some comments to SagoTextField and SagoTextBox. Made SagoTextField holder invalidation aware.
also made the human and bat spawn in the right locations.
Changed files
diff --git a/src/sago/SagoDataHolder.hpp b/src/sago/SagoDataHolder.hpp
index 3676cc9..068650a 100644
--- a/src/sago/SagoDataHolder.hpp
+++ b/src/sago/SagoDataHolder.hpp
@@ -83,7 +83,7 @@ public:
SagoDataHolder();
explicit SagoDataHolder(SDL_Renderer* renderer);
/**
- * Return a pointer to the given texture. The pointer is valid for the lifetime of SagoDataHolder object it was taken from or invalidateAll is called.
+ * Return a pointer to the given texture. The pointer is valid for the lifetime of SagoDataHolder object it was taken from or until invalidateAll is called.
* @param textureName Name of the texture
* @return Pointer to the loaded texture
*/
@@ -106,6 +106,11 @@ public:
* Setting a new renderer might cause all old textures to no longer match the renderer format.
*/
void invalidateAll(SDL_Renderer* renderer);
+ /**
+ * The version number. Changes everytime the pointers are invalidated.
+ * Can be used to determen if it is neccecary to get a new pointer.
+ * @return A globally unique number.
+ */
Uint64 getVersion() const;
virtual ~SagoDataHolder();
private:
diff --git a/src/sago/SagoTextBox.cpp b/src/sago/SagoTextBox.cpp
index 26ad966..47971e1 100644
--- a/src/sago/SagoTextBox.cpp
+++ b/src/sago/SagoTextBox.cpp
@@ -97,7 +97,6 @@ void SagoTextBox::AppendLineToCache(const std::string& text) {
void SagoTextBox::SplitAndAppendLineToCache(TTF_Font* font, const std::string& text) {
int width = data->maxWidth;
TTF_SizeUTF8(font, text.c_str(),&width, nullptr);
- std::cerr << "Width: " << width << "\n";
if (data->maxWidth <= 0 || width <= data->maxWidth) {
AppendLineToCache(text);
return;
diff --git a/src/sago/SagoTextBox.hpp b/src/sago/SagoTextBox.hpp
index 2658371..a1d4c38 100644
--- a/src/sago/SagoTextBox.hpp
+++ b/src/sago/SagoTextBox.hpp
@@ -40,6 +40,13 @@ public:
void SetFont(const char* fontName);
void SetFontSize(int fontSize);
void SetOutline(int outlineSize, const SDL_Color& color);
+ /**
+ * Sets the max width to generate. SagoTextBox will insert line breaks to keep the width below this number.
+ * Outline is not included in the width: If you have a 2 pixels outline the rendere may go 2 pixels beyond.
+ * You will always need to generate at least one char per line. If max width is too low one char will be drawn per line even if it goes above max width.
+ * Setting this to 0 will disable the feature.
+ * @param width The maximum width before forcing a line break
+ */
void SetMaxWidth(int width);
const std::string& GetText() const;
void Draw(SDL_Renderer* target, int x, int y);
diff --git a/src/sago/SagoTextField.cpp b/src/sago/SagoTextField.cpp
index ce6cc23..2a3641c 100644
--- a/src/sago/SagoTextField.cpp
+++ b/src/sago/SagoTextField.cpp
@@ -71,6 +71,7 @@ struct SagoTextField::SagoTextFieldData {
int outline = 0;
std::string text = "";
std::string renderedText = "";
+ Uint64 renderedVersion = 0;
};
SagoTextField::SagoTextField() {
@@ -156,13 +157,14 @@ void SagoTextField::UpdateCache(SDL_Renderer* target) {
oh.reset();
}
data->renderedText = data->text;
+ data->renderedVersion = data->tex->getVersion();
}
void SagoTextField::Draw(SDL_Renderer* target, int x, int y) {
if (data->text.empty()) {
return;
}
- if (data->text != data->renderedText) {
+ if (data->text != data->renderedText || data->renderedVersion != data->tex->getVersion()) {
UpdateCache(target);
}
if (!data->texture) {
diff --git a/src/sago/SagoTextField.hpp b/src/sago/SagoTextField.hpp
index 99aaf03..b725199 100644
--- a/src/sago/SagoTextField.hpp
+++ b/src/sago/SagoTextField.hpp
@@ -29,21 +29,62 @@ SOFTWARE.
namespace sago {
+/**
+ * This is a text field.
+ * It represents a line of text to be drawn on screen. It is not possible to have line breaks.
+ * If line breaks are needed use SagoTextBox instead.
+ *
+ * This object renderes to a texture and cahces the texture. The texture will be automatically refreshed if the text changes, the SagoDataHolder is invalidated or ClearCache is called.
+ * Normally all values will be set at the beginning before text is drawn.
+ * SetHolder MUST be called before the field is drawn!
+ */
class SagoTextField {
public:
SagoTextField();
SagoTextField(SagoTextField&& o) noexcept;
SagoTextField& operator=(const SagoTextField&& base) = delete;
virtual ~SagoTextField();
+ /**
+ * Sets the data holder. This is MANDATORY
+ * @param holder The data holder to fetch the fonts from
+ */
void SetHolder(SagoDataHolder* holder);
+ /**
+ * Set the text to display.
+ * @param text The actual UTF-8 encoded text
+ */
void SetText(const char* text);
void SetColor(const SDL_Color& color);
+ /**
+ * Set the name of the font. Must be known to the data holder.
+ * @param fontName Name of the font as required by SagoDataHolder
+ */
void SetFont(const char* fontName);
void SetFontSize(int fontSize);
+ /**
+ * Enable outline against the font.
+ * @param outlineSize Number of pixels of outline.
+ * @param color The color of the outline.
+ */
void SetOutline(int outlineSize, const SDL_Color& color);
+ /**
+ * Get the text we are currently drawing
+ * @return The text
+ */
const std::string& GetText() const;
void Draw(SDL_Renderer* target, int x, int y);
+ /**
+ * Updates the cache.
+ * You normally do not want to call this from the outside as it is done just in time.
+ * Unless you want to precache of course....
+ * @param target Target the the text will eventually be rendered to
+ */
void UpdateCache(SDL_Renderer* target);
+ /**
+ * Clears the cache and forces the SagoTextField to render it again the next time it is drawn.
+ * Can be used if you have changed font, color or sizes.
+ * Changing the text implices a cache clear.
+ */
void ClearCache();
private:
SagoTextField(const SagoTextField& orig) = delete;
diff --git a/src/saland.cpp b/src/saland.cpp
index d51b829..b8125db 100644
--- a/src/saland.cpp
+++ b/src/saland.cpp
@@ -77,10 +77,9 @@ public:
textField.SetText("Saland Adventures - The game that has a very long subtitle to test the outline");
textField.SetOutline(3, SDL_Color{255,165,0,255});
textField.Draw(target, 10, 10);
- textBox.SetText("This is some text. It is also quite long. \nIt must take several lines!\nEven 3 lines!\nand 4!\nAlso5\n\nAnd a blank one above!gg\ngg"
- "\nabcdefghijklmnopqrstuvwxyz");
+ textBox.SetText("This is an attempt to create 2d rpg with a modifiable world.\nLet's see how it goes!");
textBox.SetOutline(1, SDL_Color{255,0,0,255});
- textBox.SetMaxWidth(300);
+ textBox.SetMaxWidth(800);
textBox.Draw(target, 300,300);
circleRGBA(target,
150, 150, 75,
diff --git a/src/saland/Game.cpp b/src/saland/Game.cpp
index 0e7c43f..48f11aa 100644
--- a/src/saland/Game.cpp
+++ b/src/saland/Game.cpp
@@ -152,6 +152,7 @@ Game::Game() {
myFixtureDef.shape = &circleShape; //this is a pointer to the shape above
myFixtureDef.density = 10.0f;
data->human->body->CreateFixture(&myFixtureDef); //add a fixture to the body
+ data->human->body->SetTransform(b2Vec2(data->human->X / 32.0f, data->human->Y / 32.0f),data->human->body->GetAngle());
b2BodyDef batBodyDef;
batBodyDef.type = b2_dynamicBody;
@@ -162,6 +163,7 @@ Game::Game() {
batDef.shape = &circleShape;
batDef.density = 10.0f;
bat->body->CreateFixture(&batDef);
+ bat->body->SetTransform(b2Vec2(bat.get()->X / 32.0f, bat.get()->Y / 32.0f),bat->body->GetAngle());
b2BodyDef barrelBodyDef;
barrelBodyDef.type = b2_staticBody;