Debian Codegraph @

engine/core/video/image.h

raw
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
46 /** Base Class for Images.
47 */
48 class Image : public IResource {
49 public:
50 /** Constructor.
51 */
52 Image(IResourceLoader* loader = 0);
53 Image(const std::string& name, IResourceLoader* loader = 0);
54
55 /** Constructor.
56 * @note Takes ownership of the SDL Surface
57 * @param surface SDL Surface in RGBA format
58 */
59 Image(SDL_Surface* surface);
60 Image(const std::string& name, SDL_Surface* surface);
61
62 /** Constructor
63 * @param data Pointer to the imagedata (needs to be in RGBA, 8 bits per channel).
64 * @param width Width of the image.
65 * @param height Height of the image.
66 */
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
70 /** Destructor.
71 */
72 virtual ~Image();
73
74 /** Invalidates the Image causing it to be reset or re-loaded
75 */
76 virtual void invalidate() = 0;
77
78 /** Renders itself to the current render target (main screen or attached destination image) at the rectangle rect.
79 * Convenience function
80 * @param rect The position and clipping where to draw this image to.
81 * @param alpha The alpha value, with which to draw self.
82 * @param rgb The color value of overlay if any.
83 */
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
91 /** Removes underlying SDL_Surface from the image (if exists) and returns this
92 * @note this effectively causes SDL_Surface not to be freed on destruction
93 */
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
99 /** This frees the current suface and replaces it with the
100 * surface passed in the parameter (which can be NULL).
101 * @see Image::reset(SDL_Surface* surface)
102 * @param surface the SDL_Surface to use for this image
103 */
104 virtual void setSurface(SDL_Surface* surface) = 0;
105
106 /** Saves the image using given filename.
107 */
108 void saveImage(const std::string& filename);
109
110 /** Saves the SDL_Surface to png format
111 */
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
138 /** After this call all image data will be taken from the given image and its subregion
139 */
140 virtual void useSharedImage(const ImagePtr& shared, const Rect& region) = 0;
141
142 /** Forces to load the image into internal memory of GPU
143 */
144 virtual void forceLoadInternal() = 0;
145
146 /** Returns true if this image shares data with another one
147 */
148 bool isSharedImage() const { return m_shared; }
149
150 /** Returns area of the image it occupies in the shared image
151 */
152 const Rect& getSubImageRect() const { return m_subimagerect; }
153
154 /** Copies given image into this one with respect to given offsets
155 */
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
166 /** Resets the image to default values (including the x and y shift
167 * values), frees the current surface and sets the surface to the
168 * passed SDL_Surface (which can be NULL).
169 * @see IImage::setSurface(SDL_Surface* surface)
170 * @param surface the SDL_Surface to use for this image
171 */
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
177 Rect m_subimagerect;
178
179 private:
180 std::string createUniqueImageName();
181 };
182 }
183
184 #endif