Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/graphics/text_engine/multi_line_text.cc @ 8778

Last change on this file since 8778 was 8768, checked in by bensch, 18 years ago

trunk: font: cleaner draw() of Texts

File size: 4.3 KB
Line 
1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11   ### File Specific:
12   main-programmer: Benjamin Grauer
13   co-programmer: ...
14*/
15
16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_GRAPHICS
17
18#include "multi_line_text.h"
19#include "font.h"
20
21/**
22 * @brief creates a new Text Element
23 * @param fontFile the Font to render this text in
24 * @param type The renderType to display this font in
25 */
26MultiLineText::MultiLineText(const std::string& fontFile, unsigned int textSize, float lineWidth)
27  : Text(fontFile, textSize)
28{
29  this->setClassID(CL_MULTI_LINE_TEXT, "MultiLineText");
30
31  this->lineSpacing = 1.0;
32  this->lineCount = 0;
33  this->setLineWidth(lineWidth);
34}
35
36/**
37 * @brief sets the maximum Line width
38 * @param lineWidth the maximum lineWidth.
39 */
40void MultiLineText::setLineWidth(float lineWidth)
41{
42  this->lineWidth = lineWidth;
43  this->setSizeX2D(lineWidth);
44  this->setupTextWidth();
45}
46
47
48/**
49 * @param lineSpacing: the Spacing between the lines
50 */
51void MultiLineText::setLineSpacing(float lineSpacing)
52{
53   this->lineSpacing = lineSpacing;
54   this->setupTextWidth();
55}
56
57
58/**
59 * @brief draws the Text
60 */
61void MultiLineText::draw() const
62{
63  if (unlikely(this->text().empty()))
64    return;
65  glPushMatrix();
66  glPushAttrib(GL_ENABLE_BIT);
67  // transform for alignment.
68  // TODO make the Stuff with the alignment
69  if (this->getAlignment() == TEXT_ALIGN_RIGHT)
70    glTranslatef(-this->getSizeX2D(), 0, 0);
71  else if (this->getAlignment() == TEXT_ALIGN_CENTER || this->getAlignment() == TEXT_ALIGN_SCREEN_CENTER)
72    glTranslatef(-this->getSizeX2D()/2, 0, 0);
73
74  // drawing this Text.
75  this->font().select();
76
77  glTranslatef(this->getAbsCoor2D().x, this->getAbsCoor2D().y, 0);
78  glRotatef(this->getAbsDir2D(), 0, 0, 1);
79
80  Glyph* tmpGlyph;
81  float posX = 0.0f;
82  float posY = 0.0f;
83  unsigned int lineNumber = 0;
84
85  glBegin(GL_QUADS);
86  for (unsigned int i = 0; i < this->text().size(); ++i)
87  {
88    if (unlikely(this->lineEnds.size() > lineNumber && i == this->lineEnds[lineNumber]))
89    {
90      // go to the next Line.
91      ++lineNumber;
92      posX = 0.0f;
93      posY += this->lineSpacing + this->size(); //this->font().getMaxHeight();
94    }
95
96    if(likely((tmpGlyph = this->font().getGlyphArray()[this->text()[i]]) != NULL))
97    {
98      glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[2]);
99      glVertex2d(posX+tmpGlyph->maxX*this->size(), posY);
100
101      glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[3]);
102      glVertex2d(posX+tmpGlyph->maxX*this->size(), posY + this->size());
103
104      glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[3]);
105      glVertex2d(posX+tmpGlyph->minX*this->size(), posY+ this->size());
106
107      glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[2]);
108      glVertex2d(posX+tmpGlyph->minX*this->size(), posY);
109
110      posX += tmpGlyph->advance * this->size();
111    }
112  }
113  glEnd();
114  glPopAttrib();
115  glPopMatrix();
116}
117
118
119/**
120 * @brief setting up the Text-Width if DYNAMIC
121 */
122void MultiLineText::setupTextWidth()
123{
124  this->lineEnds.clear();
125  float width = 0.0f;
126  float maxWidth = this->lineWidth / this->size();
127
128  for (unsigned int i = 0; i < this->text().size(); i++)
129  {
130    if (width > maxWidth || this->text()[i] == '\n')
131    {
132      if (likely(i > 0))
133      {
134        this->lineEnds.push_back( i -1 );
135        width = this->font().getGlyphArray()[this->text()[i-1]]->advance;
136      }
137      else
138        width = 0.0f;
139    }
140
141    // Advance the Text.
142    if(this->font().getGlyphArray()[this->text()[i]] != NULL)
143      width += this->font().getGlyphArray()[this->text()[i]]->advance;
144  }
145  this->lineCount = lineEnds.size() + 1;
146  this->setSizeY2D((this->lineEnds.size()+1) * (this->lineSpacing + this->font().getMaxHeight()));
147}
148
149/**
150 * @brief print out some nice debug output
151 */
152void MultiLineText::debug() const
153{
154 printf("Debug %s::%s: %d lines\n", this->getClassName(), this->getName(), this->getLineCount());
155
156 std::string tmpText = this->text();
157 std::vector<unsigned int> ends = this->lineEnds;
158 ends.push_back(tmpText.size());
159
160 unsigned int prev = 0;
161  for (unsigned int i = 0; i < ends.size(); i++)
162  {
163    printf("Line %d: %s\n", i, tmpText.substr(prev, ends[i] - prev).c_str());
164    prev = ends[i];
165  }
166}
Note: See TracBrowser for help on using the repository browser.