File: atari-cursor.cpp

package info (click to toggle)
scummvm 2.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 450,580 kB
  • sloc: cpp: 4,299,825; asm: 28,322; python: 12,901; sh: 11,302; java: 9,289; xml: 7,895; perl: 2,639; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (298 lines) | stat: -rw-r--r-- 8,561 bytes parent folder | download
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
/* 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 3 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, see <http://www.gnu.org/licenses/>.
 *
 */

#include "atari-cursor.h"

#include <cassert>

#include "atari-graphics.h"
#include "atari-screen.h"
//#include "backends/platform/atari/atari-debug.h"

byte Cursor::_palette[256*3] = {};

Cursor::Cursor(const AtariGraphicsManager *manager, const Screen *screen, int x, int y)
		: _manager(manager)
		, _parentScreen(screen)
		, _boundingSurf(screen->offsettedSurf)
		, _x(x)
		, _y(y) {
}

void Cursor::update() {
	if (!_buf) {
		_outOfScreen = true;
		_savedRect = _alignedDstRect = Common::Rect();
		return;
	}

	if (!_visible || (!_positionChanged && !_surfaceChanged))
		return;

	_srcRect = Common::Rect(_width, _height);

	_dstRect = Common::Rect(
		_x - _hotspotX,	// left
		_y - _hotspotY,	// top
		_x - _hotspotX + _width,	// right
		_y - _hotspotY + _height);	// bottom

	_outOfScreen = !_boundingSurf->clip(_srcRect, _dstRect);

	if (!_outOfScreen) {
		assert(_srcRect.width() == _dstRect.width());
		assert(_srcRect.height() == _dstRect.height());

		const int dstBitsPerPixel = _manager->getBitsPerPixel(_parentScreen->offsettedSurf->format);

		// non-direct rendering never uses 4bpp but maybe in the future ...
		_savedRect = _manager->alignRect(
			_dstRect.left * dstBitsPerPixel / 8,	// fake 4bpp by 8bpp's x/2
			_dstRect.top,
			_dstRect.right * dstBitsPerPixel / 8,	// fake 4bpp by 8bpp's width/2
			_dstRect.bottom);

		// this is used only in flushBackground()
		_alignedDstRect = _manager->alignRect(
			_dstRect.left + _xOffset,
			_dstRect.top,
			_dstRect.right + _xOffset,
			_dstRect.bottom);
	}
}

void Cursor::updatePosition(int deltaX, int deltaY) {
	//atari_debug("Cursor::updatePosition: %d, %d", deltaX, deltaX);

	if (deltaX == 0 && deltaY == 0)
		return;

	_x += deltaX;
	_y += deltaY;

	if (_x < 0)
		_x = 0;
	else if (_x >= _boundingSurf->w)
		_x = _boundingSurf->w - 1;

	if (_y < 0)
		_y = 0;
	else if (_y >= _boundingSurf->h)
		_y = _boundingSurf->h - 1;

	_positionChanged = true;
}

void Cursor::setSurface(const void *buf, int w, int h, int hotspotX, int hotspotY, uint32 keycolor) {
	if (w == 0 || h == 0 || buf == nullptr) {
		_buf = nullptr;
		return;
	}

	_buf = (const byte *)buf;
	_width = w;
	_height = h;
	_hotspotX = hotspotX;
	_hotspotY = hotspotY;
	_keycolor = keycolor;

	_surfaceChanged = true;
}

void Cursor::setPalette(const byte *colors, uint start, uint num) {
	memcpy(&_palette[start * 3], colors, num * 3);

	_surfaceChanged = true;
}

void Cursor::convertSurfaceTo(const Graphics::PixelFormat &format) {
	const int cursorWidth = (_srcRect.width() + 15) & (-16);
	const int cursorHeight = _height;
	const bool isCLUT8 = format.isCLUT8();

	if (_surface.w != cursorWidth || _surface.h != cursorHeight || _surface.format != format) {
		if (!isCLUT8 && _surface.format != format) {
			_rShift = format.rLoss - format.rShift;
			_gShift = format.gLoss - format.gShift;
			_bShift = format.bLoss - format.bShift;

			_rMask = format.rMax() << format.rShift;
			_gMask = format.gMax() << format.gShift;
			_bMask = format.bMax() << format.bShift;
		}

		_surface.create(cursorWidth, cursorHeight, format);

		extern bool g_unalignedPitch;
		const bool old_unalignedPitch = g_unalignedPitch;
		g_unalignedPitch = true;
		_surfaceMask.create(_surface.w / 8, _surface.h, format);	// 1 bpl
		g_unalignedPitch = old_unalignedPitch;
	}

	const int srcRectWidth = _srcRect.width();

	const byte *src = _buf + _srcRect.left;
	byte *dst = (byte *)_surface.getPixels();
	uint16 *dstMask = (uint16 *)_surfaceMask.getPixels();
	const int srcPadding = _width - srcRectWidth;
	const int dstPadding = _surface.w - srcRectWidth;

	for (int j = 0; j < cursorHeight; ++j) {
		for (int i = 0; i < srcRectWidth; ++i) {
			const uint32 color = *src++;
			const uint16 bit = 1 << (15 - (i % 16));

			if (color != _keycolor) {
				if (!isCLUT8) {
					// Convert CLUT8 to RGB332/RGB121 palette
					*dst++ = ((_palette[color*3 + 0] >> _rShift) & _rMask)
						   | ((_palette[color*3 + 1] >> _gShift) & _gMask)
						   | ((_palette[color*3 + 2] >> _bShift) & _bMask);
				} else {
					*dst++ = color;
				}

				// clear bit
				*dstMask &= ~bit;
			} else {
				*dst++ = 0x00;

				// set bit
				*dstMask |= bit;
			}

			if (bit == 0x0001)
				dstMask++;
		}

		src += srcPadding;

		if (dstPadding) {
			memset(dst, 0x00, dstPadding);
			dst += dstPadding;

			*dstMask |= ((1 << dstPadding) - 1);
			dstMask++;
		}
	}
}

