aboutsummaryrefslogtreecommitdiff
path: root/engines/gob/cmpfile.cpp
blob: 67b378475de43f285c84b20ce4977cf1bb635ca4 (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
/* 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.
 *
 */

#include "common/stream.h"
#include "common/substream.h"
#include "common/str.h"

#include "gob/gob.h"
#include "gob/util.h"
#include "gob/surface.h"
#include "gob/video.h"
#include "gob/dataio.h"
#include "gob/rxyfile.h"
#include "gob/cmpfile.h"

namespace Gob {

CMPFile::CMPFile(GobEngine *vm, const Common::String &baseName,
                 uint16 width, uint16 height, uint8 bpp) :
	_vm(vm), _width(width), _height(height), _bpp(bpp), _maxWidth(0), _maxHeight(0),
	_surface(0), _coordinates(0) {

	if (baseName.empty())
		return;

	const Common::String rxyFile = Util::setExtension(baseName, ".RXY");
	const Common::String cmpFile = Util::setExtension(baseName, ".CMP");

	if (!_vm->_dataIO->hasFile(cmpFile))
		return;

	loadRXY(rxyFile);
	createSurface();

	loadCMP(cmpFile);
}

CMPFile::CMPFile(GobEngine *vm, const Common::String &cmpFile, const Common::String &rxyFile,
                 uint16 width, uint16 height, uint8 bpp) :
	_vm(vm), _width(width), _height(height), _bpp(bpp), _maxWidth(0), _maxHeight(0),
	_surface(0), _coordinates(0) {

	if (cmpFile.empty() || !_vm->_dataIO->hasFile(cmpFile))
		return;

	loadRXY(rxyFile);
	createSurface();

	loadCMP(cmpFile);
}

CMPFile::CMPFile(GobEngine *vm, Common::SeekableReadStream &cmp, Common::SeekableReadStream &rxy,
                 uint16 width, uint16 height, uint8 bpp) :
	_vm(vm), _width(width), _height(height), _bpp(bpp), _maxWidth(0), _maxHeight(0),
	_surface(0), _coordinates(0) {

	loadRXY(rxy);
	createSurface();

	loadCMP(cmp);
}

CMPFile::CMPFile(GobEngine *vm, Common::SeekableReadStream &cmp,
                 uint16 width, uint16 height, uint8 bpp) :
	_vm(vm), _width(width), _height(height), _bpp(bpp), _maxWidth(0), _maxHeight(0),
	_surface(0), _coordinates(0) {

	createRXY();
	createSurface();

	loadCMP(cmp);
}

CMPFile::~CMPFile() {
	delete _surface;
	delete _coordinates;
}

bool CMPFile::empty() const {
	return (_surface == 0) || (_coordinates == 0);
}

uint16 CMPFile::getSpriteCount() const {
	if (empty())
		return 0;

	return _coordinates->size();
}

void CMPFile::loadCMP(const Common::String &cmp) {
	Common::SeekableReadStream *dataCMP = _vm->_dataIO->getFile(cmp);
	if (!dataCMP)
		return;

	loadCMP(*dataCMP);

	delete dataCMP;
}

void CMPFile::loadRXY(const Common::String &rxy) {
	Common::SeekableReadStream *dataRXY = 0;
	if (!rxy.empty())
		dataRXY = _vm->_dataIO->getFile(rxy);

	if (dataRXY)
		loadRXY(*dataRXY);
	else
		createRXY();

	_height = _coordinates->getHeight();

	delete dataRXY;
}

void CMPFile::loadCMP(Common::SeekableReadStream &cmp) {
	uint32 size = cmp.size();
	byte  *data = new byte[size];

	if (cmp.read(data, size) != size) {
		delete[] data;

		return;
	}

	_vm->_video->drawPackedSprite(data, _surface->getWidth(), _surface->getHeight(), 0, 0, 0, *_surface);

	delete[] data;
}

void CMPFile::loadRXY(Common::SeekableReadStream &rxy) {
	bool bigEndian = (_vm->getEndiannessMethod() == kEndiannessMethodBE) ||
	                 ((_vm->getEndiannessMethod() == kEndiannessMethodSystem) &&
	                  (_vm->getEndianness() == kEndiannessBE));

	Common::SeekableSubReadStreamEndian sub(&rxy, 0, rxy.size(), bigEndian, DisposeAfterUse::NO);

	_coordinates = new RXYFile(sub);

	for (uint i = 0; i < _coordinates->size(); i++) {
		const RXYFile::Coordinates &c = (*_coordinates)[i];

		if (c.left == 0xFFFF)
			continue;

		const uint16 width  = c.right  - c.left + 1;
		const uint16 height = c.bottom - c.top  + 1;

		_maxWidth  = MAX(_maxWidth , width);
		_maxHeight = MAX(_maxHeight, height);
	}
}

void CMPFile::createRXY() {
	_coordinates = new RXYFile(_width, _height);

	_maxWidth  = _width;
	_maxHeight = _height;
}

void CMPFile::createSurface() {
	if (_width == 0)
		_width = 320;
	if (_height == 0)
		_height = 200;

	_surface = new Surface(_width, _height, _bpp);
}

bool CMPFile::getCoordinates(uint16 sprite, uint16 &left, uint16 &top, uint16 &right, uint16 &bottom) const {
	if (empty() || (sprite >= _coordinates->size()))
		return false;

	left   = (*_coordinates)[sprite].left;
	top    = (*_coordinates)[sprite].top;
	right  = (*_coordinates)[sprite].right;
	bottom = (*_coordinates)[sprite].bottom;

	return left != 0xFFFF;
}

uint16 CMPFile::getWidth(uint16 sprite) const {
	if (empty() || (sprite >= _coordinates->size()))
		return 0;

	return (*_coordinates)[sprite].right  - (*_coordinates)[sprite].left + 1;
}

uint16 CMPFile::getHeight(uint16 sprite) const {
	if (empty() || (sprite >= _coordinates->size()))
		return 0;

	return (*_coordinates)[sprite].bottom - (*_coordinates)[sprite].top  + 1;
}

void CMPFile::getMaxSize(uint16 &width, uint16 &height) const {
	width  = _maxWidth;
	height = _maxHeight;
}

void CMPFile::draw(Surface &dest, uint16 sprite, uint16 x, uint16 y, int32 transp) const {
	if (empty())
		return;

	if (sprite >= _coordinates->size())
		return;

	const RXYFile::Coordinates &coords = (*_coordinates)[sprite];

	draw(dest, coords.left, coords.top, coords.right, coords.bottom, x, y, transp);
}

void CMPFile::draw(Surface &dest, uint16 left, uint16 top, uint16 right, uint16 bottom,
                   uint16 x, uint16 y, int32 transp) const {

	if (!_surface)
		return;

	if (left == 0xFFFF)
		return;

	dest.blit(*_surface, left, top, right, bottom, x, y, transp);
}

uint16 CMPFile::addSprite(uint16 left, uint16 top, uint16 right, uint16 bottom) {
	if (empty())
		return 0;

	const uint16 height = bottom - top  + 1;
	const uint16 width  = right  - left + 1;

	_maxWidth  = MAX(_maxWidth , width);
	_maxHeight = MAX(_maxHeight, height);

	return _coordinates->add(left, top, right, bottom);
}

void CMPFile::recolor(uint8 from, uint8 to) {
	if (_surface)
		_surface->recolor(from, to);
}

} // End of namespace Gob