Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

surface.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-2003  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 #ifndef surfaceH
00022  #define surfaceH
00023 
00024  #include "../libs/sdlmm/src/sdlmm.h"
00025  #include "../basestreaminterface.h"
00026 
00027  typedef SDLmm::SPoint SPoint;
00028 
00030 class DI_Color : public SDL_Color {
00031 public:
00032         DI_Color();
00033         DI_Color(const SDL_Color& c);
00034         DI_Color(Uint32 c);
00035         DI_Color(Uint8 r, Uint8 g, Uint8 b);
00036 
00037         DI_Color& operator=(const SDL_Color& c);
00038 
00039         DI_Color& operator=(Uint32 c);
00040 
00041         // operator Uint32() const;
00042 
00043         inline Uint32 MapRGB(SDL_PixelFormat* format) const {
00044                 return SDL_MapRGB(format, r, g, b);
00045         }
00046 
00047         inline Uint32 MapRGBA(SDL_PixelFormat* format, Uint8 a) const {
00048                 return SDL_MapRGBA(format, r, g, b, a);
00049         }
00050 
00051         inline bool operator!=(const DI_Color& c) const {
00052       return !operator==(c);// ((r != c.r) || (g != c.g) || (b != c.b));
00053         }
00054    
00055    inline bool operator==(const DI_Color& c) const {
00056       return ((r == c.r) && (g == c.g) && (b == c.b));
00057    }
00058    
00059 };
00060 
00061 
00062  class Surface: public SDLmm::Surface {
00063       void* pixelDataPointer; // in some situations
00064     public:
00065       static const Uint32 transparent = 0;
00066       static const Uint32 opaque = 255l;
00067       explicit Surface( SDL_Surface *surface);
00068       Surface(const SDLmm::Surface& other);
00069       Surface() : SDLmm::Surface(NULL), pixelDataPointer(NULL) {};
00070 
00071       Surface Duplicate() const;
00072 
00073       static Surface createSurface( int width, int height, SDLmm::Color color = 255 );
00074       static Surface createSurface( int width, int height, int depth, SDLmm::Color color = 0xff0000ff );
00075       
00076       static Surface Wrap( SDL_Surface *surface) { surface->refcount++; return Surface(surface);};
00077       
00078       static void SetScreen( SDL_Surface* screen );
00079       
00083       void  newFromBGI( void* img );
00084       void* toBGI() const;
00085 
00086       void FillTransparent();
00087       
00088       void read ( tnstream& stream ) ;
00089       void readImageFile ( tnstream& stream ) ;
00090       void write ( tnstream& stream ) const;
00091       void strech ( int width, int height );
00092 
00093       void writeDefaultPixelFormat ( tnstream& stream ) ;
00094       static void readDefaultPixelFormat ( tnstream& stream );
00095 
00097       void assignDefaultPalette();
00098 
00099       void assignPalette(SDL_Color* colors, int startColor = 0, int colorNum = 256 );
00100 
00102       void detectColorKey( bool RLE = false );
00103 
00104       bool isTransparent( SDLmm::Color col ) const;
00105 
00106       void ColorKey2AlphaChannel() ;
00107 
00108       bool hasAlpha();
00109       /*
00110       SDLmm::ColorRGB GetRGB(SDLmm::Color pixel) const;
00111       SDLmm::ColorRGBA GetRGBA(SDLmm::Color pixel) const;
00112       */
00113 
00114       int getMemoryFootprint() const;
00115       
00116       SDL_Surface* getBaseSurface() { return me; };
00117       const SDL_Surface* getBaseSurface() const { return me; };
00118       ~Surface();
00119    protected:
00120       virtual int getDepthFormat() { return -1; };
00121       void convert();
00122            
00123    private:
00124       static SDLmm::PixelFormat* default8bit;
00125       static SDLmm::PixelFormat* default32bit;
00126 
00127  };
00128 
00129  class TypedSurfaceBase  : public Surface{
00130     protected:
00131       explicit TypedSurfaceBase( SDL_Surface *surface) : Surface(surface) {};
00132       TypedSurfaceBase(const SDLmm::Surface& other) : Surface( other ) {};
00133       TypedSurfaceBase() : Surface(NULL) {};
00134  };     
00135  
00136  template<int colorDepth> class TypedSurface : public TypedSurfaceBase {
00137     public:
00138       static const int depth = colorDepth;
00139       explicit TypedSurface( SDL_Surface *surface) : TypedSurfaceBase(surface) {};
00140       
00142       explicit TypedSurface( SDLmm::Surface& surface , int depthCheck ) : TypedSurfaceBase(surface) {
00143          assert ( surface.GetPixelFormat().BytesPerPixel() == depth );
00144          assert ( depthCheck == depth );
00145       };
00146       
00147       TypedSurface(const TypedSurface<colorDepth>& other) : TypedSurfaceBase( other ) {};
00148       TypedSurface() : TypedSurfaceBase(NULL) {};
00149    protected:
00150       virtual int getDepthFormat() { return depth; };
00151  };
00152  
00153  typedef TypedSurface<1> Surface8;
00154  typedef TypedSurface<4> Surface32;
00155  
00156  
00157  template<int depth> TypedSurface<depth>& castSurface( Surface& s ) {
00158     assert ( s.GetPixelFormat().BytesPerPixel() == depth );
00159     return static_cast<TypedSurface<depth>& >(s);
00160  };
00161  
00162  
00163  void applyFieldMask( Surface& s, int x = 0, int y = 0, bool detectColorKey = true );
00164  
00166  void applyLegacyFieldMask( Surface& s, int x = 0, int y = 0, bool detectColorKey = false );
00167 
00168  Surface rotateSurface( Surface& s, int degrees );
00169 
00170 class SurfaceLock {
00171       Surface& surf;
00172    public:
00173       SurfaceLock( Surface& s ) : surf(s) { s.Lock(); };
00174       ~SurfaceLock() { surf.Unlock(); };
00175 };
00176 
00177  
00178  
00179 #endif
00180 

Generated on Tue Jun 24 01:27:52 2008 for Advanced Strategic Command by  doxygen 1.4.2