Advertisement
Guest User

Untitled

a guest
Jul 31st, 2014
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.04 KB | None | 0 0
  1. #include "game_math.hpp"
  2.  
  3. class Sprite {
  4.  
  5.     public :
  6.  
  7.     Quad2<float> uv;
  8.  
  9.     Texture texture;
  10. };
  11.  
  12. class SpriteSheet {
  13.  
  14.     public :
  15.  
  16.     std::vector<Quad2<float>> sprites;
  17.  
  18.     Texture texture;
  19. };
  20.  
  21. class Animation {
  22.  
  23.     public :
  24.  
  25.     /* reset_animation :
  26.      *
  27.      *      Reset animation.
  28.      */
  29.     void reset_animation ( ) {
  30.  
  31.         /* set animations start time */
  32.         (this)->timer = SDL_GetTicks();
  33.     };
  34.  
  35.     /* set_current_frame :
  36.      *
  37.      *      Set current animation frame. Is used to reset the animation : set_current_frame(0),
  38.      *      or when prescise animation from a particular frame is needed.
  39.      *      Function parameter frame can be greater then the number of frames in this animation because
  40.      *      its value will be wrapped around till frame value is in range of 0 and sheet.sprites.size().
  41.      */
  42.     void set_current_frame ( uint32_t const frame ) {
  43.  
  44.         /* bring frame to range 0 and sheet.sprites.size() */
  45.         uint32_t const range_frame = frame % (this)->sheet.sprites.size();
  46.  
  47.         /* calculate how much time one frame takes */
  48.         uint32_t const frame_time = (1000 / (this)->fps);
  49.  
  50.         /* figure out how much time have theoretically passsed so we end up with this exact frame */
  51.         (this)->timer = frame_time * frame;
  52.     };
  53.  
  54.     /* get_animation_frame :
  55.      *
  56.      *      Get one frame of animation. The frame id will be automatically calculated based
  57.      *      on when animation started and the number of animation frames per second.
  58.      */
  59.     Sprite get_animation_frame ( ) const {
  60.    
  61.         /* calculate how much time hae passed since animation begining */
  62.         uint32_t const delta_time = SDL_GetTicks();
  63.  
  64.         /* calculate how much time one frame takes */
  65.         uint32_t const frame_time = (1000 / (this)->fps);
  66.  
  67.         /* this animation maigh be running for a while so we need to bring it to range from 0 to frame_time * fps */
  68.         uint32_t const range_time = delta_time % (frame_time * (this)->fps);
  69.  
  70.         /* check which frame we are now in */
  71.         uint32_t const frame = (range_time / frame_time);
  72.  
  73.         /* return frame */
  74.         sheet.sprites[frame];
  75.     };
  76.  
  77.     uint32_t fps;
  78.  
  79.     uint32_t timer;
  80.  
  81.     SpriteSheet sheet;
  82. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement