aboutsummaryrefslogtreecommitdiff
path: root/engines/gob/scenery_v1.cpp
blob: 5912d961004afee9603da412015b18ce9bf7041b (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
/* ScummVM - Scumm Interpreter
 * Copyright (C) 2004 Ivan Dubrov
 * Copyright (C) 2004-2006 The ScummVM project
 *
 * 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.
 *
 * $URL$
 * $Id$
 *
 */

#include "common/stdafx.h"
#include "common/endian.h"

#include "gob/gob.h"
#include "gob/scenery.h"
#include "gob/anim.h"
#include "gob/draw.h"
#include "gob/game.h"
#include "gob/util.h"
#include "gob/cdrom.h"

namespace Gob {

Scenery_v1::Scenery_v1(GobEngine *vm) : Scenery(vm) {
}

int16 Scenery_v1::loadAnim(char search) {
	if (_vm->_cdrom->_cdPlaying) {
		while (_vm->_cdrom->getTrackPos() != -1)
		    _vm->_util->longDelay(50);

		_vm->_cdrom->_cdPlaying = false;
	}

	return Scenery::loadAnim(search);
}

// flags & 1 - do capture all area animation is occupying
// flags & 4 == 0 - calculate animation final size
// flags & 2 != 0 - don't check with "toRedraw"'s
// flags & 4 != 0 - checkk view toRedraw
void Scenery_v1::updateAnim(int16 layer, int16 frame, int16 animation, int16 flags,
	    int16 drawDeltaX, int16 drawDeltaY, char doDraw) {
	AnimLayer *layerPtr;
	PieceDesc **pictPtr;

	uint16 pieceIndex;
	uint16 pictIndex;

	int16 curFrame;

	int16 left;
	int16 right;
	int16 top;
	int16 bottom;

	byte highX;
	byte highY;

	int16 i;
	int16 transp;

	int16 destX;
	int16 destY;

	if (layer >= _animations[animation].layersCount)
		return;

	layerPtr = &_animations[animation].layers[layer];

	if (frame >= layerPtr->framesCount)
		return;

	if (flags & 1)		// Do capture
	{
		updateAnim(layer, frame, animation, 0, drawDeltaX,
		    drawDeltaY, 0);

		if (_toRedrawLeft == -12345)	// Some magic number?
			return;

		_vm->_game->capturePush(_toRedrawLeft, _toRedrawTop,
		    _toRedrawRight - _toRedrawLeft + 1,
		    _toRedrawBottom - _toRedrawTop + 1);

		*_pCaptureCounter = *_pCaptureCounter + 1;
	}
	pictPtr = _animations[animation].pieces;
	curFrame = 0;

	for (i = 0; i < frame; i++, curFrame++) {
		while (layerPtr->frames[curFrame].notFinal == 1)
			curFrame++;
	}

	if ((flags & 4) == 0) {
		_toRedrawLeft = -12345;
	} else {
		_toRedrawLeft =
		    MAX(_toRedrawLeft, _vm->_anim->_areaLeft);
		_toRedrawTop =
		    MAX(_toRedrawTop, _vm->_anim->_areaTop);
		_toRedrawRight =
		    MIN(_toRedrawRight,
		    (int16)(_vm->_anim->_areaLeft + _vm->_anim->_areaWidth - 1));
		_toRedrawBottom =
		    MIN(_toRedrawBottom,
		    (int16)(_vm->_anim->_areaTop + _vm->_anim->_areaHeight - 1));
	}

	transp = layerPtr->transp ? 3 : 0;

	curFrame--;
	do {
		curFrame++;

		pieceIndex = layerPtr->frames[curFrame].pieceIndex;
		pictIndex = layerPtr->frames[curFrame].pictIndex;

		destX = layerPtr->frames[curFrame].destX;
		destY = layerPtr->frames[curFrame].destY;

		highX = pictIndex & 0xc0;
		highY = pictIndex & 0x30;
		highX >>= 6;
		highY >>= 4;
		if (destX >= 0)
			destX += ((uint16)highX) << 7;
		else
			destX -= ((uint16)highX) << 7;

		if (destY >= 0)
			destY += ((uint16)highY) << 7;
		else
			destY -= ((uint16)highY) << 7;

		if (drawDeltaX == 1000)
			destX += layerPtr->posX;
		else
			destX += drawDeltaX;

		if (drawDeltaY == 1000)
			destY += layerPtr->posY;
		else
			destY += drawDeltaY;

		pictIndex = (pictIndex & 15) - 1;

		left = READ_LE_UINT16(&pictPtr[pictIndex][pieceIndex].left);
		right = READ_LE_UINT16(&pictPtr[pictIndex][pieceIndex].right);
		top = READ_LE_UINT16(&pictPtr[pictIndex][pieceIndex].top);
		bottom = READ_LE_UINT16(&pictPtr[pictIndex][pieceIndex].bottom);

		if (flags & 2) {
			if (destX < _vm->_anim->_areaLeft) {
				left += _vm->_anim->_areaLeft - destX;
				destX = _vm->_anim->_areaLeft;
			}

			if (left <= right
			    && destX + right - left >=
			    _vm->_anim->_areaLeft + _vm->_anim->_areaWidth)
				right -=
				    (destX + right - left) -
				    (_vm->_anim->_areaLeft + _vm->_anim->_areaWidth) +
				    1;

			if (destY < _vm->_anim->_areaTop) {
				top += _vm->_anim->_areaTop - destY;
				destY = _vm->_anim->_areaTop;
			}

			if (top <= bottom
			    && destY + bottom - top >=
			    _vm->_anim->_areaTop + _vm->_anim->_areaHeight)
				bottom -=
				    (destY + bottom - top) -
				    (_vm->_anim->_areaTop + _vm->_anim->_areaHeight) +
				    1;

		} else if (flags & 4) {
			if (destX < _toRedrawLeft) {
				left += _toRedrawLeft - destX;
				destX = _toRedrawLeft;
			}

			if (left <= right
			    && destX + right - left > _toRedrawRight)
				right -=
				    destX + right - left - _toRedrawRight;

			if (destY < _toRedrawTop) {
				top += _toRedrawTop - destY;
				destY = _toRedrawTop;
			}

			if (top <= bottom
			    && destY + bottom - top > _toRedrawBottom)
				bottom -=
				    destY + bottom - top - _toRedrawBottom;
		}

		if (left > right || top > bottom)
			continue;

		if (doDraw) {
			_vm->_draw->_sourceSurface =
			    _animPictToSprite[animation * 7 + pictIndex];
			_vm->_draw->_destSurface = 21;

			_vm->_draw->_spriteLeft = left;
			_vm->_draw->_spriteTop = top;
			_vm->_draw->_spriteRight = right - left + 1;
			_vm->_draw->_spriteBottom = bottom - top + 1;
			_vm->_draw->_destSpriteX = destX;
			_vm->_draw->_destSpriteY = destY;
			_vm->_draw->_transparency = transp;
			_vm->_draw->spriteOperation(DRAW_BLITSURF);
		}

		if ((flags & 4) == 0) {
			if (_toRedrawLeft == -12345) {
				_toRedrawLeft = destX;
				_animLeft = destX;
				_toRedrawTop = destY;
				_animTop = destY;
				_toRedrawRight = destX + right - left;
				_toRedrawBottom = destY + bottom - top;
			} else {
				_toRedrawLeft =
				    MIN(_toRedrawLeft, destX);
				_toRedrawTop =
				    MIN(_toRedrawTop, destY);
				_toRedrawRight =
				    MAX(_toRedrawRight,
				    (int16)(destX + right - left));
				_toRedrawBottom =
				    MAX(_toRedrawBottom,
				    (int16)(destY + bottom - top));
			}
		}
	} while (layerPtr->frames[curFrame].notFinal == 1);
}

} // End of namespace Gob