OpenRaider  0.1.4-dev
Open Source Tomb Raider Game Engine implementation
Font.cpp
Go to the documentation of this file.
1 
8 #include <vector>
9 
10 #include "global.h"
11 #include "Log.h"
12 #include "utils/strings.h"
13 #include "system/FontImGui.h"
14 #include "system/FontTRLE.h"
15 #include "system/FontTTF.h"
16 #include "system/Shader.h"
17 #include "system/Font.h"
18 
19 bool Font::isInit = false;
20 std::string Font::fontName;
21 bool Font::showFontBox = false;
22 
26 }
27 
28 int Font::initialize(std::string font) {
29  fontName = font;
30  if (stringEndsWith(fontName, ".pc")) {
32  } else if (stringEndsWith(fontName, ".ttf")) {
34  }
35 
36  if (font != "") {
37  Log::get(LOG_ERROR) << "Unknown font file format: " << font << Log::endl;
38  return -1;
39  } else {
40  return 0;
41  }
42 }
43 
44 unsigned int Font::widthText(float scale, std::string s) {
45  if (stringEndsWith(fontName, ".pc")) {
46  return FontTRLE::widthText(scale, s);
47  } else if (stringEndsWith(fontName, ".ttf")) {
48  return FontTTF::widthText(scale, s);
49  } else {
50  return FontImGui::widthText(scale, s);
51  }
52 }
53 
54 unsigned int Font::heightText(float scale, unsigned int maxWidth, std::string s) {
55  if (stringEndsWith(fontName, ".pc")) {
56  return FontTRLE::heightText(scale, maxWidth, s);
57  } else if (stringEndsWith(fontName, ".ttf")) {
58  return FontTTF::heightText(scale, maxWidth, s);
59  } else {
60  return FontImGui::heightText(scale, maxWidth, s);
61  }
62 }
63 
64 void Font::drawText(unsigned int x, unsigned int y, float scale,
65  glm::vec4 color, std::string s) {
66  if (stringEndsWith(fontName, ".pc")) {
67  FontTRLE::drawText(x, y, scale, color, s);
68  } else if (stringEndsWith(fontName, ".ttf")) {
69  FontTTF::drawText(x, y, scale, color, s);
70  } else {
71  FontImGui::drawText(x, y, scale, color, s);
72  }
73 
74  if (showFontBox) {
75  unsigned int w = widthText(scale, s);
76  unsigned int h = heightText(scale, w + 100, s); // Should always be one line
77  drawFontBox(x, y, w, h);
78  }
79 }
80 
81 void Font::drawTextWrapped(unsigned int x, unsigned int y, float scale,
82  glm::vec4 color, unsigned int maxWidth, std::string s) {
83  if (stringEndsWith(fontName, ".pc")) {
84  FontTRLE::drawTextWrapped(x, y, scale, color, maxWidth, s);
85  } else if (stringEndsWith(fontName, ".ttf")) {
86  FontTTF::drawTextWrapped(x, y, scale, color, maxWidth, s);
87  } else {
88  FontImGui::drawTextWrapped(x, y, scale, color, maxWidth, s);
89  }
90 
91  if (showFontBox) {
92  unsigned int w = widthText(scale, s);
93  if (w > maxWidth)
94  w = maxWidth;
95  unsigned int h = heightText(scale, w, s); // Should always be one line
96  drawFontBox(x, y, w, h);
97  }
98 }
99 
100 void Font::drawTextCentered(unsigned int x, unsigned int y, float scale,
101  glm::vec4 color, unsigned int width, std::string s) {
102  drawText(x + ((width / 2) - (widthText(scale, s) / 2)), y, scale, color, s);
103 }
104 
105 void Font::drawFontBox(unsigned int x, unsigned int y, unsigned int w, unsigned int h) {
106  static ShaderBuffer vert, uv;
107  std::vector<glm::vec2> vertices, uvs;
108 
109  vertices.emplace_back(x, y);
110  vertices.emplace_back(x + w, y);
111  vertices.emplace_back(x + w, y + h);
112  vertices.emplace_back(x, y + h);
113  vertices.emplace_back(x, y);
114 
115  uvs.emplace_back(0.0f, 0.0f);
116  uvs.emplace_back(1.0f, 0.0f);
117  uvs.emplace_back(1.0f, 1.0f);
118  uvs.emplace_back(0.0f, 1.0f);
119  uvs.emplace_back(0.0f, 0.0f);
120 
121  vert.bufferData(vertices);
122  uv.bufferData(uvs);
123 
124  Shader::drawGL(vert, uv, glm::vec4(1.0f, 1.0f, 0.0f, 1.0f), TEXTURE_WHITE,
125  TextureStorage::SYSTEM, GL_LINE_STRIP);
126 }
127 
Default Font Implementation.
void bufferData(int elem, int size, void *data)
Definition: Shader.cpp:18
static void drawTextWrapped(unsigned int x, unsigned int y, float scale, glm::vec4 color, unsigned int maxWidth, std::string s)
Definition: FontImGui.cpp:50
static unsigned int widthText(float scale, std::string s)
Definition: Font.cpp:44
static void drawTextWrapped(unsigned int x, unsigned int y, float scale, glm::vec4 color, unsigned int maxWidth, std::string s)
Definition: Font.cpp:81
String handling utilities.
static void drawText(unsigned int x, unsigned int y, float scale, glm::vec4 color, std::string s)
Definition: FontTRLE.cpp:149
static int initialize(std::string f)
Definition: FontTTF.cpp:100
static int initialize(std::string font)
Definition: FontTRLE.cpp:28
OpenGL Shader Implementation.
#define TEXTURE_WHITE
static void drawTextCentered(unsigned int x, unsigned int y, float scale, glm::vec4 color, unsigned int width, std::string s)
Definition: Font.cpp:100
static unsigned int widthText(float scale, std::string s)
Definition: FontTRLE.cpp:128
static unsigned int heightText(float scale, unsigned int maxWidth, std::string s)
Definition: FontTRLE.cpp:175
static unsigned int heightText(float scale, unsigned int maxWidth, std::string s)
Definition: Font.cpp:54
Included everywhere.
static unsigned int widthText(float scale, std::string s)
Definition: FontTTF.cpp:147
static void drawText(unsigned int x, unsigned int y, float scale, glm::vec4 color, std::string s)
Definition: FontImGui.cpp:25
static LogLevel & get(int level)
Definition: Log.cpp:14
TrueType Font Implementation.
static int initialize(std::string font="")
Definition: Font.cpp:28
Global Logging Utility.
static void shutdown()
Definition: FontTRLE.cpp:25
bool stringEndsWith(std::string s, std::string suffix, bool casesensitive=false)
Definition: strings.cpp:32
static void drawTextWrapped(unsigned int x, unsigned int y, float scale, glm::vec4 color, unsigned int maxWidth, std::string s)
Definition: FontTTF.cpp:182
static unsigned int widthText(float scale, std::string s)
Definition: FontImGui.cpp:17
static unsigned int heightText(float scale, unsigned int maxWidth, std::string s)
Definition: FontTTF.cpp:165
static void drawGL(ShaderBuffer &vertices, ShaderBuffer &uvs, glm::vec4 color, unsigned int texture, TextureStorage store=TextureStorage::SYSTEM, unsigned int mode=GL_TRIANGLES, ShaderTexture *target=nullptr, Shader &shader=textShader)
Definition: Shader.cpp:278
static void shutdown()
Definition: Font.cpp:23
Font Interface.
static const char endl
Definition: Log.h:35
#define LOG_ERROR
Definition: Log.h:19
static void shutdown()
Definition: FontTTF.cpp:138
static std::string fontName
Definition: Font.h:44
static void drawTextWrapped(unsigned int x, unsigned int y, float scale, glm::vec4 color, unsigned int maxWidth, std::string s)
Definition: FontTRLE.cpp:208
static void drawText(unsigned int x, unsigned int y, float scale, glm::vec4 color, std::string s)
Definition: Font.cpp:64
static void drawFontBox(unsigned int x, unsigned int y, unsigned int w, unsigned int h)
Definition: Font.cpp:105
static const float scale
Definition: Sprite.cpp:15
static bool showFontBox
Definition: Font.h:45
static bool isInit
Definition: Font.h:43
static void drawText(unsigned int x, unsigned int y, float scale, glm::vec4 color, std::string s)
Definition: FontTTF.cpp:160
static unsigned int heightText(float scale, unsigned int maxWidth, std::string s)
Definition: FontImGui.cpp:42
Tomb Raider Level Editor Font loader.