FIFE  2008.0
 All Classes Namespaces Functions Variables Enumerations Enumerator Pages
image.h
1 /***************************************************************************
2  * Copyright (C) 2005-2008 by the FIFE team *
3  * http://www.fifengine.de *
4  * This file is part of FIFE. *
5  * *
6  * FIFE is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU Lesser General Public *
8  * License as published by the Free Software Foundation; either *
9  * version 2.1 of the License, or (at your option) any later version. *
10  * *
11  * This library is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14  * Lesser General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU Lesser General Public *
17  * License along with this library; if not, write to the *
18  * Free Software Foundation, Inc., *
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
20  ***************************************************************************/
21 
22 #ifndef FIFE_VIDEO_IMAGE_H
23 #define FIFE_VIDEO_IMAGE_H
24 
25 // Standard C++ library includes
26 #include <stack>
27 
28 // 3rd party library includes
29 #include <SDL.h>
30 #include <png.h>
31 
32 // FIFE includes
33 // These includes are split up in two parts, separated by one empty line
34 // First block: files included from the FIFE root src directory
35 // Second block: files included from the same folder
36 #include "util/base/fife_stdint.h"
37 #include "util/base/resourceclass.h"
38 #include "util/resource/resource.h"
39 #include "util/structures/point.h"
40 #include "util/structures/rect.h"
41 
42 namespace FIFE {
43 
44  class AbstractImage {
45  public:
46  virtual ~AbstractImage() {}
47 
51  virtual SDL_Surface* getSurface() = 0;
52 
57  virtual unsigned int getWidth() const = 0;
58 
61  virtual unsigned int getHeight() const = 0;
62 
66  virtual const Rect& getArea() = 0;
67 
70  virtual bool putPixel(int x, int y, int r, int g, int b, int a = 255) = 0;
71 
74  virtual void drawLine(const Point& p1, const Point& p2, int r, int g, int b, int a = 255) = 0;
75 
78  virtual void drawTriangle(const Point& p1, const Point& p2, const Point& p3, int r, int g, int b, int a = 255) = 0;
79 
82  virtual void drawRectangle(const Point& p, uint16_t w, uint16_t h, uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255) = 0;
83 
86  virtual void fillRectangle(const Point& p, uint16_t w, uint16_t h, uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255) = 0;
87 
90  virtual void drawQuad(const Point& p1, const Point& p2, const Point& p3, const Point& p4, int r, int g, int b, int a = 255) = 0;
91 
94  virtual void drawVertex(const Point& p, const uint8_t size, int r, int g, int b, int a = 255) = 0;
95 
98  virtual void drawLightPrimitive(const Point& p, uint8_t intensity, float radius, int subdivisions, float xstretch, float ystretch, uint8_t red, uint8_t green, uint8_t blue) = 0;
99 
102  virtual void getPixelRGBA(int x, int y, uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a) = 0;
103 
108  virtual void pushClipArea(const Rect& cliparea, bool clear=true) = 0;
109 
113  virtual void popClipArea() = 0;
114 
118  virtual const Rect& getClipArea() const = 0;
119 
122  virtual void saveImage(const std::string& filename) = 0;
123 
129  virtual void setAlphaOptimizerEnabled(bool enabled) = 0;
130 
133  virtual bool isAlphaOptimizerEnabled() = 0;
134  };
135 
138  class Image : public ResourceClass, public AbstractImage {
139  public:
144  Image(SDL_Surface* surface);
145 
151  Image(const uint8_t* data, unsigned int width, unsigned int height);
152 
155  virtual void invalidate() = 0;
156 
163  virtual void render(const Rect& rect, SDL_Surface* dst, unsigned char alpha = 255) = 0;
164 
170  void render(const Rect& rect, unsigned char alpha = 255);
171 
175  SDL_Surface* detachSurface();
176 
177  virtual ~Image();
178  SDL_Surface* getSurface() { return m_surface; }
179  unsigned int getWidth() const;
180  unsigned int getHeight() const;
181  const Rect& getArea();
182  void setXShift(int xshift);
183  inline int getXShift() const {
184  return m_xshift;
185  }
186  void setYShift(int yshift);
187  inline int getYShift() const {
188  return m_yshift;
189  }
190  void getPixelRGBA(int x, int y, uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a);
191  void pushClipArea(const Rect& cliparea, bool clear=true);
192  void popClipArea();
193  const Rect& getClipArea() const;
194  void setAlphaOptimizerEnabled(bool enabled) { m_isalphaoptimized = enabled; }
195  bool isAlphaOptimizerEnabled() { return m_isalphaoptimized; }
196 
197  protected:
201  virtual void setClipArea(const Rect& cliparea, bool clear) = 0;
202  //saves images to png format
203  virtual void saveAsPng(const std::string& filename, SDL_Surface& surface);
207  virtual void clearClipArea();
208 
209  // The SDL Surface used.
210  SDL_Surface* m_surface;
211  // The X shift of the Image
212  int m_xshift;
213  // The Y shift of the Image
214  int m_yshift;
215 
216  class ClipInfo {
217  public:
218  Rect r;
219  bool clearing;
220  };
221  std::stack<ClipInfo> m_clipstack;
222 
223  // image area
224  Rect m_area;
225  bool m_isalphaoptimized;
226 
227  private:
228  void reset(SDL_Surface* surface);
229  };
230 
231 }
232 
233 #endif