GameKit  0.0.1a
C++ gamedev tools
Text.cpp
Go to the documentation of this file.
1 /*
2  * =====================================================================================
3  *
4  * Filename: Text.cpp
5  *
6  * Description:
7  *
8  * Created: 30/12/2018 19:05:40
9  *
10  * Author: Quentin Bazin, <quent42340@gmail.com>
11  *
12  * =====================================================================================
13  */
14 #include <algorithm>
15 
16 #include "gk/graphics/Text.hpp"
18 #include "gk/core/Debug.hpp"
19 
20 namespace gk {
21 
22 Text::Text(const std::string &fontResourceName, int ptsize) {
23  m_characterSize = ptsize;
24 
25  setFont(fontResourceName);
26 }
27 
28 Text::Text(const std::string &text, const std::string &fontResourceName, int ptsize) {
29  m_text = text;
30  m_characterSize = ptsize;
31 
32  setFont(fontResourceName);
33 }
34 
35 Text::Text(const std::string &text, const Font &font, int ptsize) {
36  m_text = text;
37  m_font = &font;
38  m_characterSize = ptsize;
39 
40  m_isUpdateNeeded = true;
41 }
42 
44  if (m_isUpdateNeeded) update();
45 
46  return {{(int)getPosition().x, (int)getPosition().y}, {m_image.width(), m_image.height()}};
47 }
48 
49 void Text::setFont(const std::string &resourceName) {
50  setFont(gk::ResourceHandler::getInstance().get<gk::Font>(resourceName));
51 }
52 
53 void Text::update() const {
54  m_isUpdateNeeded = false;
55 
56  if (!m_font || m_text.empty() || m_characterSize < 0) return;
57 
58  // FIXME: Add a conversion function between gk::Color and SDL_Color
59  SDL_Color color;
60  color.r = m_color.r * 255.0f;
61  color.g = m_color.g * 255.0f;
62  color.b = m_color.b * 255.0f;
63  color.a = m_color.a * 255.0f;
64 
65  TTF_Font *font = m_font->getFont(m_characterSize);
66  TTF_SetFontStyle(font, m_style);
67 
68  SDL_Surface* surface = nullptr;
69  if (m_isWrapped)
70  surface = TTF_RenderUTF8_Blended_Wrapped(font, m_text.c_str(), color, m_size.x);
71  else
72  surface = TTF_RenderUTF8_Blended(font, m_text.c_str(), color);
73 
74  if (surface) {
75  m_texture.loadFromSurface(surface);
76  SDL_FreeSurface(surface);
77 
79 
80  if (m_isCentered)
82  m_size.x / 2 - m_texture.width() / 2,
83  m_size.y / 2 - m_texture.height() / 2
84  );
85  else
86  m_image.setPosition(0, 0);
87 
88  if (m_isScaled) {
89  int width = std::min((int)m_texture.width(), m_size.x);
90  int height = std::min((int)m_texture.height(), m_size.y);
91  m_image.setPosRect(0, 0, width, height);
92  }
93  else
95  }
96  else
97  DEBUG("Unable to create text image for '" + m_text + "':", TTF_GetError());
98 
99  // FIXME: Save old style instead of restoring Normal
100  TTF_SetFontStyle(font, Style::Normal);
101 }
102 
103 void Text::draw(RenderTarget &target, RenderStates states) const {
104  if (m_isUpdateNeeded)
105  update();
106 
107  states.transform *= getTransform();
108  states.texture = &m_texture;
109  target.draw(m_image, states);
110 }
111 
112 } // namespace gk
113 
const std::string & text() const
Definition: Text.hpp:39
void load(const Image &image)
Definition: Image.cpp:33
Texture m_texture
Definition: Text.hpp:71
static ResourceHandler & getInstance()
bool m_isCentered
Definition: Text.hpp:77
#define DEBUG(args...)
Definition: Debug.hpp:31
void draw(RenderTarget &target, RenderStates states) const override
Draw the object to a render target.
Definition: Text.cpp:103
bool m_isWrapped
Definition: Text.hpp:79
const Texture * texture
u16 height() const
Definition: Image.hpp:42
const Vector3f & getPosition() const
const Font * m_font
Definition: Text.hpp:64
Image m_image
Definition: Text.hpp:70
void loadFromSurface(SDL_Surface *surface)
Load the texture from a SDL_Surface.
Definition: Texture.cpp:67
void setPosition(float x, float y, float z=0)
float g
Green component.
Definition: Color.hpp:112
void draw(const IDrawable &drawable, const RenderStates &states=RenderStates::Default)
Transform transform
float a
Alpha (opacity) component.
Definition: Color.hpp:114
Style m_style
Definition: Text.hpp:66
IntRect getLocalBounds()
Definition: Text.cpp:43
void update() const
Definition: Text.cpp:53
const Transform & getTransform() const
gk::Vector2i m_size
Definition: Text.hpp:76
float b
Blue component.
Definition: Color.hpp:113
float r
Red component.
Definition: Color.hpp:111
u16 height() const
Return the height of the texture.
Definition: Texture.hpp:124
int m_characterSize
Definition: Text.hpp:74
Text()=default
TTF_Font * getFont(int ptsize) const
Get the SDL font for a specific font size.
Definition: Font.cpp:34
u16 width() const
Return the width of the texture.
Definition: Texture.hpp:116
void setFont(const Font &font)
Definition: Text.hpp:36
bool m_isScaled
Definition: Text.hpp:78
Class for loading and manipulating character fonts.
Definition: Font.hpp:30
std::string m_text
Definition: Text.hpp:73
bool m_isUpdateNeeded
Definition: Text.hpp:69
Color m_color
Definition: Text.hpp:67
u16 width() const
Definition: Image.hpp:41
void setPosRect(float x, float y, u16 width, u16 height)
Definition: Image.cpp:70