CMU462 Library Documentation  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 "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 
67  int init();
68 
73  void render();
74 
79  void resize(size_t w, size_t h);
80 
95  int add_line(float x, float y, std::string text = "",
96  size_t size = 16, Color color = Color::White);
97 
103  void del_line(int line_id);
104 
112  void set_anchor(int line_id, float x, float y);
113 
120  void set_text(int line_id, std::string text);
121 
128  void set_size(int line_id, size_t size);
129 
136  void set_color(int line_id, Color color);
137 
138  private:
139 
140  // draw a single line
141  void draw_line(OSDLine line);
142 
143  // internal scale factors
144  float sx, sy;
145 
146  // line id counter
147  int next_id;
148 
149  // freetype
150  char* font; size_t font_size;
151  FT_Library* ft; FT_Face* face;
152 
153  // lines to draw
154  std::vector<OSDLine> lines;
155 
156  // GL stuff
157  GLuint vbo;
158  GLuint program;
159  GLint attribute_coord;
160  GLint uniform_tex;
161  GLint uniform_color;
162 
163  // GL helpers
164  GLuint compile_shaders();
165  GLint get_attribu(GLuint program, const char *name);
166  GLint get_uniform(GLuint program, const char *name);
167 
168 }; // class textOSD
169 
170 } // namespace CMU462
171 
172 #endif // CMU462_TEXTOSD_H
void set_size(int line_id, size_t size)
Set the font size of a given line.
Definition: osdtext.cpp:153
void resize(size_t w, size_t h)
Resize internal scales when context size has changed.
Definition: osdtext.cpp:93
void set_color(int line_id, Color color)
Set the font color of a given line.
Definition: osdtext.cpp:164
Definition: color.cpp:10
void set_anchor(int line_id, float x, float y)
Set the anchor position of a given line.
Definition: osdtext.cpp:130
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:99
void del_line(int line_id)
Deletes a line.
Definition: osdtext.cpp:119
~OSDText()
Destructor.
Definition: osdtext.cpp:30
int init()
Initializes resources required for rendering text.
Definition: osdtext.cpp:41
void render()
Draw the text OSD.
Definition: osdtext.cpp:80
void set_text(int line_id, std::string text)
Set the text of a given line.
Definition: osdtext.cpp:142
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:13