Advertisement
Guest User

Untitled

a guest
Jan 24th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.20 KB | None | 0 0
  1. -- SpriteSheet.h --
  2. #pragma once
  3.  
  4. #include <SFML/Graphics.hpp>
  5.  
  6. class SpriteSheet
  7. {
  8. public:
  9. SpriteSheet(int w, int h, int r, int c, sf::Texture* text);
  10.  
  11. sf::Sprite getSprite(int, int);
  12.  
  13. void setupSpriteSheet(int w, int h, int r, int c, sf::Texture*);
  14.  
  15. sf::Texture* spriteSheet = NULL;
  16. int width, height, rows, cols;
  17.  
  18. };
  19.  
  20. -- SpriteSheet.cpp --
  21. #include "SpriteSheet.h"
  22.  
  23. SpriteSheet::SpriteSheet(int w, int h, int r, int c, sf::Texture* text)
  24. :width(w),
  25. height(h),
  26. rows(r),
  27. cols(c),
  28. spriteSheet(text)
  29. {
  30. }
  31.  
  32. sf::Sprite SpriteSheet::getSprite(int r, int c)
  33. {
  34. sf::Sprite returnSprite;
  35. if ((r <= rows && r >= 0) && (c <= cols && c >= 0))
  36. {
  37. returnSprite.setTexture(*spriteSheet);
  38.  
  39. returnSprite.setTextureRect(sf::IntRect(r*width, c*cols, width, height));
  40. }
  41.  
  42. return returnSprite;
  43. }
  44.  
  45. void SpriteSheet::setupSpriteSheet(int w, int h, int r, int c, sf::Texture* text)
  46. {
  47. spriteSheet = text;
  48.  
  49. width = w;
  50. height = h;
  51. rows = r;
  52. cols = c;
  53. }
  54.  
  55. -- AnimationContainer.h --
  56.  
  57. #pragma once
  58.  
  59. #include "SpriteSheet.h"
  60.  
  61. class Animation
  62. {
  63. public:
  64. Animation()
  65. {
  66. animationFrames = 0;
  67. };
  68.  
  69. Animation(int frames)
  70. {
  71. animationFrames = frames;
  72. };
  73.  
  74. Animation(int frames, int delay)
  75. {
  76. animationFrames = frames;
  77. animationDelay = delay;
  78. };
  79.  
  80. Animation(int frames, int delay, int w, int h, int r)
  81. {
  82. animationFrames = frames;
  83. animationDelay = delay;
  84.  
  85. setupAnimation(w, h, r);
  86. };
  87.  
  88. void setupAnimation(int w, int h, int r)
  89. {
  90. sf::IntRect* newAnimation = new sf::IntRect[animationFrames];
  91. for (int i = 0; i < animationFrames; i++)
  92. {
  93. sf::IntRect rect((r * h), (i * w), w, h);
  94. newAnimation[i] = rect;
  95. }
  96. animation = newAnimation;
  97. };
  98.  
  99. void setAnimationFrames(int frames)
  100. {
  101. animationFrames = frames;
  102. }
  103.  
  104. void setDelay(int delay)
  105. {
  106. animationDelay = delay;
  107. }
  108.  
  109. void resetAnimation()
  110. {
  111. curAnimationFrame = 0;
  112. }
  113.  
  114. void update(sf::IntRect* rect, int* dt)
  115. {
  116. if ((*dt) > animationDelay)
  117. {
  118. if (curAnimationFrame == (animationFrames - 1))
  119. {
  120. resetAnimation();
  121. *dt = 0;
  122. }
  123. else
  124. {
  125. curAnimationFrame++;
  126. *dt = 0;
  127. }
  128. }
  129.  
  130. *rect = animation[curAnimationFrame];
  131. };
  132.  
  133. /** Debug remove later
  134. void update(sf::IntRect* rect, int dt)
  135. {
  136. if (dt >= animationDelay)
  137. {
  138. if (curAnimationFrame == (animationFrames - 1))
  139. resetAnimation();
  140. else
  141. curAnimationFrame++;
  142. }
  143.  
  144. rect = &(animation[curAnimationFrame]);
  145. };
  146. **/
  147.  
  148. // Debug Remove Later
  149. int getFrame()
  150. {
  151. return curAnimationFrame;
  152. };
  153.  
  154. sf::IntRect getFrameRect()
  155. {
  156. return animation[curAnimationFrame];
  157. };
  158.  
  159.  
  160. private:
  161. int animationFrames = 0;
  162. int curAnimationFrame = 0;
  163. // Delay in milliseconds
  164. int animationDelay = 100;
  165. sf::IntRect* animation = NULL;
  166. };
  167.  
  168.  
  169. class AnimationContainer
  170. {
  171. public:
  172. AnimationContainer(SpriteSheet* sheet)
  173. {
  174. animationSpriteSheet = sheet;
  175. setupAnimations();
  176. };
  177.  
  178. void setupAnimations()
  179. {
  180. numAnimations = (*animationSpriteSheet).rows;
  181.  
  182. if ((*animationSpriteSheet).rows > 0 && (*animationSpriteSheet).cols > 0)
  183. {
  184. Animation* newAnimations = new Animation[(*animationSpriteSheet).rows];
  185.  
  186. for (int i = 0; i < (*animationSpriteSheet).rows; i++)
  187. {
  188. newAnimations[i].setAnimationFrames((*animationSpriteSheet).cols);
  189. newAnimations[i].setupAnimation((*animationSpriteSheet).width, (*animationSpriteSheet).height, i);
  190. }
  191.  
  192. animations = newAnimations;
  193. }
  194.  
  195. };
  196.  
  197. void update(sf::IntRect* rect, int* dt)
  198. {
  199. animations[currentAnimation].update(rect, dt);
  200. };
  201.  
  202. /** Debug Remove Later
  203.  
  204. void update(sf::IntRect* rect, int dt)
  205. {
  206. animations[currentAnimation].update(rect, dt);
  207. };
  208. **/
  209.  
  210. // Debug remove Later
  211. int getAnimationFrame()
  212. {
  213. return animations[currentAnimation].getFrame();
  214. }
  215. sf::IntRect getFrameRect()
  216. {
  217. return animations[currentAnimation].getFrameRect();
  218. }
  219.  
  220. void changeAnimation(int animNum)
  221. {
  222. if (animNum != currentAnimation)
  223. {
  224. if (animNum >= 0 && animNum < numAnimations)
  225. currentAnimation = animNum;
  226. }
  227. };
  228.  
  229.  
  230. private:
  231. SpriteSheet* animationSpriteSheet = NULL;
  232. Animation* animations = NULL;
  233.  
  234. int currentAnimation = 0, numAnimations = 0;
  235.  
  236. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement