| 1 |
/*************************************************************************** |
| 2 |
* Copyright (C) 2005-2019 by the FIFE team * |
| 3 |
* http://www.fifengine.net * |
| 4 |
* This file is part of FIFE. * |
| 5 |
* * |
| 6 |
* FIFE is free software; you can redistribute it and/or * |
| 7 |
* modify it under the terms of the GNU Lesser General Public * |
| 8 |
* License as published by the Free Software Foundation; either * |
| 9 |
* version 2.1 of the License, or (at your option) any later version. * |
| 10 |
* * |
| 11 |
* This library is distributed in the hope that it will be useful, * |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * |
| 14 |
* Lesser General Public License for more details. * |
| 15 |
* * |
| 16 |
* You should have received a copy of the GNU Lesser General Public * |
| 17 |
* License along with this library; if not, write to the * |
| 18 |
* Free Software Foundation, Inc., * |
| 19 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * |
| 20 |
***************************************************************************/ |
| 21 |
|
| 22 |
#ifndef FIFE_VIDEO_ANIMATION_H |
| 23 |
#define FIFE_VIDEO_ANIMATION_H |
| 24 |
|
| 25 |
// Standard C++ library includes |
| 26 |
#include <cassert> |
| 27 |
#include <map> |
| 28 |
#include <vector> |
| 29 |
|
| 30 |
// Platform specific includes |
| 31 |
#include "util/base/fife_stdint.h" |
| 32 |
#include "util/resource/resource.h" |
| 33 |
|
| 34 |
#include "image.h" |
| 35 |
|
| 36 |
// 3rd party library includes |
| 37 |
|
| 38 |
// FIFE includes |
| 39 |
// These includes are split up in two parts, separated by one empty line |
| 40 |
// First block: files included from the FIFE root src directory |
| 41 |
// Second block: files included from the same folder |
| 42 |
|
| 43 |
namespace FIFE { |
| 44 |
|
| 45 |
/** Animation. |
| 46 |
* |
| 47 |
* A container of Images describing an animation. Animation itself does |
| 48 |
* not take care of animating the images. Instead it contains images |
| 49 |
* having associated timestamps. It is the responsibility of the |
| 50 |
* animation user to query frames based on current timestamp and show |
| 51 |
* returned images on screen. |
| 52 |
*/ |
| 53 |
class Animation : public IResource { |
| 54 |
public: |
| 55 |
/** Constructor. |
| 56 |
*/ |
| 57 |
Animation(IResourceLoader* loader = 0); |
| 58 |
Animation(const std::string& name, IResourceLoader* loader = 0); |
| 59 |
|
| 60 |
/** Destructor. Decreases the reference count of all referred images. |
| 61 |
*/ |
| 62 |
~Animation(); |
| 63 |
|
| 64 |
virtual size_t getSize(); |
| 65 |
|
| 66 |
virtual void load(); |
| 67 |
virtual void free(); |
| 68 |
|
| 69 |
void invalidate(); |
| 70 |
|
| 71 |
/** Adds new frame into animation |
| 72 |
* Frames must be added starting from first frame. Increases the reference |
| 73 |
* count of the given image. |
| 74 |
* @param image Pointer to Image. Does not transfer the ownership |
| 75 |
* @param duration Duration for given frame in the animation |
| 76 |
*/ |
| 77 |
void addFrame(ImagePtr image, uint32_t duration); |
| 78 |
|
| 79 |
/** Get the frame index that matches given timestamp. In case there is no exact match, |
| 80 |
* correct frame is calculated. E.g. if there are frames for timestamps 50 and 100 |
| 81 |
* and frame for 75 is asked, frame associated with value 50 is given back. |
| 82 |
* In case the timestamp is past the sequence, negative value is returned |
| 83 |
* @see addFrame |
| 84 |
*/ |
| 85 |
int32_t getFrameIndex(uint32_t timestamp); |
| 86 |
|
| 87 |
/** Gets the frame iamge that matches the given index. If no matches found, returns an invalid ImagePtr |
| 88 |
*/ |
| 89 |
ImagePtr getFrame(int32_t index); |
| 90 |
|
| 91 |
/** Gets the frame image that matches the given timestamp. |
| 92 |
*/ |
| 93 |
ImagePtr getFrameByTimestamp(uint32_t timestamp); |
| 94 |
|
| 95 |
/** Gets all frame images. |
| 96 |
*/ |
| 97 |
std::vector<ImagePtr> getFrames(); |
| 98 |
|
| 99 |
/** Gets the frame duration for given (indexed) frame. Returns negative value in case |
| 100 |
* of incorrect index |
| 101 |
*/ |
| 102 |
int32_t getFrameDuration(int32_t index) const; |
| 103 |
|
| 104 |
/** Get the number of frames |
| 105 |
*/ |
| 106 |
uint32_t getFrameCount() const; |
| 107 |
|
| 108 |
/** Sets the action frame. Action frame is the frame when the related |
| 109 |
* action actually happens. E.g. in case of punch animation, action frame is |
| 110 |
* the frame when punch hits the target. In case there is no associated |
| 111 |
* action frame, value is negative |
| 112 |
* @param num index of the action frame. |
| 113 |
*/ |
| 114 |
void setActionFrame(int32_t num) { m_action_frame = num; } |
| 115 |
|
| 116 |
/** Gets the action frame. |
| 117 |
* @see setActionFrame |
| 118 |
*/ |
| 119 |
int32_t getActionFrame() const { return m_action_frame; } |
| 120 |
|
| 121 |
/** Animation direction tells how this animation is associated |
| 122 |
* with movement when played starting from frame 0 |
| 123 |
* E.g. in walking animation there should be direction assigned so |
| 124 |
* that engine can choose the correct animation when characters are |
| 125 |
* moved around the map area |
| 126 |
* @param direction direction to set |
| 127 |
*/ |
| 128 |
void setDirection(uint32_t direction); |
| 129 |
|
| 130 |
/** Gets the animation direction |
| 131 |
* @see setDirection |
| 132 |
* @return direction for this animation |
| 133 |
*/ |
| 134 |
uint32_t getDirection() const { return m_direction; } |
| 135 |
|
| 136 |
/** Gets the total duration for the whole animation |
| 137 |
*/ |
| 138 |
uint32_t getDuration() const { return m_animation_endtime; } |
| 139 |
|
| 140 |
private: |
| 141 |
/** Contains information about one animation frame (duration + frame index + frame pointer) |
| 142 |
*/ |
| 143 |
struct FrameInfo { |
| 144 |
uint32_t index; |
| 145 |
uint32_t duration; |
| 146 |
ImagePtr image; |
| 147 |
}; |
| 148 |
|
| 149 |
std::string createUniqueAnimationName(); |
| 150 |
|
| 151 |
/** Checks for animation frame index overflows |
| 152 |
*/ |
| 153 |
bool isValidIndex(int32_t index) const; |
| 154 |
|
| 155 |
// Map of timestamp + associated frame |
| 156 |
std::map<uint32_t, FrameInfo> m_framemap; |
| 157 |
// vector of frames for fast indexed access |
| 158 |
std::vector<FrameInfo> m_frames; |
| 159 |
// Action frame of the Animation. |
| 160 |
int32_t m_action_frame; |
| 161 |
// time when animation ends (zero based) |
| 162 |
int32_t m_animation_endtime; |
| 163 |
// Direction for this animation |
| 164 |
uint32_t m_direction; |
| 165 |
|
| 166 |
}; |
| 167 |
|
| 168 |
typedef SharedPtr<Animation> AnimationPtr; |
| 169 |
|
| 170 |
} |
| 171 |
|
| 172 |
#endif |