FIFE  be64c707dea6b3250bd4355bf5c825d25920087d
image.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2019 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 #define PNG_SKIP_SETJMP_CHECK
31 #include <png.h>
32 
33 // FIFE includes
34 // These includes are split up in two parts, separated by one empty line
35 // First block: files included from the FIFE root src directory
36 // Second block: files included from the same folder
37 #include "util/base/fife_stdint.h"
38 #include "util/resource/resource.h"
39 #include "util/structures/point.h"
40 #include "util/structures/rect.h"
41 
42 namespace FIFE {
43  class Image;
44  typedef SharedPtr<Image> ImagePtr;
45 
48  class Image : public IResource {
49  public:
52  Image(IResourceLoader* loader = 0);
53  Image(const std::string& name, IResourceLoader* loader = 0);
54 
59  Image(SDL_Surface* surface);
60  Image(const std::string& name, SDL_Surface* surface);
61 
67  Image(const uint8_t* data, uint32_t width, uint32_t height);
68  Image(const std::string& name, const uint8_t* data, uint32_t width, uint32_t height);
69 
72  virtual ~Image();
73 
76  virtual void invalidate() = 0;
77 
84  virtual void render(const Rect& rect, uint8_t alpha = 255, uint8_t const* rgb = 0) = 0;
85  virtual void render(const Rect& rect, const ImagePtr& overlay, uint8_t alpha = 255, uint8_t const* rgb = 0) {}
86  virtual void renderZ(const Rect& rect, float vertexZ, uint8_t alpha = 255, uint8_t const* rgb = 0) {}
87  virtual void renderZ(const Rect& rect, float vertexZ, const ImagePtr& overlay, uint8_t alpha = 255, uint8_t const* rgb = 0) {}
88 
89  virtual void renderZ(const Rect& rect, float vertexZ, uint8_t alpha = 255, bool forceNewBatch = false, uint8_t const* rgb = 0) {}
90 
94  SDL_Surface* detachSurface();
95 
96  SDL_Surface* getSurface() { assert(m_surface); return m_surface; }
97  const SDL_Surface* getSurface() const { assert(m_surface); return m_surface; }
98 
104  virtual void setSurface(SDL_Surface* surface) = 0;
105 
108  void saveImage(const std::string& filename);
109 
112  static void saveAsPng(const std::string& filename, const SDL_Surface& surface);
113  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);
114 
115  uint32_t getWidth() const;
116  uint32_t getHeight() const;
117  Rect getArea() const;
118 
119  void setXShift(int32_t xshift) {
120  m_xshift = xshift;
121  }
122  int32_t getXShift() const {
123  return m_xshift;
124  }
125  void setYShift(int32_t yshift) {
126  m_yshift = yshift;
127  }
128  int32_t getYShift() const {
129  return m_yshift;
130  }
131 
132  void getPixelRGBA(int32_t x, int32_t y, uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a);
133 
134  virtual size_t getSize();
135  virtual void load();
136  virtual void free();
137 
140  virtual void useSharedImage(const ImagePtr& shared, const Rect& region) = 0;
141 
144  virtual void forceLoadInternal() = 0;
145 
148  bool isSharedImage() const { return m_shared; }
149 
152  const Rect& getSubImageRect() const { return m_subimagerect; }
153 
156  virtual void copySubimage(uint32_t xoffset, uint32_t yoffset, const ImagePtr& img);
157 
158  protected:
159  // The SDL Surface used.
160  SDL_Surface* m_surface;
161  // The X shift of the Image
162  int32_t m_xshift;
163  // The Y shift of the Image
164  int32_t m_yshift;
165 
172  void reset(SDL_Surface* surface);
173 
174  // Does this image share data with another
175  bool m_shared;
176  // Area which this image occupy in shared image
178 
179  private:
180  std::string createUniqueImageName();
181  };
182 }
183 
184 #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:87
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:364
virtual void load()
Definition: image.cpp:124
void setXShift(int32_t xshift)
Definition: image.h:119
Base Class for Images.
Definition: image.h:48
int32_t m_xshift
Definition: image.h:162
SDL_Surface * m_surface
Definition: image.h:160
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
Rect getArea() const
Definition: image.cpp:176
int32_t getYShift() const
Definition: image.h:128
SharedPtr< Image > ImagePtr
Definition: image.h:43
virtual size_t getSize()
Definition: image.cpp:169
virtual ~Image()
Destructor.
Definition: image.cpp:120
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:85
SDL_Surface * getSurface()
Definition: image.h:96
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
uint32_t getHeight() const
Definition: image.cpp:160
int32_t getXShift() const
Definition: image.h:122
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:86
Rect m_subimagerect
Definition: image.h:177
bool isSharedImage() const
Returns true if this image shares data with another one.
Definition: image.h:148
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:175
virtual void forceLoadInternal()=0
Forces to load the image into internal memory of GPU.
const Rect & getSubImageRect() const
Returns area of the image it occupies in the shared image.
Definition: image.h:152
const SDL_Surface * getSurface() const
Definition: image.h:97
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:125
virtual void renderZ(const Rect &rect, float vertexZ, uint8_t alpha=255, bool forceNewBatch=false, uint8_t const *rgb=0)
Definition: image.h:89
uint32_t getWidth() const
Definition: image.cpp:151
int32_t m_yshift
Definition: image.h:164