Labyrinth
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
font.hpp
1 //////////////////////////////
2 //
3 // File: lxs/coregraphics/font.hpp
4 // Author: Maciej Kaminski
5 // E-mail: maciej.a.kaminski@gmail.com
6 // Website: maciejkaminski.pl
7 //
8 // File creation date: Nov 27 2012
9 // This file is part of Project Labyrinth
10 // This file is going to be released under terms of GNU GPL in near future
11 //
12 //////////////////////////////
13 
14 #ifndef __LXS_FONT__
15 #define __LXS_FONT__
16 #include "helper/opengl.h"
17 #include<vector>
18 #include<string>
19 #include<map>
20 #include<boost/shared_ptr.hpp>
21 namespace LxS
22 {
23  namespace CoreGraphics
24  {
25  class Renderable;
26  //! font - a piece of text actually (printed using particular font typeface)
27  class Font : public Renderable
28  {
29 
30  boost::shared_ptr<Shader> p_ShaderForReal;
31  //! squares in form:
32  //! 3 coords for pos, 2 for tex coord, 3 for color, enuff;
33  std::vector<GLfloat> vertices;
34  //!indices to squares as triangles (for rendering in GLES using vertex list)
35  std::vector<GLshort> indices;
36  //! structure representing character position - xywh - on a texture
37  struct c4
38  {
39  float x;
40  float y;
41  float w;
42  float h;
43  };
44  //! map where characters lie in texture
45  std::map<int,c4> fontmap;
46 
47  float cursor_x;
48  float cursor_y;
49 
50  float translation_x;
51  float translation_y;
52 
53  float r,g,b; // font color
54  float letter_height;
55 
56  float right_edge;
57  float left_edge;
58  float top_edge;
59  float bottom_edge;
60 
61  //! string - path to font-describing file
62  std::string font_face;
63  public:
64  Font();
65  //! set path to font-describing file
66  //! all glyphs are rendered with the same font;
67  //! text must be cleared before changing font;
68  //! font name can be (and if you surely want undefault name, should be)
69  //! set before initing
70  void SetFontFace(const std::string & _nface);
71  void Animate(float dt){}
72  //! set up a font face; if it is a first instance of Font class using
73  //! particular typeface, it will be loaded from disk; otherwise it will
74  //! be reused
75  void Initialize(); // load and set up some resources
76  //! print particular text using a bitmap font (append to what was already
77  //! written)
78  void Print(const char * text );
79  //! erase n last characters
80  //! \param n amount of characters to be erased
81  void EraseFromEnd(int n);
82  //! set font color
83  void SetFontColor(float _r, float _g, float _b) {r=_r;g=_g;b=_b;}
84  //! render a font
85  void Render(Rendermode mode);
86  //! free resources (textures etc if unused)
87  void UnloadResources();
88  //! centimeters from bottom-left - set position at which new text
89  //! will be appended
90  void SetXY(float x, float y)
91  {
92  cursor_y=y-translation_y;
93  cursor_x=x-translation_x;
94  }
95  //! move ALL the contents of this object. This is constant-time function
96  //! regardless of how many characters are there; good for GPU-assisted
97  //! scrolling of heug text
98  void TranslateXY(float x, float y)
99  {
100  translation_x+=x;
101  translation_y+=y;
102  }
103  //! set text height in centimeters per line;
104  void SetHeight(float h) { letter_height=h;}
105  //! get height of a next character
106  float GetHeight() { return letter_height;}
107  //! erase all printed characters
108  void Clear();
109  //! texture size ofc.
110  unsigned long GetTexelsCount();
111  //! number of characters
112  unsigned long GetFacesCount() { return indices.size()/3;}
113  //! rightmst X coordinate of text
114  float GetRightEdge();
115  //! leftmost X coordinate of text
116  float GetLeftEdge();
117  //! topmost Y coordinate of text
118  float GetTopEdge();
119  //! downmost Y coordinate of text
120  float GetBottomEdge();
121  };
122  }
123 }
124 
125 #endif
126 
127