jubilant-funicular
SpriteFont.h
1 #ifndef NTA_SPRITEFONT_H_INCLUDED
2 #define NTA_SPRITEFONT_H_INCLUDED
3 
4 #include <map>
5 
6 #include <SDL2/SDL_ttf.h>
7 
8 #include "nta/SpriteBatch.h"
9 
10 #define FIRST_PRINTABLE_CHAR ((char)32) //space
11 #define LAST_PRINTABLE_CHAR ((char)126) //~
12 #define NUM_PRINTABLE_CHARS (LAST_PRINTABLE_CHAR-FIRST_PRINTABLE_CHAR+1)
13 #define SPRITE_FONT_TAB_SIZE 4
14 
15 namespace nta {
16  class SpriteFont;
17  class ContextData;
19  struct CharGlyph {
21  glm::vec4 uvRect;
23  glm::vec2 size;
24  };
26  class FontMap {
27  private:
29  struct CharRect {
30  CharRect(){}
31  CharRect(glm::vec2 tl, glm::vec2 d, bool a) : topLeft(tl), dimensions(d), addedToMap(a) {
32  }
33  glm::vec2 topLeft;
34  glm::vec2 dimensions;
35  bool addedToMap;
36  };
38  bool isOverlapping(const CharRect& rect) const;
40  CharRect* m_rects = nullptr;
41  public:
43  FontMap();
44  ~FontMap();
46  glm::vec2 getBoundingDimensions() const;
48  void addRect(char c, crvec2 dimensions);
50  void position();
51 
52  friend SpriteFont;
53  };
55  class SpriteFont {
56  private:
60  void init(TTF_Font* font);
62  CharGlyph* m_charGlyphs = nullptr;
64  GLuint m_texId = 0;
66  int m_fontHeight = 0;
67  public:
68  SpriteFont() {}
72  glm::vec2 measure(crstring text) const;
74  int font_height() const { return m_fontHeight; }
76  GLuint getTexture() const { return m_texId; }
78  void drawText(SpriteBatch& batch, crstring text, crvec2 topLeft, crvec2 scale,
79  crvec4 color = glm::vec4(1), float depth = NTA_DEFAULT_DEPTH) const;
80  void drawText(SpriteBatch& batch, crstring text, crvec4 posRect, crvec4 color = glm::vec4(1),
81  float depth = NTA_DEFAULT_DEPTH) const;
82  void drawText(SpriteBatch& batch, crvec2 corner1, crvec2 corner2, crstring text,
83  crvec4 color = glm::vec4(1), float depth = NTA_DEFAULT_DEPTH) const;
85  void drawTexture(SpriteBatch& batch, crvec4 posRect = glm::vec4(-100, 100, 200, 200)) const;
87  void destroy();
88  friend ContextData;
89  };
90 }
91 
92 #endif // NTA_SPRITEFONT_H_INCLUDED
nta::SpriteFont::getTexture
GLuint getTexture() const
Returns the (id of) the texture used for rendering font.
Definition: SpriteFont.h:76
nta::FontMap::CharRect
a rectangle representing the location of a char in the FontMap
Definition: SpriteFont.h:29
nta::SpriteFont::font_height
int font_height() const
returns m_fontHeight
Definition: SpriteFont.h:74
nta::CharGlyph::size
glm::vec2 size
the size of the rendered glyph
Definition: SpriteFont.h:23
nta::SpriteFont::m_texId
GLuint m_texId
the idea of the generated texture
Definition: SpriteFont.h:64
nta::FontMap::position
void position()
positions map so that the topleft is at (0,0)
Definition: FontMap.cpp:64
nta::CharGlyph::uvRect
glm::vec4 uvRect
the rectangle containing this glyph in the texture
Definition: SpriteFont.h:21
nta::ContextData
Definition: ContextData.h:17
nta::FontMap::isOverlapping
bool isOverlapping(const CharRect &rect) const
returns whether or not rect is overlapping with any existing CharRect
Definition: FontMap.cpp:26
nta::SpriteFont::m_charGlyphs
CharGlyph * m_charGlyphs
a collection of glyphs for each char
Definition: SpriteFont.h:62
nta::SpriteBatch
Definition: SpriteBatch.h:87
nta::SpriteFont::destroy
void destroy()
Destroys this SpriteFont.
Definition: SpriteFont.cpp:65
nta::FontMap::addRect
void addRect(char c, crvec2 dimensions)
adds a rectangle and associates it with c (replacing any preexisting rectangle)
Definition: FontMap.cpp:39
nta::FontMap::FontMap
FontMap()
constructor and destructor
Definition: FontMap.cpp:7
nta
Definition: Animation2D.h:6
nta::SpriteFont::m_fontHeight
int m_fontHeight
the height of the font
Definition: SpriteFont.h:66
nta::SpriteFont::measure
glm::vec2 measure(crstring text) const
returns the dimensions of the rectangle containing the text
Definition: SpriteFont.cpp:75
nta::FontMap::m_rects
CharRect * m_rects
the rectangles making up the FontMap
Definition: SpriteFont.h:40
nta::SpriteFont::drawText
void drawText(SpriteBatch &batch, crstring text, crvec2 topLeft, crvec2 scale, crvec4 color=glm::vec4(1), float depth=NTA_DEFAULT_DEPTH) const
renders text with specified location, color, scale, etc.
Definition: SpriteFont.cpp:97
nta::CharGlyph
represents a single char in the texture
Definition: SpriteFont.h:19
nta::SpriteFont::~SpriteFont
~SpriteFont()
destructor
Definition: SpriteFont.h:70
nta::FontMap
represents the organization of a texture containing the characters
Definition: SpriteFont.h:26
nta::SpriteFont::init
void init(TTF_Font *font)
Definition: SpriteFont.cpp:5
nta::SpriteFont
Loads in a .ttf file, creates a font texture from it which is then used to render text.
Definition: SpriteFont.h:55
nta::FontMap::getBoundingDimensions
glm::vec2 getBoundingDimensions() const
returns the dimensions of the rectangle that contains the FontMap
Definition: FontMap.cpp:16
nta::SpriteFont::drawTexture
void drawTexture(SpriteBatch &batch, crvec4 posRect=glm::vec4(-100, 100, 200, 200)) const
renders texture
Definition: SpriteFont.cpp:126