ColorTransform_PlayerColor.h

Go to the documentation of this file.
00001 /*
00002     This file is part of Advanced Strategic Command; http://www.asc-hq.de
00003     Copyright (C) 1994-2010  Martin Bickel  and  Marc Schellenberger
00004  
00005     This program is free software; you can redistribute it and/or modify
00006     it under the terms of the GNU General Public License as published by
00007     the Free Software Foundation; either version 2 of the License, or
00008     (at your option) any later version.
00009  
00010     This program is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013     GNU General Public License for more details.
00014  
00015     You should have received a copy of the GNU General Public License
00016     along with this program; see the file COPYING. If not, write to the
00017     Free Software Foundation, Inc., 59 Temple Place, Suite 330,
00018     Boston, MA  02111-1307  USA
00019 */
00020 
00021 
00022 #ifndef ColorTransform_PlayerColorH
00023 #define ColorTransform_PlayerColorH
00024 
00025 
00026 #include "../playercolor.h"
00027 #include "colorizer.h"
00028 
00029 template<int pixelsize>
00030 class ColorTransform_PlayerCol
00031 {
00032       typedef typename PixelSize2Type<pixelsize>::PixelType PixelType;
00033    protected:
00034       ColorTransform_PlayerCol()
00035       {}
00036       ;
00037 
00038       PixelType transform( PixelType col)
00039       {
00040          return col;
00041       };
00042 
00043       void init( const Surface& src )
00044       {}
00045       ;
00046 
00047    public:
00048       ColorTransform_PlayerCol( const PlayerColor& player )
00049       {}
00050       ;
00051       ColorTransform_PlayerCol ( NullParamType npt )
00052       {}
00053       ;
00054 
00055       void setPlayer( int player )
00056       {}
00057       ;
00058 };
00059 
00060 template<>
00061 class ColorTransform_PlayerCol<1>
00062 {
00063       int shift;
00064       typedef PixelSize2Type<1>::PixelType PixelType;
00065 
00066    protected:
00067       ColorTransform_PlayerCol() : shift(0)
00068       {}
00069       ;
00070 
00071       PixelType transform( PixelType col)
00072       {
00073          if ( col >= 16 && col < 24 )
00074             return col + shift;
00075          else
00076             return col;
00077       };
00078 
00079       void init( const Surface& src )
00080    {}
00081       ;
00082 
00083    public:
00084       ColorTransform_PlayerCol( const PlayerColor& player )
00085       {
00086          setPlayer( player.getNum() );
00087       };
00088 
00089       ColorTransform_PlayerCol ( NullParamType npt ) : shift(0)
00090       {}
00091       ;
00092 
00093       void setPlayer( int player )
00094       {
00095          shift = player*8;
00096       };
00097 };
00098 
00099 
00100 template<int pixelsize>
00101 class ColorTransform_PlayerTrueCol
00102 {
00103       typedef typename PixelSize2Type<pixelsize>::PixelType PixelType;
00104       bool lateConversion;
00105       DI_Color sourceColor;
00106 
00107       PixelType refColor;
00108       int refr, refg, refb;  // this is NOT RED, GREEN, BLUE, but device dependant. Should be renamed
00109    protected:
00110       ColorTransform_PlayerTrueCol() : refColor(0),refr(0),refg(0),refb(0)
00111       {}
00112       ;
00113 
00114       PixelType transform( PixelType col)
00115       {
00116          int r = (col >> 16) & 0xff;
00117          int g = (col >> 8) & 0xff;
00118          int b = (col ) & 0xff;
00119 
00120          if ( g==0 && b==0) {
00121             return ((refr * r / 256) << 16) + ((refg * r / 256) << 8) + (refb * r / 256) + (col & 0xff000000);
00122          } else
00123             if ( r==255 && g==b ) {
00124                return ((refr + ( 255-refr) * g / 255) << 16) + ((refg + ( 255-refg) * g / 255) << 8) + (refb + ( 255-refb) * g / 255) + (col & 0xff000000);
00125             } else
00126                return col;
00127       };
00128 
00129       void init( const Surface& src )
00130       {
00131          if ( lateConversion ) {
00132             setColor( src.GetPixelFormat().MapRGB( sourceColor ));
00133             lateConversion = false;
00134          }
00135       }
00136 
00137    public:
00138       ColorTransform_PlayerTrueCol( PixelType color ) : lateConversion( false )
00139       {
00140          setColor(color);
00141       };
00142       ColorTransform_PlayerTrueCol( DI_Color color ) : lateConversion( false )
00143       {
00144          setColor(color);
00145       };
00146       ColorTransform_PlayerTrueCol ( NullParamType npt ) : lateConversion( false ) , refColor(0),refr(0),refg(0),refb(0)
00147       {}
00148       ;
00149 
00150       void setColor( PixelType color )
00151       {
00152          refColor = color;
00153          refr = (color >> 16) & 0xff;
00154          refg = (color >> 8) & 0xff;
00155          refb = (color ) & 0xff;
00156       };
00157 
00158       void setColor( DI_Color color )
00159       {
00160          lateConversion = true;
00161          sourceColor = color;
00162       };
00163 };
00164 
00165 template<int pixelsize>
00166 class ColorTransform_PlayerTrueColHSV
00167 {
00168       typedef typename PixelSize2Type<pixelsize>::PixelType PixelType;
00169       int player;
00170       
00171       int rshift;
00172       int gshift;
00173       int bshift;
00174       int ashift;
00175    protected:
00176       ColorTransform_PlayerTrueColHSV() : player(0)
00177       {}
00178       ;
00179 
00180       PixelType transform( PixelType col)
00181       {
00182          if ( (col >> ashift) != Surface::transparent ) {
00183             int r = (col >> rshift) & 0xff;
00184             int g = (col >> gshift) & 0xff;
00185             int b = (col >> bshift) & 0xff;
00186    
00187             DI_Color d = colorSwitch.switchC( player,r,g,b);
00188             return (d.r << rshift) + (d.g << gshift) + (d.b << bshift)  + (col & (0xff << ashift));
00189          } else
00190             return col;
00191 
00192       };
00193 
00194 
00195    public:
00196       ColorTransform_PlayerTrueColHSV( int playerNum )  : player ( playerNum )
00197       {
00198       };
00199       ColorTransform_PlayerTrueColHSV ( NullParamType npt ) : player(0)
00200       {};
00201 
00202       void setPlayer( int playerNum )
00203       {
00204          player = playerNum;
00205       }
00206       
00207       void init( const Surface& src )
00208       {
00209          rshift = src.GetPixelFormat().Rshift();
00210          gshift = src.GetPixelFormat().Gshift();
00211          bshift = src.GetPixelFormat().Bshift();
00212          ashift = src.GetPixelFormat().Ashift();
00213       }
00214 
00215 };
00216 
00217   template<>
00218   class ColorTransform_PlayerCol<4> : public ColorTransform_PlayerTrueCol<4> {
00219      
00220      public:
00221         ColorTransform_PlayerCol( const PlayerColor& player ) : ColorTransform_PlayerTrueCol<4>( player.getColor() )
00222         {
00223         };
00224    };
00225 
00226 
00227 
00228 #endif

Generated on Mon May 14 01:31:44 2012 for Advanced Strategic Command by  doxygen 1.5.1