Common::Rect Cursor::flushBackground(const Common::Rect &alignedRect, bool directRendering) {
	if (_savedRect.isEmpty())
		return _savedRect;

	if (!alignedRect.isEmpty() && alignedRect.contains(_alignedDstRect)) {
		// better would be _visibilityChanged but update() ignores it
		_positionChanged = true;

		_savedRect = Common::Rect();
	} else if (alignedRect.isEmpty() || alignedRect.intersects(_alignedDstRect)) {
		// better would be _visibilityChanged but update() ignores it
		_positionChanged = true;

		if (directRendering)
			restoreBackground();
		else
			return _alignedDstRect;
	}

	return Common::Rect();
}

void Cursor::saveBackground() {
	if (_savedRect.isEmpty())
		return;

	// as this is used only for direct rendering, we don't need to worry about offsettedSurf
	// having different dimensions than the source surface
	Graphics::Surface &dstSurface = *_parentScreen->offsettedSurf;

	//atari_debug("Cursor::saveBackground: %d %d %d %d", _savedRect.left, _savedRect.top, _savedRect.width(), _savedRect.height());

	// save native pixels (i.e. bitplanes)
	if (_savedBackground.w != _savedRect.width()
		|| _savedBackground.h != _savedRect.height()
		|| _savedBackground.format != dstSurface.format) {
		_savedBackground.create(_savedRect.width(), _savedRect.height(), dstSurface.format);
	}

	_savedBackground.copyRectToSurface(dstSurface, 0, 0, _savedRect);
}

void Cursor::draw() {
	Graphics::Surface &dstSurface = *_parentScreen->offsettedSurf;
	const int dstBitsPerPixel     = _manager->getBitsPerPixel(dstSurface.format);

	//atari_debug("Cursor::draw: %d %d %d %d", _dstRect.left, _dstRect.top, _dstRect.width(), _dstRect.height());

	if (_surfaceChanged || _srcRect.width() != _previousSrcRect.width()) {
		_previousSrcRect = _srcRect;

		// TODO: some sort of in-place C2P directly into convertSurfaceTo() ...
		convertSurfaceTo(dstSurface.format);
		{
			// c2p in-place (will do nothing on regular Surface::copyRectToSurface)
			Graphics::Surface surf;
			surf.init(
				_surface.w,
				_surface.h,
				_surface.pitch * dstBitsPerPixel / 8,	// 4bpp is not byte per pixel anymore
				_surface.getPixels(),
				_surface.format);
			_manager->copyRectToSurface(
				surf, _surface,
				0, 0,
				Common::Rect(_surface.w, _surface.h));
		}
	}

	// don't use _srcRect.right as 'x2' as this must be aligned first
	// (_surface.w is recalculated thanks to convertSurfaceTo())
	_manager->drawMaskedSprite(
		dstSurface,
		_surface, _surfaceMask,
		_dstRect.left + _xOffset, _dstRect.top,
		Common::Rect(0, _srcRect.top, _surface.w, _srcRect.bottom));

	_visibilityChanged = _positionChanged = _surfaceChanged = false;
}

void Cursor::restoreBackground() {
	if (_savedRect.isEmpty())
		return;

	assert(_savedBackground.getPixels());

	//atari_debug("Cursor::restoreBackground: %d %d %d %d", _savedRect.left, _savedRect.top, _savedRect.width(), _savedRect.height());

	// as this is used only for direct rendering, we don't need to worry about offsettedSurf
	// having different dimensions than the source surface
	Graphics::Surface &dstSurface = *_parentScreen->offsettedSurf;

	// restore native pixels (i.e. bitplanes)
	dstSurface.copyRectToSurface(
		_savedBackground,
		_savedRect.left, _savedRect.top,
		Common::Rect(_savedBackground.w, _savedBackground.h));

	_savedRect = Common::Rect();
}