Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5343 was 5343, checked in by bensch, 19 years ago

orxonox/trunk: split open the text-engine

File size: 4.9 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   for some fonts and licenses visit: =http://www.dafont.com/en/font.php=
16
17   !! IMPORTANT !! When using ttf fonts clear the license issues prior to
18   adding them to orxonox. This is really important, because we do not
19   want to offend anyone.
20*/
21
22#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_FONT
23
24#include "text_engine.h"
25
26using namespace std;
27
28#include <stdlib.h>
29#include <stdio.h>
30#include <string.h>
31
32#include "graphics_engine.h"
33#include "resource_manager.h"
34#include "class_list.h"
35
36#include "p_node.h"
37#include "vector.h"
38#include "debug.h"
39#include "list.h"
40
41///////////////////
42/// TEXT-ENGINE ///
43///////////////////
44/**
45 *  standard constructor
46*/
47TextEngine::TextEngine ()
48{
49   this->setClassID(CL_TEXT_ENGINE, "TextEngine");
50   this->setName("TextEngine");
51   this->enableFonts();
52}
53
54/**
55 *  the singleton reference to this class
56*/
57TextEngine* TextEngine::singletonRef = NULL;
58
59/**
60 *  standard deconstructor
61
62*/
63TextEngine::~TextEngine ()
64{
65  // first remove all the remaining Texts (if any).
66  tList<BaseObject>* textList = ClassList::getList(CL_TEXT);
67  if (textList != NULL)
68  {
69    tIterator<BaseObject>* textIterator = textList->getIterator();
70    Text* text = dynamic_cast<Text*>(textIterator->firstElement());
71    while( text != NULL)
72    {
73      delete text;
74      text = dynamic_cast<Text*>(textIterator->nextElement());
75    }
76    delete textIterator;
77  }
78  // delete all remaining fonts (this is done in the ResourceManager)
79  tList<BaseObject>* fontList = ClassList::getList(CL_FONT);
80  if (fontList != NULL)
81  {
82    tIterator<BaseObject>* fontIterator = fontList->getIterator();
83    Font* font = dynamic_cast<Font*>(fontIterator->firstElement());
84    while( font != NULL)
85    {
86      ResourceManager::getInstance()->unload(font, RP_GAME);
87      font = dynamic_cast<Font*>(fontIterator->nextElement());
88    }
89    delete fontIterator;
90  }
91
92  this->disableFonts();
93
94  TextEngine::singletonRef = NULL;
95}
96
97/**
98 *  function to enable TTF_Fonts
99*/
100void TextEngine::enableFonts()
101{
102  if (!TTF_WasInit())
103    {
104      if(TTF_Init()==-1)
105        PRINTF(1)("TTF_Init: %s\n", TTF_GetError());
106
107      TextEngine::checkVersion();
108    }
109  else
110    PRINTF(4)("Fonts already initialized\n");
111}
112
113/**
114 *  function to disable TTF_fonts
115*/
116void TextEngine::disableFonts()
117{
118  if (TTF_WasInit())
119    {
120      Font::removeDefaultFont();
121      TTF_Quit();
122    }
123  else
124    PRINTF(4)("Fonts were not initialized.\n");
125}
126
127/**
128 *  creates a new Text with a certain font.
129 * @see Font::Font
130 * @see Text::Text
131*/
132Text* TextEngine::createText(const char* fontFile, unsigned int fontSize, int textType)
133{
134  Font* tmpFont;
135  Text* newText;
136  Vector tmpVec;
137
138  tmpFont = (Font*)ResourceManager::getInstance()->load(fontFile, TTF, RP_GAME, &fontSize);
139  if (!tmpFont)
140    {
141      PRINTF(2)("Font %s could not be loaded, probably file not found\n", fontFile);
142      return NULL;
143    }
144  else
145    return new Text(tmpFont, TEXT_RENDER_DYNAMIC);
146}
147
148/**
149 *  outputs some nice Debug information
150
151   @todo there should also be something outputted about Font
152*/
153void TextEngine::debug() const
154{
155  tList<BaseObject>* textList = ClassList::getList(CL_TEXT);
156  if (textList != NULL)
157  {
158    PRINT(0)("+-------------------------------+\n");
159    PRINT(0)("+ TEXT ENGINE DEBUG INFORMATION +\n");
160    PRINT(0)("+-------------------------------+\n");
161    PRINT(0)("Reference: %p; Text Counts: %d\n", this, textList->getSize());
162
163    tIterator<BaseObject>* textIterator = textList->getIterator();
164    Text* text = dynamic_cast<Text*>(textIterator->firstElement());
165    while( text != NULL)
166      {
167        text->debug();
168        text = dynamic_cast<Text*>(textIterator->nextElement());
169      }
170    delete textIterator;
171    PRINT(0)("+---------------------------TE--+\n");
172  }
173}
174
175
176/**
177 *  checks if the compiled version and the local version of SDL_ttf match.
178 * @returns true if match, false otherwise
179*/
180bool TextEngine::checkVersion()
181{
182  SDL_version compile_version;
183  SDL_version link_version;
184  TTF_VERSION(&compile_version);
185  link_version = *TTF_Linked_Version();
186
187  if (compile_version.major == link_version.major &&
188      compile_version.minor == link_version.minor &&
189      compile_version.patch == link_version.patch)
190    {
191      return true;
192    }
193  else
194    {
195      PRINTF(2)("compiled with SDL_ttf version: %d.%d.%d\n",
196                compile_version.major,
197                compile_version.minor,
198                compile_version.patch);
199
200      PRINTF(2)("running with SDL_ttf version: %d.%d.%d\n",
201                link_version.major,
202                link_version.minor,
203                link_version.patch);
204      return false;
205    }
206}
Note: See TracBrowser for help on using the repository browser.