Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "game_math.hpp"
- class Sprite {
- public :
- Quad2<float> uv;
- Texture texture;
- };
- class SpriteSheet {
- public :
- std::vector<Quad2<float>> sprites;
- Texture texture;
- };
- class Animation {
- public :
- /* reset_animation :
- *
- * Reset animation.
- */
- void reset_animation ( ) {
- /* set animations start time */
- (this)->timer = SDL_GetTicks();
- };
- /* set_current_frame :
- *
- * Set current animation frame. Is used to reset the animation : set_current_frame(0),
- * or when prescise animation from a particular frame is needed.
- * Function parameter frame can be greater then the number of frames in this animation because
- * its value will be wrapped around till frame value is in range of 0 and sheet.sprites.size().
- */
- void set_current_frame ( uint32_t const frame ) {
- /* bring frame to range 0 and sheet.sprites.size() */
- uint32_t const range_frame = frame % (this)->sheet.sprites.size();
- /* calculate how much time one frame takes */
- uint32_t const frame_time = (1000 / (this)->fps);
- /* figure out how much time have theoretically passsed so we end up with this exact frame */
- (this)->timer = frame_time * frame;
- };
- /* get_animation_frame :
- *
- * Get one frame of animation. The frame id will be automatically calculated based
- * on when animation started and the number of animation frames per second.
- */
- Sprite get_animation_frame ( ) const {
- /* calculate how much time hae passed since animation begining */
- uint32_t const delta_time = SDL_GetTicks();
- /* calculate how much time one frame takes */
- uint32_t const frame_time = (1000 / (this)->fps);
- /* this animation maigh be running for a while so we need to bring it to range from 0 to frame_time * fps */
- uint32_t const range_time = delta_time % (frame_time * (this)->fps);
- /* check which frame we are now in */
- uint32_t const frame = (range_time / frame_time);
- /* return frame */
- sheet.sprites[frame];
- };
- uint32_t fps;
- uint32_t timer;
- SpriteSheet sheet;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement