home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2009 February / maximum-cd-2009-02.iso / DiscContents / SMC_1.6_win32.exe / src / video / font.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2008-06-27  |  4.3 KB  |  177 lines

  1. /***************************************************************************
  2.  * font.cpp  -  internal font functions
  3.  *
  4.  * Copyright (C) 2006 - 2008 Florian Richter
  5.  ***************************************************************************/
  6. /*
  7.    This program is free software; you can redistribute it and/or modify
  8.    it under the terms of the GNU General Public License as published by
  9.    the Free Software Foundation; either version 3 of the License, or
  10.    (at your option) any later version.
  11.    
  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  14. */
  15.  
  16. #include "../video/font.h"
  17. #include "../video/gl_surface.h"
  18.  
  19. /* *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** */
  20.  
  21. void Font_Delete_Ref( GL_Surface *surface )
  22. {
  23.     pFont->Delete_Ref( surface );
  24. }
  25.  
  26. /* *** *** *** *** *** *** *** Font Manager class *** *** *** *** *** *** *** *** *** *** */
  27.  
  28. cFont_Manager :: cFont_Manager( void )
  29. {
  30.     font_normal = NULL;
  31.     font_small = NULL;
  32.     font_very_small = NULL;
  33. }
  34.  
  35. cFont_Manager :: ~cFont_Manager( void )
  36. {
  37.     if( TTF_WasInit() )
  38.     {
  39.         if( font_normal )
  40.         {
  41.             TTF_CloseFont( font_normal );
  42.             font_normal = NULL;
  43.         }
  44.  
  45.         if( font_small )
  46.         {
  47.             TTF_CloseFont( font_small );
  48.             font_small = NULL;
  49.         }
  50.  
  51.         if( font_very_small )
  52.         {
  53.             TTF_CloseFont( font_very_small );
  54.             font_very_small = NULL;
  55.         }
  56.  
  57.         TTF_Quit();
  58.     }
  59. }
  60.  
  61. void cFont_Manager :: Init( void )
  62. {
  63.     // if already initialised
  64.     if( TTF_WasInit() )
  65.     {
  66.         return;
  67.     }
  68.     
  69.     // init ttf
  70.     if( TTF_Init() == -1 )
  71.     {
  72.         printf( "Error : SDL_TTF initialization failed\nReason : %s\n", SDL_GetError() );
  73.         exit( EXIT_FAILURE );
  74.     }
  75.  
  76.     // open fonts
  77.     font_normal = TTF_OpenFont( DATA_DIR "/" GUI_FONT_DIR "/default_bold.ttf", 18 );
  78.     font_small = TTF_OpenFont( DATA_DIR "/" GUI_FONT_DIR "/default_bold.ttf", 11 );
  79.     font_very_small = TTF_OpenFont( DATA_DIR "/" GUI_FONT_DIR "/default_bold.ttf", 9 );
  80.  
  81.     // if loading failed
  82.     if( !font_normal || !font_small || !font_very_small )
  83.     {
  84.         printf( "Error : Font loading failed from directory %s\n", GUI_FONT_DIR );
  85.         exit( EXIT_FAILURE );
  86.     }
  87. }
  88.  
  89. void cFont_Manager :: Add_Ref( GL_Surface *surface )
  90. {
  91.     if( !surface )
  92.     {
  93.         return;
  94.     }
  95.  
  96.     active_fonts.push_back( surface );
  97. }
  98.  
  99. void cFont_Manager :: Delete_Ref( GL_Surface *surface )
  100. {
  101.     for( unsigned int i = 0; i < active_fonts.size(); i++ )
  102.     {
  103.         // delete reference if found
  104.         if( active_fonts[i] == surface )
  105.         {
  106.             active_fonts.erase( active_fonts.begin() + i );
  107.             return;
  108.         }
  109.     }
  110. }
  111.  
  112. GL_Surface *cFont_Manager :: Render_Text( TTF_Font *font, string text, Color color )
  113. {
  114.     // get SDL Color
  115.     SDL_Color sdlcolor = color.Get_SDL_Color();
  116.     // create text surface
  117.     GL_Surface *surface = pVideo->Create_Texture( TTF_RenderUTF8_Blended( font, text.c_str(), sdlcolor ) );
  118.  
  119.     if( !surface )
  120.     {
  121.         return NULL;
  122.     }
  123.  
  124.     surface->filename = text;
  125.  
  126.     // set function if font gets deleted
  127.     surface->Set_Destruction_Function( &Font_Delete_Ref );
  128.     // add font to active fonts
  129.     Add_Ref( surface );
  130.     
  131.     return surface;
  132. }
  133.  
  134. void cFont_Manager :: Grab_Textures( void )
  135. {
  136.     // save to software memory
  137.     for( ActiveFontList::iterator itr = active_fonts.begin(), itr_end = active_fonts.end(); itr != itr_end; ++itr )
  138.     {
  139.         GL_Surface *obj = (*itr);
  140.  
  141.         // get software texture and save it
  142.         software_textures.push_back( obj->Get_Software_Texture() );
  143.         // delete hardware texture
  144.         if( glIsTexture( obj->image ) )
  145.         {
  146.             glDeleteTextures( 1, &obj->image );
  147.         }
  148.         obj->image = 0;
  149.     }
  150. }
  151.  
  152. void cFont_Manager :: Restore_Textures( void )
  153. {
  154.     // load back into hardware textures
  155.     for( Saved_Texture_List::iterator itr = software_textures.begin(), itr_end = software_textures.end(); itr != itr_end; ++itr )
  156.     {
  157.         // get saved texture
  158.         cSaved_Texture *soft_tex = (*itr);
  159.         // load it
  160.         soft_tex->base->Load_Software_Texture( soft_tex );
  161.     }
  162.  
  163.     // delete software textures
  164.     for( Saved_Texture_List::iterator itr = software_textures.begin(), itr_end = software_textures.end(); itr != itr_end; ++itr )
  165.     {
  166.         delete *itr;
  167.     }
  168.  
  169.     software_textures.clear();
  170. }
  171.  
  172. /* *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** */
  173. namespace SMC
  174. { // work around conflicts caused by lack of namespace use in smc
  175. cFont_Manager *pFont = NULL;
  176. }
  177.