Last active
December 12, 2015 09:28
-
-
Save Shaptic/4751211 to your computer and use it in GitHub Desktop.
Animation class header
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef ANIMATION_HPP | |
#define ANIMATION_HPP | |
// Framebuffer object | |
#include "Graphics/Framebuffer.hpp" | |
// Animation inherits rigid body entity | |
#include "Entity/RigidBody.hpp" | |
// For the timer | |
#include "Utils/Timer.hpp" | |
/** | |
* An animate-able extension of the CRigidBody class. | |
* This class acts exactly like a rigid body entity, but contains | |
* the ability to easily switch between sprites either automatically | |
* after a certain period of time or manually. | |
* Currently, this is meant exclusively for simple, single-texture | |
* quad meshes, as that makes material binding much easier to | |
* implement correctly. | |
* Another limitation is that all of the animations in the sprite | |
* sheet must be the exact same size. | |
**/ | |
class CAnimation : public CRigidBody | |
{ | |
public: | |
/** | |
* Animation file header that contains details about | |
* the amount of sprites in the sheet and the total | |
* dimensions of the image. This allows the loader to | |
* calculate the dimensions of each individual sprite, | |
* which all must be uniform. | |
**/ | |
struct AnimationHeader | |
{ | |
uint16_t width; | |
uint16_t height; | |
uint16_t columns; | |
uint16_t rows; | |
asset::CTexture* pTexture; | |
}; | |
CAnimation() : m_last(util::CTimer::GetTimeElapsed()), | |
m_delay(1.f), m_loops_done(0) {} | |
~CAnimation(){} | |
/** | |
* Load a custom animation file. | |
* This will load an .icanim image file, which internally | |
* specifies sprite dimensions in the sheet. | |
* | |
* @param std::string Filename | |
* @param gfx::CVertexBuffer Vertex buffer to load quad-mesh into | |
* | |
* @return TRUE on success, FALSE on failure | |
* @see Docs/ICAnim.spec | |
**/ | |
bool LoadFromFile(const std::string& filename, | |
gfx::CVertexBuffer& VBO); | |
/** | |
* Replaces the current texture and dimensions. | |
* This will pass new texture coordinate offsets to the fragment shader, | |
* as well as give the renderer a new texture to work with. | |
* At times, this causes a single frame of texture artifacting, for | |
* which the cause is currently unknown. | |
* | |
* @param AnimationHeader Header containing texture / dimension data | |
**/ | |
void SwapSpriteSheet(const AnimationHeader& Header); | |
/** | |
* Toggles animation. | |
* A CAnimation with animation disabled acts exactly like a | |
* CEntity for all intents and purposes. Disabling animation | |
* leaves the currently active texture as the one rendered, | |
* as opposed to the original texture. | |
* | |
* @param bool Enable / Disable animation | |
**/ | |
inline void EnableAnimation(bool flag) | |
{ m_enabled = flag; } | |
/** | |
* Sets the rate of animation. | |
* The internal sprites will iterate as this limit is reached. | |
* It is absolutely essential that you call Update() at the end | |
* of each frame in order to track this value properly. | |
**/ | |
void SetAnimationRate(const float delta); | |
/** | |
* Iterates the current texture to the next one in the queue. | |
* @return TRUE if iterated to next one, | |
* FALSE if looping to beginning. | |
**/ | |
bool NextSprite(); | |
/** | |
* Iterates the current texture to the previous one in the queue. | |
* @return TRUE if iterated to previous one, | |
* FALSE if looping to end. | |
**/ | |
bool PrevSprite(); | |
/** | |
* Updates the current time delta value. | |
* This method will accurately measure time elapsed between | |
* texture switches and will switch accordingly if necessary. | |
**/ | |
virtual void Update(); | |
/// The quantity of full animations done. | |
inline uint32_t GetLoopCount() const | |
{ return m_loops_done; } | |
inline uint16_t GetAnimationCount() const | |
{ return m_SheetDetails.rows * m_SheetDetails.columns; } | |
inline AnimationHeader& GetHeader() | |
{ return m_SheetDetails; } | |
/** | |
* Sets the static sprite for when animation is disabled. | |
* This value is clipped to the range [0, GetAnimationCount()) | |
**/ | |
void SetAnimation(const uint8_t index); | |
private: | |
math::vector2_t m_TexcDim; // Calculated texture dimensions | |
AnimationHeader m_SheetDetails; // Internal sprite sheet details | |
uint8_t m_active; // Currently active sprite | |
float m_delay; // Delay between texture swaps | |
float m_last; // Time since last Next/PrevSprite() call | |
bool m_enabled; // Is animation enabled? | |
int m_tc_loc, m_tc_str // Shader uniform locations | |
int m_loops_done; // Loops completed | |
}; | |
#endif // ANIMATION_HPP |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment