FIFE
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
image.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2013 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 render(const Rect& rect, const ImagePtr& overlay, uint8_t alpha = 255, uint8_t const* rgb = 0) {}
85  virtual void renderZ(const Rect& rect, float vertexZ, uint8_t alpha = 255, uint8_t const* rgb = 0) {}
86  virtual void renderZ(const Rect& rect, float vertexZ, const ImagePtr& overlay, uint8_t alpha = 255, uint8_t const* rgb = 0) {}
87 
88  virtual void renderZ(const Rect& rect, float vertexZ, uint8_t alpha = 255, bool forceNewBatch = false, uint8_t const* rgb = 0) {}
89 
93  SDL_Surface* detachSurface();
94 
95  SDL_Surface* getSurface() { assert(m_surface); return m_surface; }
96  const SDL_Surface* getSurface() const { assert(m_surface); return m_surface; }
97 
103  virtual void setSurface(SDL_Surface* surface) = 0;
104 
107  void saveImage(const std::string& filename);
108 
111  static void saveAsPng(const std::string& filename, const SDL_Surface& surface);
112  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);
113 
114  uint32_t getWidth() const;
115  uint32_t getHeight() const;
116  const Rect& getArea() const;
117 
118  void setXShift(int32_t xshift) {
119  m_xshift = xshift;
120  }
121  int32_t getXShift() const {
122  return m_xshift;
123  }
124  void setYShift(int32_t yshift) {
125  m_yshift = yshift;
126  }
127  int32_t getYShift() const {
128  return m_yshift;
129  }
130 
131  void getPixelRGBA(int32_t x, int32_t y, uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a);
132 
133  virtual size_t getSize();
134  virtual void load();
135  virtual void free();
136 
139  virtual void useSharedImage(const ImagePtr& shared, const Rect& region) = 0;
140 
143  virtual void forceLoadInternal() = 0;
144 
147  bool isSharedImage() const { return m_shared; }
148 
151  const Rect& getSubImageRect() const { return m_subimagerect; }
152 
155  virtual void copySubimage(uint32_t xoffset, uint32_t yoffset, const ImagePtr& img);
156 
157  protected:
158  // The SDL Surface used.
159  SDL_Surface* m_surface;
160  // The X shift of the Image
161  int32_t m_xshift;
162  // The Y shift of the Image
163  int32_t m_yshift;
164 
171  void reset(SDL_Surface* surface);
172 
173  // Does this image share data with another
174  bool m_shared;
175  // Area which this image occupy in shared image
177 
178  private:
179  std::string createUniqueImageName();
180  };
181 }
182 
183 #endif
std::string createUniqueImageName()
Definition: image.cpp:301
Image(IResourceLoader *loader=0)
Constructor.
Definition: image.cpp:41
void reset(SDL_Surface *surface)
Resets the image to default values (including the x and y shift values), frees the current surface an...
Definition: image.cpp:110
virtual void renderZ(const Rect &rect, float vertexZ, const ImagePtr &overlay, uint8_t alpha=255, uint8_t const *rgb=0)
Definition: image.h:86
void saveImage(const std::string &filename)
Saves the image using given filename.
Definition: image.cpp:227
virtual void invalidate()=0
Invalidates the Image causing it to be reset or re-loaded.
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)
Definition: image.cpp:362
virtual void load()
Definition: image.cpp:124
void setXShift(int32_t xshift)
Definition: image.h:118
const SDL_Surface * getSurface() const
Definition: image.h:96
Base Class for Images.
Definition: image.h:47
int32_t m_xshift
Definition: image.h:161
SDL_Surface * m_surface
Definition: image.h:159
const Rect & getSubImageRect() const
Returns area of the image it occupies in the shared image.
Definition: image.h:151
int32_t getXShift() const
Definition: image.h:121
SDL_Surface * detachSurface()
Removes underlying SDL_Surface from the image (if exists) and returns this.
Definition: image.cpp:145
static void saveAsPng(const std::string &filename, const SDL_Surface &surface)
Saves the SDL_Surface to png format.
Definition: image.cpp:231
void getPixelRGBA(int32_t x, int32_t y, uint8_t *r, uint8_t *g, uint8_t *b, uint8_t *a)
Definition: image.cpp:181
int32_t getYShift() const
Definition: image.h:127
SharedPtr< Image > ImagePtr
Definition: image.h:42
const Rect & getArea() const
Definition: image.cpp:176
virtual size_t getSize()
Definition: image.cpp:169
virtual ~Image()
Destructor.
Definition: image.cpp:120
uint32_t getHeight() const
Definition: image.cpp:160
unsigned char uint8_t
Definition: core.h:38
virtual void render(const Rect &rect, const ImagePtr &overlay, uint8_t alpha=255, uint8_t const *rgb=0)
Definition: image.h:84
SDL_Surface * getSurface()
Definition: image.h:95
virtual void copySubimage(uint32_t xoffset, uint32_t yoffset, const ImagePtr &img)
Copies given image into this one with respect to given offsets.
Definition: image.cpp:315
virtual void setSurface(SDL_Surface *surface)=0
This frees the current suface and replaces it with the surface passed in the parameter (which can be ...
virtual void renderZ(const Rect &rect, float vertexZ, uint8_t alpha=255, uint8_t const *rgb=0)
Definition: image.h:85
uint32_t getWidth() const
Definition: image.cpp:151
Rect m_subimagerect
Definition: image.h:176
bool isSharedImage() const
Returns true if this image shares data with another one.
Definition: image.h:147
virtual void render(const Rect &rect, uint8_t alpha=255, uint8_t const *rgb=0)=0
Renders itself to the current render target (main screen or attached destination image) at the rectan...
virtual void free()
Definition: image.cpp:135
bool m_shared
Definition: image.h:174
virtual void forceLoadInternal()=0
Forces to load the image into internal memory of GPU.
virtual void useSharedImage(const ImagePtr &shared, const Rect &region)=0
After this call all image data will be taken from the given image and its subregion.
unsigned int uint32_t
Definition: core.h:40
void setYShift(int32_t yshift)
Definition: image.h:124
virtual void renderZ(const Rect &rect, float vertexZ, uint8_t alpha=255, bool forceNewBatch=false, uint8_t const *rgb=0)
Definition: image.h:88
int32_t m_yshift
Definition: image.h:163