aboutsummaryrefslogtreecommitdiff
path: root/engines/tony/window.cpp
blob: 013be84b4bf778cc0b79070ef84d6dc4a2a42b52 (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
/* 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.
 *
 */

/*
 * This code is based on original Tony Tough source code
 *
 * Copyright (c) 1997-2003 Nayma Software
 */

#include "common/scummsys.h"
#include "graphics/surface.h"
#include "util.h"
#include "tony/window.h"
#include "tony/game.h"
#include "tony/tony.h"

namespace Tony {


/****************************************************************************\
*       RMWindow Methods
\****************************************************************************/

RMWindow::RMWindow() {
	_showDirtyRects = false;
}

RMWindow::~RMWindow() {
	close();
	RMText::unload();
	RMGfxTargetBuffer::freeBWPrecalcTable();
}

/**
 * Initializes the graphics window
 */
void RMWindow::init() {
	Graphics::PixelFormat pixelFormat(2, 5, 5, 5, 0, 10, 5, 0, 0);
	initGraphics(RM_SX, RM_SY, true, &pixelFormat);

	_bGrabScreenshot = false;
	_bGrabThumbnail = false;
	_bGrabMovie = false;
	_wiping = false;
}

void RMWindow::copyRectToScreen(const byte *buf, int pitch, int x, int y, int w, int h) {
	if (GLOBALS._bCfgAnni30) {
		if (!RMGfxTargetBuffer::_precalcTable) {
			RMGfxTargetBuffer::createBWPrecalcTable();
			g_vm->getEngine()->getPointer().updateCursor();
		}
		Graphics::Surface *screen = g_system->lockScreen();
		const uint16 *src = (const uint16 *)buf;
		for (int i = 0; i < h; i++) {
			uint16 *dst = (uint16 *)screen->getBasePtr(x, y + i);
			for (int j = 0; j < w; j++) {
				dst[j] = RMGfxTargetBuffer::_precalcTable[src[j] & 0x7FFF];
			}
			src += (pitch / 2);
		}
		g_system->unlockScreen();
	} else {
		if (RMGfxTargetBuffer::_precalcTable) {
			RMGfxTargetBuffer::freeBWPrecalcTable();
			g_vm->getEngine()->getPointer().updateCursor();
		}
		g_system->copyRectToScreen(buf, pitch, x, y, w, h);
	}
 }

/**
 * Close the window
 */
void RMWindow::close() {
}

void RMWindow::grabThumbnail(uint16 *thumbmem) {
	_bGrabThumbnail = true;
	_wThumbBuf = thumbmem;
}

/**
 * Repaint the screen
 */
void RMWindow::repaint() {
	g_system->updateScreen();
}

/**
 * Wipes an area of the screen
 */
void RMWindow::wipeEffect(Common::Rect &rcBoundEllipse) {
	if ((rcBoundEllipse.left == 0) && (rcBoundEllipse.top == 0) &&
	        (rcBoundEllipse.right == RM_SX) && (rcBoundEllipse.bottom == RM_SY)) {
		// Full screen clear wanted, so use shortcut method
		g_system->fillScreen(0);
	} else {
		// Clear the designated area a line at a time
		uint16 line[RM_SX];
		Common::fill(line, line + RM_SX, 0);

		// Loop through each line
		for (int yp = rcBoundEllipse.top; yp < rcBoundEllipse.bottom; ++yp) {
			copyRectToScreen((const byte *)&line[0], RM_SX * 2, rcBoundEllipse.left, yp, rcBoundEllipse.width(), 1);
		}
	}
}

void RMWindow::getNewFrame(RMGfxTargetBuffer &bigBuf, Common::Rect *rcBoundEllipse) {
	// Get a pointer to the bytes of the source buffer
	byte *lpBuf = bigBuf;

	if (rcBoundEllipse != NULL) {
		// Circular wipe effect
		getNewFrameWipe(lpBuf, *rcBoundEllipse);
		_wiping = true;
	} else if (_wiping) {
		// Just finished a wiping effect, so copy the full screen
		copyRectToScreen(lpBuf, RM_SX * 2, 0, 0, RM_SX, RM_SY);
		_wiping = false;

	} else {
		// Standard screen copy - iterate through the dirty rects
		Common::List<Common::Rect> dirtyRects = bigBuf.getDirtyRects();
		Common::List<Common::Rect>::iterator i;

		// If showing dirty rects, copy the entire screen background and set up a surface pointer
		Graphics::Surface *s = NULL;
		if (_showDirtyRects) {
			copyRectToScreen(lpBuf, RM_SX * 2, 0, 0, RM_SX, RM_SY);
			s = g_system->lockScreen();
		}

		for (i = dirtyRects.begin(); i != dirtyRects.end(); ++i) {
			Common::Rect &r = *i;
			const byte *lpSrc = lpBuf + (RM_SX * 2) * r.top + (r.left * 2);
			copyRectToScreen(lpSrc, RM_SX * 2, r.left, r.top, r.width(), r.height());
		}

		if (_showDirtyRects) {
			for (i = dirtyRects.begin(); i != dirtyRects.end(); ++i) {
				// Frame the copied area with a rectangle
				s->frameRect(*i, 0xffffff);
			}

			g_system->unlockScreen();
		}
	}

	if (_bGrabThumbnail) {
		// Need to generate a thumbnail
		RMSnapshot s;

		s.grabScreenshot(lpBuf, 4, _wThumbBuf);
		_bGrabThumbnail = false;
	}

	// Clear the dirty rect list
	bigBuf.clearDirtyRects();
}

/**
 * Copies a section of the game frame in a circle bounded by the specified rectangle
 */
void RMWindow::getNewFrameWipe(byte *lpBuf, Common::Rect &rcBoundEllipse) {
	// Clear the screen
	g_system->fillScreen(0);

	if (!rcBoundEllipse.isValidRect())
		return;

	Common::Point center(rcBoundEllipse.left + rcBoundEllipse.width() / 2,
	                     rcBoundEllipse.top + rcBoundEllipse.height() / 2);

	// The rectangle technically defines the area inside the ellipse, with the corners touching
	// the ellipse boundary. Since we're currently simulating the ellipse using a plain circle,
	// we need to calculate a necessary width using the hypotenuse of X/2 & Y/2
	int x2y2 = (rcBoundEllipse.width() / 2) * (rcBoundEllipse.width() / 2) +
	           (rcBoundEllipse.height() / 2) * (rcBoundEllipse.height() / 2);
	int radius = 0;
	while ((radius * radius) < x2y2)
		++radius;

	// Proceed copying a circular area of the frame with the calculated radius onto the screen
	int error = -radius;
	int x = radius;
	int y = 0;

	while (x >= y) {
		plotSplices(lpBuf, center, x, y);

		error += y;
		++y;
		error += y;

		if (error >= 0) {
			error -= x;
			--x;
			error -= x;
		}
	}
}

/**
 * Handles drawing the line splices for the circle of viewable area
 */
void RMWindow::plotSplices(const byte *lpBuf, const Common::Point &center, int x, int y) {
	plotLines(lpBuf, center, x, y);
	if (x != y)
		plotLines(lpBuf, center, y, x);
}

/**
 * Handles drawing the line splices for the circle of viewable area
 */
void RMWindow::plotLines(const byte *lpBuf, const Common::Point &center, int x, int y) {
	// Skips lines that have no width (i.e. at the top of the circle)
	if ((x == 0) || (y > center.y))
		return;

	const byte *pSrc;
	int xs = MAX(center.x - x, 0);
	int width = MIN(RM_SX - xs, x * 2);

	if ((center.y - y) >= 0) {
		// Draw line in top half of circle
		pSrc = lpBuf + ((center.y - y) * RM_SX * 2) + xs * 2;
		copyRectToScreen(pSrc, RM_SX * 2, xs, center.y - y, width, 1);
	}

	if ((center.y + y) < RM_SY) {
		// Draw line in bottom half of circle
		pSrc = lpBuf + ((center.y + y) * RM_SX * 2) + xs * 2;
		copyRectToScreen(pSrc, RM_SX * 2, xs, center.y + y, width, 1);
	}
}

void RMWindow::showDirtyRects(bool v) {
	_showDirtyRects = v; 
}

/****************************************************************************\
*       RMSnapshot Methods
\****************************************************************************/

void RMSnapshot::grabScreenshot(byte *lpBuf, int dezoom, uint16 *lpDestBuf) {
	uint16 *src = (uint16 *)lpBuf;

	int dimx = RM_SX / dezoom;
	int dimy = RM_SY / dezoom;

	uint16 *cursrc;

	if (lpDestBuf == NULL)
		src += (RM_SY - 1) * RM_BBX;

	if (dezoom == 1 && 0) {
		byte *curOut = _rgb;

		for (int y = 0; y < dimy; y++) {
			for (int x = 0; x < dimx; x++) {
				cursrc = &src[RM_SKIPX + x];

				*curOut++ = ((*cursrc) & 0x1F) << 3;
				*curOut++ = (((*cursrc) >> 5) & 0x1F) << 3;
				*curOut++ = (((*cursrc) >> 10) & 0x1F) << 3;

				if (lpDestBuf)
					*lpDestBuf++ = *cursrc;
			}

			if (lpDestBuf == NULL)
				src -= RM_BBX;
			else
				src += RM_BBX;
		}
	} else {
		uint32 k = 0;
		for (int y = 0; y < dimy; y++) {
			for (int x = 0; x < dimx; x++) {
				cursrc = &src[RM_SKIPX + x * dezoom];
				int sommar, sommab, sommag, curv;
				sommar = sommab = sommag = 0;

				for (int v = 0; v < dezoom; v++) {
					for (int u = 0; u < dezoom; u++) {
						if (lpDestBuf == NULL)
							curv = -v;
						else
							curv = v;

						sommab += cursrc[curv * RM_BBX + u] & 0x1F;
						sommag += (cursrc[curv * RM_BBX + u] >> 5) & 0x1F;
						sommar += (cursrc[curv * RM_BBX + u] >> 10) & 0x1F;
					}
				}
				_rgb[k + 0] = (byte)(sommab * 8 / (dezoom * dezoom));
				_rgb[k + 1] = (byte)(sommag * 8 / (dezoom * dezoom));
				_rgb[k + 2] = (byte)(sommar * 8 / (dezoom * dezoom));

				if (lpDestBuf != NULL)
					lpDestBuf[k / 3] = ((int)_rgb[k + 0] >> 3) | (((int)_rgb[k + 1] >> 3) << 5) |
					                   (((int)_rgb[k + 2] >> 3) << 10);

				k += 3;
			}

			if (lpDestBuf == NULL)
				src -= RM_BBX * dezoom;
			else
				src += RM_BBX * dezoom;
		}
	}
}

} // End of namespace Tony