Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: fixed a SegFault (that this worked before is some wonder :)

File size: 4.0 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
21using namespace std;
22
23/**
24 * @brief creates a new Text Element
25 * @param fontFile the Font to render this text in
26 * @param type The renderType to display this font in
27 */
28MultiLineText::MultiLineText(const std::string& fontFile, unsigned int textSize)
29  : Text(fontFile, textSize)
30{
31  this->setClassID(CL_MULTI_LINE_TEXT, "MultiLineText");
32
33  this->lineSpacing = 1.0f;
34  this->lineWidth = 100.0f;
35  this->setupTextWidth();
36}
37
38/**
39 * @brief sets the maximum Line width
40 * @param lineWidth the maximum lineWidth.
41 */
42void MultiLineText::setLineWidth(float lineWidth)
43{
44  this->lineWidth = lineWidth;
45  this->setupTextWidth();
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->getText().empty()))
64    return;
65  glPushMatrix();
66  // transform for alignment.
67  // TODO make the Stuff with the alignment
68  if (this->getAlignment() == TEXT_ALIGN_RIGHT)
69    glTranslatef(-this->getSizeX2D(), 0, 0);
70  else if (this->getAlignment() == TEXT_ALIGN_CENTER || this->getAlignment() == TEXT_ALIGN_SCREEN_CENTER)
71    glTranslatef(-this->getSizeX2D()/2, 0, 0);
72
73  // drawing this Text.
74  // setting the Blending effects
75  glColor4f(this->getColor().x, this->getColor().y, this->getColor().z, this->getBlending());
76  glEnable(GL_BLEND);
77  glEnable(GL_TEXTURE_2D);
78  glBlendFunc(GL_SRC_ALPHA, GL_ONE);
79  glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, GL_MODULATE );
80
81  glBindTexture(GL_TEXTURE_2D, this->getFont()->getTexture());
82  glTranslatef(this->getAbsCoor2D().x, this->getAbsCoor2D().y, 0);
83  glRotatef(this->getAbsDir2D(), 0, 0, 1);
84
85  Glyph* tmpGlyph;
86  float posX = 0.0f;
87  float posY = 0.0f;
88  unsigned int lineNumber = 0;
89
90  glBegin(GL_QUADS);
91  for (unsigned int i = 0; i < this->getText().size(); ++i)
92  {
93    if (unlikely(!this->lineEnds.empty() && i == this->lineEnds[lineNumber]))
94    {
95      // go to the next Line.
96      ++lineNumber;
97      posX = 0.0f;
98      posY += this->lineSpacing + this->getFont()->getMaxHeight();
99    }
100
101    if(likely((tmpGlyph = this->getFont()->getGlyphArray()[this->getText()[i]]) != NULL))
102    {
103      glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[2]);
104      glVertex2d(posX+tmpGlyph->maxX*this->getSize(), posY);
105
106      glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[3]);
107      glVertex2d(posX+tmpGlyph->maxX*this->getSize(), posY + this->getSize());
108
109      glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[3]);
110      glVertex2d(posX+tmpGlyph->minX*this->getSize(), posY+ this->getSize());
111
112      glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[2]);
113      glVertex2d(posX+tmpGlyph->minX*this->getSize(), posY);
114
115      posX += tmpGlyph->advance * this->getSize();
116    }
117  }
118  glEnd();
119  glPopMatrix();
120}
121
122/**
123 * @brief setting up the Text-Width if DYNAMIC
124 */
125void MultiLineText::setupTextWidth()
126{
127  this->lineEnds.clear();
128  float width = 0.0f;
129  float maxWidth = this->lineWidth / this->getSize();
130
131  for (unsigned int i = 0; i < this->getText().size(); i++)
132  {
133    if (width > maxWidth || this->getText()[i] == '\n')
134    {
135      this->lineEnds.push_back(i);
136      width = 0.0f;
137    }
138    // Advance the Text.
139    if(this->getFont()->getGlyphArray()[this->getText()[i]] != NULL)
140      width += this->getFont()->getGlyphArray()[this->getText()[i]]->advance;
141  }
142  this->setSizeY2D(this->lineEnds.size() * (this->lineSpacing + this->getFont()->getMaxHeight()));
143}
Note: See TracBrowser for help on using the repository browser.