aboutsummaryrefslogtreecommitdiff
path: root/engines/pegasus/elements.cpp
blob: 79faa1ae85b8ea8a687a6e461f9784b7286ded07 (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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
/* 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.
 *
 * Additional copyright for this file:
 * Copyright (C) 1995-1997 Presto Studios, Inc.
 *
 * 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 "common/macresman.h"
#include "common/stream.h"

#include "pegasus/elements.h"
#include "pegasus/graphics.h"
#include "pegasus/surface.h"

namespace Pegasus {

DisplayElement::DisplayElement(const tDisplayElementID id) : IDObject(id) {
	_elementIsDisplaying = false;
	_elementIsVisible = false;
	_elementOrder = 0;
	_triggeredElement = this;
	_nextElement = 0;
}

DisplayElement::~DisplayElement() {
	if (isDisplaying())
		((PegasusEngine *)g_engine)->_gfx->removeDisplayElement(this);
}

void DisplayElement::setDisplayOrder(const tDisplayOrder order) {
	if (_elementOrder != order) {
		_elementOrder = order;
		if (isDisplaying()) {
			((PegasusEngine *)g_engine)->_gfx->removeDisplayElement(this);
			((PegasusEngine *)g_engine)->_gfx->addDisplayElement(this);
			triggerRedraw();
		}
	}
}

void DisplayElement::startDisplaying() {
	if (!isDisplaying()) {
		((PegasusEngine *)g_engine)->_gfx->addDisplayElement(this);
		triggerRedraw();
	}
}

void DisplayElement::stopDisplaying() {
	if (isDisplaying()) {
		triggerRedraw();
		((PegasusEngine *)g_engine)->_gfx->removeDisplayElement(this);
	}
}

void DisplayElement::setBounds(const tCoordType left, const tCoordType top, const tCoordType right, const tCoordType bottom) {
	setBounds(Common::Rect(left, top, right, bottom));
}

void DisplayElement::getBounds(Common::Rect &r) const {
	r = _bounds;
}

void DisplayElement::sizeElement(const tCoordType h, const tCoordType v) {
	Common::Rect newBounds = _bounds;
	newBounds.right = _bounds.left + h;
	newBounds.bottom = _bounds.top + v;
	setBounds(newBounds);
}

void DisplayElement::moveElementTo(const tCoordType h, const tCoordType v) {
	Common::Rect newBounds = _bounds;
	newBounds.moveTo(h, v);
	setBounds(newBounds);
}

void DisplayElement::moveElement(const tCoordType dh, const tCoordType dv) {
	Common::Rect newBounds = _bounds;
	newBounds.translate(dh, dv);
	setBounds(newBounds);
}

void DisplayElement::getLocation(tCoordType &h, tCoordType &v) const {
	h = _bounds.left;
	v = _bounds.top;
}

void DisplayElement::centerElementAt(const tCoordType h, const tCoordType v) {
	Common::Rect newBounds = _bounds;
	newBounds.moveTo(h - (_bounds.width() / 2), v - (_bounds.height() / 2));
	setBounds(newBounds);
}

void DisplayElement::getCenter(tCoordType &h, tCoordType &v) const {
	h = (_bounds.left + _bounds.right) / 2;
	v = (_bounds.top + _bounds.bottom) / 2;
}

void DisplayElement::setBounds(const Common::Rect &r) {
	if (r != _bounds) {
		triggerRedraw();
		_bounds = r;
		triggerRedraw();
	}
}

void DisplayElement::hide() {
	if (_elementIsVisible) {
		triggerRedraw();
		_elementIsVisible = false;
	}
}

void DisplayElement::show() {
	if (!_elementIsVisible) {
		_elementIsVisible = true;
		triggerRedraw();
	}
}

//	Only invalidates this element's bounding rectangle if all these conditions are true:
//	--	The triggered element is this element.
//	--	The element is displaying on the display list.
//	--	The element is visible.
//	--	The element is part of the active layer OR is one of the reserved items.
void DisplayElement::triggerRedraw() {
	GraphicsManager *gfx = ((PegasusEngine *)g_engine)->_gfx;

	if (_triggeredElement == this) {
		if (validToDraw(gfx->getBackOfActiveLayer(), gfx->getFrontOfActiveLayer()))
			gfx->invalRect(_bounds);
	} else {
		_triggeredElement->triggerRedraw();
	}
}

void DisplayElement::setTriggeredElement(DisplayElement *element) {
	if (element)
		_triggeredElement = element;
	else
		_triggeredElement = this;
}

bool DisplayElement::validToDraw(tDisplayOrder backLayer, tDisplayOrder frontLayer) {
	return	isDisplaying() && _elementIsVisible &&
			(getObjectID() <= kHighestReservedElementID ||
			(getDisplayOrder() >= backLayer &&
			getDisplayOrder() <= frontLayer));
}

DropHighlight::DropHighlight(const tDisplayElementID id) : DisplayElement(id) {
	_highlightColor = g_system->getScreenFormat().RGBToColor(0x48, 0x80, 0xD8);
	_thickness = 2;
	_cornerDiameter = 0;
}

void DropHighlight::draw(const Common::Rect &) {
	Graphics::Surface *screen = ((PegasusEngine *)g_engine)->_gfx->getWorkArea();

	// Since this is only used in two different ways, I'm only
	// going to implement it in those two ways. Deal with it.

	Common::Rect rect = _bounds;
	rect.grow(-_thickness);
	screen->frameRect(rect, _highlightColor);
	rect.grow(1);
	screen->frameRect(rect, _highlightColor);

	if (_cornerDiameter == 8 && _thickness == 4) {
		rect.grow(1);
		screen->frameRect(rect, _highlightColor);
		screen->hLine(rect.left + 1, rect.top - 1, rect.right - 2, _highlightColor);
		screen->hLine(rect.left + 1, rect.bottom, rect.right - 2, _highlightColor);
		screen->vLine(rect.left - 1, rect.top + 1, rect.bottom - 2, _highlightColor);
		screen->vLine(rect.right, rect.top + 1, rect.bottom - 2, _highlightColor);
	}
}

IdlerAnimation::IdlerAnimation(const tDisplayElementID id) : Animation(id) {
	_lastTime = 0xffffffff;
}

void IdlerAnimation::startDisplaying() {
	if (!isDisplaying()) {
		Animation::startDisplaying();
		startIdling();
	}
}

void IdlerAnimation::stopDisplaying() {
	if (isDisplaying()) {
		Animation::stopDisplaying();
		stopIdling();
	}
}

void IdlerAnimation::useIdleTime() {
	uint32 currentTime = getTime();

	if (currentTime != _lastTime) {
		_lastTime = currentTime;
		timeChanged(_lastTime);
	}
}

void IdlerAnimation::timeChanged(const TimeValue) {
	triggerRedraw();
}

FrameSequence::FrameSequence(const tDisplayElementID id) : IdlerAnimation(id) {
	_duration = 0;
	_currentFrameNum = 0;
	_resFork = new Common::MacResManager();
	_numFrames = 0;
}

FrameSequence::~FrameSequence() {
	delete _resFork;
}

void FrameSequence::useFileName(const Common::String &fileName) {
	_resFork->open(fileName);
}

void FrameSequence::openFrameSequence() {
	if (!_resFork->hasResFork())
		return;

	Common::SeekableReadStream *res = _resFork->getResource(MKTAG('P', 'F', 'r', 'm'), 0x80);

	if (!res)
		return;

	uint32 scale = res->readUint32BE();
	_bounds.top = res->readUint16BE();
	_bounds.left = res->readUint16BE();
	_bounds.bottom = res->readUint16BE();
	_bounds.right = res->readUint16BE();
	_numFrames = res->readUint16BE();
	_duration = 0;

	_frameTimes.clear();
	for (uint32 i = 0; i < _numFrames; i++) {
		TimeValue time = res->readUint32BE();
		_duration += time;
		_frameTimes.push_back(_duration);
	}

	setScale(scale);
	setSegment(0, _duration);
	setTime(0);
	_currentFrameNum = 0;
	newFrame(_currentFrameNum);
	triggerRedraw();

	delete res;
}

void FrameSequence::closeFrameSequence() {
	stop();
	_resFork->close();
	_duration = 0;
	_numFrames = 0;
	_frameTimes.clear();
}

void FrameSequence::timeChanged(const TimeValue time) {
	int16 frameNum = 0;
	for (int16 i = _numFrames - 1; i >= 0; i--) {
		if (_frameTimes[i] < time) {
			frameNum = i;
			break;
		}
	}

	if (frameNum != _currentFrameNum) {
		_currentFrameNum = frameNum;
		newFrame(_currentFrameNum);
		triggerRedraw();
	}
}

void FrameSequence::setFrameNum(const int16 frameNum) {
	int16 f = CLIP<int>(frameNum, 0, _numFrames);

	if (_currentFrameNum != f) {
		_currentFrameNum = f;
		setTime(_frameTimes[f]);
		newFrame(f);
		triggerRedraw();
	}
}

bool FrameSequence::isSequenceOpen() const {
	return _numFrames != 0;
}

Sprite::Sprite(const tDisplayElementID id) : DisplayElement(id) {
	_numFrames = 0;
	_currentFrameNum = 0xffffffff;
	_currentFrame = 0;
}

Sprite::~Sprite() {
	discardFrames();
}

void Sprite::discardFrames() {
	if (!_frameArray.empty()) {
		for (uint32 i = 0; i < _numFrames; i++) {
			SpriteFrame *frame = _frameArray[i].frame;
			frame->_referenceCount--;
			if (frame->_referenceCount == 0)
				delete frame;
		}

		_frameArray.clear();
		_numFrames = 0;
		_currentFrame = 0;
		_currentFrameNum = 0xffffffff;
		setBounds(0, 0, 0, 0);
	}
}

void Sprite::addPICTResourceFrame(const tResIDType pictID, bool transparent, const tCoordType left, const tCoordType top) {
	SpriteFrame *frame = new SpriteFrame();
	frame->initFromPICTResource(((PegasusEngine *)g_engine)->_resFork, pictID, transparent);
	addFrame(frame, left, top);
}

uint32 Sprite::addFrame(SpriteFrame *frame, const tCoordType left, const tCoordType top) {
	SpriteFrameRec frameRecord;
	frameRecord.frame = frame;
	frameRecord.frameLeft = left;
	frameRecord.frameTop = top;
	_frameArray.push_back(frameRecord);
	_numFrames++;
	frame->_referenceCount++;

	Common::Rect frameBounds;
	frame->getSurfaceBounds(frameBounds);

	// 9/3/96
	// BB Should this be + left or - left?
	frameBounds.moveTo(_bounds.left + left, _bounds.top + top);

	frameBounds.extend(_bounds);

	if (_bounds != frameBounds)
		setBounds(frameBounds);

	return _numFrames - 1;
}

void Sprite::removeFrame(const uint32 frameNum) {
	_frameArray[frameNum].frame->_referenceCount--;
	if (_frameArray[frameNum].frame->_referenceCount == 0)
		delete _frameArray[frameNum].frame;

	// Calculate the new bounds
	Common::Rect frameBounds;
	for (uint32 i = 0; i < _numFrames; i++) {
		if (i == frameNum)
			continue;

		Common::Rect r;
		_frameArray[i].frame->getSurfaceBounds(r);
		r.translate(_frameArray[i].frameLeft, _frameArray[i].frameTop);
		frameBounds.extend(r);
	}

	_frameArray.remove_at(frameNum);

	frameBounds.moveTo(_bounds.left, _bounds.top);
	setBounds(frameBounds);

	if (_currentFrameNum == frameNum)
		triggerRedraw();
	else if (_currentFrameNum != 0xffffffff && _currentFrameNum > frameNum)
		--_currentFrameNum;
}

void Sprite::setCurrentFrameIndex(const int32 frameNum) {	
	if (frameNum < 0) {
		if (_currentFrameNum != 0xffffffff) {
			_currentFrameNum = 0xffffffff;
			_currentFrame = 0;
			triggerRedraw();
		}
	} else if (_numFrames > 0) {
		uint32 f = frameNum % _numFrames;
		if (f != _currentFrameNum) {
			_currentFrameNum = f;
			_currentFrame = &_frameArray[f];
			triggerRedraw();
		}
	}
}

SpriteFrame *Sprite::getFrame(const int32 index) {
	if (index < 0 || (uint32)index >= _numFrames)
		return 0;

	return _frameArray[index].frame;
}

void Sprite::draw(const Common::Rect &r) {
	if (_currentFrame) {
		Common::Rect frameBounds;
		_currentFrame->frame->getSurfaceBounds(frameBounds);

		frameBounds.translate(_bounds.left + _currentFrame->frameLeft, _bounds.top + _currentFrame->frameTop);
		Common::Rect r1 = frameBounds.findIntersectingRect(r);

		Common::Rect r2 = frameBounds;
		r2.translate(-_bounds.left - _currentFrame->frameLeft, -_bounds.top - _currentFrame->frameTop);

		_currentFrame->frame->drawImage(r2, r1);
	}
}

SpriteSequence::SpriteSequence(const tDisplayElementID id, const tDisplayElementID spriteID) :
		FrameSequence(id), _sprite(spriteID), _transparent(false) {
}

void SpriteSequence::openFrameSequence() {	
	if (!isSequenceOpen()) {
		FrameSequence::openFrameSequence();

		if (isSequenceOpen()) {
			uint32 numFrames = getNumFrames();

			for (uint32 i = 0; i < numFrames; ++i) {
				SpriteFrame *frame = new SpriteFrame();
				frame->initFromPICTResource(_resFork, i + 0x80, _transparent);
				_sprite.addFrame(frame, 0, 0);
			}

			_sprite.setBounds(_bounds);
		}
	}
}

void SpriteSequence::closeFrameSequence() {
	if (isSequenceOpen()) {
		FrameSequence::closeFrameSequence();
		_sprite.discardFrames();
	}
}

void SpriteSequence::setBounds(const Common::Rect &bounds) {
	FrameSequence::setBounds(bounds);
	_sprite.setBounds(_bounds);
}

void SpriteSequence::draw(const Common::Rect &r) {
	_sprite.draw(r);
}

void SpriteSequence::newFrame(const uint16 frame) {
	_sprite.setCurrentFrameIndex(frame);
}

#define DRAW_PIXEL() \
	if (bytesPerPixel == 2) \
		*((uint16 *)dst) = black; \
	else \
		*((uint32 *)dst) = black; \
	dst += bytesPerPixel

#define SKIP_PIXEL() \
	dst += bytesPerPixel

void ScreenDimmer::draw(const Common::Rect &r) {
	// We're going to emulate QuickDraw's srcOr+gray mode here
	// In this mode, every other y column is all black (odd-columns).
	// Basically, every row does three black and then one transparent
	// repeatedly.

	// The output is identical to the original

	uint32 black = g_system->getScreenFormat().RGBToColor(0, 0, 0);
	Graphics::Surface *screen = ((PegasusEngine *)g_engine)->_gfx->getWorkArea();
	byte bytesPerPixel = g_system->getScreenFormat().bytesPerPixel;

	// We're currently doing it to the whole screen to simplify the code

	for (int y = 0; y < 480; y++) {
		byte *dst = (byte *)screen->getBasePtr(0, y);

		for (int x = 0; x < 640; x += 4) {
			if (y & 1) {
				DRAW_PIXEL();
				DRAW_PIXEL();
				SKIP_PIXEL();
				DRAW_PIXEL();
			} else {
				SKIP_PIXEL();
				DRAW_PIXEL();
				DRAW_PIXEL();
				DRAW_PIXEL();
			}
		}
	}
}

#undef DRAW_PIXEL
#undef SKIP_PIXEL

SoundLevel::SoundLevel(const tDisplayElementID id) : DisplayElement(id) {
	_soundLevel = 0;
}

void SoundLevel::incrementLevel() {
	if (_soundLevel < 12) {
		_soundLevel++;
		triggerRedraw();
	}
}

void SoundLevel::decrementLevel() {
	if (_soundLevel > 0) {
		_soundLevel--;
		triggerRedraw();
	}
}

uint16 SoundLevel::getSoundLevel() {
	return CLIP<int>(_soundLevel * 22, 0, 256);
}

void SoundLevel::setSoundLevel(uint16 level) {
	uint16 newLevel = (level + 21) / 22;

	if (newLevel != _soundLevel) {
		_soundLevel = newLevel;
		triggerRedraw();
	}
}

void SoundLevel::draw(const Common::Rect &r) {
	Common::Rect levelRect(_bounds.right + (8 * (_soundLevel - 12)), _bounds.top, _bounds.right, _bounds.bottom);
	levelRect = r.findIntersectingRect(levelRect);

	if (!levelRect.isEmpty()) {
		Graphics::Surface *screen = ((PegasusEngine *)g_engine)->_gfx->getWorkArea();
		screen->fillRect(levelRect, g_system->getScreenFormat().RGBToColor(0, 0, 0));
	}
}

} // End of namespace Pegasus