#include "Textur2D.h" #include "Animation.h" #include "Bild.h" using namespace Framework; // Contents of the Textur2D class from Textur2D.h // Konstructor Textur2D::Textur2D() : ReferenceCounter() { circularAnimation = 0; animationIndex = -1; txt = 0; animData = new Array(); } // Destructor Textur2D::~Textur2D() { if (txt) txt->release(); for (auto i : *animData) { i->data->release(); delete i; } animData->release(); } // specifies whether the animation should repeat automatically // ca: 1 if the animation should repeat automatically void Textur2D::setCircularAnimation(bool ca) { circularAnimation = ca; } // sets a pointer to the texture (if not animated) // textur: The pointer to the image void Textur2D::setTexturZ(Bild* textur) { if (txt) txt->release(); txt = textur; } // adds an animation // textur: The pointer to the animation data void Textur2D::addAnimationZ(Animation2DData* textur) { animData->add(new Animation{textur, 0, 0}); } // sets the current animation // index: The index of the animation void Textur2D::setAnimation(int index) { if (animationIndex == index) return; if (animationIndex != -1) { animData->get(animationIndex)->jetzt = 0; animData->get(animationIndex)->ausgleich = 0; } animationIndex = index; int anz = animData->getEintragAnzahl(); if (animationIndex >= anz && (!circularAnimation || anz == 0)) animationIndex = -1; else if (animationIndex >= anz) animationIndex = 0; } // activates the next animation void Textur2D::nextAnimation() { if (animationIndex != -1) { animData->get(animationIndex)->jetzt = 0; animData->get(animationIndex)->ausgleich = 0; } animationIndex++; int anz = animData->getEintragAnzahl(); if (animationIndex >= anz && (!circularAnimation || anz == 0)) animationIndex = -1; else if (animationIndex >= anz) animationIndex = 0; } // sets the elapsed time since the last call // t: the elapsed time in seconds bool Textur2D::tick(double t) { if (animationIndex != -1) { Animation* a = animData->get(animationIndex); a->ausgleich += t; int tmp = a->jetzt; int tmp2 = animationIndex; a->data->lock(); if (a->ausgleich >= 1.0 / a->data->getFPS()) { a->ausgleich -= 1.0 / a->data->getFPS(); if (++(a->jetzt) >= a->data->getBildAnzahl()) { a->jetzt = 0; if (!a->data->istWiederhohlend()) nextAnimation(); } } a->data->unlock(); if (tmp != a->jetzt || tmp2 != animationIndex) return 1; } return 0; } // returns the current texture Bild* Textur2D::zTextur() const { if (animationIndex != -1) return animData->get(animationIndex) ->data->zBild(animData->get(animationIndex)->jetzt); return txt; }