aboutsummaryrefslogtreecommitdiff
path: root/graphics/managed_surface.h
blob: 8cbd4632669b1e1cfc731b724018b5308720fca9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#ifndef GRAPHICS_MANAGED_SURFACE_H
#define GRAPHICS_MANAGED_SURFACE_H

#include "graphics/pixelformat.h"
#include "graphics/surface.h"
#include "common/rect.h"
#include "common/types.h"

namespace Graphics {

class Font;

/**
 * A derived graphics surface, which handles automatically managing the allocated
 * surface data block, as well as introducing several new blitting methods
 */
class ManagedSurface {
	friend class Font;
private:
	/**
	 * The Graphics::Surface that the managed surface encapsulates
	 */
	Surface _innerSurface;

	/**
	 * If set, the inner surface will be freed when the surface is recreated,
	 * as well as when the surface is destroyed
	 */
	DisposeAfterUse::Flag _disposeAfterUse;

	/**
	 * Stores the owning surface if this If this managed surface represents
	 * a sub-section of another
	 */
	ManagedSurface *_owner;

	/**
	 * For sub-section areas of an owning parent managed surface, this represents
	 * the offset from the parent's top-left corner this sub-surface starts at
	 */
	Common::Point _offsetFromOwner;
protected:
	/**
	 * Clips the given source bounds so the passed destBounds will be entirely on-screen
	 */
	bool clip(Common::Rect &srcBounds, Common::Rect &destBounds);

	/**
	 * Base method that descendent classes can override for recording affected
	 * dirty areas of the surface
	 */
	virtual void addDirtyRect(const Common::Rect &r);
public:
	uint16 &w;
	uint16 &h;
	uint16 &pitch;
	PixelFormat &format;
public:
	/**
	 * Create the managed surface
	 */
	ManagedSurface();

	/**
	 * Create a managed surface from another one.
	 * If the source surface is maintaining it's own surface data, then
	 * this surface will create it's own surface of the same size and copy
	 * the contents from the source surface
	 */
	ManagedSurface(ManagedSurface &surf);

	/**
	 * Create the managed surface
	 */
	ManagedSurface(int width, int height);
	
	/**
	 * Create the managed surface
	 */
	ManagedSurface(int width, int height, const Graphics::PixelFormat &pixelFormat);
	
	/**
	 * Create the managed surface
	 */
	ManagedSurface(ManagedSurface &surf, const Common::Rect &bounds);

	/**
	 * Destroy the managed surface
	 */
	virtual ~ManagedSurface();

	/**
	 * Implements automatic conversion to a Graphics::Surface by
	 * simply returning the inner surface. This must be const,
	 * because we don't want changes being done directly to it,
	 * since it would bypass dirty rect handling
	 */
	operator const Surface &() const { return _innerSurface; }
	const Surface &rawSurface() const { return _innerSurface; }

	/**
	 * Reassign one managed surface to another one
	 * Note that if the source has a managed surface, it will be duplicated
	 */
	ManagedSurface &operator=(ManagedSurface &surf);

	/**
	 * Returns true if the surface has not yet been allocated
	 */
	bool empty() const { return w == 0 || h == 0 || _innerSurface.getPixels() == nullptr; }

	/**
	 * Returns true if the surface is managing its own pixels
	 */
	DisposeAfterUse::Flag disposeAfterUse() const { return _disposeAfterUse; }

	/**
	 * Return a pointer to the pixel at the specified point.
	 *
	 * @param x The x coordinate of the pixel.
	 * @param y The y coordinate of the pixel.
	 * @return Pointer to the pixel.
	 */
	inline const void *getBasePtr(int x, int y) const {
		return _innerSurface.getBasePtr(x, y);
	}

	/**
	 * Return a pointer to the pixel at the specified point.
	 *
	 * @param x The x coordinate of the pixel.
	 * @param y The y coordinate of the pixel.
	 * @return Pointer to the pixel.
	 */
	inline void *getBasePtr(int x, int y) {
		return _innerSurface.getBasePtr(x, y);
	}

	/**
	 * Get a reference to the pixel data
	 */
	inline void *getPixels() { return _innerSurface.getPixels(); }
	inline const void *getPixels() const { return _innerSurface.getPixels(); }

	/**
	 * Sets the pixel data.
	 */
	virtual void setPixels(void *newPixels);

	/**
	 * Allocate memory for the pixel data of the surface.
	 */
	virtual void create(uint16 width, uint16 height);

	/**
	 * Allocate memory for the pixel data of the surface.
	 */
	virtual void create(uint16 width, uint16 height, const PixelFormat &pixelFormat);

	/**
	 * Sets up the surface as a sub-section of another passed parent surface. This surface
	 * will not own the pixels, and any dirty rect notifications will automatically be
	 * passed to the original parent surface.
	 * @remarks		Note that this differs from Graphics::Surface::getSubArea, in that that
	 * method only adds a single initial dirty rect for the whole area, and then none further
	 */
	virtual void create(ManagedSurface &surf, const Common::Rect &bounds);

	/**
	 * Release the memory used by the pixels memory of this surface. This is the
	 * counterpart to create().
	 */
	virtual void free();

	/**
	 * Clears any pending dirty rects that have been generated for the surface
	 */
	virtual void clearDirtyRects() {}

	/**
	 * When the managed surface is a sub-section of a parent surface, returns the
	 * the offset in the parent surface that the surface starts at
	 */
	const Common::Point getOffsetFromOwner() const { return _offsetFromOwner; }

	/**
	 * Return a rect giving the bounds of the surface
	 */
	const Common::Rect getBounds() const {
		return Common::Rect(0, 0, this->w, this->h);
	}
	
