FIFE  2008.0
 All Classes Namespaces Functions Variables Enumerations Enumerator Pages
image.h
1 /***************************************************************************
2  * Copyright (C) 2005-2011 by the FIFE team *
3  * http://www.fifengine.net *
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/resource/resource.h"
38 #include "util/structures/point.h"
39 #include "util/structures/rect.h"
40 
41 namespace FIFE {
42  class Image;
43  typedef SharedPtr<Image> ImagePtr;
44 
47  class Image : public IResource {
48  public:
51  Image(IResourceLoader* loader = 0);
52  Image(const std::string& name, IResourceLoader* loader = 0);
53 
58  Image(SDL_Surface* surface);
59  Image(const std::string& name, SDL_Surface* surface);
60 
66  Image(const uint8_t* data, uint32_t width, uint32_t height);
67  Image(const std::string& name, const uint8_t* data, uint32_t width, uint32_t height);
68 
71  virtual ~Image();
72 
75  virtual void invalidate() = 0;
76 
83  virtual void render(const Rect& rect, uint8_t alpha = 255, uint8_t const* rgb = 0) = 0;
84  virtual void renderZ(const Rect& rect, float vertexZ, uint8_t alpha = 255, bool forceNewBatch = false, uint8_t const* rgb = 0) {}
85 
89  SDL_Surface* detachSurface();
90 
91  SDL_Surface* getSurface() { assert(m_surface); return m_surface; }
92  const SDL_Surface* getSurface() const { assert(m_surface); return m_surface; }
93 
99  virtual void setSurface(SDL_Surface* surface) = 0;
100 
103  void saveImage(const std::string& filename);
104 
107  static void saveAsPng(const std::string& filename, const SDL_Surface& surface);
108  static bool putPixel(SDL_Surface* surface, int32_t x, int32_t y, uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255);
109 
110  uint32_t getWidth() const;
111  uint32_t getHeight() const;
112  const Rect& getArea() const;
113 
114  void setXShift(int32_t xshift) {
115  m_xshift = xshift;
116  }
117  int32_t getXShift() const {
118  return m_xshift;
119  }
120  void setYShift(int32_t yshift) {
121  m_yshift = yshift;
122  }
123  int32_t getYShift() const {
124  return m_yshift;
125  }
126 
127  void getPixelRGBA(int32_t x, int32_t y, uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a);
128 
129  virtual size_t getSize();
130  virtual void load();
131  virtual void free();
132 
135  virtual void useSharedImage(const ImagePtr& shared, const Rect& region) = 0;
136 
139  virtual void forceLoadInternal() = 0;
140 
143  bool isSharedImage() const { return m_shared; }
144 
147  const Rect& getSubImageRect() const { return m_subimagerect; }
148 
151  virtual void copySubimage(uint32_t xoffset, uint32_t yoffset, const ImagePtr& img);
152 
153  protected:
154  // The SDL Surface used.
155  SDL_Surface* m_surface;
156  // The X shift of the Image
157  int32_t m_xshift;
158  // The Y shift of the Image
159  int32_t m_yshift;
160 
167  void reset(SDL_Surface* surface);
168 
169  // Does this image share data with another
170  bool m_shared;
171  // Area which this image occupy in shared image
172  Rect m_subimagerect;
173 
174  private:
175  std::string createUniqueImageName();
176  };
177 }
178 
179 #endif
Image(IResourceLoader *loader=0)
Definition: image.cpp:41
void reset(SDL_Surface *surface)
Definition: image.cpp:110
void saveImage(const std::string &filename)
Definition: image.cpp:222
virtual void invalidate()=0
const Rect & getSubImageRect() const
Definition: image.h:147
SDL_Surface * detachSurface()
Definition: image.cpp:140
static void saveAsPng(const std::string &filename, const SDL_Surface &surface)
Definition: image.cpp:226
virtual ~Image()
Definition: image.cpp:120
virtual void copySubimage(uint32_t xoffset, uint32_t yoffset, const ImagePtr &img)
Definition: image.cpp:310
virtual void setSurface(SDL_Surface *surface)=0
bool isSharedImage() const
Definition: image.h:143
virtual void render(const Rect &rect, uint8_t alpha=255, uint8_t const *rgb=0)=0
virtual void forceLoadInternal()=0
virtual void useSharedImage(const ImagePtr &shared, const Rect &region)=0