Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/soundEngine/src/lib/graphics/font/fontset.cc @ 3715

Last change on this file since 3715 was 3478, checked in by patrick, 20 years ago

orxonox/trunk: fixed font leak and removed sfont again

File size: 7.8 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   * This file is extended to the needs of the orxonox-project.         *
17   * The main difference to the version of Marius is, that textures get *
18   * loaded by orxonox Texture-class, and that the comments are set up, *
19   * in doxygen style                                                   *
20   * the Copyright of the original file is below this copyright, and we *
21   * hope not to offend Marius through this.                            *
22                                                                      ***
23
24*/
25
26/***************************************************************************
27                          cone3dfont.cpp  -  description
28                             -------------------
29    copyright            : (C) 2001 by Marius Andra aka Cone3D
30    email                : marius@hot.ee
31    ICQ                  : 43999510
32 ***************************************************************************/
33
34/***************************************************************************
35 *                                                                         *
36 *   This program is free software; you can redistribute it and/or modify  *
37 *   it under the terms of the GNU General Public License as published by  *
38 *   the Free Software Foundation; either version 2 of the License, or     *
39 *   (at your option) any later version.                                   *
40 *                                                                         *
41 ***************************************************************************/
42
43
44#include "fontset.h"
45#include <stdarg.h>
46
47#include "../importer/texture.h"
48
49/**
50    \brief initializes a new fontset
51*/
52FontSet::FontSet()
53{
54  this->sizex=1.0f;
55  this->sizey=1.0f;
56  this->font = NULL;
57}
58
59/**
60    \brief initializes a new fontset with a texture for the font
61    \param fontFile The fontfile to load
62*/
63FontSet::FontSet(char* fontFile)
64{
65  this->sizex=1.0f;
66  this->sizey=1.0f;
67  this->font = NULL;
68 
69}
70
71/**
72   \brief deletes the Fontset by freeing all allocated space
73   and then deleting the Texture.
74*/
75FontSet::~FontSet()
76{
77  killFont();
78  // delete the texture;
79  delete font;
80}
81
82/**
83   \brief sets the size of the font
84   \param x the width of the font
85   \param y the height of the font
86*/
87int FontSet::setSize(float x, float y)
88{
89  sizex=x;
90  sizey=y;
91 
92  return 1;
93}
94
95/**
96   \brief builds the fontset
97   \param file the name of the file to get the fontset from.
98*/
99int FontSet::buildFont(char *file)
100{
101  int loop1;
102  float cx, cy;
103 
104  if (!font)
105    delete font;
106  font = new Texture();
107  font->loadImage(file);
108 
109  base=glGenLists(256);                          // Creating 256 Display Lists
110  glBindTexture(GL_TEXTURE_2D, font->getTexture()); // Select Our Font Texture
111  for (loop1=0; loop1<256; loop1++)              // Loop Through All 256 Lists
112    {
113      cx=(float)(loop1%16)/16.0f;                  // X Position Of Current Character
114      cy=(float)(loop1/16)/16.0f;                  // Y Position Of Current Character
115     
116      glNewList(base+loop1,GL_COMPILE);            // Start Building A List
117      glBegin(GL_QUADS);                           // Use A Quad For Each Character
118      glTexCoord2f(cx,1.0f-cy-0.0625f);          // Texture Coord (Bottom Left)
119      glVertex2d(0,16);                          // Vertex Coord (Bottom Left)
120      glTexCoord2f(cx+0.0625f,1.0f-cy-0.0625f);  // Texture Coord (Bottom Right)
121      glVertex2i(16,16);                         // Vertex Coord (Bottom Right)
122      glTexCoord2f(cx+0.0625f,1.0f-cy-0.001f);   // Texture Coord (Top Right)
123      glVertex2i(16,0);                          // Vertex Coord (Top Right)
124      glTexCoord2f(cx,1.0f-cy-0.001f);           // Texture Coord (Top Left)
125      glVertex2i(0,0);                           // Vertex Coord (Top Left)
126      glEnd();                                     // Done Building Our Quad (Character)
127      glTranslated(12,0,0);                        // Move To The Right Of The Character
128      glEndList();                                 // Done Building The Display List
129    }                                              // Loop Until All 256 Are Built
130  return 1;
131}
132
133/**
134   \brief deletes the display lists.
135*/
136int FontSet::killFont(void)                          // Delete The Font From Memory
137{
138  glDeleteLists(base,256);                       // Delete All 256 Display Lists
139  return 1;
140}
141
142/**
143   \brief prints out some text
144   \param x most left coordinate of the text.
145   \param y the top coordinate of the text
146   \param type Type of text
147   \param fmt The text to display
148   \param ... more text
149*/
150int FontSet::printText(int x, int y, char type, char *fmt,...)// Where The Printing Happens
151{
152  char    text[1024];                            // Holds Our String
153  int blendOn,scissorOn,textureOn,lightOn;  // Holds the previous GL settings
154  int depthOn,matrixMode,screenStats[4],blendSrc,blendDst;
155  char typ=type;
156
157  va_list ap;                                    // Pointer To List Of Arguments
158
159  if (fmt == NULL)                               // If There's No Text
160    return 1;                                      // Do Nothing
161 
162  va_start(ap, fmt);                             // Parses The String For Variables
163  vsprintf(text, fmt, ap);                       // And Converts Symbols To Actual Numbers
164  va_end(ap);                                    // Results Are Stored In Text
165 
166  if (type>3)                                     // Did User Choose An Invalid Character Set?
167    type=3;                                       // If So, Select Set 2 (Italic)
168 
169  textureOn = glIsEnabled(GL_TEXTURE_2D); // Were textures enabled?
170  depthOn = glIsEnabled(GL_DEPTH_TEST);  // Was GL_DEPTH_TEST enabled?
171  lightOn = glIsEnabled(GL_LIGHTING); // Was GL_LIGHTING enabled?
172  scissorOn = glIsEnabled(GL_SCISSOR_TEST);  // etc.
173  glGetIntegerv(GL_MATRIX_MODE, &matrixMode);
174  glGetIntegerv(GL_VIEWPORT, screenStats);
175  blendOn= glIsEnabled(GL_BLEND);
176  glGetIntegerv(GL_BLEND_SRC, &blendSrc);
177  glGetIntegerv(GL_BLEND_DST, &blendDst);
178 
179  if (depthOn) glDisable(GL_DEPTH_TEST);  // If they were enabled/disabled
180  if (!textureOn) glEnable(GL_TEXTURE_2D); // then enable/disable them
181  if (!blendOn) glEnable(GL_BLEND);
182  if (!scissorOn) glEnable(GL_SCISSOR_TEST);
183  if (lightOn) glDisable(GL_LIGHTING);
184  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // Set the correct blending mode
185 
186  glMatrixMode(GL_PROJECTION); // and initalize Ortho mode
187  glPushMatrix();
188  glLoadIdentity();
189  glOrtho(0.0f,screenStats[2],screenStats[3],0.0f,-1.0f,1.0f);
190  glMatrixMode(GL_MODELVIEW);
191  glPushMatrix();
192  glBindTexture(GL_TEXTURE_2D, font->getTexture());
193
194  if(type>1) typ=typ-2;
195  glLoadIdentity();
196  glTranslated(x,y,0);
197  glListBase(base-32+(128*typ));
198  glScalef(sizex,sizey,1.0f);
199  glCallLists(strlen(text),GL_UNSIGNED_BYTE, text); // Write The Text To The Screen
200 
201  if(type>1)
202    {
203      glLoadIdentity();
204      glTranslated(x+1,y,0);
205      glListBase(base-32+(128*(type-2)));
206      glScalef(sizex,sizey,1.0f);
207      glCallLists(strlen(text),GL_UNSIGNED_BYTE, text); // Write The Text To The Screen
208    }
209
210  glBindTexture(GL_TEXTURE_2D, 0); // Fix everything up
211  glMatrixMode(GL_PROJECTION);
212  glPopMatrix();
213  glMatrixMode(GL_MODELVIEW);
214  glPopMatrix();
215
216  if (depthOn) glEnable(GL_DEPTH_TEST);
217  if (!textureOn) glDisable(GL_TEXTURE_2D);
218  if (!blendOn) glDisable(GL_BLEND);
219  if (!scissorOn) glDisable(GL_SCISSOR_TEST);
220  if (lightOn) glEnable(GL_LIGHTING);
221
222  glBlendFunc(blendSrc, blendDst);
223  glMatrixMode(matrixMode);
224
225  return 1;
226}
Note: See TracBrowser for help on using the repository browser.