Menge
Modular Pedestrian Simulation Framework for Research and Development
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
TextWriter.h
Go to the documentation of this file.
1 /*
2 
3 License
4 
5 Menge
6 Copyright and trademark 2012-14 University of North Carolina at Chapel Hill.
7 All rights reserved.
8 
9 Permission to use, copy, modify, and distribute this software and its documentation
10 for educational, research, and non-profit purposes, without fee, and without a
11 written agreement is hereby granted, provided that the above copyright notice,
12 this paragraph, and the following four paragraphs appear in all copies.
13 
14 This software program and documentation are copyrighted by the University of North
15 Carolina at Chapel Hill. The software program and documentation are supplied "as is,"
16 without any accompanying services from the University of North Carolina at Chapel
17 Hill or the authors. The University of North Carolina at Chapel Hill and the
18 authors do not warrant that the operation of the program will be uninterrupted
19 or error-free. The end-user understands that the program was developed for research
20 purposes and is advised not to rely exclusively on the program for any reason.
21 
22 IN NO EVENT SHALL THE UNIVERSITY OF NORTH CAROLINA AT CHAPEL HILL OR THE AUTHORS
23 BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
24 DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
25 DOCUMENTATION, EVEN IF THE UNIVERSITY OF NORTH CAROLINA AT CHAPEL HILL OR THE
26 AUTHORS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 THE UNIVERSITY OF NORTH CAROLINA AT CHAPEL HILL AND THE AUTHORS SPECIFICALLY
29 DISCLAIM ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
30 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE AND ANY STATUTORY WARRANTY
31 OF NON-INFRINGEMENT. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND
32 THE UNIVERSITY OF NORTH CAROLINA AT CHAPEL HILL AND THE AUTHORS HAVE NO OBLIGATIONS
33 TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
34 
35 Any questions or comments should be sent to the authors {menge,geom}@cs.unc.edu
36 
37 */
38 
44 #ifndef __TEXT_WRITER_H__
45 #define __TEXT_WRITER_H__
46 
47 #include "CoreConfig.h"
48 #include "image.h"
49 #include <string>
50 #include "SDL/SDL_ttf.h"
51 #include <map>
52 
53 namespace Menge {
54 
55  namespace SceneGraph {
59  typedef std::map< int, TTF_Font * > FontMap;
60 
64  typedef FontMap::iterator FontMapItr;
65 
69  typedef FontMap::const_iterator FontMapCItr;
70 
77  class MENGE_API TextWriter {
81  static std::string DEFAULT_FONT;
82 
86  static const int DEFAULT_SIZE;
87  public:
93  static void setDefaultFont( const std::string & fontName );
94 
98  enum Alignment {
99  NO_ALIGN = 0x0, // 0
100  VCENTER = 0x1, // 1
101  TOP = 0x2, // 2
102  BOTTOM = 0x4, // 4
103  LEFT = 0x8, // 8
104  LEFT_CENTER = 0x9, // 9
105  LEFT_TOP = 0xA, // 10
106  LEFT_BOTTOM = 0xC, // 12
107  RIGHT = 0x10, // 16
108  RIGHT_CENTER = 0x11, // 17
109  RIGHT_TOP = 0x12, // 18
110  RIGHT_BOTTOM = 0x14, // 20
111  HCENTER = 0x20, // 32
112  CENTERED = 0x21, // 33
113  CENTER_TOP = 0x22, // 34
114  CENTER_BOTTOM = 0x24 // 36
115  };
116 
120  static TextWriter * Instance();
121 
126  void newGLContext();
127 
141  void printText( const std::string & text, Alignment alignment, float anchorX, float anchorY, int fontSize, bool currColor=false, bool transparent=true );
142 
155  void printText( const std::string & text, float left, float bottom, int fontSize, bool currColor=false, bool transparent=true );
156 
172  void printAlignText( const std::string & text, Alignment alignment, int fontSize, float hpad, float vpad, bool currColor=false, bool transparent=true );
173 
189  void printAnchorText( const std::string & text, float anchorX, float anchorY, float xWeight, float yWeight, int fontSize, bool currColor=false, bool transparent=true );
190 
200  void textSize( const std::string & text, int fontSize, float & textWidth, float & textHeight );
201 
211  void resize( int w, int h );
212 
218  inline int getViewWidth() const { return _width; }
219 
225  inline int getViewHeight() const { return _height; }
226 
237  bool setFont( const std::string & fontName );
238 
248  void setDefaultColor( float r, float g, float b, float alpha=1.f );
249 
259  inline int flipY( int y ) { return _height - y; }
260 
261  // TODO: Support multiple families of fonts
262 
263  protected:
271  TTF_Font * getFont( int size );
272 
276  void closeFonts();
277 
281  TextWriter();
282 
286  ~TextWriter();
287 
292 
296  int _width;
297 
301  int _height;
302 
306  void makeTextDList();
307 
311  void makeGLTexture();
312 
318  void sdlSurfaceToGLTex( SDL_Surface * surf );
319 
333  void printLine( TTF_Font * font, const char * text, float left, float bottom, int lineNum );
334 
338  FontMap _fonts;
339 
343  std::string _fontName;
344 
348  GLuint _textID;
349 
353  GLuint _textQuadID;
354 
358  float _defColor[4];
359 
360  };
361  } // namespace SceneGraph
362 } // namespace Menge
363 #endif //__TEXT_WRITER_H__
GLuint _textID
Texture object for texturing the rendered text.
Definition: TextWriter.h:348
std::string _fontName
Name of font.
Definition: TextWriter.h:343
int _height
The height of the viewport the TextWriter considers when computing text positions.
Definition: TextWriter.h:301
The core namespace. All elements of Menge are contained in this namespace.
Definition: AgentGenerator.cpp:43
Sets up the proper compiler directives for platform and dll export/import.
Alignment
Enumeration for controlling text alignment.
Definition: TextWriter.h:98
int getViewHeight() const
Reports TextWriter's belief of the viewport width.
Definition: TextWriter.h:225
int getViewWidth() const
Reports TextWriter's belief of the viewport width.
Definition: TextWriter.h:218
The interface for loading and using images in the scene graph.
int flipY(int y)
A utility function to map from screen to image coordinates.
Definition: TextWriter.h:259
The SceneGraph (SceneGraph) name space, containing all elements to use in a SceneGraph.
static TextWriter * _instance
The singleton pointer.
Definition: TextWriter.h:291
FontMap _fonts
Map of all font sizes for this font face.
Definition: TextWriter.h:338
int _width
The width of the viewport the TextWriter considers when computing text positions. ...
Definition: TextWriter.h:296
GLuint _textQuadID
Call list for the text surface.
Definition: TextWriter.h:353
A singleton class responsible for writing text to an OpenGL context.
Definition: TextWriter.h:77