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
|
////////////////////////////////////////////////////////////
//
// SFE - Simple and Fast Extensions for SFML
// Copyright (C) 2012-2013 Clement Fauconnier (cfauconnier86@gmail.com)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
#ifndef SFE_ANIMHANDLER_HPP
#define SFE_ANIMHANDLER_HPP
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFE/Animation.hpp>
#include <SFML/Graphics.hpp>
#include <vector>
#include <string>
namespace sfe
{
////////////////////////////////////////////////////////////
/// \brief An animation Handler to manage all the animations
/// of a single animated entity
///
////////////////////////////////////////////////////////////
class AnimHandler
{
public :
////////////////////////////////////////////////////////////
/// \brief Create an animHandler
///
/// \param filename Directory of the file used for the board
/// \param eyeWidth Width of the eye used to cut the board.
/// Defaulting to 32 pixels.
/// \param eyeHeight Height of the eye used to cut the board.
/// If not defined, eyeWidth is used
///
////////////////////////////////////////////////////////////
AnimHandler(const std::string& filename, const int eyeWidth = 32, const int eyeHeight = -1);
////////////////////////////////////////////////////////////
/// \brief Destroy the animHandler
///
////////////////////////////////////////////////////////////
~AnimHandler();
////////////////////////////////////////////////////////////
/// \brief Play the animation called \a name
///
/// \param name Name of the animation
///
/// @pre Animation must exist. If not, a message will be
/// displayed on the error standard, to warn users that
/// an error occured, but no exception will be thrown,
/// so it is to user repsonsability to handle error cases
///
/// Be careful, because of AnimHandler nature (single animated entity),
/// only the last animation played will be drawable on a renderTarget.
///
////////////////////////////////////////////////////////////
void play(const std::string& name);
////////////////////////////////////////////////////////////
/// \brief Add an animation to the animHandler
///
/// \param name Name of the animation
/// \param rowPosition Row on the board to use with (begin at 0).
/// \param spritesNumber Number of sprites wich compose the animation
/// \param frequency Frame Rate of the animation (img/s). Defaulting to NTSC
///
/// \see sfe::Animation
///
////////////////////////////////////////////////////////////
void newAnimation(const std::string& name, const int rowPosition, const int spritesNumber, const int frequency = 30);
////////////////////////////////////////////////////////////
/// \brief Draw the current animation to a render target
///
/// \param target Render target to draw to
///
/// @pre If no texture has been set to the board, SFML will
/// display a white shape of the size of the eye instead.
///
////////////////////////////////////////////////////////////
void draw(sf::RenderTarget& target) const;
////////////////////////////////////////////////////////////
/// \brief Get the current animation of the animHandler
///
/// \return The current animation, wich is the last played.
/// If none has been played, returns the first one
/// added to the animHandler. If no animation exist,
/// return a nullptr.
///
////////////////////////////////////////////////////////////
Animation* getCurrentAnimation();
////////////////////////////////////////////////////////////
/// \brief Get the animation named \a name
///
/// @pre Animation must exist. If not, a message will be displayed
/// to warn users that an error occured, but no exception
/// will be thrown. It is user responsability to handle error cases.
///
/// \return The animation named \a name, if exists. If not,
/// return a nullptr.
///
////////////////////////////////////////////////////////////
Animation* getAnimation(const std::string& name);
protected :
////////////////////////////////////////////////////////////
/// \brief Move the eye on the board, for the animation \a anim
///
/// \param anim The animation to update
///
////////////////////////////////////////////////////////////
void moveEye(Animation& anim);
////////////////////////////////////////////////////////////
// Member data
////////////////////////////////////////////////////////////
std::vector<Animation*> animationsVector; ///< Vector of all the animations of the animHandler.
sf::Texture* board; ///< Board used for all the animations.
sf::IntRect eye; ///< The eye used to move into the board and target a sprite.
std::string currentAnimationName; ///< The name of the current animation
};
} // namespace sfe
#endif // SFE_ANIMHANDLER_HPP
////////////////////////////////////////////////////////////
/// \class sfe::AnimHandler
/// \ingroup anims
///
/// sfe:AnimHandler is a class that allows to easily
/// manage a single animated entity, that can be drawn on a render target.
///
/// sfe::AnimHandler works in combination with the sfe::Animation class and.
/// the sfe::Catalog static class.
///
/// sfe::AnimHandler can be used with the Catalog, but can also be an attribute
/// to another class, like a "MyCharacter" class for example.
///
/// Usage example:
/// \code
///
/// // Declare AnimHandler with a size of 32*32 pixels
/// sfe::AnimHandler Toto("Toto.png");
///
/// // Declare an animHandler for animations with size 64*64 pixels
/// sfe::AnimHandler Titi("Titi.png", 64);
///
/// // Declare an animHandler with 79*119 pixels
/// sfe::AnimHandler Tata("Tata.png", 79, 119);
///
/// // Create animations and add them to the animHandlers
/// Toto.newAnimation("Idle Left", 0, 3);
/// Toto.newAnimation("walk left", 1, 3);
/// Titi.newAnimation("Jump", 11, 17);
/// Titi.NewAnimation("Danse", 12, 24);
///
/// // Add the animHandler Toto to the Catalog
/// sfe::Catalog::add(Toto, "Toto");
///
/// // [...]
/// bool JumpHasBeenPressed = false;
///
/// // Handle events
/// if (event.type == sf::Event::KeyPressed)
/// {
/// if (event.key.code == sf::Keyboard::Space)
/// {
/// JumpHasBeenPressed = true;
/// }
/// }
///
/// // [...]
///
/// if (JumpHasBeenPressed)
/// {
/// someMethodToHandleJump();
/// Titi.Play("Jump");
/// }
///
/// // Draw it
/// Toto.draw(window);
/// Titi.draw(window);
///
/// // This will be ignored, because no animation exists for this entity.
/// // A message will be displayed on the standard error to warn user that
/// // an error occured.
/// Tata.draw(window);
/// \endcode
///
/// \see sfe::Animations
///
//////////////////////////////////////////////////////////// |
Partager