Textur2D.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #include "Textur2D.h"
  2. #include "Bild.h"
  3. #include "Animation.h"
  4. using namespace Framework;
  5. // inhalt der Textur2D Klasse aus Textur2D.h
  6. // Konstructor
  7. Textur2D::Textur2D()
  8. {
  9. circularAnimation = 0;
  10. animationIndex = -1;
  11. txt = 0;
  12. animData = new Array< Animation* >();
  13. ref = 1;
  14. }
  15. // Destructor
  16. Textur2D::~Textur2D()
  17. {
  18. if( txt )
  19. txt->release();
  20. for( auto i = animData->getArray(); i.set; i++ )
  21. {
  22. i.var->data->release();
  23. delete i.var;
  24. }
  25. animData->release();
  26. }
  27. // Legt fest, ob die animation sich automatisch wiederhohlen soll
  28. // ca: 1, falls sich die animation automatisch wiederhohlen soll
  29. void Textur2D::setCircularAnimation( bool ca )
  30. {
  31. circularAnimation = ca;
  32. }
  33. // setzt einen Zeiger auf die Textur (fals nicht animiert)
  34. // textur: Der Zeiger auf das Bild
  35. void Textur2D::setTexturZ( Bild *textur )
  36. {
  37. if( txt )
  38. txt->release();
  39. txt = textur;
  40. }
  41. // fügt eine Animation hinzu
  42. // textur: Der Zeiger auf die Animationsdaten
  43. void Textur2D::addAnimationZ( Animation2DData *textur )
  44. {
  45. animData->add( new Animation{ textur, 0, 0 } );
  46. }
  47. // aktiviert die nachfolgende animation
  48. void Textur2D::nextAnimation()
  49. {
  50. if( animationIndex != -1 )
  51. {
  52. animData->get( animationIndex )->jetzt = 0;
  53. animData->get( animationIndex )->ausgleich = 0;
  54. }
  55. animationIndex++;
  56. int anz = animData->getEintragAnzahl();
  57. if( animationIndex >= anz && ( !circularAnimation || anz == 0 ) )
  58. animationIndex = -1;
  59. else if( animationIndex >= anz )
  60. animationIndex = 0;
  61. }
  62. // setzt die vergangene Zeit seit dem letzten Aufruf
  63. // t: die vergangene Zeit in sekunden
  64. bool Textur2D::tick( double t )
  65. {
  66. if( animationIndex != -1 )
  67. {
  68. Animation *a = animData->get( animationIndex );
  69. a->ausgleich += t;
  70. int tmp = a->jetzt;
  71. int tmp2 = animationIndex;
  72. a->data->lock();
  73. if( a->ausgleich >= 1.0 / a->data->getFPS() )
  74. {
  75. a->ausgleich -= 1.0 / a->data->getFPS();
  76. if( ++(a->jetzt) >= a->data->getBildAnzahl() )
  77. {
  78. a->jetzt = 0;
  79. if( !a->data->istWiederhohlend() )
  80. nextAnimation();
  81. }
  82. }
  83. a->data->unlock();
  84. if( tmp != a->jetzt || tmp2 != animationIndex )
  85. return 1;
  86. }
  87. return 0;
  88. }
  89. // gibt die aktuelle Textur zurück
  90. Bild *Textur2D::zTextur() const
  91. {
  92. if( animationIndex != -1 )
  93. return animData->get( animationIndex )->data->zBild( animData->get( animationIndex )->jetzt );
  94. return txt;
  95. }
  96. // erhöht den Reference Counter um 1 und gibt this zurück
  97. Textur2D *Textur2D::getThis()
  98. {
  99. ref++;
  100. return this;
  101. }
  102. // verringert den reference counter um 1 und löscht sich selbst, falls er 0 erreicht
  103. // gibt 0 zurück
  104. Textur2D *Textur2D::release()
  105. {
  106. if( !--ref )
  107. delete this;
  108. return 0;
  109. }