aboutsummaryrefslogtreecommitdiff
path: root/engines/mutationofjb/room.cpp
blob: 783edae268a0e74eaf7af020a8f5cbd02216a891 (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
/* 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());
}

void Room::redraw() {
	if (!_game->isCurrentSceneMap()) {
		Common::Rect rect(0, 0, GAME_AREA_WIDTH, GAME_AREA_HEIGHT);
		_screen->blitFrom(_background.rawSurface(), rect, Common::Point(0, 0));
	}

	Scene *const currentScene = _game->getGameData().getCurrentScene();
	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);
		}
	}
}

}