aboutsummaryrefslogtreecommitdiff
path: root/engines/toon/hotspot.cpp
blob: 0573e92fef0790e47405102cfa87c88c57e6a132 (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
/* 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$
*
*/

#include "toon/hotspot.h"
#include "toon/tools.h"

namespace Toon {

Hotspots::Hotspots(ToonEngine *vm) : _vm(vm) {
	_items = NULL;
	_numItems = 0;
}

Hotspots::~Hotspots() {
	delete[] _items;
}

void Hotspots::load(Common::ReadStream *Stream) {
	delete[] _items;

	_numItems = Stream->readSint16BE();
	_items = new HotspotData[_numItems];

	for (int32 i = 0; i < _numItems; i++) {
		for (int32 a = 0; a < 256; a++)
			_items[i].setData(a, Stream->readSint16BE());
	}
}

void Hotspots::save(Common::WriteStream *Stream) {
	Stream->writeSint16BE(_numItems);

	for (int32 i = 0; i < _numItems; i++) {
		for (int32 a = 0; a < 256; a++)
			Stream->writeSint16BE(_items[i].getData(a));
	}
}

int32 Hotspots::FindBasedOnCorner(int32 x, int32 y) {
	debugC(1, kDebugHotspot, "FindBasedOnCorner(%d, %d)", x, y);

	for (int32 i = 0; i < _numItems; i++) {
		if (x == _items[i].getX1()) {
			if (y == _items[i].getY1()) {
				if (_items[i].getMode() == -1)
					return _items[i].getRef();

				return i;
			}
		}
	}
	return -1;
}

int32 Hotspots::Find(int32 x, int32 y) {
	debugC(6, kDebugHotspot, "Find(%d, %d)", x, y);

	int32 priority = -1;
	int32 foundId = -1;
	int32 testId = -1;

	for (int i = 0; i < _numItems; i++) {
		if (x >= _items[i].getX1() && x <= _items[i].getX2() && y >= _items[i].getY1() && y <= _items[i].getY2()) {
			if (_items[i].getMode() == -1)
				testId = _items[i].getRef();
			else
				testId = i;

			if (_items[testId].getPriority() > priority) {
				foundId = testId;
				priority = _items[testId].getPriority();
			}
		}
	}
	return foundId;
}

bool Hotspots::LoadRif(Common::String rifName, Common::String additionalRifName) {
	debugC(1, kDebugHotspot, "LoadRif(%s, %s)", rifName.c_str(), additionalRifName.c_str());

	uint32 size = 0;
	uint8 *rifData = _vm->resources()->getFileData(rifName, &size);
	if (!rifData)
		return false;

	uint32 size2 = 0;
	uint8 *rifData2 = 0;
	if (additionalRifName.size())
		rifData2 = _vm->resources()->getFileData(additionalRifName, &size2);

	// figure out the number of hotspots based on file size
	int32 rifsize = READ_BE_UINT32(&rifData[4]);
	int32 rifsize2 = 0;

	if (size2)
		rifsize2 = READ_BE_UINT32(&rifData2[4]);

	_numItems = (rifsize + rifsize2) / 512;

	delete[] _items;
	_items = new HotspotData[_numItems];

	// RIFs are compressed in RNC1
	RncDecoder decoder;
	decoder.unpackM1(rifData, size, _items);
	if (rifsize2) {
		RncDecoder decoder2;
		decoder2.unpackM1(rifData2 , size2, _items + (rifsize >> 9));
		for (int32 i = 0; i < (rifsize2 >> 9); i++) {
			HotspotData *hot = _items + (rifsize >> 9) + i;
			hot->setData(0, hot->getX1() + 1280);
			hot->setData(2, hot->getX2() + 1280);
			if (hot->getMode() == -1)
				hot->setData(5, hot->getRef() + (rifsize >> 9));
		}
	}

	return true;
}

HotspotData *Hotspots::Get(int32 id) {
	debugC(5, kDebugHotspot, "Get(%d)", id);

	if (id < 0 || id >= _numItems)
		return 0;
	else
		return &_items[id];
}

} // End of namespace Toon