JNR
laFont.cpp
1 /*
2 
3 Jump'n'Run Engine
4 http://www.atanaslaskov.com/jnr/
5 
6 BSD LICENSE
7 Copyright (c) 2007-2013, Atanas Laskov
8 All rights reserved.
9 
10 Redistribution and use in source and binary forms, with or without
11 modification, are permitted provided that the following conditions are met:
12 1. Redistributions of source code must retain the above copyright notice,
13 this list of conditions and the following disclaimer.
14 2. Redistributions in binary form must reproduce the above copyright notice,
15 this list of conditions and the following disclaimer in the documentation
16 and/or other materials provided with the distribution.
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL ATANAS LASKOV BE LIABLE FOR ANY
21 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 */
29 
30 //
31 // FILE: laFont.cpp
32 //
33 // Copyright (C) 2007-2013 Atanas Laskov, <latanas@gmail.com>
34 //
35 #include "stdafx.h"
36 #include "Core.h"
37 
38 char laFont::_str[256] = "";
39 
40 laFont::laFont(void)
41 {
42  _dSz = 1;
43  _nAlignH = M_AL;
44  _nAlignV = M_AM;
45 }
46 
47 laFont::~laFont(void)
48 {
49 }
50 
51 void laFont::discard()
52 {
53 }
54 
55 void laFont::ctlSize(double sz)
56 {
57  _dSz = sz;
58 }
59 
60 //#include <GL\GL.h>
61 
62 void laFont::_draw_string(laRenderer* r, laPoint3 pos, char* strStr)
63 {
64  ERRORLEVEL_BEGIN;
65  double len = lenght(strStr);
66  unsigned nLenght = strlen(strStr);
67 
68  // Adjust for horizontal align
69  //
70  switch(_nAlignH)
71  {
72  case M_AL:
73  break;
74  case M_AR:
75  pos[0] -= len;
76  break;
77  case M_AC:
78  pos[0] -= len/2.0;
79  break;
80  }
81 
82  // Adjust for vertical align
83  //
84  switch(_nAlignV)
85  {
86  case M_AT:
87  break;
88  case M_AB:
89  pos[1] -= _dSz;
90  break;
91  case M_AM:
92  pos[1] -= _dSz/2.0;
93  break;
94  }
95 
96  // Prepare chars
97  //
98  laPoint3 *pquad = r->vquadsData(0, nLenght);
99  laPoint2 uv, *puv = r->vquadsTexCoord(0, nLenght);
100 
101  int index, nvisisble=0;
102  double x_original = pos.x();
103 
104  for(unsigned i=0; i<nLenght; i++)
105  {
106  if( strStr[i] == '#') // use # as new line marker
107  {
108  pos[0] = x_original;
109  pos[1] += _dSz*1.1; // line spacing 1.1
110  }
111  else
112  {
113  pos[0] += _dSz*_dWidthFactor;
114  index = strStr[i] - _charStart;
115 
116  if( (index>=0) /*&& (index<_nCharsPerLine*2)*/)
117  {
118  nvisisble++;
119 
120  *(pquad++) = pos;
121  *(pquad++) = pos + laPoint3(_dSz*_dWidthFactor, 0);
122  *(pquad++) = pos + laPoint3(_dSz*_dWidthFactor, _dSz);
123  *(pquad++) = pos + laPoint3(0, _dSz);
124 
125  uv = laPoint2( ((index%_nCharsPerLine)*_dTexCharW), 1.01 - (index/_nCharsPerLine+1)*_dTexCharH );
126 
127  *(puv++) = uv + laPoint2(0,_dTexCharH);
128  *(puv++) = uv + laPoint2(_dTexCharW,_dTexCharH);
129  *(puv++) = uv + laPoint2(_dTexCharW,0);
130  *(puv++) = uv;
131  }
132  }
133  }
134 
135  // Render chars
136  //
137  _tex.use();
138  r->vquadsDraw(nvisisble);
139 
140  ERRORLEVEL_END;
141 }
142 
143 void laFont::draw(const laPoint3 &pos, char* strFormat, ...)
144 {
145  //PROFILE_REN(laFont_draw);
146 
147  char text[256];
148  va_list ap;
149 
150  if ( !strFormat ) return;
151 
152  va_start(ap, strFormat);
153  vsnprintf(text, 250, strFormat, ap);
154  va_end(ap);
155 
156  text[255] = '\0';
157  _draw_string(::laSystemIntegrator::getRenderer(), pos, text);
158 }
159 
160 void laFont::load(class laFileParser *fp)
161 {
162  char strName[128];
163  char strFileName[128];
164  unsigned nCharW;
165  unsigned nCharH;
166  laRenderer* r = ::laSystemIntegrator::getIntegrator()->getRenderer();
167 
168  //fp.readSectionSeparator();
169 
170  fp->readText(strName);
171  sprintf(strFileName, "%s%s", M_DIR_FONT, strName);
172  _tex.load(strFileName, M_TEX_TMAP);
173 
174  fp->readText(strName);
175  _charStart = strName[0];
176 
177  fp->readUnsigned(&nCharW);
178  fp->readUnsigned(&nCharH);
179  _dTexCharW = 1.0/((double)_tex.w/nCharW);
180  _dTexCharH = 1.0/((double)_tex.h/nCharH);
181  _nCharsPerLine = ((double)_tex.w)/nCharW;
182 
183  _dWidthFactor = (double)nCharW/nCharH;
184 
185  //::laSystemIntegrator::getEnvironment()->message(M_FALSE, "%d %d %f %f %d", nCharW, nCharH, _dTexCharW, _dTexCharH, _nCharsPerLine);
186 
187  fp->readDouble(&_dSz);
188  _dSz *= M_UNIT;
189 }
190 
191 // Obfuscate and de-obfuscate string
192 //
193 char* laFont::obfuscated(char* str)
194 {
195  unsigned n = strlen(str);
196  unsigned char k;
197 
198  ASSERT(n<255, "Can't produce obfuscated string for lenght %d", n);
199 
200  // Apply XOR encription; The key is based on strlen() and different for each char
201  //
202  for(unsigned i=0; i<n; i++)
203  {
204  _str[i] = (str[i]) ^ ( (n*3 - i) % 127 + 1 );
205  //TODO: Sometimes can endup 0 and the string is truncated; fix by tweaking the key
206  }
207  _str[n] = '\0';
208 
209  return _str;
210 }
211 
212 // Quote obfuscated string literal
213 //
214 char* laFont::obfuscated_escape(char* str_in, char* str_out) {
215  unsigned n = strlen(str_in);
216  str_out[0] = '\0';
217 
218  for(unsigned i=0; i<n; i++)
219  {
220  char snum[6];
221  sprintf(snum, "\\x%x", (unsigned)(str_in[i]));
222  strcat(str_out, snum);
223  }
224 
225  return str_out;
226 }
227 
228 // Get built-in obfuscated string
229 // NOTE: It is preferable these few strings are here and not directly in the headers;
230 // Placing the headers would require re-compiling the whole project when they change
231 // and also it would be easier to single out the string values if the headers are redistributed
232 //
233 char* laFont::getSourceStr(unsigned i)
234 {
235  //Copyright (C) 2010 Atanas Laskov, <laskov@sen-works.net>
236  //[Jumping RON] Copyright (C) 2010 Atanas Laskov
237  //[Level Generation and Editing System] Copyright (C) 2010 Atanas Laskov
238 
239  switch(i)
240  {
241  case 0: return laFont::obfuscated("\x69\x46\x58\x5e\x54\x4c\x43\x4b\x56\x1\x8\x5c\x37\x3d\x2e\x2b\x2b\x29\x38\x56\x62\x74\x7a\x72\x61\x31\x5c\x6e\x7d\x66\x63\x7d\x26\x29\x34\x6b\x67\x76\x6f\x6c\x74\x41\xc\x1b\x13\x51\xc\x15\xb\x13\x4\x58\x1b\x11\x7\x4c");
242  case 1: return laFont::obfuscated("\x57\x41\x7f\x64\x78\x6e\x68\x62\x24\x51\x4d\x4f\x22\x5e\x3e\x13\xb\x3\xb\x11\x10\x1e\x1\x54\x5b\x31\x58\x50\x5d\x5e\x5c\x5c\x4b\x2b\x1d\x9\x9\x7\x16\x44\x2f\x3\x12\xb\x30\x28");
243  case 2: return laFont::obfuscated("\xf\x1f\x37\x27\x35\x23\x6e\xa\x29\x25\x2f\x3b\x29\x33\x2f\x2a\x2a\x63\x23\x2f\x24\x1f\x7b\x59\x55\x4f\x53\x57\x5f\x17\x65\x4c\x47\x47\x57\x5c\x6d\xf\x6d\x42\x5c\x52\x58\x40\x4f\x4f\x52\x5\xc\x60\xb\x1\x12\x2f\x2f\x2d\x3c\x5a\x6e\x78\x76\x76\x65\x35\x58\x72\x61\x7a\x7f\x79");
244 
245  default:
246  ASSERT(M_FALSE, "Undeifned String (%d)", i);
247  }
248 }
laPoint2 * vquadsTexCoord(unsigned nQuad, unsigned nCount=1)
Get a pointer to VQ texture coordinates.
Definition: laRenderer.h:284
#define M_AB
Text align bottom.
#define M_UNIT
Unit of 1 meter.
2D Point
Definition: laPoint_novec.h:41
#define M_AM
Text align middle.
#define M_AL
Text align left.
#define M_AT
Text align top.
#define M_AR
Text align right.
virtual void vquadsDraw(unsigned nQuads, laPoint2 *ar_uv=NULL, laColor *ar_color=NULL, M_BOOL bBillboards=M_FALSE, M_BOOL bUseColorArrays=M_FALSE)=0
Draw an array of VQ, starting with the psecified pointers (or the first VQ if null) ...
#define M_AC
Text align center.
Virtual interface for the Engine graphics renderer.
Definition: laRenderer.h:98
laPoint3 * vquadsData(unsigned nQuad, unsigned nCount=1)
Get a pointer to VQ vertex data.
Definition: laRenderer.h:278
#define M_DIR_FONT
Fonts directory.
#define M_TEX_TMAP
Transparent texture with an alpha channel.
File Parser.
Definition: laFileParser.h:41