Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: unloading of fonts now without a Warning from the ResourceManager

File size: 4.5 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_GRAPHICS
23
24#include "text_engine.h"
25#include "text.h"
26#include "font.h"
27
28using namespace std;
29
30#include <stdlib.h>
31#include <stdio.h>
32#include <string.h>
33
34#include "graphics_engine.h"
35#include "resource_manager.h"
36#include "class_list.h"
37
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 (There should not be Anything to do here)
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      if (likely(font != Font::getDefaultFont()))
87        ResourceManager::getInstance()->unload(font, RP_GAME);
88      font = dynamic_cast<Font*>(fontIterator->nextElement());
89    }
90    delete fontIterator;
91  }
92
93  this->disableFonts();
94
95  TextEngine::singletonRef = NULL;
96}
97
98/**
99 *  function to enable TTF_Fonts
100*/
101void TextEngine::enableFonts()
102{
103  if (!TTF_WasInit())
104    {
105      if(TTF_Init()==-1)
106        PRINTF(1)("TTF_Init: %s\n", TTF_GetError());
107
108      TextEngine::checkVersion();
109    }
110  else
111    PRINTF(4)("Fonts already initialized\n");
112}
113
114/**
115 *  function to disable TTF_fonts
116*/
117void TextEngine::disableFonts()
118{
119  if (TTF_WasInit())
120    {
121      Font::removeDefaultFont();
122      TTF_Quit();
123    }
124  else
125    PRINTF(4)("Fonts were not initialized.\n");
126}
127
128/**
129 *  outputs some nice Debug information
130 *
131 * @todo there should also be something outputted about Font
132 */
133void TextEngine::debug() const
134{
135  tList<BaseObject>* textList = ClassList::getList(CL_TEXT);
136  if (textList != NULL)
137  {
138    PRINT(0)("+-------------------------------+\n");
139    PRINT(0)("+ TEXT ENGINE DEBUG INFORMATION +\n");
140    PRINT(0)("+-------------------------------+\n");
141    PRINT(0)("Reference: %p; Text Counts: %d\n", this, textList->getSize());
142
143    tIterator<BaseObject>* textIterator = textList->getIterator();
144    Text* text = dynamic_cast<Text*>(textIterator->firstElement());
145    while( text != NULL)
146      {
147        text->debug();
148        text = dynamic_cast<Text*>(textIterator->nextElement());
149      }
150    delete textIterator;
151    PRINT(0)("+---------------------------TE--+\n");
152  }
153}
154
155
156/**
157 *  checks if the compiled version and the local version of SDL_ttf match.
158 * @returns true if match, false otherwise
159*/
160bool TextEngine::checkVersion()
161{
162  SDL_version compile_version;
163  SDL_version link_version;
164  TTF_VERSION(&compile_version);
165  link_version = *TTF_Linked_Version();
166
167  if (compile_version.major == link_version.major &&
168      compile_version.minor == link_version.minor &&
169      compile_version.patch == link_version.patch)
170    {
171      return true;
172    }
173  else
174    {
175      PRINTF(2)("compiled with SDL_ttf version: %d.%d.%d\n",
176                compile_version.major,
177                compile_version.minor,
178                compile_version.patch);
179
180      PRINTF(2)("running with SDL_ttf version: %d.%d.%d\n",
181                link_version.major,
182                link_version.minor,
183                link_version.patch);
184      return false;
185    }
186}
Note: See TracBrowser for help on using the repository browser.