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
|
/* 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 "mutationofjb/room.h"
#include "mutationofjb/animationdecoder.h"
#include "mutationofjb/encryptedfile.h"
#include "mutationofjb/game.h"
#include "mutationofjb/gamedata.h"
#include "mutationofjb/util.h"
#include "common/rect.h"
#include "common/str.h"
#include "graphics/screen.h"
namespace MutationOfJB {
enum {
GAME_AREA_WIDTH = 320,
GAME_AREA_HEIGHT = 139
};
class RoomAnimationDecoderCallback : public AnimationDecoderCallback {
public:
RoomAnimationDecoderCallback(Room &room) : _room(room) {}
virtual void onFrame(int frameNo, Graphics::Surface &surface) override;
virtual void onPaletteUpdated(byte palette[PALETTE_SIZE]) override;
private:
Room &_room;
};
void RoomAnimationDecoderCallback::onPaletteUpdated(byte palette[PALETTE_SIZE]) {
_room._screen->setPalette(palette, 0x00, 0xC0); // Load only 0xC0 colors.
}
void RoomAnimationDecoderCallback::onFrame(int frameNo, Graphics::Surface &surface) {
if (frameNo == 0) {
Common::Rect rect(0, 0, GAME_AREA_WIDTH, GAME_AREA_HEIGHT);
if (_room._game->isCurrentSceneMap()) {
rect = Common::Rect(0, 0, 320, 200);
} else {
_room._background.blitFrom(surface, rect, Common::Point(0, 0));
}
_room._screen->blitFrom(surface, rect, Common::Point(0, 0));
}
const int frameNo1 = frameNo + 1;
Scene *scene = _room._game->getGameData().getCurrentScene();
if (scene) {
const uint8 noObjects = scene->getNoObjects();
for (int i = 0; i < noObjects; ++i) {
Object &object = scene->_objects[i];
const uint16 startFrame = (object._roomFrameMSB << 8) + object._roomFrameLSB;
if (frameNo1 >= startFrame && frameNo1 < startFrame + object._numFrames) {
const int x = object._x;
const int y = object._y;
const int w = (object._width + 3) / 4 * 4; // Original code uses this to round up width to a multiple of 4.
const int h = object._height;
Common::Rect rect(x, y, x + w, y + h);
const Graphics::Surface sharedSurface = surface.getSubArea(rect);
Graphics::Surface outSurface;
outSurface.copyFrom(sharedSurface);
_room._surfaces[_room._objectsStart[i] + frameNo1 - startFrame] = outSurface;
}
}
}
}
Room::Room(Game *game, Graphics::Screen *screen) : _game(game), _screen(screen), _background(GAME_AREA_WIDTH, GAME_AREA_HEIGHT) {}
bool Room::load(uint8 roomNumber, bool roomB) {
_objectsStart.clear();
_surfaces.clear(); // TODO: Fix memory leak.
Scene *const scene = _game->getGameData().getCurrentScene();
if (scene) {
const uint8 noObjects = scene->getNoObjects();
for (int i = 0; i < noObjects; ++i) {
uint8 firstIndex = 0;
if (i != 0) {
firstIndex = _objectsStart[i - 1] + scene->_objects[i - 1]._numFrames;
}
_objectsStart.push_back(firstIndex);
uint8 numAnims = scene->_objects[i]._numFrames;
while (numAnims--) {
_surfaces.push_back(Graphics::Surface());
}
}
}
const Common::String fileName = Common::String::format("room%d%s.dat", roomNumber, roomB ? "b" : "");
AnimationDecoder decoder(fileName);
RoomAnimationDecoderCallback callback(*this);
return decoder.decode(&callback);
}
// TODO: Take the threshold value from ATN data.
struct ThresholdBlitOperation {
byte operator()(const byte srcColor, const byte destColor) {
if (destColor <= 0xBF) {
// Within threshold - replace destination with source color.
return srcColor;
}
// Outside of threshold - keep destination color.
return destColor;
}
};
void Room::drawObjectAnimation(uint8 objectId, int animOffset) {
Scene *const scene = _game->getGameData().getCurrentScene();
if (!scene) {
return;
}
Object *const object = scene->getObject(objectId);
if (!object) {
return;
}
const int startFrame = _objectsStart[objectId - 1];
const int animFrame = startFrame + animOffset;
blit_if(_surfaces[animFrame], *_screen, Common::Point(object->_x, object->_y), ThresholdBlitOperation());
if (!_game->isCurrentSceneMap())
blit_if(_surfaces[animFrame], _background, Common::Point(object->_x, object->_y), ThresholdBlitOperation());
}
void Room::drawObject(uint8 objectId, uint8 overrideFrame) {
Scene *const currentScene = _game->getGameData().getCurrentScene();
Object *const object = currentScene->getObject(objectId);
drawObjectAnimation(objectId, (overrideFrame ? overrideFrame : object->_currentFrame) - _objectsStart[objectId - 1] - 1);
}
void Room::drawBitmap(uint8 bitmapId) {
GameData &gameData = _game->getGameData();
Scene *const scene = gameData.getCurrentScene();
if (!scene) {
return;
}
Bitmap *const bitmap = scene->getBitmap(bitmapId);
if (!bitmap) {
return;
}
Common::Rect bitmapArea(bitmap->_x1, bitmap->_y1, bitmap->_x2 + 1, bitmap->_y2 + 1);
drawFrames(bitmap->_roomFrame - 1, bitmap->_roomFrame - 1, bitmapArea, 0xC0);
}
void Room::drawStatic(Static *const stat) {
if (!stat || !stat->allowsImplicitPickup()) {
return;
}
const uint8 frame = stat->_active ? 1 : 2; // Hardcoded values. Active is taken from frame 1 and inactive from frame 2.
const Common::Rect staticArea(stat->_x, stat->_y, stat->_x + stat->_width, stat->_y + stat->_height);
drawFrames(frame, frame, staticArea, 0xC0); // Hardcoded threshold.
}
void Room::drawFrames(int fromFrame, int toFrame, const Common::Rect &area, uint8 threshold) {
GameData &gameData = _game->getGameData();
Scene *const scene = gameData.getCurrentScene();
if (!scene) {
return;
}
const Common::String fileName = Common::String::format("room%d%s.dat", gameData._currentScene, gameData._partB ? "b" : "");
{
AnimationDecoder decoder(fileName, *_screen);
decoder.setPartialMode(fromFrame, toFrame, area, threshold);
decoder.decode(nullptr);
if (!area.isEmpty())
_screen->getSubArea(area); // Add dirty rect.
else
_screen->makeAllDirty();
}
if (!_game->isCurrentSceneMap()) {
AnimationDecoder decoder(fileName, _background);
decoder.setPartialMode(fromFrame, toFrame, area, threshold);
decoder.decode(nullptr);
}
}
void Room::initialDraw() {
Scene *const currentScene = _game->getGameData().getCurrentScene();
for (uint8 i = 0; i < currentScene->getNoStatics(); ++i) {
Static *const stat = currentScene->getStatic(i + 1);
if (stat->_active && stat->allowsImplicitPickup()) {
drawStatic(stat);
}
}
for (uint8 i = 0; i < currentScene->getNoObjects(); ++i) {
Object *const obj = currentScene->getObject(i + 1);
if (obj->_active) {
drawObjectAnimation(i + 1, obj->_currentFrame - _objectsStart[i] - 1);
}
}
for (uint8 i = 0; i < currentScene->getNoBitmaps(); ++i) {
Bitmap *const bitmap = currentScene->getBitmap(i + 1);
if (bitmap->_isVisible && bitmap->_roomFrame > 0) {
drawBitmap(i + 1);
}
}
}
void Room::redraw(bool useBackgroundBuffer) {
if (useBackgroundBuffer && !_game->isCurrentSceneMap()) {
Common::Rect rect(0, 0, GAME_AREA_WIDTH, GAME_AREA_HEIGHT);
_screen->blitFrom(_background.rawSurface(), rect, Common::Point(0, 0));
} else {
initialDraw();
}
}
}
|