HOG2
TextOverlay.cpp
Go to the documentation of this file.
1 /*
2  * TextOverlay.cpp
3  *
4  * Created by Nathan Sturtevant on 7/15/07.
5  * Copyright 2007 Nathan Sturtevant, University of Alberta. All rights reserved.
6  *
7  */
8 
9 #include "TextOverlay.h"
10 #include "GLUtil.h"
11 #include <string.h>
12 #include <stdint.h>
13 
14 using std::string;
15 
17 {
18  maxNumLines = maxLines;
19  index = 0;
20  bold = false;
21 }
22 
24 {
25  for (uint32_t x = 0; x < text.size(); x++)
26  {
27  //delete [] text[x];
28  //text[x] = 0;
29  }
30 }
31 
32 void TextOverlay::SetBold(bool useBold)
33 {
34  bold = useBold;
35 }
36 
37 void TextOverlay::AddLine(const char *line)
38 {
39  if (line == 0)
40  return;
41  string txt(line);
42  text.push_back(txt);
43  if (text.size()-index > maxNumLines)
44  index++;
45 }
46 
48 {
49  if (text.size() > 0)
50  return text.back().c_str();
51  return 0;
52 }
53 
55 {
56  if (text.size() > 0)
57  text.back().resize(text.back().length()-1);
58 }
59 
60 void TextOverlay::AppendToLine(const char *line)
61 {
62  string txt(line);
63  if (text.size() > 0)
64  text.back().append(txt);
65  else
66  text.push_back(txt);
67 }
68 
70 {
71  text.resize(0);
72  index = 0;
73 }
74 
75 void TextOverlay::OpenGLDraw(int window)
76 {
77  if (text.size() == 0)
78  return;
79 
80  glDisable(GL_LIGHTING);
81  if (bold)
82  glLineWidth(3.0);
83  else
84  glLineWidth(2.0);
85  glEnable(GL_LINE_SMOOTH);
86 
87  GLint matrixMode;
88 
89  glDisable(GL_DEPTH_TEST);
90  glEnable(GL_BLEND); // for text fading
91  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // ditto
92  // set orthograhic 1:1 pixel transform in local view coords
93  glGetIntegerv(GL_MATRIX_MODE, &matrixMode);
94  glMatrixMode(GL_PROJECTION);
95  glPushMatrix();
96  glLoadIdentity();
97  glFrustum(-1, 1, -1, 1, 1.9, 2.1);
98  //gluLookAt(0, 0, -2, 0, 0, 0, 0, -1, 0);
99  glMatrixMode(GL_MODELVIEW);
100  glPushMatrix();
101 
102  // draw transparent background behind text
103  glLoadIdentity();
104  glBegin(GL_QUADS);
105  glColor4f(0.0, 0.0, 0.0, 0.5);
106  glVertex3f(-1-0.01, -1-0.07, 0.0);
107  glVertex3f( 1+0.01, -1-0.07, 0.0);
108 // glVertex3f( 1+0.01, -1+(text.size()-index-1)*0.06+0.01, 0.0);
109 // glVertex3f(-1-0.01, -1+(text.size()-index-1)*0.06+0.01, 0.0);
110  glVertex3f( 1+0.01, -1+(maxNumLines-index-1)*0.06+0.01, 0.0);
111  glVertex3f(-1-0.01, -1+(maxNumLines-index-1)*0.06+0.01, 0.0);
112  glEnd();
113 
114  int lineOffset = 0;
115  for (unsigned int x = index; x < text.size(); x++)
116  {
117 
118  int charOffset = 0;
119  while (charOffset != -1)
120  {
121  glLoadIdentity();
122  glTranslatef(-1, -1+lineOffset*0.06, 0);
123  glScalef(1.0/(24*120.0), 1.0/(24*120.0), 1);
124  glRotatef(180, 0.0, 0.0, 1.0);
125  glRotatef(180, 0.0, 1.0, 0.0);
126  glColor3f(1.0, 1.0, 1.0);
127  lineOffset++;
128 // printf("Drawing '%s' on line %d\n", &(text[x].c_str()[charOffset]), lineOffset);
129  if (x == text.size()-1)
130  charOffset = DrawString(text[x]+"_", charOffset);
131  else
132  charOffset = DrawString(text[x], charOffset);
133  }
134  }
135  if (lineOffset > maxNumLines)
136  index++;
137 
138  // reset orginal martices
139  glPopMatrix(); // GL_MODELVIEW
140  glMatrixMode(GL_PROJECTION);
141  glPopMatrix();
142  glMatrixMode(matrixMode);
143  glEnable(GL_DEPTH_TEST);
144  glDisable(GL_LINE_SMOOTH);
145  glLineWidth(1.0);
146 
147 }
148 
150 {
151  const float linesPerPage = 30.0f;
152 // int lineOffset = 0;
153 // printf("%f: ", -1.0f+maxNumLines*2.0f/(linesPerPage));
154  display.FillRect({-1.0, -1.0, 1.0, -1.0f+maxNumLines*2.0f/(linesPerPage)}, Colors::darkgray);
155  display.FrameRect({-1.0, -1.0, 1.0, -1.0f+maxNumLines*2.0f/(linesPerPage)}, Colors::white, 0.1*2.0/linesPerPage);
156  for (unsigned int x = index; x < text.size(); x++)
157  {
158  Graphics::point p(-1, -1+(x-index+1)*(2.0/(linesPerPage)));
159  display.DrawText(text[x].c_str(), p, Colors::white, 2.0/(linesPerPage),
161 // printf("%f ", p.y);
162  }
163 // printf("\n");
164 }
165 
166 int TextOverlay::DrawString(std::string txt, int start)
167 {
168  int cnt = 0;
169  for (unsigned int x = start; x < txt.length(); x++)
170  {
171  if (txt[x] == '\n')
172  {
173  return x+1;
174  }
175  if (txt[x] == '\t')
176  {
177  // for (int x = 0; x < 3; x++)
178  // glutStrokeCharacter(GLUT_STROKE_MONO_ROMAN, ' ');
179  //return x+1;
180  //cnt+=3;
181  }
182  else {
183  //glutStrokeCharacter(GLUT_STROKE_MONO_ROMAN, txt[x]);
184  cnt++;
185  }
186  if ((cnt > 45) && (txt[x] == ' '))
187  {
188  return x+1;
189  }
190  if (cnt > 50)
191  {
192  return x+1;
193  }
194  }
195  return -1;
196 }
Graphics::point
Definition: Graphics.h:32
TextOverlay::OpenGLDraw
void OpenGLDraw(int window)
Definition: TextOverlay.cpp:75
TextOverlay::AppendToLine
void AppendToLine(const char *)
Definition: TextOverlay.cpp:60
TextOverlay::maxNumLines
int maxNumLines
Definition: TextOverlay.h:32
TextOverlay::text
std::vector< std::string > text
Definition: TextOverlay.h:30
TextOverlay::Clear
void Clear()
Definition: TextOverlay.cpp:69
TextOverlay::TextOverlay
TextOverlay(int maxLines=34)
Definition: TextOverlay.cpp:16
Graphics::Display
Definition: Graphics.h:146
Colors::darkgray
const rgbColor darkgray
Definition: Colors.h:123
TextOverlay::AddLine
void AddLine(const char *)
Definition: TextOverlay.cpp:37
TextOverlay::bold
bool bold
Definition: TextOverlay.h:33
TextOverlay::DeleteChar
void DeleteChar()
Definition: TextOverlay.cpp:54
TextOverlay::GetLastLine
const char * GetLastLine()
Definition: TextOverlay.cpp:47
GLUtil.h
TextOverlay::~TextOverlay
~TextOverlay()
Definition: TextOverlay.cpp:23
TextOverlay::DrawString
int DrawString(std::string, int start=0)
Definition: TextOverlay.cpp:166
Graphics::Display::FrameRect
void FrameRect(rect r, rgbColor c, float lineWidth)
Definition: Graphics.cpp:73
TextOverlay.h
Graphics::textAlignLeft
@ textAlignLeft
Definition: Graphics.h:21
Colors::white
const rgbColor white
Definition: Colors.h:120
Graphics::textBaselineBottom
@ textBaselineBottom
Definition: Graphics.h:28
TextOverlay::SetBold
void SetBold(bool useBold)
Definition: TextOverlay.cpp:32
Graphics::Display::DrawText
void DrawText(const char *text, point location, rgbColor c, float height, const char *typeface=0)
Definition: Graphics.cpp:221
TextOverlay::index
int index
Definition: TextOverlay.h:31
TextOverlay::Draw
void Draw(Graphics::Display &display) const
Definition: TextOverlay.cpp:149
Graphics::Display::FillRect
void FillRect(rect r, rgbColor c)
Definition: Graphics.cpp:101