Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

merged the new Font-Implementation back here
merged with svn merge https://svn.orxonox.net/orxonox/branches/fontdata . -r8752:HEAD
no conflicts, naturally

File size: 4.7 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  // setting the Blending effects
76  glActiveTexture(GL_TEXTURE0);
77
78/*  glColor4fv(&this->color()[0]);
79  glEnable(GL_BLEND);
80  glEnable(GL_TEXTURE_2D);
81  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
82  glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, GL_MODULATE );
83
84  glBindTexture(GL_TEXTURE_2D, this->font().getTexture());*/
85  this->font().select();
86  glTranslatef(this->getAbsCoor2D().x, this->getAbsCoor2D().y, 0);
87  glRotatef(this->getAbsDir2D(), 0, 0, 1);
88
89  Glyph* tmpGlyph;
90  float posX = 0.0f;
91  float posY = 0.0f;
92  unsigned int lineNumber = 0;
93
94  glBegin(GL_QUADS);
95  for (unsigned int i = 0; i < this->text().size(); ++i)
96  {
97    if (unlikely(this->lineEnds.size() > lineNumber && i == this->lineEnds[lineNumber]))
98    {
99      // go to the next Line.
100      ++lineNumber;
101      posX = 0.0f;
102      posY += this->lineSpacing + this->size(); //this->font().getMaxHeight();
103    }
104
105    if(likely((tmpGlyph = this->font().getGlyphArray()[this->text()[i]]) != NULL))
106    {
107      glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[2]);
108      glVertex2d(posX+tmpGlyph->maxX*this->size(), posY);
109
110      glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[3]);
111      glVertex2d(posX+tmpGlyph->maxX*this->size(), posY + this->size());
112
113      glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[3]);
114      glVertex2d(posX+tmpGlyph->minX*this->size(), posY+ this->size());
115
116      glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[2]);
117      glVertex2d(posX+tmpGlyph->minX*this->size(), posY);
118
119      posX += tmpGlyph->advance * this->size();
120    }
121  }
122  glEnd();
123  glPopAttrib();
124  glPopMatrix();
125}
126
127
128/**
129 * @brief setting up the Text-Width if DYNAMIC
130 */
131void MultiLineText::setupTextWidth()
132{
133  this->lineEnds.clear();
134  float width = 0.0f;
135  float maxWidth = this->lineWidth / this->size();
136
137  for (unsigned int i = 0; i < this->text().size(); i++)
138  {
139    if (width > maxWidth || this->text()[i] == '\n')
140    {
141      if (likely(i > 0))
142      {
143        this->lineEnds.push_back( i -1 );
144        width = this->font().getGlyphArray()[this->text()[i-1]]->advance;
145      }
146      else
147        width = 0.0f;
148    }
149
150    // Advance the Text.
151    if(this->font().getGlyphArray()[this->text()[i]] != NULL)
152      width += this->font().getGlyphArray()[this->text()[i]]->advance;
153  }
154  this->lineCount = lineEnds.size() + 1;
155  this->setSizeY2D((this->lineEnds.size()+1) * (this->lineSpacing + this->font().getMaxHeight()));
156}
157
158/**
159 * @brief print out some nice debug output
160 */
161void MultiLineText::debug() const
162{
163 printf("Debug %s::%s: %d lines\n", this->getClassName(), this->getName(), this->getLineCount());
164
165 std::string tmpText = this->text();
166 std::vector<unsigned int> ends = this->lineEnds;
167 ends.push_back(tmpText.size());
168
169 unsigned int prev = 0;
170  for (unsigned int i = 0; i < ends.size(); i++)
171  {
172    printf("Line %d: %s\n", i, tmpText.substr(prev, ends[i] - prev).c_str());
173    prev = ends[i];
174  }
175}
Note: See TracBrowser for help on using the repository browser.