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

  1. /***************************************************************************
  2.  * img_manager.cpp  -  Image Handler/Manager
  3.  *
  4.  * Copyright (C) 2003 - 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/img_manager.h"
  17. #include "../video/renderer.h"
  18. #include "../core/i18n.h"
  19.  
  20. /* *** *** *** *** *** cSaved_Texture *** *** *** *** *** *** *** *** *** *** *** *** */
  21.  
  22. cSaved_Texture :: cSaved_Texture( void )
  23. {
  24.     base = NULL;
  25.     pixels = NULL;
  26.  
  27.     width = 0;
  28.     height = 0;
  29.     format = 0;
  30.  
  31.     min_filter = 0;
  32.     mag_filter = 0;
  33.     wrap_s = 0;
  34.     wrap_t = 0;
  35. }
  36.  
  37. cSaved_Texture :: ~cSaved_Texture( void )
  38. {
  39.     if( pixels )
  40.     {
  41.         delete[] pixels;
  42.     }
  43. }
  44.  
  45. /* *** *** *** *** *** *** cImage_Manager *** *** *** *** *** *** *** *** *** *** *** */
  46.  
  47. cImage_Manager :: cImage_Manager( void )
  48. : cObject_Manager<GL_Surface>()
  49. {
  50.     high_texture_id = 0;
  51. }
  52.  
  53. cImage_Manager :: ~cImage_Manager( void )
  54. {
  55.     Delete_All();
  56. }
  57.  
  58. void cImage_Manager :: Add( GL_Surface *obj )
  59. {
  60.     if( !obj )
  61.     {
  62.         return;
  63.     }
  64.  
  65.     // it is now managed
  66.     obj->managed = 1;
  67.  
  68.     // Add
  69.     cObject_Manager<GL_Surface>::Add( obj );
  70. }
  71.  
  72. GL_Surface *cImage_Manager :: Get_Pointer( string path )
  73. {
  74.     for( GL_Surface_List::iterator itr = objects.begin(), itr_end = objects.end(); itr != itr_end; ++itr )
  75.     {
  76.         GL_Surface *obj = (*itr);
  77.  
  78.         // return first match
  79.         if( obj->filename.compare( path ) == 0 )
  80.         {
  81.             return obj;
  82.         }
  83.     }
  84.  
  85.     // not found
  86.     return NULL;
  87. }
  88.  
  89. GL_Surface *cImage_Manager :: Copy( string path )
  90. {
  91.     for( GL_Surface_List::iterator itr = objects.begin(), itr_end = objects.end(); itr != itr_end; ++itr )
  92.     {
  93.         // get object
  94.         GL_Surface *obj = (*itr);
  95.  
  96.         // first match
  97.         if( obj->filename.compare( path ) == 0 )
  98.         {
  99.             return obj->Copy();
  100.         }
  101.     }
  102.  
  103.     // not found
  104.     return NULL;
  105. }
  106.  
  107. void cImage_Manager :: Grab_Textures( bool from_file /* = 0 */, bool draw_gui /* = 0 */ )
  108. {
  109.     // progress bar
  110.     CEGUI::ProgressBar *progress_bar = NULL;
  111.  
  112.     if( draw_gui )
  113.     {
  114.         // get progress bar
  115.         progress_bar = static_cast<CEGUI::ProgressBar *>(CEGUI::WindowManager::getSingleton().getWindow( "progress_bar" ));
  116.         progress_bar->setProgress( 0 );
  117.         // set loading screen text
  118.         Loading_Screen_Draw_Text( _("Saving Textures") );
  119.     }
  120.  
  121.     unsigned int loaded_files = 0;
  122.     unsigned int file_count = objects.size();
  123.  
  124.     // save all textures
  125.     for( GL_Surface_List::iterator itr = objects.begin(), itr_end = objects.end(); itr != itr_end; ++itr )
  126.     {
  127.         // get surface
  128.         GL_Surface *obj = (*itr);
  129.  
  130.         // skip surfaces with an already deleted texture
  131.         if( !glIsTexture( obj->image ) )
  132.         {
  133.             continue;
  134.         }
  135.  
  136.         // get software texture and save it to software memory
  137.         saved_textures.push_back( obj->Get_Software_Texture( from_file ) );
  138.         // delete hardware texture
  139.         if( glIsTexture( obj->image ) )
  140.         {
  141.             glDeleteTextures( 1, &obj->image );
  142.         }
  143.  
  144.         // count files
  145.         loaded_files++;
  146.  
  147.         // draw
  148.         if( draw_gui )
  149.         {
  150.             // update progress
  151.             progress_bar->setProgress( static_cast<float>(loaded_files) / static_cast<float>(file_count) );
  152.  
  153.             // clear screen
  154.             pVideo->Clear_Screen();
  155.             pVideo->Draw_Rect( NULL, 0.01f, &black );
  156.  
  157.             // Render
  158.             pRenderer->Render();
  159.             pGuiSystem->renderGUI();
  160.             pRenderer_GUI->Render();
  161.             SDL_GL_SwapBuffers();
  162.         }
  163.     }
  164. }
  165.  
  166. void cImage_Manager :: Restore_Textures( bool draw_gui /* = 0 */ )
  167. {
  168.     // progress bar
  169.     CEGUI::ProgressBar *progress_bar = NULL;
  170.  
  171.     if( draw_gui )
  172.     {
  173.         // get progress bar
  174.         progress_bar = static_cast<CEGUI::ProgressBar *>(CEGUI::WindowManager::getSingleton().getWindow( "progress_bar" ));
  175.         progress_bar->setProgress( 0 );
  176.         // set loading screen text
  177.         Loading_Screen_Draw_Text( _("Restoring Textures") );
  178.     }
  179.  
  180.     unsigned int loaded_files = 0;
  181.     unsigned int file_count = saved_textures.size();
  182.  
  183.     // load back into hardware textures
  184.     for( Saved_Texture_List::iterator itr = saved_textures.begin(), itr_end = saved_textures.end(); itr != itr_end; ++itr )
  185.     {
  186.         // get saved texture
  187.         cSaved_Texture *soft_tex = (*itr);
  188.  
  189.         // load it
  190.         soft_tex->base->Load_Software_Texture( soft_tex );
  191.         // delete
  192.         delete soft_tex;
  193.  
  194.         // count files
  195.         loaded_files++;
  196.  
  197.         // draw
  198.         if( draw_gui )
  199.         {
  200.             // update progress
  201.             progress_bar->setProgress( static_cast<float>(loaded_files) / static_cast<float>(file_count) );
  202.  
  203.             // clear screen
  204.             pVideo->Clear_Screen();
  205.             pVideo->Draw_Rect( NULL, 0.01f, &black );
  206.  
  207.             // Render
  208.             pRenderer->Render();
  209.             pGuiSystem->renderGUI();
  210.             pRenderer_GUI->Render();
  211.             SDL_GL_SwapBuffers();
  212.         }
  213.     }
  214.  
  215.     saved_textures.clear();
  216. }
  217.  
  218. void cImage_Manager :: Delete_Image_Textures( void )
  219. {
  220.     for( GL_Surface_List::iterator itr = objects.begin(), itr_end = objects.end(); itr != itr_end; ++itr ) 
  221.     {
  222.         // get object
  223.         GL_Surface *obj = (*itr);
  224.  
  225.         if( obj->auto_del_img && glIsTexture( obj->image ) )
  226.         {
  227.             glDeleteTextures( 1, &obj->image );
  228.         }
  229.     }
  230. }
  231.  
  232. void cImage_Manager :: Delete_Hardware_Textures( void )
  233. {
  234.     // delete all hardware surfaces
  235.     for( GLuint i = 0; i < high_texture_id; i++ )
  236.     {
  237.         if( glIsTexture( i ) )
  238.         {
  239.             printf( "ImageManager : deleting texture %d\n", i );
  240.             glDeleteTextures( 1, &i );
  241.         }
  242.     }
  243.  
  244.     high_texture_id = 0;
  245. }
  246.  
  247. void cImage_Manager :: Delete_All( void )
  248. {
  249.     // stops GL_Surface destructor from checking if GL texture id still in use
  250.     Delete_Image_Textures();
  251.     cObject_Manager<GL_Surface>::Delete_All();
  252. }
  253.  
  254. /* *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** */
  255.  
  256. cImage_Manager *pImage_Manager = NULL;
  257.