	/**
	 * Copies another surface into this one
	 */
	void blitFrom(const Surface &src);

	/**
	 * Copies another surface into this one at a given destination position
	 */
	void blitFrom(const Surface &src, const Common::Point &destPos);

	/**
	 * Copies another surface into this one at a given destination position
	 */
	void blitFrom(const Surface &src, const Common::Rect &srcRect,
		const Common::Point &destPos);

	/**
	 * Copies another surface into this one ignoring pixels of a designated transparent color
	 * @param src			Source surface
	 * @param transColor	Transparency color to ignore copying
	 * @param flipped		Specifies whether to horizontally flip the image
	 * @param overrideColor	Optional color to use instead of non-transparent pixels from
	 *						the source surface
	 */
	void transBlitFrom(const Surface &src, uint transColor = 0, bool flipped = false, uint overrideColor = 0);

	/**
	 * Copies another surface into this one ignoring pixels of a designated transparent color
	 * @param src			Source surface
	 * @param destPos		Destination position to draw the surface
	 * @param transColor	Transparency color to ignore copying
	 * @param flipped		Specifies whether to horizontally flip the image
	 * @param overrideColor	Optional color to use instead of non-transparent pixels from
	 *						the source surface
	 */
	void transBlitFrom(const Surface &src, const Common::Point &destPos,
		uint transColor = 0, bool flipped = false, uint overrideColor = 0);

	/**
	 * Copies another surface into this one ignoring pixels of a designated transparent color
	 * @param src			Source surface
	 * @param srcRect		Sub-section of source surface to draw
	 * @param destPos		Destination position to draw the surface
	 * @param transColor	Transparency color to ignore copying
	 * @param flipped		Specifies whether to horizontally flip the image
	 * @param overrideColor	Optional color to use instead of non-transparent pixels from
	 *						the source surface
	 */
	void transBlitFrom(const Surface &src, const Common::Rect &srcRect, const Common::Point &destPos,
		uint transColor = 0, bool flipped = false, uint overrideColor = 0);

	/**
	 * Copies another surface into this one ignoring pixels of a designated transparent color
	 * @param src			Source surface
	 * @param srcRect		Sub-section of source surface to draw
	 * @param destRect		Destination area to draw the surface in. This can be sized differently
	 *						then srcRect, allowing for arbitrary scaling of the image
	 * @param transColor	Transparency color to ignore copying
	 * @param flipped		Specifies whether to horizontally flip the image
	 * @param overrideColor	Optional color to use instead of non-transparent pixels from
	 *						the source surface
	 */
	void transBlitFrom(const Surface &src, const Common::Rect &srcRect, const Common::Rect &destRect,
		uint transColor = 0, bool flipped = false, uint overrideColor = 0);

	/**
	 * Clear the entire surface
	 */
	void clear(uint color = 0);

	/**
	 * Mark the entire surface as dirty
	 */
	void markAllDirty();

	/**
	 * Copies a bitmap to the Surface internal buffer. The pixel format
	 * of buffer must match the pixel format of the Surface.
	 */
	void copyRectToSurface(const void *buffer, int srcPitch, int destX, int destY, int width, int height) {
		_innerSurface.copyRectToSurface(buffer, srcPitch, destX, destY, width, height);
	}

	/**
	 * Copies a bitmap to the Surface internal buffer. The pixel format
	 * of buffer must match the pixel format of the Surface.
	 */
	void copyRectToSurface(const Graphics::Surface &srcSurface, int destX, int destY, const Common::Rect subRect) {
		_innerSurface.copyRectToSurface(srcSurface, destX, destY, subRect);
	}

	/**
	 * Copy the data from another Surface, reinitializing the
	 * surface to match the dimensions of the passed surface
	 */
	void copyFrom(const ManagedSurface &surf) {
		clearDirtyRects();
		_innerSurface.copyFrom(surf._innerSurface);
	}

	/**
	 * Draw a line.
	 */
	void drawLine(int x0, int y0, int x1, int y1, uint32 color) {
		_innerSurface.drawLine(x0, y0, x1, y1, color);
		addDirtyRect(Common::Rect(x0, y0, x1, y1));
	}

	/**
	 * Draw a thick line.
	 */
	void drawThickLine(int x0, int y0, int x1, int y1, int penX, int penY, uint32 color) {
		_innerSurface.drawThickLine(x0, y0, x1, y1, penX, penY, color);
		addDirtyRect(Common::Rect(x0, y0, x1 + penX, y1 + penY));
	}

	/**
	 * Draw a horizontal line.
	 */
	void hLine(int x, int y, int x2, uint32 color) {
		_innerSurface.hLine(x, y, x2, color);
		addDirtyRect(Common::Rect(x, y, x2 + 1, y + 1));
	}

	/**
	 * Draw a vertical line.
	 */
	void vLine(int x, int y, int y2, uint32 color) {
		_innerSurface.vLine(x, y, y2, color);
		addDirtyRect(Common::Rect(x, y, x + 1, y2 + 1));
	}

	/**
	 * Fill a rect with a given color.
	 */
	void fillRect(Common::Rect r, uint32 color) {
		_innerSurface.fillRect(r, color);
		addDirtyRect(r);
	}

	/**
	 * Draw a frame around a specified rect.
	 */
	void frameRect(const Common::Rect &r, uint32 color) {
		_innerSurface.frameRect(r, color);
		addDirtyRect(r);
	}

	/**
	 * Returns a sub-area of the screen, but only adds a single initial dirty rect
	 * for the retrieved area.
	 */
	Surface getSubArea(const Common::Rect &area) {
		addDirtyRect(area);
		return _innerSurface.getSubArea(area);
	}
};

} // End of namespace Graphics


#endif