aboutsummaryrefslogtreecommitdiff
path: root/engines/tinsel/object.cpp
blob: f91e37a0631d60f74b664297a43a7996c7509634 (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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
/* 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.
 *
 * $URL$
 * $Id$
 *
 * This file contains the Object Manager code.
 */

#include "tinsel/object.h"
#include "tinsel/background.h"
#include "tinsel/cliprect.h"	// object clip rect defs
#include "tinsel/graphics.h"	// low level interface
#include "tinsel/handle.h"
#include "tinsel/text.h"
#include "tinsel/tinsel.h"

#define	OID_EFFECTS	0x2000			// generic special effects object id

namespace Tinsel {

// list of all objects
static OBJECT *objectList = 0;

// pointer to free object list
static OBJECT *pFreeObjects = 0;

#ifdef DEBUG
// diagnostic object counters
static int numObj = 0;
static int maxObj = 0;
#endif

void FreeObjectList() {
	if (objectList) {
		free(objectList);
		objectList = NULL;
	}
}

/**
 * Kills all objects and places them on the free list.
 */

void KillAllObjects() {
	int i;

#ifdef DEBUG
	// clear number of objects in use
	numObj = 0;
#endif

	if (objectList == NULL) {
		// first time - allocate memory for object list
		objectList = (OBJECT *)calloc(NUM_OBJECTS, sizeof(OBJECT));

		// make sure memory allocated
		if (objectList == NULL) {
			error("Cannot allocate memory for object data");
		}
	}

	// place first object on free list
	pFreeObjects = objectList;

	// link all other objects after first
	for (i = 1; i < NUM_OBJECTS; i++) {
		objectList[i - 1].pNext = objectList + i;
	}

	// null the last object
	objectList[NUM_OBJECTS - 1].pNext = NULL;
}


#ifdef	DEBUG
/**
 * Shows the maximum number of objects used at once.
 */

void ObjectStats() {
	printf("%i objects of %i used.\n", maxObj, NUM_OBJECTS);
}
#endif

/**
 * Allocate a object from the free list.
 */
OBJECT *AllocObject() {
	OBJECT *pObj = pFreeObjects;	// get a free object

	// check for no free objects
	assert(pObj != NULL);

	// a free object exists

	// get link to next free object
	pFreeObjects = pObj->pNext;

	// clear out object
	memset(pObj, 0, sizeof(OBJECT));

	// set default drawing mode and set changed bit
	pObj->flags = DMA_WNZ | DMA_CHANGED;

#ifdef DEBUG
	// one more object in use
	if (++numObj > maxObj)
		maxObj = numObj;
#endif

	// return new object
	return pObj;
}

bool isValidObject(OBJECT *obj) {
	return (obj >= objectList && obj <= objectList + NUM_OBJECTS - 1);
}

/**
 * Copy one object to another.
 * @param pDest			Destination object
 * @param pSrc			Source object
 */
void CopyObject(OBJECT *pDest, OBJECT *pSrc) {
	// save previous dimensions etc.
	Common::Rect rcSave = pDest->rcPrev;

	// make a copy
	memcpy(pDest, pSrc, sizeof(OBJECT));

	// restore previous dimensions etc.
	pDest->rcPrev = rcSave;

	// set changed flag in destination
	pDest->flags |= DMA_CHANGED;

	// null the links
	pDest->pNext = pDest->pSlave = NULL;
}

/**
 * Inserts an object onto the specified object list. The object
 * lists are sorted in Z Y order.
 * @param pObjList			List to insert object onto
 * @param pInsObj			Object to insert
 */

void InsertObject(OBJECT *pObjList, OBJECT *pInsObj) {
	OBJECT *pPrev, *pObj;	// object list traversal pointers

	// validate object pointer
	assert(isValidObject(pInsObj));

	for (pPrev = pObjList, pObj = pObjList->pNext; pObj != NULL; pPrev = pObj, pObj = pObj->pNext) {
		// check Z order
		if (pInsObj->zPos < pObj->zPos) {
			// object Z is lower than list Z - insert here
			break;
		} else if (pInsObj->zPos == pObj->zPos) {
			// Z values are the same - sort on Y
			if (fracToDouble(pInsObj->yPos) <= fracToDouble(pObj->yPos)) {
				// object Y is lower than or same as list Y - insert here
				break;
			}
		}
	}

	// insert obj between pPrev and pObj
	pInsObj->pNext = pObj;
	pPrev->pNext = pInsObj;
}


/**
 * Deletes an object from the specified object list and places it
 * on the free list.
 * @param pObjList			List to delete object from
 * @param pDelObj			Object to delete
 */
void DelObject(OBJECT *pObjList, OBJECT *pDelObj) {
	OBJECT *pPrev, *pObj;	// object list traversal pointers
	const Common::Rect rcScreen(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);

	// validate object pointer
	assert(isValidObject(pDelObj));

#ifdef DEBUG
	// one less object in use
	--numObj;
	assert(numObj >= 0);
#endif

	for (pPrev = pObjList, pObj = pObjList->pNext; pObj != NULL; pPrev = pObj, pObj = pObj->pNext) {
		if (pObj == pDelObj) {
			// found object to delete

			if (IntersectRectangle(pDelObj->rcPrev, pDelObj->rcPrev, rcScreen)) {
				// allocate a clipping rect for objects previous pos
				AddClipRect(pDelObj->rcPrev);
			}

			// make PREV next = OBJ next - removes OBJ from list
			pPrev->pNext = pObj->pNext;

			// place free list in OBJ next
			pObj->pNext = pFreeObjects;

			// add OBJ to top of free list
			pFreeObjects = pObj;

			// delete objects palette
			if (pObj->pPal)
				FreePalette(pObj->pPal);

			// quit
			return;
		}
	}

	// if we get to here - object has not been found on the list
	// This can be triggered in Act 3 in DW1 while talking to the guard,
	// so this has been turned to a warning instead of an error
	warning("DelObject(): formally 'assert(0)!'");
}


/**
 * Sort the specified object list in Z Y order.
 * @param pObjList			List to sort
 */
void SortObjectList(OBJECT *pObjList) {
	OBJECT *pPrev, *pObj;	// object list traversal pointers
	OBJECT head;		// temporary head of list - because pObjList is not usually a OBJECT

	// put at head of list
	head.pNext = pObjList->pNext;

	// set head of list dummy OBJ Z Y values to lowest possible
	head.yPos = intToFrac(MIN_INT16);
	head.zPos = MIN_INT;

	for (pPrev = &head, pObj = head.pNext; pObj != NULL; pPrev = pObj, pObj = pObj->pNext) {
		// check Z order
		if (pObj->zPos < pPrev->zPos) {
			// object Z is lower than previous Z

			// remove object from list
			pPrev->pNext = pObj->pNext;

			// re-insert object on list
			InsertObject(pObjList, pObj);

			// back to beginning of list
			pPrev = &head;
			pObj  = head.pNext;
		} else if (pObj->zPos == pPrev->zPos) {
			// Z values are the same - sort on Y
			if (fracToDouble(pObj->yPos) < fracToDouble(pPrev->yPos)) {
				// object Y is lower than previous Y

				// remove object from list
				pPrev->pNext = pObj->pNext;

				// re-insert object on list
				InsertObject(pObjList, pObj);

				// back to beginning of list
				pPrev = &head;
				pObj  = head.pNext;
			}
		}
	}
}

/**
 * Returns the animation offsets of a image, dependent on the
 * images orientation flags.
 * @param hImg			Iimage to get animation offset of
 * @param flags			Images current flags
 * @param pAniX			Gets set to new X animation offset
 * @param pAniY			Gets set to new Y animation offset
 */
void GetAniOffset(SCNHANDLE hImg, int flags, int *pAniX, int *pAniY) {
	if (hImg) {
		const IMAGE *pImg = (const IMAGE *)LockMem(hImg);

		// set ani X
		*pAniX = (int16) FROM_LE_16(pImg->anioffX);

		// set ani Y
		*pAniY = (int16) FROM_LE_16(pImg->anioffY);

		if (flags & DMA_FLIPH) {
			// we are flipped horizontally

			// set ani X = -ani X + width - 1
			*pAniX = -*pAniX + FROM_LE_16(pImg->imgWidth) - 1;
		}

		if (flags & DMA_FLIPV) {
			// we are flipped vertically

			// set ani Y = -ani Y + height - 1
			*pAniY = -*pAniY + (FROM_LE_16(pImg->imgHeight) & ~C16_FLAG_MASK) - 1;
		}
	} else
		// null image
		*pAniX = *pAniY = 0;
}


/**
 * Returns the x,y position of an objects animation point.
 * @param pObj			Pointer to object
 * @param pPosX			Gets set to objects X animation position
 * @param pPosY			Gets set to objects Y animation position
 */
void GetAniPosition(OBJECT *pObj, int *pPosX, int *pPosY) {
	// validate object pointer
	assert(isValidObject(pObj));

	// get the animation offset of the object
	GetAniOffset(pObj->hImg, pObj->flags, pPosX, pPosY);

	// from animation offset and objects position - determine objects animation point
	*pPosX += fracToInt(pObj->xPos);
	*pPosY += fracToInt(pObj->yPos);
}

/**
 * Initialise a object using a OBJ_INIT structure to supply parameters.
 * @param pInitTbl			Pointer to object initialisation table
 */
OBJECT *InitObject(const OBJ_INIT *pInitTbl) {
	// allocate a new object
	OBJECT *pObj = AllocObject();

	// make sure object created
	assert(pObj != NULL);

	// set objects shape
	pObj->hImg = pInitTbl->hObjImg;

	// set objects ID
	pObj->oid = pInitTbl->objID;

	// set objects flags
	pObj->flags = DMA_CHANGED | pInitTbl->objFlags;

	// set objects Z position
	pObj->zPos = pInitTbl->objZ;

	// get pointer to image
	if (pInitTbl->hObjImg) {
		int aniX, aniY;		// objects animation offsets
		PALQ *pPalQ = NULL;	// palette queue pointer
		const IMAGE *pImg = (const IMAGE *)LockMem(pInitTbl->hObjImg);	// handle to image

		if (pImg->hImgPal) {
			// allocate a palette for this object
			pPalQ = AllocPalette(FROM_LE_32(pImg->hImgPal));

			// make sure palette allocated
			assert(pPalQ != NULL);
		}

		// assign palette to object
		pObj->pPal = pPalQ;

		// set objects size
		pObj->width  = FROM_LE_16(pImg->imgWidth);
		pObj->height = FROM_LE_16(pImg->imgHeight) & ~C16_FLAG_MASK;
		pObj->flags &= ~C16_FLAG_MASK;
		pObj->flags |= FROM_LE_16(pImg->imgHeight) & C16_FLAG_MASK;

		// set objects bitmap definition
		pObj->hBits = FROM_LE_32(pImg->hImgBits);

		// get animation offset of object
		GetAniOffset(pObj->hImg, pInitTbl->objFlags, &aniX, &aniY);

		// set objects X position - subtract ani offset
		pObj->xPos = intToFrac(pInitTbl->objX - aniX);

		// set objects Y position - subtract ani offset
		pObj->yPos = intToFrac(pInitTbl->objY - aniY);
	} else {	// no image handle - null image

		// set objects X position
		pObj->xPos = intToFrac(pInitTbl->objX);

		// set objects Y position
		pObj->yPos = intToFrac(pInitTbl->objY);
	}

	// return new object
	return pObj;
}

/**
 * Give a object a new image and new orientation flags.
 * @param pAniObj			Object to be updated
 * @param newflags			Objects new flags
 * @param hNewImg			Objects new image
 */
void AnimateObjectFlags(OBJECT *pAniObj, int newflags, SCNHANDLE hNewImg) {
	// validate object pointer
	assert(isValidObject(pAniObj));

	if (pAniObj->hImg != hNewImg
		|| (pAniObj->flags & DMA_HARDFLAGS) != (newflags & DMA_HARDFLAGS)) {
		// something has changed

		int oldAniX, oldAniY;	// objects old animation offsets
		int newAniX, newAniY;	// objects new animation offsets

		// get objects old animation offsets
		GetAniOffset(pAniObj->hImg, pAniObj->flags, &oldAniX, &oldAniY);

		// get objects new animation offsets
		GetAniOffset(hNewImg, newflags, &newAniX, &newAniY);

		if (hNewImg) {
			// get pointer to image
			const IMAGE *pNewImg = (IMAGE *)LockMem(hNewImg);

			// setup new shape
			pAniObj->width  = FROM_LE_16(pNewImg->imgWidth);
			pAniObj->height = FROM_LE_16(pNewImg->imgHeight) & ~C16_FLAG_MASK;
			newflags &= ~C16_FLAG_MASK;
			newflags |= FROM_LE_16(pNewImg->imgHeight) & C16_FLAG_MASK;

			// set objects bitmap definition
			pAniObj->hBits  = FROM_LE_32(pNewImg->hImgBits);
		} else {	// null image
			pAniObj->width  = 0;
			pAniObj->height = 0;
			pAniObj->hBits  = 0;
		}

		// set objects flags and signal a change
		pAniObj->flags = newflags | DMA_CHANGED;

		// set objects image
		pAniObj->hImg = hNewImg;

		// adjust objects position - subtract new from old for difference
		pAniObj->xPos += intToFrac(oldAniX - newAniX);
		pAniObj->yPos += intToFrac(oldAniY - newAniY);
	}
}

/**
 * Give an object a new image.
 * @param pAniObj			Object to animate
 * @param hNewImg			Objects new image
 */
void AnimateObject(OBJECT *pAniObj, SCNHANDLE hNewImg) {
	// dont change the objects flags
	AnimateObjectFlags(pAniObj, pAniObj->flags, hNewImg);
}

/**
 * Creates a rectangle object of the given dimensions and returns
 * a pointer to the object.
 * @param hPal			Palette for the rectangle object
 * @param colour		Which colour offset from the above palette
 * @param width			Width of rectangle
 * @param height		Height of rectangle
 */
OBJECT *RectangleObject(SCNHANDLE hPal, int colour, int width, int height) {
	// template for initialising the rectangle object
	static const OBJ_INIT rectObj = {0, DMA_CONST, OID_EFFECTS, 0, 0, 0};
	PALQ *pPalQ;		// palette queue pointer

	// allocate and init a new object
	OBJECT *pRect = InitObject(&rectObj);

	// allocate a palette for this object
	pPalQ = AllocPalette(hPal);

	// make sure palette allocated
	assert(pPalQ != NULL);

	// assign palette to object
	pRect->pPal = pPalQ;

	// set colour in the palette
	pRect->constant = colour;

	// set rectangle width
	pRect->width = width;

	// set rectangle height
	pRect->height = height;

	// return pointer to rectangle object
	return pRect;
}

/**
 * Creates a translucent rectangle object of the given dimensions
 * and returns a pointer to the object.
 * @param width			Width of rectangle
 * @param height		Height of rectangle
 */
OBJECT *TranslucentObject(int width, int height) {
	// template for initialising the rectangle object
	static const OBJ_INIT rectObj = {0, DMA_TRANS, OID_EFFECTS, 0, 0, 0};

	// allocate and init a new object
	OBJECT *pRect = InitObject(&rectObj);

	// set rectangle width
	pRect->width = width;

	// set rectangle height
	pRect->height = height;

	// return pointer to rectangle object
	return pRect;
}

} // End of namespace Tinsel