Animation.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /**
  2. * Copyright (c) 2006-2009 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. #ifndef LOVE_OPENGL_ANIMATION_H
  21. #define LOVE_OPENGL_ANIMATION_H
  22. // LOVE
  23. #include "../Drawable.h"
  24. #include "Image.h"
  25. // STD
  26. #include <vector>
  27. namespace love
  28. {
  29. namespace graphics
  30. {
  31. namespace opengl
  32. {
  33. // Represents a single frame.
  34. struct AnimationFrame
  35. {
  36. float x, y; // Top left corner of the frame.
  37. float w, h; // Size of the frame.
  38. int preDelay; // Delay to previous frame.
  39. int postDelay; // Delay to next frame.
  40. };
  41. class Animation : public Drawable
  42. {
  43. private:
  44. // The source of the animation.
  45. Image * image;
  46. // Delays between frames.
  47. // delays[0] is the delay between frames[0] and frames[1].
  48. std::vector<float> delays;
  49. // Holds all the frames.
  50. std::vector<AnimationFrame> frames;
  51. // Animation mode.
  52. int mode;
  53. // The current frame.
  54. int current;
  55. // True if playing, false otherwise.
  56. bool playing;
  57. // "Left over"-time.
  58. float timeBuffer;
  59. // Used for bounce mode.
  60. int direction;
  61. // Overall speed. (1 = normal).
  62. float speed;
  63. public:
  64. /**
  65. * Creates an Animation with no frames.
  66. * @param image The image to use as the source.
  67. **/
  68. Animation(Image * image);
  69. /**
  70. * Creates an Animation with frames from top-left to bottom right.
  71. * @param image The image to use as the source.
  72. * @param fw The width of each frame.
  73. * @param fh The height of each frame.
  74. * @param delay The delay after each frame.
  75. * @param num The number of frames. (0 = all)
  76. **/
  77. Animation(Image * image, float fw, float fh, float delay, int num = 0);
  78. virtual ~Animation();
  79. /**
  80. * Adds a single frame.
  81. * @param x The top-left corner of the frame.
  82. * @param y The top-right corner of the frame.
  83. * @param w The width of the frame.
  84. * @param h The height of the frame.
  85. * @param delay The delay after the frame.
  86. **/
  87. void addFrame(float x, float y, float w, float h, float delay);
  88. /**
  89. * Sets the current animation mode, and reset.
  90. **/
  91. void setMode(int mode);
  92. /**
  93. * Causes the Animation to start playing.
  94. **/
  95. void play();
  96. /**
  97. * Causes the Animation to stop.
  98. **/
  99. void stop();
  100. /**
  101. * Resets the Animation.
  102. **/
  103. void reset();
  104. /**
  105. * Resets timebuffers, and sets the current frame directly.
  106. **/
  107. void seek(int frame);
  108. /**
  109. * Gets the current frame.
  110. **/
  111. int getCurrentFrame() const;
  112. /**
  113. * Gets amount of frames.
  114. **/
  115. int getSize() const;
  116. /**
  117. * Sets the delay after a frame.
  118. **/
  119. void setDelay(int frame, float delay);
  120. /**
  121. * Sets the overall animation speed.
  122. **/
  123. void setSpeed(float speed);
  124. /**
  125. * Gets the overall animation speed.
  126. **/
  127. float getSpeed() const;
  128. void update(float dt);
  129. void draw(float x, float y, float angle, float sx, float sy, float ox, float oy) const;
  130. float getWidth() const;
  131. float getHeight() const;
  132. };
  133. } // opengl
  134. } // graphics
  135. } // love
  136. #endif // LOVE_OPENGL_ANIMATION_H