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
|
/* 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 3 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, see <http://www.gnu.org/licenses/>.
*
*/
#include "queen/bankman.h"
#include "queen/resource.h"
#include "common/debug.h"
namespace Queen {
BankManager::BankManager(Resource *res)
: _res(res) {
memset(_frames, 0, sizeof(_frames));
memset(_banks, 0, sizeof(_banks));
}
BankManager::~BankManager() {
for (uint32 i = 0; i < MAX_BANKS_NUMBER; ++i) {
close(i);
}
eraseFrames(true);
}
void BankManager::load(const char *bankname, uint32 bankslot) {
debug(9, "BankManager::load(%s, %d)", bankname, bankslot);
assert(bankslot < MAX_BANKS_NUMBER);
PackedBank *bank = &_banks[bankslot];
if (!scumm_stricmp(bankname, bank->name)) {
debug(9, "BankManager::load() bank '%s' already loaded", bankname);
return;
}
close(bankslot);
if (_res->getPlatform() == Common::kPlatformAmiga && !_res->fileExists(bankname)) {
debug(9, "BankManager::load() bank '%s' doesn't exist", bankname);
return;
}
bank->data = _res->loadFile(bankname);
if (_res->getPlatform() == Common::kPlatformAmiga) {
uint16 entries = READ_BE_UINT16(bank->data + 4);
debug(9, "BankManager::load() entries = %d", entries);
assert(entries < MAX_BANK_SIZE);
uint32 offset = 6;
_banks[bankslot].indexes[0] = offset;
for (uint16 i = 1; i <= entries; ++i) {
_banks[bankslot].indexes[i] = offset;
uint16 dataSize = READ_BE_UINT16(bank->data + offset + 10);
offset += dataSize + 12;
}
} else {
uint16 entries = READ_LE_UINT16(bank->data);
debug(9, "BankManager::load() entries = %d", entries);
assert(entries < MAX_BANK_SIZE);
uint32 offset = 2;
_banks[bankslot].indexes[0] = offset;
for (uint16 i = 1; i <= entries; ++i) {
_banks[bankslot].indexes[i] = offset;
uint16 w = READ_LE_UINT16(bank->data + offset + 0);
uint16 h = READ_LE_UINT16(bank->data + offset + 2);
offset += w * h + 8;
}
}
// mark this bank as loaded
Common::strcpy_s(bank->name, bankname);
}
static void convertPlanarBitmap(uint8 *dst, int dstPitch, const uint8 *src, int w, int h, int plane) {
assert(w != 0 && h != 0);
int planarSize = plane * h * w * 2;
uint8 *planarBuf = new uint8[ planarSize ];
uint8 *dstPlanar = planarBuf;
while (planarSize > 0) {
if (src[0] == 0) {
int count = src[1];
memset(dstPlanar, 0, count);
dstPlanar += count;
src += 2;
planarSize -= count;
} else {
*dstPlanar++ = *src++;
--planarSize;
}
}
src = planarBuf;
int i = 0;
int planeSize = h * w * 2;
while (h--) {
for (int x = 0; x < w * 2; ++x) {
for (int b = 0; b < 8; ++b) {
const uint8 mask = (1 << (7 - b));
uint8 color = 0;
for (int p = 0; p < plane; ++p) {
if (src[planeSize * p + i] & mask) {
color |= (1 << p);
}
}
dst[8 * x + b] = color;
}
++i;
}
dst += dstPitch;
}
delete[] planarBuf;
}
void BankManager::unpack(uint32 srcframe, uint32 dstframe, uint32 bankslot) {
debug(9, "BankManager::unpack(%d, %d, %d)", srcframe, dstframe, bankslot);
assert(bankslot < MAX_BANKS_NUMBER);
PackedBank *bank = &_banks[bankslot];
assert(bank->data != nullptr);
assert(dstframe < MAX_FRAMES_NUMBER);
BobFrame *bf = &_frames[dstframe];
delete[] bf->data;
bf->data = nullptr;
const uint8 *p = bank->data + bank->indexes[srcframe];
if (_res->getPlatform() == Common::kPlatformAmiga) {
uint16 w = READ_BE_UINT16(p + 0);
uint16 h = READ_BE_UINT16(p + 2);
uint16 plane = READ_BE_UINT16(p + 4);
bf->xhotspot = READ_BE_UINT16(p + 6);
bf->yhotspot = READ_BE_UINT16(p + 8);
bf->width = w * 16;
bf->height = h;
uint32 size = bf->width * bf->height;
if (size != 0) {
bf->data = new uint8[ size ];
convertPlanarBitmap(bf->data, bf->width, p + 12, w, h, plane);
}
} else {
bf->width = READ_LE_UINT16(p + 0);
bf->height = READ_LE_UINT16(p + 2);
bf->xhotspot = READ_LE_UINT16(p + 4);
bf->yhotspot = READ_LE_UINT16(p + 6);
uint32 size = bf->width * bf->height;
if (size != 0) {
bf->data = new uint8[ size ];
memcpy(bf->data, p + 8, size);
}
}
}
void BankManager::overpack(uint32 srcframe, uint32 dstframe, uint32 bankslot) {
debug(9, "BankManager::overpack(%d, %d, %d)", srcframe, dstframe, bankslot);
assert(bankslot < MAX_BANKS_NUMBER);
PackedBank *bank = &_banks[bankslot];
assert(bank->data != nullptr);
assert(dstframe < MAX_FRAMES_NUMBER);
BobFrame *bf = &_frames[dstframe];
const uint8 *p = bank->data + bank->indexes[srcframe];
if (_res->getPlatform() == Common::kPlatformAmiga) {
uint16 w = READ_BE_UINT16(p + 0);
uint16 h = READ_BE_UINT16(p + 2);
uint16 plane = READ_BE_UINT16(p + 4);
uint16 src_w = w * 16;
uint16 src_h = h;
if (bf->width < src_w || bf->height < src_h) {
unpack(srcframe, dstframe, bankslot);
} else {
convertPlanarBitmap(bf->data, bf->width, p + 12, w, h, plane);
}
} else {
uint16 src_w = READ_LE_UINT16(p + 0);
uint16 src_h = READ_LE_UINT16(p + 2);
// unpack if destination frame is smaller than source
if (bf->width < src_w || bf->height < src_h) {
unpack(srcframe, dstframe, bankslot);
} else {
// copy data 'over' destination frame (without updating frame header)
memcpy(bf->data, p + 8, src_w * src_h);
}
}
}
void BankManager::close(uint32 bankslot) {
debug(9, "BankManager::close(%d)", bankslot);
assert(bankslot < MAX_BANKS_NUMBER);
PackedBank *bank = &_banks[bankslot];
delete[] bank->data;
memset(bank, 0, sizeof(PackedBank));
}
BobFrame *BankManager::fetchFrame(uint32 index) {
debug(9, "BankManager::fetchFrame(%d)", index);
assert(index < MAX_FRAMES_NUMBER);
BobFrame *bf = &_frames[index];
assert((bf->width == 0 && bf->height == 0) || bf->data != nullptr);
return bf;
}
void BankManager::eraseFrame(uint32 index) {
debug(9, "BankManager::eraseFrame(%d)", index);
assert(index < MAX_FRAMES_NUMBER);
BobFrame *bf = &_frames[index];
delete[] bf->data;
memset(bf, 0, sizeof(BobFrame));
}
void BankManager::eraseFrames(bool joe) {
for (uint32 i = joe ? 0 : FRAMES_JOE; i < MAX_FRAMES_NUMBER; ++i) {
eraseFrame(i);
}
}
} // End of namespace Queen
|