sdl2_impl 0.1.0
Vendor SDL2 2.32 : IGraphic2Module / IAudioModule
Loading...
Searching...
No Matches
SdlText.hpp
Go to the documentation of this file.
1
11#ifndef SDLTEXT_HPP_
12#define SDLTEXT_HPP_
13
14//Sdl
15#include <SDL.h>
16#include <SDL_ttf.h>
17
18//Interface
19#include "IText.hpp"
20
21//encapsulation
22#include "SdlFont.hpp"
23
24#include <string>
25
37class SdlText : public graphic::IText {
38
39 public:
40 SdlText(std::string data, SdlFont &font) : _text(data), _font(&font) {}
41
42 ~SdlText() override { drop(); }
43
44 bool isReady() const override { return _font && _font->isReady(); }
45
46 void setText(const std::string text) override { _text = text; _dirty = true; }
47 std::string getText() const override { return _text; }
48
49 void setFont(graphic::IFont *font) override {
50 _font = static_cast<SdlFont *>(font);
51 _dirty = true;
52 }
53
54 void setFontSize(unsigned int size) override { _size = size; _dirty = true; }
55 unsigned int getFontSize() const override { return _size; }
56
57 void setTextColor(Color color) override { _color = color; _dirty = true; }
58 Color getTextColor() const override { return _color; }
59
60 void setPosition(Vector2f position) override { _position = position; }
61 Vector2f getPosition() const override { return _position; }
62
63 //la rotation passe par le dessin : elle ne change pas la texture
64 void setRotation(float angle) override { _rotation = angle; }
65 float getRotation() const override { return _rotation; }
66
67 friend class SdlWindow;
68
69 private:
71 SDL_Texture *handle(SDL_Renderer *renderer) const {
72 if (!renderer || !_font || !_font->isReady())
73 return nullptr;
74 if (_texture && !_dirty && _owner == renderer)
75 return _texture;
76
77 drop();
78
79 const SDL_Color color{_color.r, _color.g, _color.b, _color.a};
80 /* La variante _Wrapped, et pas la simple : celle-ci rend une seule
81 * ligne et avale les '\n', la ou sfml et raylib les respectent.
82 * Une longueur de zero ne coupe qu'aux retours a la ligne. */
83 SDL_Surface *surface = TTF_RenderUTF8_Blended_Wrapped(_font->handle(_size), _text.c_str(), color, 0);
84
85 if (!surface)
86 return nullptr;
87 _texture = SDL_CreateTextureFromSurface(renderer, surface);
88 _width = surface->w;
89 _height = surface->h;
90 SDL_FreeSurface(surface);
91
92 _owner = renderer;
93 _dirty = false;
94 return _texture;
95 }
96
97 void drop() const {
98 if (_texture)
99 SDL_DestroyTexture(_texture);
100 _texture = nullptr;
101 _owner = nullptr;
102 }
103
104 std::string _text;
105 SdlFont *_font = nullptr;
106 unsigned _size = 24;
107 Color _color{255, 255, 255, 255};
108 Vector2f _position{0, 0};
109 float _rotation = 0.f;
110
111 /* mutable : le cache n'est pas l'etat du texte, seulement sa forme
112 * televersee. handle() est const parce que dessiner ne modifie pas
113 * ce que le texte DIT. */
114 mutable SDL_Texture *_texture = nullptr;
115 mutable SDL_Renderer *_owner = nullptr;
116 mutable bool _dirty = true;
117 mutable int _width = 0;
118 mutable int _height = 0;
119};
120
123#endif /* !SDLTEXT_HPP_ */
Une police ouverte a une taille donnee.
Definition SdlFont.hpp:32
bool isReady() const override
Definition SdlFont.hpp:46
TTF_Font * handle(unsigned size) const
La police, recalee sur size si besoin.
Definition SdlFont.hpp:51
Une chaine rendue en texture, refaite quand elle change.
Definition SdlText.hpp:37
void setRotation(float angle) override
Definition SdlText.hpp:64
void setText(const std::string text) override
Definition SdlText.hpp:46
float getRotation() const override
Definition SdlText.hpp:65
Vector2f getPosition() const override
Definition SdlText.hpp:61
void setFont(graphic::IFont *font) override
Definition SdlText.hpp:49
Color getTextColor() const override
Definition SdlText.hpp:58
SdlText(std::string data, SdlFont &font)
Definition SdlText.hpp:40
void setTextColor(Color color) override
Definition SdlText.hpp:57
~SdlText() override
Definition SdlText.hpp:42
void setPosition(Vector2f position) override
Definition SdlText.hpp:60
std::string getText() const override
Definition SdlText.hpp:47
bool isReady() const override
Definition SdlText.hpp:44
void setFontSize(unsigned int size) override
Definition SdlText.hpp:54
unsigned int getFontSize() const override
Definition SdlText.hpp:55
Une fenetre SDL et son renderer, implemente IWindow2.
Definition SdlWindow.hpp:50