| 1 | /* |
|---|
| 2 | orxonox - the future of 3D-vertical-scrollers |
|---|
| 3 | |
|---|
| 4 | Copyright (C) 2004 orx |
|---|
| 5 | |
|---|
| 6 | This program is free software; you can redistribute it and/or modify |
|---|
| 7 | it under the terms of the GNU General Public License as published by |
|---|
| 8 | the Free Software Foundation; either version 2, or (at your option) |
|---|
| 9 | any later version. |
|---|
| 10 | |
|---|
| 11 | ### File Specific: |
|---|
| 12 | main-programmer: Benjamin Grauer |
|---|
| 13 | co-programmer: ... |
|---|
| 14 | */ |
|---|
| 15 | |
|---|
| 16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_ANIM |
|---|
| 17 | |
|---|
| 18 | #include "animation_player.h" |
|---|
| 19 | |
|---|
| 20 | #include "compiler.h" |
|---|
| 21 | |
|---|
| 22 | using namespace std; |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | /** |
|---|
| 26 | \brief standard constructor |
|---|
| 27 | */ |
|---|
| 28 | AnimationPlayer::AnimationPlayer () |
|---|
| 29 | { |
|---|
| 30 | this->setClassID(CL_ANIMATION_PLAYER, "AnimationPlayer"); |
|---|
| 31 | |
|---|
| 32 | this->animationList = new tList<Animation>(); |
|---|
| 33 | this->play(); |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | /** |
|---|
| 37 | \brief the singleton reference to this class |
|---|
| 38 | */ |
|---|
| 39 | AnimationPlayer* AnimationPlayer::singletonRef = NULL; |
|---|
| 40 | |
|---|
| 41 | /** |
|---|
| 42 | \returns a Pointer to this Class |
|---|
| 43 | */ |
|---|
| 44 | AnimationPlayer* AnimationPlayer::getInstance(void) |
|---|
| 45 | { |
|---|
| 46 | if (!AnimationPlayer::singletonRef) |
|---|
| 47 | AnimationPlayer::singletonRef = new AnimationPlayer(); |
|---|
| 48 | return AnimationPlayer::singletonRef; |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | /** |
|---|
| 52 | \brief standard deconstructor |
|---|
| 53 | |
|---|
| 54 | !! DANGER !! when unloading the AnimationPlayer no other Function |
|---|
| 55 | should reference any Animations, from the animationList because it |
|---|
| 56 | automatically deletes them. |
|---|
| 57 | This usually happens when unloading a World. |
|---|
| 58 | */ |
|---|
| 59 | AnimationPlayer::~AnimationPlayer () |
|---|
| 60 | { |
|---|
| 61 | // deleting the Animation List AND all the elements of the List |
|---|
| 62 | this->flush(); |
|---|
| 63 | delete this->animationList; |
|---|
| 64 | |
|---|
| 65 | AnimationPlayer::singletonRef = NULL; |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | /** |
|---|
| 69 | \brief adds an Animation to the AnimationList. |
|---|
| 70 | \param animation the Animation to handle |
|---|
| 71 | |
|---|
| 72 | when adding a Animation the Animation will too be deleted when |
|---|
| 73 | the AnimationPlayer gets deleted. Consider not adding it, or |
|---|
| 74 | unadding it with animation->notHandled(); |
|---|
| 75 | */ |
|---|
| 76 | void AnimationPlayer::addAnimation(Animation* animation) |
|---|
| 77 | { |
|---|
| 78 | this->animationList->add(animation); |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | /** |
|---|
| 82 | \brief removes an Animation from the Animation List, WITHOUT deleting it. |
|---|
| 83 | \param animation the Anmination to remove from the List |
|---|
| 84 | */ |
|---|
| 85 | void AnimationPlayer::removeAnimation(Animation* animation) |
|---|
| 86 | { |
|---|
| 87 | this->animationList->remove(animation); |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | /** |
|---|
| 91 | \brief empties the list AND deletes all the Animations |
|---|
| 92 | */ |
|---|
| 93 | void AnimationPlayer::flush(void) |
|---|
| 94 | { |
|---|
| 95 | // deleting the Animation List AND all the elements of the List |
|---|
| 96 | tIterator<Animation>* animIt = this->animationList->getIterator(); |
|---|
| 97 | Animation* anim = animIt->nextElement(); |
|---|
| 98 | while( anim != NULL) |
|---|
| 99 | { |
|---|
| 100 | delete anim; |
|---|
| 101 | this->animationList->remove(anim); |
|---|
| 102 | anim = animIt->nextElement(); |
|---|
| 103 | } |
|---|
| 104 | delete animIt; |
|---|
| 105 | |
|---|
| 106 | delete this->animationList; |
|---|
| 107 | this->animationList = new tList<Animation>(); |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | /** |
|---|
| 111 | \brief Ticks all the animations in animationList |
|---|
| 112 | \param timePassed the time passed since the last tick. |
|---|
| 113 | */ |
|---|
| 114 | void AnimationPlayer::tick(float timePassed) |
|---|
| 115 | { |
|---|
| 116 | if (this->bRunning) |
|---|
| 117 | { |
|---|
| 118 | // iterate through all the animations and tick them. |
|---|
| 119 | tIterator<Animation>* animIt = this->animationList->getIterator(); |
|---|
| 120 | Animation* anim = animIt->nextElement(); |
|---|
| 121 | while( anim != NULL) |
|---|
| 122 | { |
|---|
| 123 | anim->tick(timePassed); |
|---|
| 124 | if(unlikely(anim->ifDelete())) |
|---|
| 125 | { |
|---|
| 126 | this->animationList->remove(anim); |
|---|
| 127 | delete anim; |
|---|
| 128 | } |
|---|
| 129 | anim = animIt->nextElement(); |
|---|
| 130 | } |
|---|
| 131 | delete animIt; |
|---|
| 132 | } |
|---|
| 133 | } |
|---|
| 134 | /** |
|---|
| 135 | \brief starts playing the AnimationPlayer |
|---|
| 136 | */ |
|---|
| 137 | void AnimationPlayer::play(void) |
|---|
| 138 | { |
|---|
| 139 | this->bRunning = true; |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | /** |
|---|
| 143 | \brief pauses playing of the AnimationPlayer |
|---|
| 144 | */ |
|---|
| 145 | void AnimationPlayer::pause(void) |
|---|
| 146 | { |
|---|
| 147 | this->bRunning = false; |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | |
|---|
| 151 | Animation* AnimationPlayer::getObjectFromBaseObject(const BaseObject* baseObject) const |
|---|
| 152 | { |
|---|
| 153 | tIterator<Animation>* animIt = this->animationList->getIterator(); |
|---|
| 154 | Animation* anim = animIt->nextElement(); |
|---|
| 155 | while( anim != NULL) |
|---|
| 156 | { |
|---|
| 157 | if(anim->getBaseObject() == baseObject) |
|---|
| 158 | { |
|---|
| 159 | delete animIt; |
|---|
| 160 | return anim; |
|---|
| 161 | } |
|---|
| 162 | anim = animIt->nextElement(); |
|---|
| 163 | } |
|---|
| 164 | delete animIt; |
|---|
| 165 | |
|---|
| 166 | } |
|---|
| 167 | |
|---|
| 168 | |
|---|
| 169 | |
|---|
| 170 | /** |
|---|
| 171 | \brief Outputs some nice debug-information |
|---|
| 172 | */ |
|---|
| 173 | void AnimationPlayer::debug(void) |
|---|
| 174 | { |
|---|
| 175 | PRINT(0)("+------------------------------------+\n"); |
|---|
| 176 | PRINT(0)("+ ANIMATION PLAYER DEBUG INFORMATION +\n"); |
|---|
| 177 | PRINT(0)("+------------------------------------+\n"); |
|---|
| 178 | PRINT(0)("| Reference: %p\n", this); |
|---|
| 179 | PRINT(0)("| CountOfAnims %d\n", this->animationList->getSize()); |
|---|
| 180 | PRINT(0)("-Animation Information---------------+\n"); |
|---|
| 181 | // Per ANIMATION DEBUG |
|---|
| 182 | tIterator<Animation>* animIt = this->animationList->getIterator(); |
|---|
| 183 | Animation* anim = animIt->nextElement(); |
|---|
| 184 | while( anim != NULL) |
|---|
| 185 | { |
|---|
| 186 | // anim->debug(); |
|---|
| 187 | anim = animIt->nextElement(); |
|---|
| 188 | } |
|---|
| 189 | delete animIt; |
|---|
| 190 | |
|---|
| 191 | PRINT(0)("+--------------------------------AP--+\n"); |
|---|
| 192 | } |
|---|