BARE2D
Font.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <GL/glew.h>
4 #include <glm/glm.hpp>
5 
6 #include <vector>
7 
8 #define FIRST_PRINTABLE_CHAR ((char)32)
9 #define LAST_PRINTABLE_CHAR ((char)126)
10 #define MAX_TEXTURE_RES 4096
11 
12 namespace BARE2D {
13 
14 /**
15  * @class CharacterGlyph
16  * @brief Represents a render glyph, modified for fonts!
17  */
19 {
20  char character;
21  glm::vec4 uvRect;
22  glm::vec2 size;
23 };
24 
25 /**
26  * @class Font
27  * @brief Essentially just a wrapper for the SDL TTF_Font type.
28  */
29 class Font
30 {
31  public:
32  Font();
33  ~Font();
34 
35  /**
36  * @brief Creates font resources
37  * @param fontFile The file to load the font from. Should be .ttf
38  * @param size The size of the font (in points). This is basically just the quality of the font loaded. Has nothing
39  * to do with rendered size.
40  */
41  void init(const char* fontFile, int size);
42 
43  /**
44  * @brief Destroys font resources
45  */
46  void dispose();
47 
48  unsigned int getHeight() const
49  {
50  return m_height;
51  }
52 
53  /**
54  * @brief Gets a character glyph. Does no checking to make sure it exists!
55  * @param index The index of the glyph
56  * @return A pointer to the glyph.
57  */
58  CharacterGlyph* getGlyph(int& index)
59  {
60  return &(m_characterGlyphs[index]);
61  }
62 
63  /**
64  * @return The GL-provided texture ID
65  */
66  GLuint getTextureID() const
67  {
68  return m_textureID;
69  }
70 
71  /**
72  * @return The number of characters in the font. Generally LAST_PRINTABLE_CHAR - FIRST_PRINTABLE_CHAR
73  */
74  unsigned int getCharacters() const
75  {
76  return m_regLength;
77  }
78 
79  /**
80  * @brief Measures the dimensions of some given text
81  * @param s The text to be measured
82  * @return The dimensions of the text
83  */
84  glm::vec2 measure(const char* s);
85 
86  private:
87  static std::vector<int>* createRows(glm::ivec4* rectangles,
88  int rectanglesLength,
89  int rows,
90  int padding,
91  int& width);
92 
93  // The texture which holds the font graphics
94  GLuint m_textureID = 0;
95 
96  // The height of the font
97  unsigned int m_height;
98 
99  // The start and length values of the possible characters displayed
100  unsigned int m_regStart, m_regLength;
101 
102  // The rendering glyphs (for ease of use in the FontRenderer)
104 };
105 
106 } // namespace BARE2D
BARE2D::Font
Essentially just a wrapper for the SDL TTF_Font type.
Definition: Font.hpp:29
BARE2D
Definition: App.cpp:13
BARE2D::Font::createRows
static std::vector< int > * createRows(glm::ivec4 *rectangles, int rectanglesLength, int rows, int padding, int &width)
Definition: Font.cpp:322
BARE2D::Font::getCharacters
unsigned int getCharacters() const
Definition: Font.hpp:74
BARE2D::Font::~Font
~Font()
Definition: Font.cpp:27
BARE2D::Font::getGlyph
CharacterGlyph * getGlyph(int &index)
Gets a character glyph. Does no checking to make sure it exists!
Definition: Font.hpp:58
BARE2D::CharacterGlyph
Represents a render glyph, modified for fonts!
Definition: Font.hpp:18
BARE2D::CharacterGlyph::size
glm::vec2 size
Definition: Font.hpp:22
BARE2D::Font::m_height
unsigned int m_height
Definition: Font.hpp:97
BARE2D::Font::m_regLength
unsigned int m_regLength
Definition: Font.hpp:100
BARE2D::Font::getTextureID
GLuint getTextureID() const
Definition: Font.hpp:66
BARE2D::Font::measure
glm::vec2 measure(const char *s)
Measures the dimensions of some given text.
Definition: Font.cpp:277
BARE2D::CharacterGlyph::uvRect
glm::vec4 uvRect
Definition: Font.hpp:21
BARE2D::Font::m_textureID
GLuint m_textureID
Definition: Font.hpp:94
BARE2D::Font::Font
Font()
Definition: Font.cpp:24
BARE2D::Font::getHeight
unsigned int getHeight() const
Definition: Font.hpp:48
BARE2D::Font::m_regStart
unsigned int m_regStart
Definition: Font.hpp:100
BARE2D::Font::m_characterGlyphs
CharacterGlyph * m_characterGlyphs
Definition: Font.hpp:103
BARE2D::CharacterGlyph::character
char character
Definition: Font.hpp:20
BARE2D::Font::dispose
void dispose()
Destroys font resources.
Definition: Font.cpp:261
BARE2D::Font::init
void init(const char *fontFile, int size)
Creates font resources.
Definition: Font.cpp:30