aboutsummaryrefslogtreecommitdiff
path: root/engines/sherlock/surface.cpp
blob: 48045beacdfab6e7663fec81d09d4ce6c61f8371 (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
/* 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.
 *
 */

#include "sherlock/surface.h"
#include "sherlock/sherlock.h"
#include "sherlock/resources.h"
#include "common/system.h"
#include "graphics/palette.h"

namespace Sherlock {

Surface::Surface(uint16 width, uint16 height, Common::Platform platform) : Fonts(), _freePixels(true) {
	create(width, height, platform);
}

Surface::Surface() : Fonts(), _freePixels(false) {
}

Surface::~Surface() {
	if (_freePixels)
		_surface.free();
}

void Surface::create(uint16 width, uint16 height, Common::Platform platform) {
	if (_freePixels)
		_surface.free();

	if (platform == Common::kPlatform3DO) {
		_surface.create(width, height, Graphics::PixelFormat(2, 5, 6, 5, 0, 11, 5, 0, 0));
	} else {
		_surface.create(width, height, Graphics::PixelFormat::createFormatCLUT8());
	}
	_freePixels = true;
}

Graphics::PixelFormat Surface::getPixelFormat() {
	return _surface.format;
}

void Surface::blitFrom(const Surface &src) {
	blitFrom(src, Common::Point(0, 0));
}

void Surface::blitFrom(const ImageFrame &src) {
	blitFrom(src._frame, Common::Point(0, 0));
}

void Surface::blitFrom(const Graphics::Surface &src) {
	blitFrom(src, Common::Point(0, 0));
}

void Surface::blitFrom(const Surface &src, const Common::Point &pt) {
	blitFrom(src, pt, Common::Rect(0, 0, src._surface.w, src._surface.h));
}

void Surface::blitFrom(const ImageFrame &src, const Common::Point &pt) {
	blitFrom(src._frame, pt, Common::Rect(0, 0, src._frame.w, src._frame.h));
}

void Surface::blitFrom(const Graphics::Surface &src, const Common::Point &pt) {
	blitFrom(src, pt, Common::Rect(0, 0, src.w, src.h));
}

void Surface::blitFrom(const Graphics::Surface &src, const Common::Point &pt, const Common::Rect &srcBounds) {
	Common::Rect srcRect = srcBounds;
	Common::Rect destRect(pt.x, pt.y, pt.x + srcRect.width(), pt.y + srcRect.height());

	if (srcRect.isValidRect() && clip(srcRect, destRect)) {
		// Surface is at least partially or completely on-screen
		addDirtyRect(destRect);
		_surface.copyRectToSurface(src, destRect.left, destRect.top, srcRect);
	}
}

void Surface::blitFrom(const ImageFrame &src, const Common::Point &pt, const Common::Rect &srcBounds) {
	blitFrom(src._frame, pt, srcBounds);
}

void Surface::blitFrom(const Surface &src, const Common::Point &pt, const Common::Rect &srcBounds) {
	blitFrom(src._surface, pt, srcBounds);
}

void Surface::transBlitFrom(const ImageFrame &src, const Common::Point &pt,
		bool flipped, int overrideColor, int scaleVal) {
	transBlitFrom(src._frame, pt + src._offset, flipped, overrideColor, scaleVal);
}

void Surface::transBlitFrom(const Surface &src, const Common::Point &pt,
		bool flipped, int overrideColor, int scaleVal) {
	const Graphics::Surface &s = src._surface;
	transBlitFrom(s, pt, flipped, overrideColor, scaleVal);
}

void Surface::transBlitFrom(const Graphics::Surface &src, const Common::Point &pt,
		bool flipped, int overrideColor, int scaleVal) {
	if (scaleVal == 256) {
		transBlitFromUnscaled(src, pt, flipped, overrideColor);
		return;
	}

	int scaleX = SCALE_THRESHOLD * SCALE_THRESHOLD / scaleVal;
	int scaleY = scaleX;
	int scaleXCtr = 0, scaleYCtr = 0;

	for (int yCtr = 0, destY = pt.y; yCtr < src.h && destY < this->h(); ++yCtr) {
		// Handle skipping lines if Y scaling
		scaleYCtr += scaleY;
		
		while (scaleYCtr >= SCALE_THRESHOLD && destY < this->h()) {
			scaleYCtr -= SCALE_THRESHOLD;

			if (destY >= 0) {
				// Handle drawing the line
				const byte *pSrc = (const byte *)src.getBasePtr(flipped ? src.w - 1 : 0, yCtr);
				byte *pDest = (byte *)getBasePtr(pt.x, destY);
				scaleXCtr = 0;

				for (int xCtr = 0, destX = pt.x; xCtr < src.w && destX < this->w(); ++xCtr) {
					// Handle horizontal scaling
					scaleXCtr += scaleX;

					while (scaleXCtr >= SCALE_THRESHOLD && destX < this->w()) {
						scaleXCtr -= SCALE_THRESHOLD;

						// Only handle on-screen pixels
						if (destX >= 0 && *pSrc != TRANSPARENCY)
							*pDest = *pSrc;

						++pDest;
						++destX;
					}

					pSrc = pSrc + (flipped ? -1 : 1);
				}
			}

			++destY;
		}
	}
}

void Surface::transBlitFromUnscaled(const Graphics::Surface &src, const Common::Point &pt,
		bool flipped, int overrideColor) {
	Common::Rect drawRect(0, 0, src.w, src.h);
	Common::Rect destRect(pt.x, pt.y, pt.x + src.w, pt.y + src.h);

	// Clip the display area to on-screen
	if (!clip(drawRect, destRect))
		// It's completely off-screen
		return;

	if (flipped)
		drawRect = Common::Rect(src.w - drawRect.right, src.h - drawRect.bottom,
			src.w - drawRect.left, src.h - drawRect.top);

	Common::Point destPt(destRect.left, destRect.top);
	addDirtyRect(Common::Rect(destPt.x, destPt.y, destPt.x + drawRect.width(),
		destPt.y + drawRect.height()));

	switch (src.format.bytesPerPixel) {
	case 1:
		// 8-bit palettized: Draw loop
		assert(_surface.format.bytesPerPixel == 1); // Security check
		for (int yp = 0; yp < drawRect.height(); ++yp) {
			const byte *srcP = (const byte *)src.getBasePtr(
				flipped ? drawRect.right - 1 : drawRect.left, drawRect.top + yp);
			byte *destP = (byte *)getBasePtr(destPt.x, destPt.y + yp);

			for (int xp = 0; xp < drawRect.width(); ++xp, ++destP) {
				if (*srcP != TRANSPARENCY)
					*destP = overrideColor ? overrideColor : *srcP;

				srcP = flipped ? srcP - 1 : srcP + 1;
			}
		}
		break;
	case 2:
		// 3DO 15-bit RGB565: Draw loop
		assert(_surface.format.bytesPerPixel == 2); // Security check
		for (int yp = 0; yp < drawRect.height(); ++yp) {
			const uint16 *srcP = (const uint16 *)src.getBasePtr(
				flipped ? drawRect.right - 1 : drawRect.left, drawRect.top + yp);
			uint16 *destP = (uint16 *)getBasePtr(destPt.x, destPt.y + yp);

			for (int xp = 0; xp < drawRect.width(); ++xp, ++destP) {
				if (*srcP) // RGB 0, 0, 0 -> transparent on 3DO
					*destP = *srcP; // overrideColor ? overrideColor : *srcP;

				srcP = flipped ? srcP - 1 : srcP + 1;
			}
		}
		break;
	default:
		error("Surface: unsupported bytesperpixel");
		break;
	}
}

void Surface::fillRect(int x1, int y1, int x2, int y2, byte color) {
	fillRect(Common::Rect(x1, y1, x2, y2), color);
}

void Surface::fillRect(const Common::Rect &r, byte color) {
	_surface.fillRect(r, color);
	addDirtyRect(r);
}

void Surface::fill(uint16 color) {
	_surface.fillRect(Common::Rect(_surface.w, _surface.h), color);
}

bool Surface::clip(Common::Rect &srcBounds, Common::Rect &destBounds) {
	if (destBounds.left >= _surface.w || destBounds.top >= _surface.h ||
			destBounds.right <= 0 || destBounds.bottom <= 0)
		return false;

	// Clip the bounds if necessary to fit on-screen
	if (destBounds.right > _surface.w) {
		srcBounds.right -= destBounds.right - _surface.w;
		destBounds.right = _surface.w;
	}

	if (destBounds.bottom > _surface.h) {
		srcBounds.bottom -= destBounds.bottom - _surface.h;
		destBounds.bottom = _surface.h;
	}

	if (destBounds.top < 0) {
		srcBounds.top += -destBounds.top;
		destBounds.top = 0;
	}

	if (destBounds.left < 0) {
		srcBounds.left += -destBounds.left;
		destBounds.left = 0;
	}

	return true;
}

void Surface::clear() {
	fillRect(Common::Rect(0, 0, _surface.w, _surface.h), 0);
}

void Surface::free() {
	if (_freePixels) {
		_surface.free();
		_freePixels = false;
	}
}

void Surface::setPixels(byte *pixels, int width, int height, Graphics::PixelFormat pixelFormat) {
	_surface.format = pixelFormat;
	_surface.w = width;
	_surface.h = height;
	_surface.pitch = width * pixelFormat.bytesPerPixel;
	_surface.setPixels(pixels);
}

void Surface::writeString(const Common::String &str, const Common::Point &pt, byte overrideColor) {
	Fonts::writeString(this, str, pt, overrideColor);
}

void Surface::maskArea(const ImageFrame &src, const Common::Point &pt, int scrollX) {
	// TODO
	error("TODO: maskArea");
}


} // End of namespace Sherlock