aboutsummaryrefslogtreecommitdiff
path: root/engines/zvision/graphics/render_manager.h
blob: cb71308bc371e6b8b8d6a587a8b46dd9ccfc9f37 (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
/* 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 ZVISION_RENDER_MANAGER_H
#define ZVISION_RENDER_MANAGER_H

#include "zvision/graphics/render_table.h"
#include "zvision/fonts/truetype_font.h"

#include "common/rect.h"
#include "common/hashmap.h"

#include "graphics/surface.h"


class OSystem;

namespace Common {
class String;
class SeekableReadStream;
}

namespace Video {
class VideoDecoder;
}

namespace ZVision {

class RenderManager {
public:
	RenderManager(OSystem *system, uint32 windowWidth, uint32 windowHeight, const Common::Rect workingWindow, const Graphics::PixelFormat pixelFormat);
	~RenderManager();

private:
	struct AlphaDataEntry {
		Graphics::Surface *data;
		uint16 alphaColor;
		uint16 destX;
		uint16 destY;
		uint16 width;
		uint16 height;
	};

	typedef Common::HashMap<uint32, AlphaDataEntry> AlphaEntryMap;

private:
	OSystem *_system;
	const Graphics::PixelFormat _pixelFormat;

	// A buffer the exact same size as the workingWindow
	// This buffer stores everything un-warped, then does a warp at the end of the frame
	Graphics::Surface _workingWindowBuffer;
	// A buffer representing the entire screen. Any graphical updates are first done with this buffer
	// before actually being blitted to the screen
	Graphics::Surface _backBuffer;
	// A list of Alpha Entries that need to be blitted to the backbuffer
	AlphaEntryMap _alphaDataEntries;

	// A rectangle representing the portion of the working window where the pixels have been changed since last frame
	Common::Rect _workingWindowDirtyRect;
	// A rectangle representing the portion of the backbuffer where the pixels have been changed since last frame
	Common::Rect _backBufferDirtyRect;

	/** Width of the working window. Saved to prevent extraneous calls to _workingWindow.width() */
	const int _workingWidth;
	/** Height of the working window. Saved to prevent extraneous calls to _workingWindow.height() */
	const int _workingHeight;
	/** Center of the screen in the x direction */
	const int _screenCenterX;
	/** Center of the screen in the y direction */
	const int _screenCenterY;

	/** 
	 * A Rectangle centered inside the actual window. All in-game coordinates
	 * are given in this coordinate space. Also, all images are clipped to the
	 * edges of this Rectangle
	 */
	const Common::Rect _workingWindow;
	/** Used to warp the background image */
	RenderTable _renderTable;

	Graphics::Surface _currentBackground;
	/** The (x1,y1) coordinates of the subRectangle of the background that is currently displayed on the screen */
	Common::Point _backgroundOffset;
	/** The width of the current background image */
	uint16 _backgroundWidth;
	/** The height of the current background image */
	uint16 _backgroundHeight;

	/** 
	 * The "velocity" at which the background image is panning. We actually store the inverse of velocity (ms/pixel instead of pixels/ms)
	 * because it allows you to accumulate whole pixels 'steps' instead of rounding pixels every frame
	 */
	int _backgroundInverseVelocity;
	/** Holds any 'leftover' milliseconds between frames */
	uint _accumulatedVelocityMilliseconds;

public:
	void initialize();
	/**
	 * Rotates the background image in accordance to the current _backgroundInverseVelocity
	 *
	 * @param deltaTimeInMillis    The amount of time that has passed since the last frame
	 */
	void update(uint deltaTimeInMillis);

	/**
	 * Renders the current state of the backbuffer to the screen
	 */
	void renderBackbufferToScreen();

	/**
	 * Renders all AlphaEntries to the backbuffer
	 */
	void processAlphaEntries();
	/**
	 * Clears the AlphaEntry list
	 */
	void clearAlphaEntries() { _alphaDataEntries.clear(); }
	/**
	 * Removes a specific AlphaEntry from the list
	 *
	 * @param idNumber    The id number identifing the AlphaEntry
	 */
	void removeAlphaEntry(uint32 idNumber) { _alphaDataEntries.erase(idNumber); }

	/**
	 * Copies a sub-rectangle of a buffer to the working window
	 *
	 * @param buffer        The pixel data to copy to the working window
	 * @param destX         The X destination in the working window where the subRect of data should be put
	 * @param destY         The Y destination in the working window where the subRect of data should be put
	 * @param imageWidth    The width of the source image
	 * @param width         The width of the sub rectangle
	 * @param height        The height of the sub rectangle
	 */
	void copyRectToWorkingWindow(const uint16 *buffer, int32 destX, int32 destY, int32 imageWidth, int32 width, int32 height);
	/**
	 * Copies a sub-rectangle of a buffer to the working window with binary alpha support.
	 *
	 * @param buffer        The pixel data to copy to the working window
	 * @param destX         The X destination in the working window where the subRect of data should be put
	 * @param destY         The Y destination in the working window where the subRect of data should be put
	 * @param imageWidth    The width of the source image
	 * @param width         The width of the sub rectangle
	 * @param height        The height of the sub rectangle
	 * @param alphaColor    The color to interpret as meaning 'transparent'
	 * @param idNumber      A unique identifier for the data being copied over.
	 */
	void copyRectToWorkingWindow(const uint16 *buffer, int32 destX, int32 destY, int32 imageWidth, int32 width, int32 height, int16 alphaColor, uint32 idNumber);

	/**
	 * Renders the supplied text to the working window
	 *
	 * @param idNumber     A unique identifier for the text
	 * @param text         The text to be rendered
	 * @param font         The font to use to render the text
	 * @param destX        The X destination in the working window where the text should be rendered
	 * @param destY        The Y destination in the working window where the text should be rendered
	 * @param textColor    The color to render the text with (in RBG 565)
	 * @param maxWidth     The max width the text should take up.
	 * @param maxHeight    The max height the text should take up.
	 * @param align        The alignment of the text within the bounds of maxWidth
	 * @param wrap         If true, any words extending past maxWidth will wrap to a new line. If false, ellipses will be rendered to show that the text didn't fit
	 * @return             A rectangle representing where the text was drawn in the working window
	 */
	Common::Rect renderTextToWorkingWindow(uint32 idNumber, const Common::String &text, TruetypeFont *font, int destX, int destY, uint16 textColor, int maxWidth, int maxHeight = -1, Graphics::TextAlign align = Graphics::kTextAlignLeft, bool wrap = true);

	/**
	 * Fills the entire workingWindow with the specified color. Internally, the color
	 * will be converted to RGB 565 and then blitted.
	 *
	 * @param color    The color to fill the working window with. (In RGB 555)
	 */
	void clearWorkingWindowTo555Color(uint16 color);

	/**
	 * Blits the image or a portion of the image to the backbuffer. Actual screen updates won't happen until the end of the frame.
	 * The image will be clipped to fit inside the working window. Coords are in working window space, not screen space!
	 *
	 * @param fileName        Name of the image file
	 * @param destinationX    X position where the image should be put. Coords are in working window space, not screen space!
	 * @param destinationY    Y position where the image should be put. Coords are in working window space, not screen space!
	 */
	void renderImageToScreen(const Common::String &fileName, int16 destinationX, int16 destinationY, bool wrap = false);

	/**
	 * Blits the image or a portion of the image to the backbuffer. Actual screen updates won't happen until the end of the frame.
	 * The image will be clipped to fit inside the working window. Coords are in working window space, not screen space!
	 *
	 * @param stream          Surface to read the image data from
	 * @param destinationX    X position where the image should be put. Coords are in working window space, not screen space!
	 * @param destinationY    Y position where the image should be put. Coords are in working window space, not screen space!
	 */
	void renderImageToScreen(Graphics::Surface &surface, int16 destinationX, int16 destinationY, bool wrap = false);

	/**
	 * Sets the current background image to be used by the RenderManager and immediately
	 * blits it to the screen. (It won't show up until the end of the frame)
	 *
	 * @param fileName    The name of the image file
	 */
	void setBackgroundImage(const Common::String &fileName);

	/**
	 * Set the background position (_backgroundOffset). If the current RenderState is PANORAMA, the offset
	 * will be in the horizontal direction. If the current RenderState is TILT, the offset will be in the
	 * vertical direction. 
	 *
	 * This method will not render anything on the screen. So if nothing else is called that renders the 
	 * background, the change won't be seen until next frame.
	 *
	 * @param offset The amount to offset the background
	 */
	void setBackgroundPosition(int offset);
	
	/**
	 * Set the background scroll velocity. Negative velocities correspond to left / up scrolling and 
	 * positive velocities correspond to right / down scrolling
	 *
	 * @param velocity    Velocity
	 */
	void setBackgroundVelocity(int velocity);

	/**
	 * Converts a point in screen coordinate space to image coordinate space
	 *
	 * @param point    Point in screen coordinate space
	 * @return         Point in image coordinate space
	 */
	const Common::Point screenSpaceToImageSpace(const Common::Point &point);
	/**
	 * Converts a point in image coordinate space to ***PRE-WARP*** 
	 * working window coordinate space
	 *
	 * @param point    Point in image coordinate space
	 * @return         Point in PRE-WARP working window coordinate space
	 */
	const Common::Point imageSpaceToWorkingWindowSpace(const Common::Point &point);

	/**
	 * Clip a rectangle to the working window. If it returns false, the original rect
	 * is not inside the working window.
	 *
	 * @param rect    The rectangle to clip against the working window
	 * @return        Is rect at least partially inside the working window (true) or completely outside (false)
	 */
	bool clipRectToWorkingWindow(Common::Rect &rect);

	RenderTable *getRenderTable();
	uint32 getCurrentBackgroundOffset();
	const Graphics::Surface *getBackBuffer() { return &_backBuffer; }

	/**
	 * Creates a copy of surface and transposes the data.
	 *
	 * Note: The user is responsible for calling free() on the returned surface
	 * and then deleting it
	 *
	 * @param surface    The data to be transposed
	 * @return           A copy of the surface with the data transposed
	 */
	static Graphics::Surface *tranposeSurface(const Graphics::Surface *surface);

private:
	/**
	 * Renders a subRectangle of an image to the backbuffer. The destinationRect and SubRect
	 * will be clipped to image bound and to working window bounds
	 *
	 * @param buffer             Pointer to (0, 0) of the image data
	 * @param imageWidth         The width of the original image (not of the subRectangle)
	 * @param imageHeight        The width of the original image (not of the subRectangle)
	 * @param horizontalPitch    The horizontal pitch of the original image
	 * @param destinationX       The x coordinate (in working window space) of where to put the final image
	 * @param destinationY       The y coordinate (in working window space) of where to put the final image
	 * @param subRectangle       A rectangle representing the part of the image that should be rendered
	 * @param wrap               Should the image wrap (tile) if it doesn't completely fill the screen?
	 */
	void renderSubRectToScreen(Graphics::Surface &surface, int16 destinationX, int16 destinationY, bool wrap);

	/**
	 * Reads an image file pixel data into a Surface buffer. In the process
	 * it converts the pixel data from RGB 555 to RGB 565. Also, if the image
	 * is transposed, it will un-transpose the pixel data. The function will
	 * call destination::create() if the dimensions of destination do not match
	 * up with the dimensions of the image.
	 *
	 * @param fileName       The name of a .tga file
	 * @param destination    A reference to the Surface to store the pixel data in
	 */
	void readImageToSurface(const Common::String &fileName, Graphics::Surface &destination);

	/**
	 * Move the background image by an offset. If we are currently in Panorama mode,
	 * the offset will correspond to a horizontal motion. If we are currently in Tilt mode,
	 * the offset will correspond to a vertical motion. This function should not be called
	 * if we are in Flat mode. 
	 *
	 * The RenderManager will take care of wrapping the image.
	 * Ex: If the image has width 1400px, it is legal to offset 1500px.
	 *
	 * @param offset    The amount to move the background
	 */
	void moveBackground(int offset);
};

} // End of namespace ZVision

#endif