BARE2D
FontRenderer.cpp
Go to the documentation of this file.
1 #include "FontRenderer.hpp"
2 
3 namespace BARE2D {
4 
5 FontRenderer::FontRenderer(std::string& fragShader,
6  std::string& vertShader,
7  unsigned int perspectiveWidth,
8  unsigned int perspectiveHeight)
9  : BasicRenderer(fragShader, vertShader, perspectiveWidth, perspectiveHeight)
10 {}
11 
13 {}
14 
16  glm::vec2 scaling,
17  glm::vec4 destRect,
18  const char* text,
19  float depth,
20  Colour tint,
21  Justification just /**= Justification::LEFT*/,
22  glm::vec4 uvRect /**= glm::vec4(0.0f, 0.0f, 1.0f, 1.0f)*/)
23 {
24  // Apply the justification setting
25  glm::vec2 newPosition = destRect;
26  if(just == Justification::MIDDLE)
27  {
28  newPosition -= font.measure(text).x * scaling.x / 2.0f;
29  } else if(just == Justification::RIGHT)
30  {
31  newPosition += font.measure(text).x * scaling.x;
32  }
33 
34  // Map out the individual glyphs and add em to the renderer.
35  for(int characterIndex = 0; text[characterIndex] != 0; characterIndex++)
36  {
37  char c = text[characterIndex];
38  // First, check on a character-by-character basis where this character should be
39  if(c == '\n')
40  {
41  // New line, set the position one line down
42  newPosition.y += font.getHeight() * scaling.y;
43  // We should also set back to the first space.
44  newPosition.x = destRect.x;
45  } else
46  {
47  // Check to make sure we have the glyph in the font
48  int glyphIndex = c - (int)FIRST_PRINTABLE_CHAR;
49  if(glyphIndex < 0 || (unsigned int)glyphIndex > font.getCharacters())
50  {
51  // We don't support this character, set the index to the "unsupported" glyph
52  glyphIndex = font.getCharacters();
53  }
54 
55  // Get the actual glyph for data!
56  CharacterGlyph* glyph = font.getGlyph(glyphIndex);
57 
58  // initialize the glyph's placement
59  glm::vec4 updatedDestRect(newPosition, glyph->size * scaling);
60  glm::vec4 uvRectFinal(glyph->uvRect.x + uvRect.x * glyph->uvRect.z,
61  glyph->uvRect.y + uvRect.y * glyph->uvRect.w, glyph->uvRect.z * uvRect.z,
62  glyph->uvRect.w * uvRect.w);
63 
64  // Actually draw the texture!
65  BasicRenderer::draw(updatedDestRect, uvRectFinal, font.getTextureID(), depth);
66 
67  // Move the 'cursor' over to the next character slot
68  newPosition.x += glyph->size.x * scaling.x;
69  }
70  }
71 }
72 
73 } // namespace BARE2D
BARE2D::Font
Essentially just a wrapper for the SDL TTF_Font type.
Definition: Font.hpp:29
FIRST_PRINTABLE_CHAR
#define FIRST_PRINTABLE_CHAR
Definition: Font.hpp:8
BARE2D
Definition: App.cpp:13
BARE2D::FontRenderer::draw
virtual void draw(Font &font, glm::vec2 scaling, glm::vec4 destRect, const char *text, float depth, Colour tint, Justification just=Justification::LEFT, glm::vec4 uvRect=glm::vec4(0.0f, 0.0f, 1.0f, 1.0f))
Definition: FontRenderer.cpp:15
FontRenderer.hpp
BARE2D::Font::getCharacters
unsigned int getCharacters() const
Definition: Font.hpp:74
BARE2D::FontRenderer::~FontRenderer
~FontRenderer()
Definition: FontRenderer.cpp:12
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
Justification
An enum for text justification.
BARE2D::FontRenderer::FontRenderer
FontRenderer(std::string &fragShader, std::string &vertShader, unsigned int perspectiveWidth=2, unsigned int perspectiveHeight=2)
Definition: FontRenderer.cpp:5
BARE2D::Font::getTextureID
GLuint getTextureID() const
Definition: Font.hpp:66
BARE2D::BasicRenderer
Definition: BasicRenderer.hpp:18
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::Justification::RIGHT
@ RIGHT
BARE2D::Font::getHeight
unsigned int getHeight() const
Definition: Font.hpp:48
BARE2D::Colour
An RGBA 8-bit colour value.
Definition: Vertex.hpp:20
BARE2D::Justification::MIDDLE
@ MIDDLE
BARE2D::BasicRenderer::draw
virtual void draw(glm::vec4 destRect, glm::vec4 uvRect, GLuint texture, float depth, Colour colour=Colour(255, 255, 255, 255), float angle=0.0f, glm::vec2 COR=glm::vec2(0.5f))
Definition: BasicRenderer.cpp:56