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 | this->setName("AnimationPlayer"); |
---|
32 | |
---|
33 | this->animationList = new tList<Animation>(); |
---|
34 | this->play(); |
---|
35 | } |
---|
36 | |
---|
37 | /** |
---|
38 | \brief the singleton reference to this class |
---|
39 | */ |
---|
40 | AnimationPlayer* AnimationPlayer::singletonRef = NULL; |
---|
41 | |
---|
42 | /** |
---|
43 | \brief standard deconstructor |
---|
44 | |
---|
45 | !! DANGER !! when unloading the AnimationPlayer no other Function |
---|
46 | should reference any Animations, from the animationList because it |
---|
47 | automatically deletes them. |
---|
48 | This usually happens when unloading a World. |
---|
49 | */ |
---|
50 | AnimationPlayer::~AnimationPlayer () |
---|
51 | { |
---|
52 | // deleting the Animation List AND all the elements of the List |
---|
53 | this->flush(); |
---|
54 | delete this->animationList; |
---|
55 | |
---|
56 | AnimationPlayer::singletonRef = NULL; |
---|
57 | } |
---|
58 | |
---|
59 | /** |
---|
60 | \brief adds an Animation to the AnimationList. |
---|
61 | \param animation the Animation to handle |
---|
62 | |
---|
63 | when adding a Animation the Animation will too be deleted when |
---|
64 | the AnimationPlayer gets deleted. Consider not adding it, or |
---|
65 | unadding it with animation->notHandled(); |
---|
66 | */ |
---|
67 | void AnimationPlayer::addAnimation(Animation* animation) |
---|
68 | { |
---|
69 | this->animationList->add(animation); |
---|
70 | } |
---|
71 | |
---|
72 | /** |
---|
73 | \brief removes an Animation from the Animation List, WITHOUT deleting it. |
---|
74 | \param animation the Anmination to remove from the List |
---|
75 | */ |
---|
76 | void AnimationPlayer::removeAnimation(Animation* animation) |
---|
77 | { |
---|
78 | this->animationList->remove(animation); |
---|
79 | } |
---|
80 | |
---|
81 | /** |
---|
82 | \brief empties the list AND deletes all the Animations |
---|
83 | */ |
---|
84 | void AnimationPlayer::flush(void) |
---|
85 | { |
---|
86 | // deleting the Animation List AND all the elements of the List |
---|
87 | tIterator<Animation>* animIt = this->animationList->getIterator(); |
---|
88 | Animation* anim = animIt->nextElement(); |
---|
89 | while( anim != NULL) |
---|
90 | { |
---|
91 | delete anim; |
---|
92 | this->animationList->remove(anim); |
---|
93 | anim = animIt->nextElement(); |
---|
94 | } |
---|
95 | delete animIt; |
---|
96 | |
---|
97 | delete this->animationList; |
---|
98 | this->animationList = new tList<Animation>(); |
---|
99 | } |
---|
100 | |
---|
101 | /** |
---|
102 | \brief Ticks all the animations in animationList |
---|
103 | \param timePassed the time passed since the last tick. |
---|
104 | */ |
---|
105 | void AnimationPlayer::tick(float timePassed) |
---|
106 | { |
---|
107 | if (this->bRunning) |
---|
108 | { |
---|
109 | // iterate through all the animations and tick them. |
---|
110 | tIterator<Animation>* animIt = this->animationList->getIterator(); |
---|
111 | Animation* anim = animIt->nextElement(); |
---|
112 | while( anim != NULL) |
---|
113 | { |
---|
114 | anim->tick(timePassed); |
---|
115 | if(unlikely(anim->ifDelete())) |
---|
116 | { |
---|
117 | this->animationList->remove(anim); |
---|
118 | delete anim; |
---|
119 | } |
---|
120 | anim = animIt->nextElement(); |
---|
121 | } |
---|
122 | delete animIt; |
---|
123 | } |
---|
124 | } |
---|
125 | /** |
---|
126 | \brief starts playing the AnimationPlayer |
---|
127 | */ |
---|
128 | void AnimationPlayer::play(void) |
---|
129 | { |
---|
130 | this->bRunning = true; |
---|
131 | } |
---|
132 | |
---|
133 | /** |
---|
134 | \brief pauses playing of the AnimationPlayer |
---|
135 | */ |
---|
136 | void AnimationPlayer::pause(void) |
---|
137 | { |
---|
138 | this->bRunning = false; |
---|
139 | } |
---|
140 | |
---|
141 | /** |
---|
142 | \returns the animation from a certain baseobject |
---|
143 | if multiple are found, it just retruns the first one |
---|
144 | if none is found it returns NULL |
---|
145 | */ |
---|
146 | Animation* AnimationPlayer::getAnimationFromBaseObject(const BaseObject* baseObject) const |
---|
147 | { |
---|
148 | tIterator<Animation>* animIt = this->animationList->getIterator(); |
---|
149 | Animation* anim = animIt->nextElement(); |
---|
150 | while( anim != NULL) |
---|
151 | { |
---|
152 | if(anim->getBaseObject() == baseObject) |
---|
153 | { |
---|
154 | delete animIt; |
---|
155 | return anim; |
---|
156 | } |
---|
157 | anim = animIt->nextElement(); |
---|
158 | } |
---|
159 | delete animIt; |
---|
160 | return NULL; |
---|
161 | } |
---|
162 | |
---|
163 | |
---|
164 | |
---|
165 | /** |
---|
166 | \brief Outputs some nice debug-information |
---|
167 | */ |
---|
168 | void AnimationPlayer::debug(void) |
---|
169 | { |
---|
170 | PRINT(0)("+------------------------------------+\n"); |
---|
171 | PRINT(0)("+ ANIMATION PLAYER DEBUG INFORMATION +\n"); |
---|
172 | PRINT(0)("+------------------------------------+\n"); |
---|
173 | PRINT(0)("| Reference: %p\n", this); |
---|
174 | PRINT(0)("| CountOfAnims %d\n", this->animationList->getSize()); |
---|
175 | PRINT(0)("-Animation Information---------------+\n"); |
---|
176 | // Per ANIMATION DEBUG |
---|
177 | tIterator<Animation>* animIt = this->animationList->getIterator(); |
---|
178 | Animation* anim = animIt->nextElement(); |
---|
179 | while( anim != NULL) |
---|
180 | { |
---|
181 | // anim->debug(); |
---|
182 | anim = animIt->nextElement(); |
---|
183 | } |
---|
184 | delete animIt; |
---|
185 | |
---|
186 | PRINT(0)("+--------------------------------AP--+\n"); |
---|
187 | } |
---|