Advanced Strategic Command
surface.h
Go to the documentation of this file.
1 /*
2  This file is part of Advanced Strategic Command; http://www.asc-hq.de
3  Copyright (C) 1994-2010 Martin Bickel and Marc Schellenberger
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program; see the file COPYING. If not, write to the
17  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  Boston, MA 02111-1307 USA
19 */
20 
21 #ifndef surfaceH
22  #define surfaceH
23 
24  #include "../libs/sdlmm/src/sdlmm.h"
25  #include "../basestreaminterface.h"
26 
28 
30 class DI_Color : public SDL_Color {
31 public:
32  DI_Color();
33  DI_Color(const SDL_Color& c);
34  DI_Color(Uint32 c);
35  DI_Color(Uint8 r, Uint8 g, Uint8 b);
36 
37  DI_Color& operator=(const SDL_Color& c);
38 
39  DI_Color& operator=(Uint32 c);
40 
41  // operator Uint32() const;
42 
43  inline Uint32 MapRGB(SDL_PixelFormat* format) const {
44  return SDL_MapRGB(format, r, g, b);
45  }
46 
47  inline Uint32 MapRGBA(SDL_PixelFormat* format, Uint8 a) const {
48  return SDL_MapRGBA(format, r, g, b, a);
49  }
50 
51  inline bool operator!=(const DI_Color& c) const {
52  return !operator==(c);// ((r != c.r) || (g != c.g) || (b != c.b));
53  }
54 
55  inline bool operator==(const DI_Color& c) const {
56  return ((r == c.r) && (g == c.g) && (b == c.b));
57  }
58 
59 };
60 
61 
62  class Surface: public SDLmm::Surface {
63  void* pixelDataPointer; // in some situations
64  public:
65  static const Uint32 transparent = 0;
66  static const Uint32 opaque = 255l;
67  explicit Surface( SDL_Surface *surface);
68  Surface(const SDLmm::Surface& other);
69  Surface() : SDLmm::Surface(NULL), pixelDataPointer(NULL) {};
70 
71  Surface Duplicate() const;
72 
73  static Surface createSurface( int width, int height, SDLmm::Color color = 255 );
74  static Surface createSurface( int width, int height, int depth, SDLmm::Color color = 0xff0000ff );
75 
76  static Surface Wrap( SDL_Surface *surface) { surface->refcount++; return Surface(surface);};
77 
78  static void SetScreen( SDL_Surface* screen );
79 
83  void newFromBGI( void* img );
84  void* toBGI() const;
85 
86  void FillTransparent();
87 
88  void read ( tnstream& stream ) ;
89  void readImageFile ( tnstream& stream ) ;
90  void write ( tnstream& stream ) const;
91  void strech ( int width, int height );
92 
93  void writeDefaultPixelFormat ( tnstream& stream ) ;
94  static void readDefaultPixelFormat ( tnstream& stream );
95 
97  void assignDefaultPalette();
98 
99  void assignPalette(SDL_Color* colors, int startColor = 0, int colorNum = 256 );
100 
102  void detectColorKey( bool RLE = false );
103 
104  bool isTransparent( SDLmm::Color col ) const;
105 
106  void ColorKey2AlphaChannel() ;
107 
108  bool hasAlpha();
109  /*
110  SDLmm::ColorRGB GetRGB(SDLmm::Color pixel) const;
111  SDLmm::ColorRGBA GetRGBA(SDLmm::Color pixel) const;
112  */
113 
114  int getMemoryFootprint() const;
115 
116  SDL_Surface* getBaseSurface() { return me; };
117  const SDL_Surface* getBaseSurface() const { return me; };
118  ~Surface();
119  protected:
120  virtual int getDepthFormat() { return -1; };
121  void convert();
122 
123  private:
124  static SDLmm::PixelFormat* default8bit;
125  static SDLmm::PixelFormat* default32bit;
126 
127  };
128 
129  class TypedSurfaceBase : public Surface{
130  protected:
131  explicit TypedSurfaceBase( SDL_Surface *surface) : Surface(surface) {};
132  TypedSurfaceBase(const SDLmm::Surface& other) : Surface( other ) {};
133  TypedSurfaceBase() : Surface(NULL) {};
134  };
135 
136  template<int colorDepth> class TypedSurface : public TypedSurfaceBase {
137  public:
138  static const int depth = colorDepth;
139  explicit TypedSurface( SDL_Surface *surface) : TypedSurfaceBase(surface) {};
140 
142  explicit TypedSurface( SDLmm::Surface& surface , int depthCheck ) : TypedSurfaceBase(surface) {
143  assert ( surface.GetPixelFormat().BytesPerPixel() == depth );
144  assert ( depthCheck == depth );
145  };
146 
149  protected:
150  virtual int getDepthFormat() { return depth; };
151  };
152 
155 
156 
157  template<int depth> TypedSurface<depth>& castSurface( Surface& s ) {
158  assert ( s.GetPixelFormat().BytesPerPixel() == depth );
159  return static_cast<TypedSurface<depth>& >(s);
160  };
161 
162 
163  void applyFieldMask( Surface& s, int x = 0, int y = 0, bool detectColorKey = true );
164 
166  void applyLegacyFieldMask( Surface& s, int x = 0, int y = 0, bool detectColorKey = false );
167 
168  Surface rotateSurface( Surface& s, int degrees );
169 
170 class SurfaceLock {
171  Surface& surf;
172  public:
173  SurfaceLock( Surface& s ) : surf(s) { s.Lock(); };
174  ~SurfaceLock() { surf.Unlock(); };
175 };
176 
177 
178 
179 #endif
180 
const SDL_Surface * getBaseSurface() const
Definition: surface.h:117
TypedSurface< 1 > Surface8
Definition: surface.h:153
A Device Independent color. Shamelessly pinched from Paragui to reduce coupling.
Definition: surface.h:30
TypedSurface< 4 > Surface32
Definition: surface.h:154
void applyLegacyFieldMask(Surface &s, int x=0, int y=0, bool detectColorKey=false)
applies a field mask that uses FEFEFE Color as Colorkey to load old images
Definition: surface.cpp:611
static const Uint32 transparent
Definition: surface.h:65
TypedSurface< depth > & castSurface(Surface &s)
Definition: surface.h:157
static void readDefaultPixelFormat(tnstream &stream)
Definition: surface.cpp:205
static Surface createSurface(int width, int height, SDLmm::Color color=255)
Definition: surface.cpp:387
void FillTransparent()
Definition: surface.cpp:419
The interface for all kinds of IO stream.
void convert()
Definition: surface.cpp:151
void strech(int width, int height)
Definition: surface.cpp:457
bool operator==(const DI_Color &c) const
Definition: surface.h:55
SDL_Surface * getBaseSurface()
Definition: surface.h:116
void ColorKey2AlphaChannel()
Definition: surface.cpp:734
Uint32 MapRGBA(SDL_PixelFormat *format, Uint8 a) const
Definition: surface.h:47
TypedSurface(SDLmm::Surface &surface, int depthCheck)
the parameter depthcheck is primarily there to prevent accidential usage of this constructor ...
Definition: surface.h:142
void read(tnstream &stream)
Definition: surface.cpp:265
void write(tnstream &stream) const
Definition: surface.cpp:218
Uint32 MapRGB(SDL_PixelFormat *format) const
Definition: surface.h:43
virtual int getDepthFormat()
Definition: surface.h:120
TypedSurfaceBase(const SDLmm::Surface &other)
Definition: surface.h:132
static void SetScreen(SDL_Surface *screen)
Definition: surface.cpp:78
~Surface()
Definition: surface.cpp:531
static const Uint32 opaque
Definition: surface.h:66
bool hasAlpha()
Definition: surface.cpp:476
SDLmm::Color Color
Definition: blitter.h:35
TypedSurface()
Definition: surface.h:148
bool operator!=(const DI_Color &c) const
Definition: surface.h:51
SDLmm::SPoint SPoint
Definition: surface.h:27
bool isTransparent(SDLmm::Color col) const
Definition: surface.cpp:515
void assignDefaultPalette()
assigns the default ASC palette to the surface (only for 8 Bit surfaces)
Definition: surface.cpp:429
void newFromBGI(void *img)
Creates an image from an BGI image structure.
DI_Color & operator=(const SDL_Color &c)
Definition: surface.cpp:51
TypedSurface(SDL_Surface *surface)
Definition: surface.h:139
void * toBGI() const
Definition: surface.cpp:702
SurfaceLock(Surface &s)
Definition: surface.h:173
static Surface Wrap(SDL_Surface *surface)
Definition: surface.h:76
Surface()
Definition: surface.h:69
DI_Color()
Definition: surface.cpp:33
Surface rotateSurface(Surface &s, int degrees)
Definition: surface.cpp:674
TypedSurface(const TypedSurface< colorDepth > &other)
Definition: surface.h:147
void readImageFile(tnstream &stream)
Definition: surface.cpp:380
virtual int getDepthFormat()
Definition: surface.h:150
void assignPalette(SDL_Color *colors, int startColor=0, int colorNum=256)
Definition: surface.cpp:444
int getMemoryFootprint() const
Definition: surface.cpp:716
void writeDefaultPixelFormat(tnstream &stream)
Definition: surface.cpp:211
~SurfaceLock()
Definition: surface.h:174
void applyFieldMask(Surface &s, int x=0, int y=0, bool detectColorKey=true)
Definition: surface.cpp:593
SDL_Surface * screen
Definition: graphics.cpp:31
TypedSurfaceBase(SDL_Surface *surface)
Definition: surface.h:131
void detectColorKey(bool RLE=false)
tries to automatically detect the color key of the surface
Definition: surface.cpp:493
Surface Duplicate() const
Definition: surface.cpp:85
const int colorDepth
Definition: basegfx.h:46
static const int depth
Definition: surface.h:138