CMU462 Library  1.0
15-462/15-662: Computer Graphics (Fall 2015)
osdtext.h
1 #ifndef CMU462_TEXTOSD_H
2 #define CMU462_TEXTOSD_H
3 
4 #include <string>
5 #include <vector>
6 
7 #include "GL/glew.h"
8 
9 #include "color.h"
10 
11 // forward declare freetype stuff
12 struct FT_LibraryRec_;
13 typedef struct FT_LibraryRec_* FT_Library;
14 struct FT_FaceRec_;
15 typedef struct FT_FaceRec_* FT_Face;
16 
17 namespace CMU462 {
18 
19 // base64 encoded embeded font
20 extern "C" char osdfont_base64[];
21 
22 struct OSDLine {
23 
24  // UID of the line
25  int id;
26 
27  // screen space anchor position
28  float x, y;
29 
30  // line content
31  std::string text;
32 
33  // font size
34  size_t size;
35 
36  // font color
37  Color color;
38 
39 };
40 
47 class OSDText {
48  public:
49 
54  OSDText();
55 
60  ~OSDText();
61 
68  int init(bool use_hdpi);
69 
74  void render();
75 
79  void clear();
80 
85  void resize(size_t w, size_t h);
86 
101  int add_line(float x, float y, std::string text = "",
102  size_t size = 16, Color color = Color::White);
103 
109  void del_line(int line_id);
110 
119  void set_anchor(int line_id, float x, float y);
120 
127  void set_text(int line_id, std::string text);
128 
135  void set_size(int line_id, size_t size);
136 
143  void set_color(int line_id, Color color);
144 
145  private:
146 
147  // draw a single line
148  void draw_line(OSDLine line);
149 
150  // HDPI displays
151  bool use_hdpi;
152 
153  // internal scale factors
154  float sx, sy;
155 
156  // line id counter
157  int next_id;
158 
159  // freetype
160  char* font; size_t font_size;
161  FT_Library* ft; FT_Face* face;
162 
163  // lines to draw
164  std::vector<OSDLine> lines;
165 
166  // GL stuff
167  GLuint vbo;
168  GLuint program;
169  GLint attribute_coord;
170  GLint uniform_tex;
171  GLint uniform_color;
172 
173  // GL helpers
174  GLuint compile_shaders();
175  GLint get_attribu(GLuint program, const char *name);
176  GLint get_uniform(GLuint program, const char *name);
177 
178 }; // class textOSD
179 
180 } // namespace CMU462
181 
182 #endif // CMU462_TEXTOSD_H
void set_size(int line_id, size_t size)
Set the font size of a given line.
Definition: osdtext.cpp:164
void resize(size_t w, size_t h)
Resize internal scales when context size has changed.
Definition: osdtext.cpp:101
void set_color(int line_id, Color color)
Set the font color of a given line.
Definition: osdtext.cpp:175
Definition: CMU462.h:8
int init(bool use_hdpi)
Initializes resources required for rendering text.
Definition: osdtext.cpp:43
void set_anchor(int line_id, float x, float y)
Set the anchor position of a given line.
Definition: osdtext.cpp:141
int add_line(float x, float y, std::string text="", size_t size=16, Color color=Color::White)
Add a line of text to the OSD.
Definition: osdtext.cpp:107
void del_line(int line_id)
Deletes a line.
Definition: osdtext.cpp:130
~OSDText()
Destructor.
Definition: osdtext.cpp:32
void clear()
Clear all the lines.
Definition: osdtext.cpp:97
void render()
Draw the text OSD.
Definition: osdtext.cpp:84
void set_text(int line_id, std::string text)
Set the text of a given line.
Definition: osdtext.cpp:153
Provides an interface for text on-screen display.
Definition: osdtext.h:47
OSDText()
Constructor.
Definition: osdtext.cpp:22
Encodes a color via additive red, green, and blue chanel values.
Definition: color.h:15