1 /* ResidualVM - A 3D game interpreter
2  *
3  * ResidualVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the AUTHORS
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (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, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #ifndef STARK_RESOURCES_ANIM_H
24 #define STARK_RESOURCES_ANIM_H
25 
26 #include "common/rect.h"
27 #include "common/str.h"
28 
29 #include "engines/stark/resources/object.h"
30 
31 namespace Stark {
32 
33 class SkeletonAnim;
34 class VisualActor;
35 class VisualProp;
36 class VisualSmacker;
37 class Visual;
38 namespace Formats {
39 class XRCReadStream;
40 }
41 
42 namespace Resources {
43 
44 class Direction;
45 class Image;
46 class Item;
47 class ItemVisual;
48 
49 /**
50  * Animation base class
51  *
52  * Animations provide a time dependent visual state to Items
53  */
54 class Anim : public Object {
55 public:
56 	static const Type::ResourceType TYPE = Type::kAnim;
57 
58 	enum SubType {
59 		kAnimImages   = 1,
60 		kAnimProp     = 2,
61 		kAnimVideo    = 3,
62 		kAnimSkeleton = 4
63 	};
64 
65 	enum ActionUsage {
66 		kActionUsagePassive = 1,
67 		kActionUsageActive  = 2
68 	};
69 
70 	enum UIUsage {
71 		kUIUsageInventory = 1,
72 		kUIUsageUseCursorPassive = 4,
73 		kUIUsageUseCursorActive = 5
74 	};
75 
76 	enum ActorUsage {
77 		kActorUsageIdle       = 1,
78 		kActorUsageWalk       = 2,
79 		kActorUsageTalk       = 3,
80 		kActorUsageRun        = 6,
81 		kActorUsageIdleAction = 10
82 	};
83 
84 	/** Anim factory */
85 	static Object *construct(Object *parent, byte subType, uint16 index, const Common::String &name);
86 
87 	Anim(Object *parent, byte subType, uint16 index, const Common::String &name);
88 	virtual ~Anim();
89 
90 	// Resource API
91 	virtual void readData(Formats::XRCReadStream *stream) override;
92 
93 	/** Sets the animation frame to be displayed */
94 	virtual void selectFrame(uint32 frameIndex);
95 
96 	/** Obtain the Visual to be used to render the animation */
97 	virtual Visual *getVisual() = 0;
98 
99 	/** Associate the animation to an Item */
100 	virtual void applyToItem(Item *item);
101 
102 	/** Dissociate the animation from an item */
103 	virtual void removeFromItem(Item *item);
104 
105 	/** Check is the animation is being used by an item */
106 	bool isInUse();
107 
108 	/** Obtain the purpose of this anim */
109 	uint32 getUsage() const;
110 
111 	/** Return the hotspot index for a point given in relative coordinates */
112 	virtual int getPointHotspotIndex(const Common::Point &point) const;
113 
114 	/** Get the animation typical duration in milliseconds */
115 	virtual uint32 getDuration() const;
116 
117 	/**
118 	 * Play the animation as an action for an item.
119 	 *
120 	 * This sets up a callback to the item for when the animation completes.
121 	 */
122 	virtual void playAsAction(ItemVisual *item);
123 
124 	/** Checks if the elapsed time since the animation start is greater than a specified duration */
125 	virtual bool isAtTime(uint32 time) const;
126 
127 	/** Get the duration in milliseconds before the animation loops ends */
128 	virtual uint32 getRemainingTime() const;
129 
130 	/** Get the anim movement speed in units per seconds */
131 	virtual uint32 getMovementSpeed() const;
132 
133 	/** Get the chance the animation has to play among other idle actions from the same anim hierarchy */
134 	virtual uint32 getIdleActionFrequency() const;
135 
136 protected:
137 	virtual void printData() override;
138 
139 	uint32 _usage;
140 	uint32 _currentFrame;
141 	uint32 _numFrames;
142 	int32 _refCount;
143 };
144 
145 /**
146  * Displays still images controlled by an AnimScript
147  */
148 class AnimImages : public Anim {
149 public:
150 	AnimImages(Object *parent, byte subType, uint16 index, const Common::String &name);
151 	virtual ~AnimImages();
152 
153 	// Resource API
154 	void readData(Formats::XRCReadStream *stream) override;
155 	void onAllLoaded() override;
156 	void saveLoad(ResourceSerializer *serializer) override;
157 
158 	// Anim API
159 	void selectFrame(uint32 frameIndex) override;
160 	Visual *getVisual() override;
161 	int getPointHotspotIndex(const Common::Point &point) const override;
162 
163 protected:
164 	void printData() override;
165 
166 	float _field_3C;
167 
168 	uint32 _currentDirection;
169 	Common::Array<Direction *> _directions;
170 
171 	Image *_currentFrameImage;
172 };
173 
174 class AnimProp : public Anim {
175 public:
176 	AnimProp(Object *parent, byte subType, uint16 index, const Common::String &name);
177 	virtual ~AnimProp();
178 
179 	// Resource API
180 	void readData(Formats::XRCReadStream *stream) override;
181 	void onPostRead() override;
182 
183 	// Anim API
184 	Visual *getVisual();
185 	uint32 getMovementSpeed() const override;
186 
187 protected:
188 	void printData() override;
189 
190 	Common::String _field_3C;
191 	Common::Array<Common::String> _meshFilenames;
192 	Common::String _textureFilename;
193 	uint32 _movementSpeed;
194 	Common::String _archiveName;
195 
196 	VisualProp *_visual;
197 };
198 
199 /**
200  * Displays a Smacker video
201  */
202 class AnimVideo : public Anim {
203 public:
204 	AnimVideo(Object *parent, byte subType, uint16 index, const Common::String &name);
205 	virtual ~AnimVideo();
206 
207 	// Resource API
208 	void readData(Formats::XRCReadStream *stream) override;
209 	void onAllLoaded() override;
210 	void onGameLoop() override;
211 	void onEnginePause(bool pause) override;
212 	void saveLoadCurrent(ResourceSerializer *serializer) override;
213 
214 	// Anim API
215 	Visual *getVisual() override;
216 	void playAsAction(ItemVisual *item) override;
217 	uint32 getDuration() const override;
218 	bool isAtTime(uint32 time) const override;
219 
220 protected:
221 	typedef Common::Array<Common::Point> PointArray;
222 	typedef Common::Array<Common::Rect> RectArray;
223 
224 	void printData() override;
225 
226 	/** Update the position of the video for the current frame */
227 	void updateSmackerPosition();
228 
229 	Common::String _smackerFile;
230 	Common::String _archiveName;
231 
232 	VisualSmacker *_smacker;
233 
234 	uint32 _width;
235 	uint32 _height;
236 
237 	PointArray _positions;
238 	RectArray _sizes;
239 
240 	int32 _frameRateOverride;
241 	bool _preload;
242 	bool _loop;
243 
244 	ItemVisual *_actionItem;
245 };
246 
247 /**
248  * Animates a 3D mesh skeleton
249  */
250 class AnimSkeleton : public Anim {
251 public:
252 	AnimSkeleton(Object *parent, byte subType, uint16 index, const Common::String &name);
253 	virtual ~AnimSkeleton();
254 
255 	// Resource API
256 	void readData(Formats::XRCReadStream *stream) override;
257 	void onPostRead() override;
258 	void onAllLoaded() override;
259 	void onGameLoop() override;
260 	void onExitLocation() override;
261 	void onPreDestroy() override;
262 
263 	// Anim API
264 	void applyToItem(Item *item) override;
265 	void removeFromItem(Item *item) override;
266 	Visual *getVisual() override;
267 	uint32 getDuration() const override;
268 	void playAsAction(ItemVisual *item) override;
269 	bool isAtTime(uint32 time) const override;
270 	uint32 getMovementSpeed() const override;
271 	uint32 getIdleActionFrequency() const override;
272 	uint32 getRemainingTime() const override;
273 
274 	/** Get the position in the animation loop in milliseconds */
275 	uint32 getCurrentTime() const;
276 
277 protected:
278 	void printData() override;
279 
280 	bool _castsShadow;
281 	Common::String _archiveName;
282 	Common::String _animFilename;
283 	bool _loop;
284 	uint32 _movementSpeed;
285 	uint32 _idleActionFrequency;
286 
287 	uint32 _totalTime;
288 	uint32 _currentTime;
289 
290 	SkeletonAnim *_seletonAnim;
291 	VisualActor *_visual;
292 
293 	ItemVisual *_actionItem;
294 };
295 
296 } // End of namespace Resources
297 } // End of namespace Stark
298 
299 #endif // STARK_RESOURCES_ANIM_H
300