ScummVM API documentation
animation_layers.h
1/* ScummVM - Graphic Adventure Engine
2 *
3 * ScummVM is the legal property of its developers, whose names
4 * are too numerous to list here. Please refer to the COPYRIGHT
5 * file distributed with this source distribution.
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21
22#ifndef HOLLYWOOD_MONSTERS_SCENES_ANIMATION_LAYERS_H
23#define HOLLYWOOD_MONSTERS_SCENES_ANIMATION_LAYERS_H
24
25#include "common/array.h"
26#include "common/types.h"
27
28namespace Hollywood {
29
30// Tracks one resource-backed sprite layer. A layer points at one chunk and one
31// descriptor table; frameMap optionally converts a scene's logical animation
32// frame into the descriptor index used by the resource.
35 visible(false),
36 chunkIndex(0),
37 descriptorCount(0),
38 frameMap(nullptr),
39 frameMapSize(0),
40 frameIndex(0),
41 previousDescriptorIndex(0),
42 hasPreviousDescriptor(false) {
43 }
44
45 void configure(uint newChunkIndex, uint16 newDescriptorCount, const byte *newFrameMap, uint newFrameMapSize) {
46 chunkIndex = newChunkIndex;
47 descriptorCount = newDescriptorCount;
48 frameMap = newFrameMap;
49 frameMapSize = newFrameMapSize;
50 reset(0);
51 }
52
53 void reset(byte newFrameIndex) {
54 frameIndex = newFrameIndex;
55 previousDescriptorIndex = descriptorIndex();
56 hasPreviousDescriptor = false;
57 }
58
59 void setFrame(byte newFrameIndex) {
60 const uint16 oldDescriptorIndex = descriptorIndex();
61 frameIndex = newFrameIndex;
62 const uint16 newDescriptorIndex = descriptorIndex();
63 if (oldDescriptorIndex != newDescriptorIndex) {
64 previousDescriptorIndex = oldDescriptorIndex;
65 hasPreviousDescriptor = true;
66 }
67 }
68
69 uint16 descriptorIndex() const {
70 if (frameMap != nullptr && frameIndex < frameMapSize)
71 return frameMap[frameIndex];
72 return frameIndex;
73 }
74
75 bool visible;
76 uint chunkIndex;
77 uint16 descriptorCount;
78 const byte *frameMap;
79 uint frameMapSize;
80 byte frameIndex;
81 uint16 previousDescriptorIndex;
82 bool hasPreviousDescriptor;
83};
84
85enum SceneAnimationStratum {
86 kSceneAnimationBehindActors,
87 kSceneAnimationInFrontOfActors,
88 kSceneAnimationActorReplacement,
89 kSceneAnimationScenePlaced
90};
91
93 SceneAnimationStratum stratum;
94 uint chunkIndex;
95 uint16 descriptorCount;
96 const byte *frameMap;
97 uint frameMapSize;
98 bool visible;
99 byte initialFrame;
100};
101
110public:
111 static constexpr uint kInvalidLayer = 0xffffffff;
112
113 void clear() {
114 _layers.clear();
115 }
116
117 template<uint size>
118 void configure(const SceneLayerSpec (&specs)[size]) {
119 // Reuse storage so same-sized reconfiguration keeps registered layers stable.
120 if (_layers.size() != size)
121 _layers.resize(size);
122 for (uint i = 0; i < size; ++i)
123 configureLayer(i, specs[i]);
124 }
125
126 void configureLayer(uint id, const SceneLayerSpec &spec) {
127 configureLayer(id, spec.stratum, spec.chunkIndex, spec.descriptorCount,
128 spec.frameMap, spec.frameMapSize, spec.visible, spec.initialFrame);
129 }
130
131 void configureLayer(uint id, SceneAnimationStratum stratum, uint chunkIndex,
132 uint16 descriptorCount, const byte *frameMap, uint frameMapSize,
133 bool visible = true, byte initialFrame = 0) {
134 if (id >= _layers.size())
135 _layers.resize(id + 1);
136
137 LayerState &state = _layers[id];
138 state.configured = true;
139 state.defaultStratum = stratum;
140 state.stratum = stratum;
141 state.defaultVisible = visible;
142 state.initialFrame = initialFrame;
143 state.layer.configure(chunkIndex, descriptorCount, frameMap, frameMapSize);
144 state.layer.reset(initialFrame);
145 state.layer.visible = visible && availableFrameCount(state.layer) != 0;
146 }
147
148 uint addLayer(SceneAnimationStratum stratum, uint chunkIndex, uint16 descriptorCount,
149 const byte *frameMap, uint frameMapSize, bool visible = true,
150 byte initialFrame = 0) {
151 const uint id = _layers.size();
152 configureLayer(id, stratum, chunkIndex, descriptorCount, frameMap,
153 frameMapSize, visible, initialFrame);
154 return id;
155 }
156
157 // Rebinds the active resource without changing default visibility or initial frame.
158 void setLayerResource(uint id, uint chunkIndex, uint16 descriptorCount,
159 const byte *frameMap, uint frameMapSize) {
160 if (!hasLayer(id))
161 return;
162
163 LayerState &state = _layers[id];
164 const bool visible = state.layer.visible;
165 state.layer.configure(chunkIndex, descriptorCount, frameMap, frameMapSize);
166 state.layer.visible = visible && availableFrameCount(state.layer) != 0;
167 }
168
169 void reset() {
170 for (uint i = 0; i < _layers.size(); ++i) {
171 LayerState &state = _layers[i];
172 if (!state.configured)
173 continue;
174 state.stratum = state.defaultStratum;
175 state.layer.reset(state.initialFrame);
176 state.layer.visible = state.defaultVisible && availableFrameCount(state.layer) != 0;
177 }
178 }
179
180 uint layerCount() const {
181 return _layers.size();
182 }
183
184 bool hasLayer(uint id) const {
185 return id < _layers.size() && _layers[id].configured;
186 }
187
188 bool isInStratum(uint id, SceneAnimationStratum stratum) const {
189 return hasLayer(id) && _layers[id].stratum == stratum;
190 }
191
192 void setLayerStratum(uint id, SceneAnimationStratum stratum) {
193 if (hasLayer(id))
194 _layers[id].stratum = stratum;
195 }
196
197 bool hasVisibleLayers(SceneAnimationStratum stratum) const {
198 for (uint i = 0; i < _layers.size(); ++i) {
199 if (_layers[i].configured && _layers[i].stratum == stratum &&
200 _layers[i].layer.visible) {
201 return true;
202 }
203 }
204 return false;
205 }
206
207 ResourceSpriteLayer &layer(uint id) {
208 return _layers[id].layer;
209 }
210
211 const ResourceSpriteLayer &layer(uint id) const {
212 return _layers[id].layer;
213 }
214
215 void setLayerVisible(uint id, bool visible) {
216 if (hasLayer(id))
217 _layers[id].layer.visible = visible && availableFrameCount(_layers[id].layer) != 0;
218 }
219
220 void setLayerChunk(uint id, uint chunkIndex) {
221 if (hasLayer(id))
222 _layers[id].layer.chunkIndex = chunkIndex;
223 }
224
225 // Changes the logical frame mapping without resetting the current frame.
226 void setLayerFrameMap(uint id, const byte *frameMap, uint frameMapSize) {
227 if (!hasLayer(id))
228 return;
229 _layers[id].layer.frameMap = frameMap;
230 _layers[id].layer.frameMapSize = frameMapSize;
231 }
232
233 void setLayerFrame(uint id, byte frameIndex) {
234 if (hasLayer(id))
235 _layers[id].layer.setFrame(frameIndex);
236 }
237
238 void resetLayer(uint id, byte frameIndex) {
239 if (hasLayer(id))
240 _layers[id].layer.reset(frameIndex);
241 }
242
243 // Resets cleanup history before making a layer visible.
244 void showLayerAtFrame(uint id, byte frameIndex) {
245 if (!hasLayer(id))
246 return;
247 _layers[id].layer.reset(frameIndex);
248 _layers[id].layer.visible = availableFrameCount(_layers[id].layer) != 0;
249 }
250
251 bool advanceLayerFrame(uint id, byte lastFrame) {
252 if (!hasLayer(id) || _layers[id].layer.frameIndex >= lastFrame)
253 return false;
254 _layers[id].layer.setFrame(_layers[id].layer.frameIndex + 1);
255 return true;
256 }
257
258 void setVisibleLayerFrame(uint id, byte frameIndex) {
259 if (hasLayer(id) && _layers[id].layer.visible)
260 _layers[id].layer.setFrame(frameIndex);
261 }
262
263 void setVisibleLayerFrameClamped(uint id, uint frameIndex) {
264 if (!hasLayer(id) || !_layers[id].layer.visible)
265 return;
266
267 const uint count = availableFrameCount(_layers[id].layer);
268 if (count == 0)
269 return;
270
271 if (frameIndex >= count)
272 frameIndex = count - 1;
273 if (frameIndex > 0xff)
274 frameIndex = 0xff;
275 _layers[id].layer.setFrame((byte)frameIndex);
276 }
277
278 byte layerFrame(uint id) const {
279 return hasLayer(id) ? _layers[id].layer.frameIndex : 0;
280 }
281
282 bool layerVisible(uint id) const {
283 return hasLayer(id) && _layers[id].layer.visible;
284 }
285
286 uint maximumVisibleFrameCount() const {
287 uint result = 0;
288 for (uint i = 0; i < _layers.size(); ++i) {
289 if (!_layers[i].configured || !_layers[i].layer.visible)
290 continue;
291 const uint count = availableFrameCount(_layers[i].layer);
292 if (count > result)
293 result = count;
294 }
295 return result;
296 }
297
298private:
299 struct LayerState {
300 LayerState() :
301 configured(false),
302 defaultStratum(kSceneAnimationBehindActors),
303 stratum(kSceneAnimationBehindActors),
304 defaultVisible(false),
305 initialFrame(0),
306 layer() {
307 }
308
309 bool configured;
310 SceneAnimationStratum defaultStratum;
311 SceneAnimationStratum stratum;
312 bool defaultVisible;
313 byte initialFrame;
315 };
316
317 uint availableFrameCount(const ResourceSpriteLayer &layer) const {
318 return layer.frameMap != nullptr ? layer.frameMapSize : layer.descriptorCount;
319 }
320
322};
323
324} // End of namespace Hollywood
325
326#endif // HOLLYWOOD_MONSTERS_SCENES_ANIMATION_LAYERS_H
size_type size() const
Definition: array.h:316
void clear()
Definition: array.h:321
void resize(size_type newSize)
Definition: array.h:412
Definition: animation_layers.h:109
Definition: animation_layers.h:33
Definition: animation_layers.h:92