home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2009 February / maximum-cd-2009-02.iso / DiscContents / SMC_1.6_win32.exe / src / level / level_background.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2008-09-08  |  9.1 KB  |  372 lines

  1. /***************************************************************************
  2.  * level_background.cpp  -  level background image and color handling class
  3.  *
  4.  * Copyright (C) 2005 - 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 "../level/level_background.h"
  17. #include "../user/preferences.h"
  18. #include "../core/game_core.h"
  19. #include "../video/gl_surface.h"
  20. #include "../core/math/utilities.h"
  21.  
  22. /* *** *** *** *** *** *** *** cBackground *** *** *** *** *** *** *** *** *** *** */
  23.  
  24. cBackground :: cBackground( void )
  25. {
  26.     Init();
  27. }
  28.  
  29. cBackground :: cBackground( CEGUI::XMLAttributes &attributes )
  30. {
  31.     Init();
  32.     Create_from_Stream( attributes );
  33. }
  34.  
  35. cBackground :: ~cBackground( void )
  36. {
  37.     //
  38. }
  39.  
  40. void cBackground :: Init( void )
  41. {
  42.     type = BG_NONE;
  43.  
  44.     posx = 0;
  45.     posy = 0;
  46.     posz = 0.00011f;
  47.  
  48.     color_1 = static_cast<Uint8>(0);
  49.     color_2 = static_cast<Uint8>(0);
  50.  
  51.     img_file_1.reserve( 120 );
  52.     img_1 = NULL;
  53.  
  54.     speedx = 1;
  55.     speedy = 1;
  56.     const_velx = 0;
  57.     const_vely = 0;
  58. }
  59.  
  60. void cBackground :: Create_from_Stream( CEGUI::XMLAttributes &attributes )
  61. {
  62.     Set_Type( static_cast<BackgroundType>(attributes.getValueAsInteger( "type" )) );
  63.  
  64.     if( type == BG_GR_HOR || type == BG_GR_VER )
  65.     {
  66.         Set_Color_1( Color( static_cast<Uint8>(attributes.getValueAsInteger( "bg_color_1_red" )), attributes.getValueAsInteger( "bg_color_1_green" ), attributes.getValueAsInteger( "bg_color_1_blue" ) ) );
  67.         Set_Color_2( Color( static_cast<Uint8>(attributes.getValueAsInteger( "bg_color_2_red" )), attributes.getValueAsInteger( "bg_color_2_green" ), attributes.getValueAsInteger( "bg_color_2_blue" ) ) );
  68.     }
  69.     else if( type == BG_IMG_BOTTOM || type == BG_IMG_TOP || type == BG_IMG_ALL )
  70.     {
  71.         Set_Pos( attributes.getValueAsFloat( "posx" ), attributes.getValueAsFloat( "posy" ) );
  72.         Set_Pos_Z( attributes.getValueAsFloat( "posz" ) );
  73.  
  74.         Set_Image( attributes.getValueAsString( "image" ).c_str() );
  75.         Set_Scroll_Speed( attributes.getValueAsFloat( "speedx" ), attributes.getValueAsFloat( "speedy" ) );
  76.         Set_Const_Velocity_X( attributes.getValueAsFloat( "const_velx" ) );
  77.         Set_Const_Velocity_Y( attributes.getValueAsFloat( "const_vely" ) );
  78.     }
  79. }
  80.  
  81. void cBackground :: Save_to_Stream( ofstream &file )
  82. {
  83.     if( type == BG_NONE && img_file_1.length() <= 3 )
  84.     {
  85.         return;
  86.     }
  87.  
  88.     // begin background
  89.     file << "\t<background>" << std::endl;
  90.  
  91.     // type
  92.     file << "\t\t<Property name=\"type\" value=\"" << type << "\" />" << std::endl;
  93.  
  94.     // gradient
  95.     if( type == BG_GR_HOR || type == BG_GR_VER )
  96.     {
  97.         // background color 1
  98.         file << "\t\t<Property name=\"bg_color_1_red\" value=\"" << static_cast<int>(color_1.red) << "\" />" << std::endl;
  99.         file << "\t\t<Property name=\"bg_color_1_green\" value=\"" << static_cast<int>(color_1.green) << "\" />" << std::endl;
  100.         file << "\t\t<Property name=\"bg_color_1_blue\" value=\"" << static_cast<int>(color_1.blue) << "\" />" << std::endl;
  101.         // background color 2
  102.         file << "\t\t<Property name=\"bg_color_2_red\" value=\"" << static_cast<int>(color_2.red) << "\" />" << std::endl;
  103.         file << "\t\t<Property name=\"bg_color_2_green\" value=\"" << static_cast<int>(color_2.green) << "\" />" << std::endl;
  104.         file << "\t\t<Property name=\"bg_color_2_blue\" value=\"" << static_cast<int>(color_2.blue) << "\" />" << std::endl;
  105.     }
  106.     // image
  107.     else if( type == BG_IMG_BOTTOM || type == BG_IMG_TOP || type == BG_IMG_ALL )
  108.     {
  109.         // position
  110.         file << "\t\t<Property name=\"posx\" value=\"" << posx << "\" />" << std::endl;
  111.         file << "\t\t<Property name=\"posy\" value=\"" << posy << "\" />" << std::endl;
  112.         file << "\t\t<Property name=\"posz\" value=\"" << posz << "\" />" << std::endl;
  113.  
  114.         // image filename
  115.         file << "\t\t<Property name=\"image\" value=\"" << img_file_1 << "\" />" << std::endl;
  116.         // speed
  117.         file << "\t\t<Property name=\"speedx\" value=\"" << speedx << "\" />" << std::endl;
  118.         file << "\t\t<Property name=\"speedy\" value=\"" << speedy << "\" />" << std::endl;
  119.         // constant velocity
  120.         file << "\t\t<Property name=\"const_velx\" value=\"" << const_velx << "\" />" << std::endl;
  121.         file << "\t\t<Property name=\"const_vely\" value=\"" << const_vely << "\" />" << std::endl;
  122.     }
  123.     else
  124.     {
  125.         printf( "Error : Unknown Background Type %d\n", type );
  126.     }
  127.  
  128.     // end background
  129.     file << "\t</background>" << std::endl;
  130. }
  131.  
  132. void cBackground :: Set_Type( BackgroundType ntype )
  133. {
  134.     type = ntype;
  135. }
  136.  
  137. void cBackground :: Set_Type( string ntype ) 
  138. {
  139.     if( ntype.compare( "Disabled" ) == 0 )
  140.     {
  141.         type = BG_NONE;
  142.     }
  143.     else if( ntype.compare( "Bottom" ) == 0 )
  144.     {
  145.         type = BG_IMG_BOTTOM;
  146.     }
  147.     else if( ntype.compare( "All" ) == 0 )
  148.     {
  149.         type = BG_IMG_ALL;
  150.     }
  151.     else
  152.     {
  153.         printf( "Warning : Unknown Background type %s\n", ntype.c_str() );
  154.     }
  155. }
  156.  
  157. void cBackground :: Set_Color_1( const Color &color )
  158.     color_1 = color; 
  159. }
  160.  
  161. void cBackground :: Set_Color_2( const Color &color )
  162. {
  163.     color_2 = color; 
  164. }
  165.  
  166. void cBackground :: Set_Image( string nimg_file_1 )
  167. {
  168.     img_file_1 = nimg_file_1;
  169.  
  170.     // empty
  171.     if( img_file_1.empty() )
  172.     {
  173.         img_1 = NULL;
  174.         return;
  175.     }
  176.  
  177.     if( img_file_1.find( DATA_DIR "/" GAME_PIXMAPS_DIR "/" ) != string::npos )
  178.     {
  179.         img_file_1.erase( 0, strlen( DATA_DIR "/" GAME_PIXMAPS_DIR "/" ) );
  180.     }
  181.  
  182.     img_1 = pVideo->Get_Surface( img_file_1 );
  183. }
  184.  
  185. void cBackground :: Set_Scroll_Speed( float x /* = 1 */, float y /* = 1 */ )
  186. {
  187.     speedx = x;
  188.     speedy = y;
  189. }
  190.  
  191. void cBackground :: Set_Pos( float x, float y )
  192. {
  193.     posx = x;
  194.     posy = y;
  195. }
  196.  
  197. void cBackground :: Set_Pos_Z( float val )
  198. {
  199.     posz = val;
  200. }
  201.  
  202. void cBackground :: Set_Const_Velocity_X( float vel )
  203. {
  204.     const_velx = vel;
  205. }
  206.  
  207. void cBackground :: Set_Const_Velocity_Y( float vel )
  208. {
  209.     const_vely = vel;
  210. }
  211.  
  212. void cBackground :: Update( void )
  213. {
  214.     if( const_velx )
  215.     {
  216.         posx += const_velx;
  217.     }
  218.  
  219.     if( const_vely )
  220.     {
  221.         posy += const_vely;
  222.     }
  223. }
  224.  
  225. void cBackground :: Draw( void )
  226.     // gradient
  227.     if( type == BG_GR_VER || type == BG_GR_HOR )
  228.     {
  229.         Draw_Gradient();
  230.     }
  231.     // image
  232.     else if( type == BG_IMG_BOTTOM || type == BG_IMG_ALL ) 
  233.     {
  234.         // if background images are disabled or no image
  235.         if( !pPreferences->level_background_images || !img_1 )
  236.         {
  237.             return;
  238.         }
  239.  
  240.         // get position
  241.         float posx_final = posx - ( ( pActive_Camera->x * 0.2f ) * speedx );
  242.         float posy_final = posy - ( ( pActive_Camera->y * 0.3f ) * speedy );
  243.  
  244.         if( type == BG_IMG_BOTTOM || type == BG_IMG_ALL )
  245.         {
  246.             posy_final += game_res_h - img_1->h;
  247.         }
  248.  
  249.         // align start position x
  250.         // to right
  251.         while( posx_final < -img_1->w )
  252.         {
  253.             posx_final += img_1->w;
  254.         }
  255.         // to left
  256.         while( posx_final > 0 )
  257.         {
  258.             posx_final -= img_1->w;
  259.         }
  260.         // align start position y
  261.         if( type == BG_IMG_ALL )
  262.         {
  263.             // to top
  264.             while( posy_final > 0 )
  265.             {
  266.                 posy_final -= img_1->h;
  267.             }
  268.             // to bottom
  269.             while( posy_final < game_res_h - img_1->h )
  270.             {
  271.                 posy_final += img_1->h;
  272.             }
  273.         }
  274.  
  275.         // draw
  276.         while( posx_final < game_res_w )
  277.         {
  278.             // draw horizontal
  279.             img_1->Blit( posx_final, posy_final, posz );
  280.  
  281.             // draw vertical
  282.             if( type == BG_IMG_ALL )
  283.             {
  284.                 float posy_temp = posy_final;
  285.  
  286.                 while( posy_temp > -game_res_h )
  287.                 {
  288.                     img_1->Blit( posx_final, posy_temp, posz );
  289.  
  290.                     posy_temp -= img_1->h;
  291.                 }
  292.             }
  293.  
  294.             posx_final += img_1->w;
  295.         }
  296.     }
  297. }
  298.  
  299. void cBackground :: Draw_Gradient( void )
  300. {
  301.     pVideo->Clear_Screen();
  302.  
  303.     // no need to draw a gradient if both colors are the same
  304.     if( color_1 == color_2 )
  305.     {
  306.         pVideo->Draw_Rect( NULL, posz, &color_1 );
  307.     }
  308.     else
  309.     {
  310.         Color color, color_start;
  311.  
  312.         // set color start
  313.         color_start = color_1;
  314.  
  315.         if( pActive_Camera->y < -1 )
  316.         {
  317.             float power = ( -pActive_Camera->y / 10000 );
  318.  
  319.             if( power > 1 )
  320.             {
  321.                 power = 1;
  322.             }
  323.  
  324.             color_start.red += static_cast<Uint8>( static_cast<float>( color_2.red - color_1.red ) * power );
  325.             color_start.green += static_cast<Uint8>( static_cast<float>( color_2.green - color_1.green ) * power );
  326.             color_start.blue += static_cast<Uint8>( static_cast<float>( color_2.blue - color_1.blue ) * power );
  327.         }
  328.  
  329.         if( type == BG_GR_VER )
  330.         {
  331.             pVideo->Draw_Gradient( NULL, posz, &color_start, &color_2, DIR_VERTICAL );
  332.         }
  333.         else if( type == BG_GR_HOR )
  334.         {
  335.             pVideo->Draw_Gradient( NULL, posz, &color_start, &color_2, DIR_HORIZONTAL );
  336.         }
  337.     }
  338. }
  339.  
  340. string cBackground :: Get_Type_Name( void )
  341. {
  342.     if( type == BG_NONE )
  343.     {
  344.         return "Disabled";
  345.     }
  346.     else if( type == BG_IMG_BOTTOM )
  347.     {
  348.         return "Bottom";
  349.     }
  350.     else if( type == BG_IMG_ALL )
  351.     {
  352.         return "All";
  353.     }
  354.  
  355.     return "Unknown";
  356. };
  357.  
  358. /* *** *** *** *** *** *** cBackground_Manager *** *** *** *** *** *** *** *** *** *** *** */
  359.  
  360. cBackground_Manager :: cBackground_Manager( void )
  361. : cObject_Manager<cBackground>()
  362. {
  363.     //
  364. }
  365.  
  366. cBackground_Manager :: ~cBackground_Manager( void )
  367. {
  368.     Delete_All();
  369. }
  370.