Calamity Engine 1.0.0
A cross-platform 2D game engine written in C++ and SDL3.
Loading...
Searching...
No Matches
label.hpp
Go to the documentation of this file.
1#pragma once
2#include "../node/components.hpp"
3#include "../definitions.hpp"
4#include "../../services/graphics/graphics.hpp"
5
45class Label : public Component {
46public:
47 Label(const std::string& text, std::shared_ptr<Font> font);
48 Label(const std::string& text);
49 Label();
50 ~Label();
51
54
55 void update(float dt);
56 void render(std::shared_ptr<Window> window);
57 std::shared_ptr<Font> font;
58 bool visible = true;
59 bool wrap = true; // text wrap. yes or no?
60 bool screenSpace = false;
61
62 Label* setText(const std::string& text);
63 std::string getText() const;
64 TTF_Text* getHandle() const;
65
66 Label* setColor(Color color);
67 Color getColor() const;
69 Label* setWrapWidth(int width); // meant for internal use
70 int getWrapWidth() const;
71 void rebuildTexture();
72 SDL_Texture *getTexture() const;
73
74 template <class Archive>
75 void save(Archive &ar) const
76 {
77 ar(CEREAL_NVP(size), CEREAL_NVP(origin), CEREAL_NVP(font), CEREAL_NVP(visible), CEREAL_NVP(wrap), CEREAL_NVP(color), CEREAL_NVP(direction), CEREAL_NVP(wrapWidth), CEREAL_NVP(text));
78 }
79
80 template <class Archive>
81 void load(Archive &ar)
82 {
83 ar(CEREAL_NVP(size), CEREAL_NVP(origin), CEREAL_NVP(font), CEREAL_NVP(visible), CEREAL_NVP(wrap), CEREAL_NVP(color), CEREAL_NVP(direction), CEREAL_NVP(wrapWidth), CEREAL_NVP(text));
84 TTF_CreateText(Services::graphics()->getTextEngine(), font->getHandle(), text.c_str(), text.size());
85 TTF_SetTextColor(handle, color.r, color.g, color.b, color.a);
86 TTF_SetTextDirection(handle, (TTF_Direction)direction);
87 TTF_SetTextWrapWidth(handle, wrapWidth);
88 }
89private:
90 std::string text = "";
91 Color color = Color::WHITE;
93 int wrapWidth = -1;
94 bool dirty = true;
95
96 TTF_Text* handle; // TTF_Text*
97 SDL_Texture *texture = nullptr;
98 Vector2 prevSize;
99};
100
Definition definitions.hpp:41
Uint8 b
Definition definitions.hpp:45
static const Color WHITE
Definition definitions.hpp:58
Uint8 g
Definition definitions.hpp:44
Uint8 r
Definition definitions.hpp:43
Uint8 a
Definition definitions.hpp:46
Definition label.hpp:45
void update(float dt)
Definition label.cpp:10
bool screenSpace
Definition label.hpp:60
~Label()
Definition label.cpp:84
void rebuildTexture()
Definition label.cpp:26
Color getColor() const
Definition label.cpp:52
std::string getText() const
Definition label.cpp:42
void render(std::shared_ptr< Window > window)
Definition label.cpp:126
void load(Archive &ar)
Definition label.hpp:81
int getWrapWidth() const
Definition label.cpp:121
Label * setColor(Color color)
Definition label.cpp:89
Vector2 size
Definition label.hpp:52
Label * setDirection(FontDirection direction)
Definition label.cpp:97
void save(Archive &ar) const
Definition label.hpp:75
SDL_Texture * getTexture() const
Definition label.cpp:21
bool visible
Definition label.hpp:58
std::shared_ptr< Font > font
Definition label.hpp:57
Label * setWrapWidth(int width)
Definition label.cpp:113
TTF_Text * getHandle() const
Definition label.cpp:47
bool wrap
Definition label.hpp:59
Vector2 origin
Definition label.hpp:53
Label()
Definition label.cpp:59
Label * setText(const std::string &text)
Definition label.cpp:105
static Graphics * graphics()
Definition services.cpp:23
FontDirection
Definition definitions.hpp:42
CEREAL_REGISTER_TYPE(Label)
CEREAL_REGISTER_POLYMORPHIC_RELATION(Component, Label)
Definition components.hpp:21
Definition definitions.hpp:77