osdtext.h
#ifndef CMU462_TEXTOSD_H
#define CMU462_TEXTOSD_H
#include <string>
#include <vector>
#include "GL/glew.h"
#include "color.h"
// forward declare freetype stuff
struct FT_LibraryRec_;
typedef struct FT_LibraryRec_* FT_Library;
struct FT_FaceRec_;
typedef struct FT_FaceRec_* FT_Face;
namespace CMU462 {
// base64 encoded embeded font
extern "C" char osdfont_base64[];
struct OSDLine {
// UID of the line
int id;
// screen space anchor position
float x, y;
// line content
std::string text;
// font size
size_t size;
// font color
Color color;
};
class OSDText {
public:
OSDText();
~OSDText();
int init(bool use_hdpi);
void render();
void clear();
void resize(size_t w, size_t h);
int add_line(float x, float y, std::string text = "",
size_t size = 16, Color color = Color::White);
void del_line(int line_id);
void set_anchor(int line_id, float x, float y);
void set_text(int line_id, std::string text);
void set_size(int line_id, size_t size);
void set_color(int line_id, Color color);
private:
// draw a single line
void draw_line(OSDLine line);
// HDPI displays
bool use_hdpi;
// internal scale factors
float sx, sy;
// line id counter
int next_id;
// freetype
char* font; size_t font_size;
FT_Library* ft; FT_Face* face;
// lines to draw
std::vector<OSDLine> lines;
// GL stuff
GLuint vbo;
GLuint program;
GLint attribute_coord;
GLint uniform_tex;
GLint uniform_color;
// GL helpers
GLuint compile_shaders();
GLint get_attribu(GLuint program, const char *name);
GLint get_uniform(GLuint program, const char *name);
}; // class textOSD
} // namespace CMU462
#endif // CMU462_TEXTOSD_H
CMU462::OSDText::set_size
void set_size(int line_id, size_t size)
Set the font size of a given line.
Definition: osdtext.cpp:164
CMU462::OSDText::resize
void resize(size_t w, size_t h)
Resize internal scales when context size has changed.
Definition: osdtext.cpp:101
CMU462::OSDText::set_color
void set_color(int line_id, Color color)
Set the font color of a given line.
Definition: osdtext.cpp:175
CMU462
Definition: CMU462.h:8
CMU462::OSDText::init
int init(bool use_hdpi)
Initializes resources required for rendering text.
Definition: osdtext.cpp:43
CMU462::OSDText::set_anchor
void set_anchor(int line_id, float x, float y)
Set the anchor position of a given line.
Definition: osdtext.cpp:141
CMU462::OSDText::add_line
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
CMU462::OSDText::del_line
void del_line(int line_id)
Deletes a line.
Definition: osdtext.cpp:130
CMU462::OSDText::~OSDText
~OSDText()
Destructor.
Definition: osdtext.cpp:32
CMU462::OSDText::clear
void clear()
Clear all the lines.
Definition: osdtext.cpp:97
CMU462::OSDText::render
void render()
Draw the text OSD.
Definition: osdtext.cpp:84
CMU462::OSDText::set_text
void set_text(int line_id, std::string text)
Set the text of a given line.
Definition: osdtext.cpp:153
CMU462::OSDText
Provides an interface for text on-screen display.
Definition: osdtext.h:47
CMU462::OSDText::OSDText
OSDText()
Constructor.
Definition: osdtext.cpp:22
CMU462::Color
Encodes a color via additive red, green, and blue chanel values.
Definition: color.h:15