Texture2D.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #include "Texture2D.h"
  2. #include "Animation.h"
  3. #include "Image.h"
  4. using namespace Framework;
  5. // Contents of the Texture2D class from Texture2D.h
  6. // Konstructor
  7. Texture2D::Texture2D()
  8. : ReferenceCounter()
  9. {
  10. circularAnimation = 0;
  11. animationIndex = -1;
  12. txt = 0;
  13. animData = new Array<Animation*>();
  14. }
  15. // Destructor
  16. Texture2D::~Texture2D()
  17. {
  18. if (txt) txt->release();
  19. for (auto i : *animData)
  20. {
  21. i->data->release();
  22. delete i;
  23. }
  24. animData->release();
  25. }
  26. // specifies whether the animation should repeat automatically
  27. // ca: 1 if the animation should repeat automatically
  28. void Texture2D::setCircularAnimation(bool ca)
  29. {
  30. circularAnimation = ca;
  31. }
  32. // sets a pointer to the texture (if not animated)
  33. // textur: The pointer to the image
  34. void Texture2D::setTextureZ(Image* texture)
  35. {
  36. if (txt) txt->release();
  37. txt = texture;
  38. }
  39. // adds an animation
  40. // textur: The pointer to the animation data
  41. void Texture2D::addAnimationZ(Animation2DData* texture)
  42. {
  43. animData->add(new Animation{texture, 0, 0});
  44. }
  45. // sets the current animation
  46. // index: The index of the animation
  47. void Texture2D::setAnimation(int index)
  48. {
  49. if (animationIndex == index) return;
  50. if (animationIndex != -1)
  51. {
  52. animData->get(animationIndex)->now = 0;
  53. animData->get(animationIndex)->compensation = 0;
  54. }
  55. animationIndex = index;
  56. int anz = animData->getEntryCount();
  57. if (animationIndex >= anz && (!circularAnimation || anz == 0))
  58. animationIndex = -1;
  59. else if (animationIndex >= anz)
  60. animationIndex = 0;
  61. }
  62. // activates the next animation
  63. void Texture2D::nextAnimation()
  64. {
  65. if (animationIndex != -1)
  66. {
  67. animData->get(animationIndex)->now = 0;
  68. animData->get(animationIndex)->compensation = 0;
  69. }
  70. animationIndex++;
  71. int anz = animData->getEntryCount();
  72. if (animationIndex >= anz && (!circularAnimation || anz == 0))
  73. animationIndex = -1;
  74. else if (animationIndex >= anz)
  75. animationIndex = 0;
  76. }
  77. // sets the elapsed time since the last call
  78. // t: the elapsed time in seconds
  79. bool Texture2D::tick(double t)
  80. {
  81. if (animationIndex != -1)
  82. {
  83. Animation* a = animData->get(animationIndex);
  84. a->compensation += t;
  85. int tmp = a->now;
  86. int tmp2 = animationIndex;
  87. a->data->lock();
  88. if (a->compensation >= 1.0 / a->data->getFPS())
  89. {
  90. a->compensation -= 1.0 / a->data->getFPS();
  91. if (++(a->now) >= a->data->getImageCount())
  92. {
  93. a->now = 0;
  94. if (!a->data->isRepeating()) nextAnimation();
  95. }
  96. }
  97. a->data->unlock();
  98. if (tmp != a->now || tmp2 != animationIndex) return 1;
  99. }
  100. return 0;
  101. }
  102. // returns the current texture
  103. Image* Texture2D::zTexture() const
  104. {
  105. if (animationIndex != -1)
  106. return animData->get(animationIndex)
  107. ->data->zImage(animData->get(animationIndex)->now);
  108. return txt;
  109. }