comparison engine/core/video/image.h @ 0:4a0efb7baf70

* Datasets becomes the new trunk and retires after that :-)
author mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
date Sun, 29 Jun 2008 18:44:17 +0000
parents
children 90005975cdbb
comparison
equal deleted inserted replaced
-1:000000000000 0:4a0efb7baf70
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 modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program 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 *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the *
18 * Free Software Foundation, Inc., *
19 * 51 Franklin St, 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 class Rect;
44
45 class AbstractImage {
46 public:
47 virtual ~AbstractImage() {}
48
49 /** Get the surface used by this image
50 * @return pointer to used surface
51 */
52 virtual SDL_Surface* getSurface() = 0;
53
54 /** Returns the @b maximum width occupied by this image.
55 * This should return the maximum width that could be covered by the
56 * image.
57 */
58 virtual unsigned int getWidth() const = 0;
59
60 /** Returns the @b maximum height occupied by this image.
61 */
62 virtual unsigned int getHeight() const = 0;
63
64 /** Gets the area of the image
65 * @return Image area in rect
66 */
67 virtual const Rect& getArea() = 0;
68
69 /** Writes pixel to given position. Returns true, if pixel was written (not out of bounds)
70 */
71 virtual bool putPixel(int x, int y, int r, int g, int b) = 0;
72
73 /** Draws line between given points with given RGBA
74 */
75 virtual void drawLine(const Point& p1, const Point& p2, int r, int g, int b) = 0;
76
77 /** Draws quad between given points with given RGBA
78 */
79 virtual void drawQuad(const Point& p1, const Point& p2, const Point& p3, const Point& p4, int r, int g, int b) = 0;
80
81 /** Returns pixel RGBA values from given position
82 */
83 virtual void getPixelRGBA(int x, int y, uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a) = 0;
84
85 /** Pushes clip area to clip stack
86 * Clip areas define which area is drawn on screen. Usable e.g. with viewports
87 * note that previous items in stack do not affect the latest area pushed
88 */
89 virtual void pushClipArea(const Rect& cliparea, bool clear=true) = 0;
90
91 /** Pops clip area from clip stack
92 * @see pushClipArea
93 */
94 virtual void popClipArea() = 0;
95
96 /** Gets the current clip area
97 * @see pushClipArea
98 */
99 virtual const Rect& getClipArea() const = 0;
100
101 /** Saves the image using given filename
102 */
103 virtual void saveImage(const std::string& filename) = 0;
104
105 /** Enable or disable the alpha 'optimizing' code
106 *
107 * @param optimize Wether the image shall be analysed for 'fake' alpha images.
108 * Only implemented by and useful for the SDL backend at the moment.
109 */
110 virtual void setAlphaOptimizerEnabled(bool enabled) = 0;
111
112 /** @see setAlphaOptimizerEnabled
113 */
114 virtual bool isAlphaOptimizerEnabled() = 0;
115 };
116
117 /** Base Class for Images.
118 */
119 class Image : public ResourceClass, public AbstractImage {
120 public:
121 /** Constructor.
122 * @note Takes ownership of the SDL Surface
123 * @param surface SDL Surface in RGBA format
124 */
125 Image(SDL_Surface* surface);
126
127 /** Constructor
128 * @param data Pointer to the imagedata (needs to be in RGBA, 8 bits per channel).
129 * @param width Width of the image.
130 * @param height Height of the image.
131 */
132 Image(const uint8_t* data, unsigned int width, unsigned int height);
133
134 //TODO: fill in these stub! Note that m_location is not properly initialized.
135 virtual const ResourceLocation& getResourceLocation() { return m_location; }
136
137 virtual void setResourceLocation(const ResourceLocation& location) { }
138 virtual void setResourceFile(const std::string& filename) { }
139
140 /** Renders itself to the Destination surface at the rectangle rect.
141 *
142 * @param rect The position and clipping where to draw this image to.
143 * @param target Target surface to draw to, e.g. main screen or other image
144 * @param alpha The alpha value, with which to draw self. opaque by default.
145 */
146 virtual void render(const Rect& rect, SDL_Surface* dst, unsigned char alpha = 255) = 0;
147
148 /** Renders itself to the main screen at the rectangle rect.
149 * Convenience function
150 * @param rect The position and clipping where to draw this image to.
151 * @param alpha The alpha value, with which to draw self.
152 */
153 void render(const Rect& rect, unsigned char alpha = 255);
154
155 /** Removes underlying SDL_Surface from the image (if exists) and returns this
156 * @note this effectively causes SDL_Surface not to be freed on destruction
157 */
158 SDL_Surface* detachSurface();
159
160 virtual ~Image();
161 SDL_Surface* getSurface() { return m_surface; }
162 unsigned int getWidth() const;
163 unsigned int getHeight() const;
164 const Rect& getArea();
165 void setXShift(int xshift);
166 inline int getXShift() const {
167 return m_xshift;
168 }
169 void setYShift(int yshift);
170 inline int getYShift() const {
171 return m_yshift;
172 }
173 void getPixelRGBA(int x, int y, uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a);
174 void pushClipArea(const Rect& cliparea, bool clear=true);
175 void popClipArea();
176 const Rect& getClipArea() const;
177 void setAlphaOptimizerEnabled(bool enabled) { m_isalphaoptimized = enabled; }
178 bool isAlphaOptimizerEnabled() { return m_isalphaoptimized; }
179
180 protected:
181 /** Sets given clip area into image
182 * @see pushClipArea
183 */
184 virtual void setClipArea(const Rect& cliparea, bool clear) = 0;
185 //saves images to png format
186 virtual void saveAsPng(const std::string& filename, SDL_Surface& surface);
187 /** Clears any possible clip areas
188 * @see pushClipArea
189 */
190 virtual void clearClipArea();
191
192 ResourceLocation m_location;
193
194 // The SDL Surface used.
195 SDL_Surface* m_surface;
196 // The X shift of the Image
197 int m_xshift;
198 // The Y shift of the Image
199 int m_yshift;
200
201 class ClipInfo {
202 public:
203 Rect r;
204 bool clearing;
205 };
206 std::stack<ClipInfo> m_clipstack;
207
208 // image area
209 Rect m_area;
210 bool m_isalphaoptimized;
211
212 private:
213 void reset(SDL_Surface* surface);
214 };
215
216 }
217
218 #endif