HOG2
TextBox.cpp
Go to the documentation of this file.
1 /*
2  * $Id: TextBox.cpp
3  * hog2
4  *
5  * Created by Nathan Sturtevant on 04/12/04.
6  * Modified by Nathan Sturtevant on 02/29/20.
7  *
8  * This file is part of HOG2. See https://github.com/nathansttt/hog2 for licensing information.
9  *
10  */
11 
12 #include <string.h>
13 #include "TextBox.h"
14 
15 TextBox::TextBox(char *_text, int _charLine, point3d topLeft, point3d bottomRight, double _duration, bool _deform, bool _scrolling)
16 {
17  tl = topLeft;
18  br = bottomRight;
19  text = new char[strlen(_text)+1];
20  strcpy(text, _text);
21  charLine = _charLine;
22  dList = 0;
23  elapsedTime = 0;
24  duration = _duration;
25  deform = _deform;
26  scrolling = _scrolling;
27 }
28 
30 {
31  delete [] text;
32  if (dList)
33  glDeleteLists(dList, 1);
34 }
35 
37 {
39 }
40 
41 void TextBox::stepTime(double amount)
42 {
43  elapsedTime += amount;
44 }
45 
47 {
48  glLineWidth(1);
49  double wide, charWidth;
50  wide = br.x-tl.x;
51  //high = tl.y-br.y;
52  charWidth = wide/charLine;
53  GLint matrixMode;
54  if (scrolling)
55  glGetIntegerv(GL_MATRIX_MODE, &matrixMode);
56 
57  glEnable(GL_BLEND); // for text fading
58  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // ditto
59 
60  if (dList == 0)
61  {
62  // set orthograhic 1:1 pixel transform in local view coords
63  glGetIntegerv(GL_MATRIX_MODE, &matrixMode);
64 
65  dList = glGenLists(1);
66  glNewList(dList, GL_COMPILE);
67 
68  glDisable(GL_DEPTH_TEST);
69  if ((deform) && (!scrolling))
70  {
71  glMatrixMode(GL_PROJECTION);
72  glPushMatrix();
73  glLoadIdentity();
74  glMatrixMode(GL_MODELVIEW);
75  glPushMatrix();
76  glLoadIdentity();
77  glColor3f(myColor.r, myColor.g, myColor.b);
78  }
79  glBegin(GL_LINES);
80  //printf("drawing text \"%s\"\n", text);
81  for (unsigned int x = 0; x < strlen(text); x++)
82  {
83  point3d np = tl;
84  np.x+=(x%charLine)*charWidth+charWidth/7.5;
85  np.z-=(x/charLine)*charWidth*2+charWidth/7.5;
86  drawChar(text[x], np, 2*(charWidth-charWidth/(7.5/2)));
87  //printf("%c/%d ", text[x], (int)text[x]);
88  }
89  //printf("\n");
90  glEnd();
91 
92  // reset orginal martices
93  if ((deform) && (!scrolling))
94  {
95  glPopMatrix(); // GL_MODELVIEW
96  glMatrixMode(GL_PROJECTION);
97  glPopMatrix();
98  glMatrixMode(matrixMode);
99  }
100  glEnable(GL_DEPTH_TEST);
101  glEndList();
102 
103  //glReportError();
104  }
105  if (dList)
106  {
107  if (scrolling)
108  {
109  if (deform)
110  {
111  glMatrixMode(GL_PROJECTION);
112  glPushMatrix();
113  glLoadIdentity();
114  glMatrixMode(GL_MODELVIEW);
115  glPushMatrix();
116  glLoadIdentity();
117  }
118  glColor3f(myColor.r, myColor.g, myColor.b);
119  glTranslated(0, elapsedTime/30.0, 0);
120  glCallList(dList);
121  glColor4f(myColor.r, myColor.g, myColor.b, .5);
122  glTranslated(0, -1.0/350.0, 0); // 700 pixel = 2, 1 pixel = 1/350
123  glCallList(dList);
124  glColor4f(myColor.r, myColor.g, myColor.b, .25);
125  glTranslated(0, -1.0/350.0, 0); // 700 pixel = 2, 1 pixel = 1/350
126  glCallList(dList);
127  glTranslated(0, -elapsedTime/30.0+2.0/350.0, 0);
128 
129  if (deform)
130  {
131  glPopMatrix(); // GL_MODELVIEW
132  glMatrixMode(GL_PROJECTION);
133  glPopMatrix();
134  glMatrixMode(matrixMode);
135  }
136  }
137  else
138  glCallList(dList);
139  }
140  glDisable(GL_BLEND); // for text fading
141 }
142 
144 {
145  return tl;
146 }
147 /*
148  * _ _ 0, 1
149  * |X|X| 2, 3/, 4\, 5, 6/, 7\, 8
150  * |\|/| 9, 10, 11, 12, 13
151  * - - 14, 15
152  * |/|\| 16, 17, 18, 19, 20
153  * |X|X| 21, 22/, 23\, 24, 25/, 26\, 27
154  * - - 28, 29
155  *
156  */
157 
158 void TextBox::drawChar(char c, point3d where, double height)
159 {
160  uint32_t bMap = getBitmap(toupper(c));
161  for (int x = 0; x < 30; x++)
162  {
163  if (bMap&0x1)
164  {
165  switch (x) {
166  case 0: drawLine(where, 0, 0, 1, 0, height); break;
167  case 1: drawLine(where, 1, 0, 1, 0, height); break;
168  case 2: drawLine(where, 0, 0, 0, 1, height); break;
169  case 3: drawLine(where, 0, 1, 1, -1, height); break;
170  case 4: drawLine(where, 0, 0, 1, 1, height); break;
171  case 5: drawLine(where, 1, 0, 0, 1, height); break;
172  case 6: drawLine(where, 1, 1, 1, -1, height); break;
173  case 7: drawLine(where, 1, 0, 1, 1, height); break;
174  case 8: drawLine(where, 2, 0, 0, 1, height); break;
175  case 9: drawLine(where, 0, 1, 0, 1, height); break;
176  case 10: drawLine(where, 0, 1, 1, 1, height); break;
177  case 11: drawLine(where, 1, 1, 0, 1, height); break;
178  case 12: drawLine(where, 1, 2, 1, -1, height); break;
179  case 13: drawLine(where, 2, 1, 0, 1, height); break;
180  case 14: drawLine(where, 0, 2, 1, 0, height); break;
181  case 15: drawLine(where, 1, 2, 1, 0, height); break;
182  case 16: drawLine(where, 0, 2, 0, 1, height); break;
183  case 17: drawLine(where, 0, 3, 1, -1, height); break;
184  case 18: drawLine(where, 1, 2, 0, 1, height); break;
185  case 19: drawLine(where, 1, 2, 1, 1, height); break;
186  case 20: drawLine(where, 2, 2, 0, 1, height); break;
187  case 21: drawLine(where, 0, 3, 0, 1, height); break;
188  case 22: drawLine(where, 0, 4, 1, -1, height); break;
189  case 23: drawLine(where, 0, 3, 1, 1, height); break;
190  case 24: drawLine(where, 1, 3, 0, 1, height); break;
191  case 25: drawLine(where, 1, 4, 1, -1, height); break;
192  case 26: drawLine(where, 1, 3, 1, 1, height); break;
193  case 27: drawLine(where, 2, 3, 0, 1, height); break;
194  case 28: drawLine(where, 0, 4, 1, 0, height); break;
195  case 29: drawLine(where, 1, 4, 1, 0, height); break;
196  }
197  }
198  bMap >>= 1;
199  }
200 }
201 
202 uint32_t TextBox::getBitmap(char c)
203 {
204  uint32_t index[128] = {
205  0x0, // 0
206  0x0, // 1
207  0x0, // 2
208  0x0, // 3
209  0x0, // 4
210  0x0, // 5
211  0x0, // 6
212  0x0, // 7
213  0x0, // 8
214  0x0, // 9
215  0x0, // 10
216  0x0, // 11
217  0x0, // 12
218  0x0, // 13
219  0x0, // 14
220  0x0, // 15
221  0x0, // 16
222  0x0, // 17
223  0x0, // 18
224  0x0, // 19
225  0x0, // 20
226  0x0, // 21
227  0x0, // 22
228  0x0, // 23
229  0x0, // 24
230  0x0, // 25
231  0x0, // 26
232  0x0, // 27
233  0x0, // 28
234  0x0, // 29
235  0x0, // 30
236  0x0, // 31
237  0x0, // 32
238  (1<<5)|(1<<11)|(1<<24), // 33 !
239  (1<<5)|(1<<8), // 34 "
240  0x0, // 35 #
241  0x0, // 36 $
242  0x0, // 37 %
243  0x0, // 38 &
244  (1<<5), // 39 '
245  (1<<3)|(1<<9)|(1<<16)|(1<<23), // 40 (
246  (1<<7)|(1<<13)|(1<<20)|(1<<25), // 41 )
247  (1<<10)|(1<<11)|(1<<12)|(1<<14)|(1<<15)|(1<<17)|(1<<18)|(1<<19), // 42 *
248  0x0, // 43 +
249  (1<<22), // 44 ,
250  (1<<15)|(1<<14), // 45 -
251  (1<<22)|(1<<23), // 46 .
252  (1<<8)|(1<<12)|(1<<18)|(1<<22), // 47 /
253  // 48 0
254  (1<<3)|(1<<7)|(1<<9)|(1<<12)|(1<<13)|(1<<16)|(1<<17)|(1<<20)|(1<<23)|(1<<25),
255  // 49 1
256  (1<<3)|(1<<5)|(1<<11)|(1<<18)|(1<<24)|(1<<28)|(1<<29),
257  // 50 2
258  (1<<3)|(1<<1)|(1<<8)|(1<<13)|(1<<15)|(1<<17)|(1<<21)|(1<<28)|(1<<29),
259  // 51 3
260  (1<<0)|(1<<7)|(1<<13)|(1<<15)|(1<<20)|(1<<25)|(1<<28),
261  // 52 4
262  (1<<3)|(1<<9)|(1<<14)|(1<<15)|(1<<11)|(1<<18)|(1<<24),
263  // 53 5
264  (1<<0)|(1<<1)|(1<<2)|(1<<9)|(1<<14)|(1<<19)|(1<<27)|(1<<28)|(1<<29),
265  // 54 6
266  (1<<0)|(1<<1)|(1<<2)|(1<<9)|(1<<14)|(1<<19)|(1<<27)|(1<<28)|(1<<29)|(1<<16)|(1<<21),
267  // 55 7
268  (1<<0)|(1<<1)|(1<<8)|(1<<12)|(1<<18)|(1<<24),
269  // 56 8
270  (1<<0)|(1<<1)|(1<<2)|(1<<8)|(1<<9)|(1<<13)|(1<<14)|(1<<15)|(1<<16)|(1<<20)|(1<<21)|(1<<27)|(1<<28)|(1<<29),
271  // 57 9
272  (1<<0)|(1<<1)|(1<<2)|(1<<8)|(1<<9)|(1<<13)|(1<<14)|(1<<15)|(1<<20)|(1<<27)|(1<<28)|(1<<29),
273  (1<<24)|(1<<11), // 58 :
274  (1<<22)|(1<<11), // 59 ;
275  0x0, // 60 <
276  0x0, // 61 =
277  0x0, // 62 >
278  (1<<2)|(1<<1)|(1<<0)|(1<<8)|(1<<12)|(1<<24), // 63 ?
279  0x0, // 64 @
280  // 65 A
281  (1<<1)|(1<<3)|(1<<8)|(1<<9)|(1<<13)|(1<<14)|(1<<15)|(1<<16)|(1<<20)|(1<<21)|(1<<27),
282  // 66 B
283  (1<<0)|(1<<2)|(1<<7)|(1<<9)|(1<<12)|(1<<14)|(1<<16)|(1<<19)|(1<<21)|(1<<27)|(1<<28)|(1<<29),
284  // 67 C
285  (1<<0)|(1<<1)|(1<<2)|(1<<9)|(1<<16)|(1<<21)|(1<<28)|(1<<29),
286  // 68 D
287  (1<<0)|(1<<2)|(1<<7)|(1<<9)|(1<<13)|(1<<16)|(1<<20)|(1<<21)|(1<<25)|(1<<28),
288  // 69 E
289  (1<<0)|(1<<1)|(1<<2)|(1<<9)|(1<<14)|(1<<16)|(1<<21)|(1<<28)|(1<<29),
290  // 70 F
291  (1<<0)|(1<<1)|(1<<2)|(1<<9)|(1<<14)|(1<<16)|(1<<21),
292  // 71 G
293  (1<<0)|(1<<1)|(1<<2)|(1<<8)|(1<<9)|(1<<15)|(1<<16)|(1<<20)|(1<<21)|(1<<25)|(1<<28),
294  // 72 H
295  (1<<2)|(1<<8)|(1<<9)|(1<<13)|(1<<14)|(1<<15)|(1<<16)|(1<<20)|(1<<21)|(1<<27),
296  // 73 I
297  (1<<0)|(1<<1)|(1<<5)|(1<<11)|(1<<18)|(1<<24)|(1<<28)|(1<<29),
298  // 74 J
299  (1<<1)|(1<<8)|(1<<13)|(1<<20)|(1<<21)|(1<<25)|(1<<28),
300  // 75 K
301  (1<<2)|(1<<9)|(1<<16)|(1<<21)|(1<<14)|(1<<12)|(1<<19)|(1<<8)|(1<<27),
302  // 76 L
303  (1<<2)|(1<<9)|(1<<16)|(1<<21)|(1<<28)|(1<<29),
304  // 77 M
305  (1<<2)|(1<<9)|(1<<16)|(1<<21)|(1<<8)|(1<<13)|(1<<20)|(1<<27)|(1<<0)|(1<<1)|(1<<5)|(1<<11),
306  // 78 N
307  (1<<2)|(1<<9)|(1<<16)|(1<<21)|(1<<0)|(1<<7)|(1<<13)|(1<<20)|(1<<27),
308  // 79 O
309  (1<<2)|(1<<9)|(1<<16)|(1<<21)|(1<<8)|(1<<13)|(1<<20)|(1<<27)|(1<<0)|(1<<1)|(1<<28)|(1<<29),
310  // 80 P
311  (1<<2)|(1<<9)|(1<<16)|(1<<21)|(1<<0)|(1<<14)|(1<<7)|(1<<12),
312  // 81 Q
313  (1<<2)|(1<<9)|(1<<16)|(1<<21)|(1<<8)|(1<<13)|(1<<20)|(1<<0)|(1<<1)|(1<<28)|(1<<25)|(1<<26),
314  // 82 R
315  (1<<2)|(1<<9)|(1<<16)|(1<<21)|(1<<0)|(1<<14)|(1<<7)|(1<<13)|(1<<15)|(1<<19)|(1<<27),
316  // 83 S
317  (1<<0)|(1<<1)|(1<<2)|(1<<9)|(1<<14)|(1<<15)|(1<<20)|(1<<27)|(1<<28)|(1<<29),
318  // 84 T
319  (1<<0)|(1<<1)|(1<<5)|(1<<11)|(1<<18)|(1<<24),
320  // 85 U
321  (1<<2)|(1<<9)|(1<<16)|(1<<21)|(1<<8)|(1<<13)|(1<<20)|(1<<27)|(1<<28)|(1<<29),
322  // 86 V
323  (1<<2)|(1<<9)|(1<<16)|(1<<21)|(1<<8)|(1<<13)|(1<<20)|(1<<25)|(1<<28),
324  // 87 W
325  (1<<2)|(1<<9)|(1<<16)|(1<<21)|(1<<8)|(1<<13)|(1<<20)|(1<<25)|(1<<28)|(1<<18)|(1<<24),
326  // 88 X
327  (1<<2)|(1<<21)|(1<<10)|(1<<17)|(1<<12)|(1<<19)|(1<<8)|(1<<27),
328  // 89 Y
329  (1<<2)|(1<<10)|(1<<18)|(1<<12)|(1<<24)|(1<<8),
330  // 90 Z
331  (1<<0)|(1<<1)|(1<<8)|(1<<12)|(1<<17)|(1<<21)|(1<<28)|(1<<29),
332  // 91
333  (1<<0)|(1<<2)|(1<<9)|(1<<16)|(1<<21)|(1<<28), // 91 [ // 0 2 9 16 21 28 // 1 8 13 20 27 29
334  0x0, // 92 (backslash)
335  (1<<1)|(1<<8)|(1<<13)|(1<<20)|(1<<27)|(1<<29), // 93 ]
336  0x0, // 94 ^
337  (1<<28)|(1<<29), // 95 _
338  0x0, // 96 `
339  0x0, // 97 a
340  0x0, // 98 b
341  0x0, // 99 c
342  0x0, // 100 d
343  0x0, // 101 e
344  0x0, // 102 f
345  0x0, // 103 g
346  0x0, // 104 h
347  0x0, // 105 i
348  0x0, // 106 j
349  0x0, // 107 k
350  0x0, // 108 l
351  0x0, // 109 m
352  0x0, // 110 n
353  0x0, // 111 o
354  0x0, // 112 p
355  0x0, // 113 q
356  0x0, // 114 r
357  0x0, // 115 s
358  0x0, // 116 t
359  0x0, // 117 u
360  0x0, // 118 v
361  0x0, // 119 w
362  0x0, // 120 x
363  0x0, // 121 y
364  0x0, // 122 z
365  0x0, // 123 {
366  0x0, // 124 |
367  0x0, // 125 }
368  0x0, // 126 ~
369  0x0, // 127 ESC
370  };
371  return index[(int)c];
372 }
373 
374 void TextBox::drawLine(point3d where, int startx, int starty, int offsetx, int offsety, double scale)
375 {
376  double unit = scale/4;
377  glVertex2f(where.x+startx*unit, where.y-starty*unit);
378  glVertex2f(where.x+startx*unit+offsetx*unit, where.y-starty*unit-offsety*unit);
379 }
380 
rgbColor::b
float b
Definition: Colors.h:71
TextBox::charLine
int charLine
Definition: TextBox.h:35
TextBox::getLocation
point3d getLocation()
Definition: TextBox.cpp:143
TextBox.h
TextBox::elapsedTime
double elapsedTime
Definition: TextBox.h:37
rgbColor::g
float g
Definition: Colors.h:71
TextBox::scrolling
bool scrolling
Definition: TextBox.h:36
TextBox::draw
void draw()
Definition: TextBox.cpp:46
TextBox::~TextBox
~TextBox()
Definition: TextBox.cpp:29
TextBox::duration
double duration
Definition: TextBox.h:38
TextBox::getBitmap
uint32_t getBitmap(char c)
Definition: TextBox.cpp:202
TextBox::drawChar
void drawChar(char c, point3d where, double height)
Definition: TextBox.cpp:158
point3d
#define point3d
Definition: GLUtil.h:123
TextBox::destroy
void destroy()
Definition: TextBox.cpp:36
height
int height
Definition: SFML_HOG.cpp:54
TextBox::TextBox
TextBox(char *text, int charLine, point3d topLeft, point3d bottomRight, double duration=4, bool deform=true, bool scrolling=false)
Definition: TextBox.cpp:15
TextBox::stepTime
void stepTime(double amount)
Definition: TextBox.cpp:41
rgbColor::r
float r
Definition: Colors.h:71
TextBox::dList
GLuint dList
Definition: TextBox.h:39
TextBox::myColor
rgbColor myColor
Definition: TextBox.h:33
TextBox::text
char * text
Definition: TextBox.h:34
TextBox::deform
bool deform
Definition: TextBox.h:36
TextBox::tl
point3d tl
Definition: TextBox.h:32
TextBox::drawLine
void drawLine(point3d where, int startx, int starty, int offsetx, int offsety, double scale)
Definition: TextBox.cpp:374
TextBox::br
point3d br
Definition: TextBox.h:32