Angel 3.2
A 2D Game Prototyping Engine
TextRendering.cpp
1 
2 // Copyright (C) 2008-2013, Shane Liesegang
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of the copyright holder nor the names of any
14 // contributors may be used to endorse or promote products derived from
15 // this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 // POSSIBILITY OF SUCH DAMAGE.
29 
30 #include "stdafx.h"
31 #include "../Infrastructure/TextRendering.h"
32 
33 #include "../Infrastructure/Camera.h"
34 #include "../Infrastructure/VecStructs.h"
35 #include "../Infrastructure/Log.h"
36 #include "../Infrastructure/World.h"
37 
38 #if ANGEL_MOBILE
39  #include <ftgl/ftgles.h>
40 #else
41  #include "FTFont.h"
42  #include "FTGLTextureFont.h"
43 #endif
44 
45 
46 std::map<String, FTFont*> _fontCache;
47 
48 const bool RegisterFont(const String& filename, int pointSize, const String& nickname)
49 {
50  std::map<String,FTFont*>::iterator it = _fontCache.find(nickname);
51  if(it != _fontCache.end())
52  {
53  UnRegisterFont(nickname);
54  }
55 
56  if (theWorld.IsHighResScreen())
57  {
58  pointSize = pointSize * 2;
59  }
60 
61  FTFont *font = new FTGLTextureFont(filename.c_str());
62  if(font->Error())
63  {
64  sysLog.Log("Failed to open font " + filename);
65  return false;
66  }
67  if(!font->FaceSize(pointSize))
68  {
69  sysLog.Log("Failed to set size.");
70  return false;
71  }
72  font->CharMap(FT_ENCODING_NONE);
73  font->UseDisplayList(true);
74 
75  _fontCache[nickname] = font;
76  return true;
77 }
78 
79 const bool IsFontRegistered(const String& nickname)
80 {
81  std::map<String,FTFont*>::iterator it = _fontCache.find(nickname);
82  if (it != _fontCache.end())
83  {
84  return true;
85  }
86  else
87  {
88  return false;
89  }
90 }
91 
92 const bool UnRegisterFont(const String& nickname)
93 {
94  std::map<String,FTFont*>::iterator it = _fontCache.find(nickname);
95  if (it == _fontCache.end())
96  {
97  sysLog.Log("No font called \"" + nickname + "\"; un-registration failed.");
98  return false;
99  }
100  delete it->second;
101  it->second = NULL;
102  _fontCache.erase(it);
103  return true;
104 }
105 
106 Vector2 DrawGameText(const String& text, const String& nickname, int pixelX, int pixelY, float angle)
107 {
108  Vector2 forReturn;
109 
110  std::map<String,FTFont*>::iterator it = _fontCache.find(nickname);
111  if (it == _fontCache.end())
112  {
113  return forReturn;
114  }
115 
116  Vec2i winDimensions;
117  winDimensions.X = theCamera.GetWindowWidth();
118  winDimensions.Y = theCamera.GetWindowHeight();
119  pixelY = winDimensions.Y - pixelY;
120 
121  //set up projection
122  glMatrixMode(GL_PROJECTION);
123  glPushMatrix();
124  glLoadIdentity();
125  gluOrtho2D(0.0f, winDimensions.X, 0.0f, winDimensions.Y);
126 
127  //set up modelview
128  glMatrixMode(GL_MODELVIEW);
129  glPushMatrix();
130  glLoadIdentity();
131  glTranslatef((GLfloat)pixelX, (GLfloat)pixelY, 0.0f);
132  glRotatef(angle, 0.0f, 0.0f, 1.0f);
133 
134  glEnable(GL_TEXTURE_2D);
135  glDisable(GL_DEPTH_TEST);
136  it->second->Render(text.c_str());
137  glDisable(GL_TEXTURE_2D);
138  glEnable(GL_DEPTH_TEST);
139 
140  glPopMatrix();
141  glMatrixMode(GL_PROJECTION);
142  glPopMatrix();
143  glMatrixMode(GL_MODELVIEW);
144 
145  float llx, lly, llz, urx, ury, urz;
146  it->second->BBox(text.c_str(), llx, lly, llz, urx, ury, urz);
147  forReturn.X = urx - llx;
148  forReturn.Y = ury - lly;
149  return forReturn;
150 }
151 
152 Vector2 DrawGameTextRaw(const String& text, const String& nickname, int pixelX, int pixelY, float angle)
153 {
154  Vector2 forReturn;
155 
156  std::map<String,FTFont*>::iterator it = _fontCache.find(nickname);
157  if (it == _fontCache.end())
158  {
159  return forReturn;
160  }
161 
162  pixelY = theCamera.GetWindowHeight() - pixelY;
163 
164  //set up modelview
165  glMatrixMode(GL_MODELVIEW);
166  glPushMatrix();
167  glLoadIdentity();
168  glTranslatef((GLfloat)pixelX, (GLfloat)pixelY, 0.0f);
169  glRotatef(angle, 0.0f, 0.0f, 1.0f);
170 
171  glEnable(GL_TEXTURE_2D);
172  glDisable(GL_DEPTH_TEST);
173  it->second->Render(text.c_str());
174  glDisable(GL_TEXTURE_2D);
175  glEnable(GL_DEPTH_TEST);
176 
177  glPopMatrix();
178 
179  float llx, lly, llz, urx, ury, urz;
180  it->second->BBox(text.c_str(), llx, lly, llz, urx, ury, urz);
181  forReturn.X = urx - llx;
182  forReturn.Y = ury - lly;
183  return forReturn;
184 }
185 
186 Vector2 GetTextExtents(const String& text, const String& nickname)
187 {
188  Vector2 forReturn;
189 
190  std::map<String,FTFont*>::iterator it = _fontCache.find(nickname);
191  if (it == _fontCache.end())
192  {
193  return forReturn;
194  }
195 
196  float llx, lly, llz, urx, ury, urz;
197  it->second->BBox(text.c_str(), llx, lly, llz, urx, ury, urz);
198  forReturn.X = urx - llx;
199  forReturn.Y = ury - lly;
200 
201  return forReturn;
202 }
203 
204 float GetTextAscenderHeight(const String& nickname)
205 {
206  std::map<String,FTFont*>::iterator it = _fontCache.find(nickname);
207  if (it == _fontCache.end())
208  {
209  return 0.0f;
210  }
211 
212  return it->second->Ascender();
213 }