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
|
/* 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 "illusions/illusions.h"
#include "illusions/backgroundresource.h"
#include "illusions/camera.h"
#include "common/str.h"
namespace Illusions {
// BackgroundResourceLoader
void BackgroundResourceLoader::load(Resource *resource) {
// TODO
debug("BackgroundResourceLoader::load() Loading background %08X from %s...", resource->_resId, resource->_filename.c_str());
BackgroundResource *backgroundResource = new BackgroundResource();
backgroundResource->load(resource->_data, resource->_dataSize);
BackgroundItem *backgroundItem = _vm->_backgroundItems->allocBackgroundItem();
backgroundItem->_bgRes = backgroundResource;
backgroundItem->_tag = resource->_tag;
backgroundItem->initSurface();
// TODO Insert objects from item44s
// TODO Insert IDs from item48s
// TODO camera_fadeClear();
int index = backgroundItem->_bgRes->findMasterBgIndex();
_vm->_camera->set(backgroundItem->_bgRes->_bgInfos[index - 1]._panPoint, backgroundItem->_bgRes->_bgInfos[index - 1]._surfInfo._dimensions);
// NOTE Skipped palette loading (not used in BBDOU)
}
void BackgroundResourceLoader::unload(Resource *resource) {
}
void BackgroundResourceLoader::buildFilename(Resource *resource) {
resource->_filename = Common::String::format("%08X.bg", resource->_resId);
}
bool BackgroundResourceLoader::isFlag(int flag) {
return
flag == kRlfLoadFile;
}
// TileMap
void TileMap::load(byte *dataStart, Common::SeekableReadStream &stream) {
_width = stream.readSint16LE();
_height = stream.readSint16LE();
stream.skip(4); // Unknown
uint32 mapOffs = stream.pos();
_map = dataStart + mapOffs;
debug("TileMap::load() _width: %d; _height: %d",
_width, _height);
}
// BgInfo
void BgInfo::load(byte *dataStart, Common::SeekableReadStream &stream) {
_flags = stream.readUint32LE();
stream.skip(2); // Unknown
_priorityBase = stream.readSint16LE();
_surfInfo.load(stream);
loadPoint(stream, _panPoint);
uint32 tileMapOffs = stream.readUint32LE();
uint32 tilePixelsOffs = stream.readUint32LE();
stream.seek(tileMapOffs);
_tileMap.load(dataStart, stream);
_tilePixels = dataStart + tilePixelsOffs;
debug("BgInfo::load() _flags: %08X; _priorityBase: %d; tileMapOffs: %08X; tilePixelsOffs: %08X",
_flags, _priorityBase, tileMapOffs, tilePixelsOffs);
}
// PriorityLayer
void PriorityLayer::load(byte *dataStart, Common::SeekableReadStream &stream) {
_width = stream.readUint16LE();
_height = stream.readUint16LE();
uint32 mapOffs = stream.readUint32LE();
uint32 valuesOffs = stream.readUint32LE();
_map = dataStart + mapOffs;
_mapWidth = READ_LE_UINT16(_map + 0);
_mapHeight = READ_LE_UINT16(_map + 2);
_map += 8;
_values = dataStart + valuesOffs;
debug("PriorityLayer::load() _width: %d; _height: %d; mapOffs: %08X; valuesOffs: %08X; _mapWidth: %d; _mapHeight: %d",
_width, _height, mapOffs, valuesOffs, _mapWidth, _mapHeight);
}
int PriorityLayer::getPriority(Common::Point pos) {
pos.x = CLIP<int16>(pos.x, 0, _width - 1);
pos.y = CLIP<int16>(pos.y, 0, _height - 1);
const int16 tx = pos.x / 32, sx = pos.x % 32;
const int16 ty = pos.y / 8, sy = pos.y % 8;
uint16 mapIndex = READ_LE_UINT16(_map + 2 * (tx + ty * _mapWidth)) - 1;
return _values[mapIndex * 32 * 8 + sx + sy * 32];
}
void ScaleLayer::load(byte *dataStart, Common::SeekableReadStream &stream) {
_height = stream.readUint16LE();
stream.skip(2);
uint32 valuesOffs = stream.readUint32LE();
_values = dataStart + valuesOffs;
debug("ScaleLayer::load() _height: %d; valuesOffs: %08X",
_height, valuesOffs);
}
int ScaleLayer::getScale(Common::Point pos) {
pos.y = CLIP<int16>(pos.y, 0, _height - 1);
return _values[pos.y];
}
// BackgroundResource
BackgroundResource::BackgroundResource() {
}
BackgroundResource::~BackgroundResource() {
// TODO Free stuff
}
void BackgroundResource::load(byte *data, uint32 dataSize) {
Common::MemoryReadStream stream(data, dataSize, DisposeAfterUse::NO);
// TODO A lot
// Load background pixels
stream.seek(0x0A);
_bgInfosCount = stream.readUint16LE();
_bgInfos = new BgInfo[_bgInfosCount];
stream.seek(0x20);
uint32 bgInfosOffs = stream.readUint32LE();
for (uint i = 0; i < _bgInfosCount; ++i) {
stream.seek(bgInfosOffs + i * 0x1C);
_bgInfos[i].load(data, stream);
}
// Load scale layers
stream.seek(0x10);
_scaleLayersCount = stream.readUint16LE();
_scaleLayers = new ScaleLayer[_scaleLayersCount];
stream.seek(0x2C);
uint32 scaleLayersOffs = stream.readUint32LE();
debug("_scaleLayersCount: %d", _scaleLayersCount);
for (uint i = 0; i < _scaleLayersCount; ++i) {
stream.seek(scaleLayersOffs + i * 8);
_scaleLayers[i].load(data, stream);
}
}
int BackgroundResource::findMasterBgIndex() {
int index = 1;
while (!_bgInfos[index - 1]._flags & 1)
++index;
return index;
}
// BackgroundItem
BackgroundItem::BackgroundItem(IllusionsEngine *vm) : _vm(vm), _tag(0), _pauseCtr(0), _bgRes(0) {
}
void BackgroundItem::initSurface() {
for (uint i = 0; i < kMaxBackgroundItemSurfaces; ++i)
_surfaces[i] = 0;
for (uint i = 0; i < _bgRes->_bgInfosCount; ++i) {
BgInfo *bgInfo = &_bgRes->_bgInfos[i];
_panPoints[i] = bgInfo->_panPoint;
_surfaces[i] = _vm->allocSurface(bgInfo->_surfInfo);
drawTiles(_surfaces[i], bgInfo->_tileMap, bgInfo->_tilePixels);
}
}
void BackgroundItem::freeSurface() {
for (uint i = 0; i < _bgRes->_bgInfosCount; ++i) {
_surfaces[i]->free();
delete _surfaces[i];
_surfaces[i] = 0;
}
}
void BackgroundItem::drawTiles(Graphics::Surface *surface, TileMap &tileMap, byte *tilePixels) {
const int kTileWidth = 32;
const int kTileHeight = 8;
const int kTileSize = kTileWidth * kTileHeight * 2;
uint tileMapIndex = 0;
for (int tileY = 0; tileY < tileMap._height; ++tileY) {
int tileDestY = tileY * kTileHeight;
int tileDestH = MIN(kTileHeight, surface->h - tileDestY);
for (int tileX = 0; tileX < tileMap._width; ++tileX) {
int tileDestX = tileX * kTileWidth;
int tileDestW = MIN(kTileWidth, surface->w - tileDestX);
uint16 tileIndex = READ_LE_UINT16(tileMap._map + 2 * tileMapIndex);
++tileMapIndex;
byte *src = tilePixels + (tileIndex - 1) * kTileSize;
byte *dst = (byte*)surface->getBasePtr(tileDestX, tileDestY);
for (int h = 0; h < tileDestH; ++h) {
for (int w = 0; w < tileDestW; ++w) {
uint16 pixel = READ_LE_UINT16(src + w * 2);
WRITE_LE_UINT16(dst + w * 2, pixel);
}
dst += surface->pitch;
src += kTileWidth * 2;
}
}
}
}
void BackgroundItem::pause() {
// TODO
++_pauseCtr;
if (_pauseCtr <= 1) {
/* TODO
for (uint i = 0; i < _bgRes->_item48sCount; ++i)
krndictRemoveID(_bgRes->_item48s[i].id);
*/
// TODO _vm->setDefPointDimensions1();
// TODO memcpy(&_savedCamera, &_vm->camera, sizeof(backgroundItem->savedCamera));
/* Unused
_savedPalette = malloc(1024);
savePalette(_savedPalette);
*/
freeSurface();
}
}
void BackgroundItem::unpause() {
// TODO
--_pauseCtr;
if (_pauseCtr <= 0) {
/* TODO
for (uint i = 0; i < _bgRes->_item48sCount; ++i)
krndictAddID(_bgRes->_item48s[i].id, _bgRes->_item48s[i]);
*/
initSurface();
/* Unused
restorePalette(_savedPalette, 1, 256);
free(_savedPalette);
_savedPalette = 0;
*/
// TODO _vm->_screen->_fadeClear();
// TODO memcpy(&_vm->camera, &_savedCamera, sizeof(SavedCamera));
/* TODO
currTime = krnxxxGetCurrentTime();
_vm->_camera.panStartTime = currTime;
*/
_vm->_backgroundItems->refreshPan();
}
}
void BackgroundItem::refreshPan(WidthHeight &dimensions) {
Common::Point screenOffs = _vm->_camera->getScreenOffset();
int x = dimensions._width - 640;
int y = dimensions._height - 480;
for (uint i = 0; i < _bgRes->_bgInfosCount; ++i) {
const BgInfo &bgInfo = _bgRes->_bgInfos[i];
if (bgInfo._flags & 1) {
_panPoints[i] = screenOffs;
} else {
Common::Point newOffs(0, 0);
if (x > 0 && bgInfo._surfInfo._dimensions._width - 640 > 0)
newOffs.x = screenOffs.x * (bgInfo._surfInfo._dimensions._width - 640) / x;
if (y > 0 && bgInfo._surfInfo._dimensions._height - 480 > 0)
newOffs.y = screenOffs.y * (bgInfo._surfInfo._dimensions._height - 480) / y;
_panPoints[i] = newOffs;
}
}
}
// BackgroundItems
BackgroundItems::BackgroundItems(IllusionsEngine *vm)
: _vm(vm) {
}
BackgroundItems::~BackgroundItems() {
}
BackgroundItem *BackgroundItems::allocBackgroundItem() {
BackgroundItem *backgroundItem = new BackgroundItem(_vm);
_items.push_back(backgroundItem);
return backgroundItem;
}
void BackgroundItems::pauseByTag(uint32 tag) {
for (ItemsIterator it = _items.begin(); it != _items.end(); ++it)
if ((*it)->_tag == tag)
(*it)->pause();
}
void BackgroundItems::unpauseByTag(uint32 tag) {
for (ItemsIterator it = _items.begin(); it != _items.end(); ++it)
if ((*it)->_tag == tag)
(*it)->unpause();
}
BackgroundItem *BackgroundItems::findActiveBackground() {
for (ItemsIterator it = _items.begin(); it != _items.end(); ++it)
if ((*it)->_pauseCtr == 0)
return (*it);
return 0;
}
BackgroundResource *BackgroundItems::getActiveBgResource() {
BackgroundItem *background = findActiveBackground();
if (background)
return background->_bgRes;
return 0;
}
WidthHeight BackgroundItems::getMasterBgDimensions() {
BackgroundItem *backgroundItem = findActiveBackground();
int16 index = backgroundItem->_bgRes->findMasterBgIndex();
return backgroundItem->_bgRes->_bgInfos[index - 1]._surfInfo._dimensions;
}
void BackgroundItems::refreshPan() {
BackgroundItem *backgroundItem = findActiveBackground();
if (backgroundItem) {
WidthHeight dimensions = getMasterBgDimensions();
backgroundItem->refreshPan(dimensions);
}
}
BackgroundItem *BackgroundItems::debugFirst() {
return *(_items.begin());
}
} // End of namespace Illusions
|