[3573] | 1 | |
---|
| 2 | |
---|
| 3 | /* |
---|
| 4 | orxonox - the future of 3D-vertical-scrollers |
---|
| 5 | |
---|
| 6 | Copyright (C) 2004 orx |
---|
| 7 | |
---|
| 8 | This program is free software; you can redistribute it and/or modify |
---|
| 9 | it under the terms of the GNU General Public License as published by |
---|
| 10 | the Free Software Foundation; either version 2, or (at your option) |
---|
| 11 | any later version. |
---|
| 12 | |
---|
| 13 | ### File Specific: |
---|
| 14 | main-programmer: Patrick Boenzli |
---|
| 15 | co-programmer: ... |
---|
| 16 | */ |
---|
| 17 | |
---|
| 18 | |
---|
| 19 | #include "simple_animation.h" |
---|
| 20 | #include "stdincl.h" |
---|
[3720] | 21 | #include "vector.h" |
---|
[3729] | 22 | #include "world_entity.h" |
---|
[3573] | 23 | |
---|
| 24 | using namespace std; |
---|
| 25 | |
---|
| 26 | |
---|
| 27 | |
---|
[3727] | 28 | SimpleAnimation* SimpleAnimation::singletonRef = 0; |
---|
[3573] | 29 | /** |
---|
[3727] | 30 | \brief gets the singleton instance |
---|
| 31 | \returns singleton instance |
---|
| 32 | */ |
---|
| 33 | SimpleAnimation* SimpleAnimation::getInstance() |
---|
| 34 | { |
---|
| 35 | if( singletonRef == NULL) |
---|
| 36 | singletonRef = new SimpleAnimation(); |
---|
| 37 | return singletonRef; |
---|
| 38 | } |
---|
| 39 | |
---|
| 40 | /** |
---|
[3573] | 41 | \brief standard constructor |
---|
| 42 | */ |
---|
[3727] | 43 | SimpleAnimation::SimpleAnimation () |
---|
[3573] | 44 | { |
---|
| 45 | this->setClassName ("SimpleAnimation"); |
---|
| 46 | this->frames = new tList<KeyFrame>(); |
---|
| 47 | this->localTime = 0; |
---|
[3719] | 48 | this->bRunning = false; |
---|
| 49 | this->currentFrame = NULL; |
---|
| 50 | this->lastFrame = NULL; |
---|
[3720] | 51 | |
---|
| 52 | this->tmpVect = new Vector(); |
---|
[3729] | 53 | this->lastPosition = new Vector(); |
---|
[3573] | 54 | } |
---|
| 55 | |
---|
| 56 | |
---|
| 57 | /** |
---|
| 58 | \brief standard deconstructor |
---|
| 59 | |
---|
| 60 | */ |
---|
| 61 | SimpleAnimation::~SimpleAnimation () |
---|
| 62 | { |
---|
[3661] | 63 | tIterator<KeyFrame>* iterator = this->frames->getIterator(); |
---|
| 64 | KeyFrame* frame = iterator->nextElement(); |
---|
[3573] | 65 | while( frame != NULL) |
---|
| 66 | { |
---|
| 67 | delete frame; |
---|
[3661] | 68 | frame = iterator->nextElement(); |
---|
[3573] | 69 | } |
---|
[3661] | 70 | delete iterator; |
---|
[3573] | 71 | delete this->frames; |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | |
---|
[3727] | 75 | /** |
---|
| 76 | \brief this determines the start of an Animator Describtion |
---|
[3573] | 77 | |
---|
[3727] | 78 | this can then be followed by different commands like addKeyFrame(..) etc. and |
---|
| 79 | will be closed with AnimatiorEnd() |
---|
| 80 | */ |
---|
[3729] | 81 | void SimpleAnimation::animatorBegin() |
---|
[3727] | 82 | { |
---|
| 83 | this->bDescriptive = true; |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | |
---|
[3573] | 87 | /** |
---|
[3727] | 88 | \brief this determines the end of an Animator Describtion |
---|
| 89 | |
---|
| 90 | this can then be followed by different commands like addKeyFrame(..) etc. and |
---|
| 91 | will be closed with AnimatiorEnd() |
---|
| 92 | */ |
---|
[3729] | 93 | void SimpleAnimation::animatorEnd() |
---|
[3727] | 94 | { |
---|
| 95 | this->workingObject = NULL; |
---|
| 96 | this->bDescriptive = false; |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | |
---|
| 100 | /** |
---|
| 101 | \brief select an object to work on by using this function |
---|
| 102 | \param object wo work on |
---|
| 103 | */ |
---|
| 104 | void SimpleAnimation::selectObject(WorldEntity* entity) |
---|
| 105 | { |
---|
| 106 | this->workingObject = entity; |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | |
---|
| 110 | |
---|
| 111 | /** |
---|
[3573] | 112 | \brief adds a keyframe with properties |
---|
| 113 | \param the point of the object |
---|
[3729] | 114 | \param and the direction of it |
---|
[3573] | 115 | \param at this time |
---|
| 116 | */ |
---|
[3729] | 117 | void SimpleAnimation::addKeyFrame(Vector* point, Quaternion* direction, float time) |
---|
[3573] | 118 | { |
---|
[3727] | 119 | if( !this->bDescriptive) |
---|
| 120 | { |
---|
| 121 | PRINTF(1)("SimpleAnimation: executing animation code outside a AnimationBegin()/AnimationEnd() - ignoring\n"); |
---|
| 122 | return; |
---|
| 123 | } |
---|
[3726] | 124 | KeyFrame* frame = new KeyFrame; |
---|
| 125 | frame->position = point; |
---|
[3729] | 126 | frame->direction = direction; |
---|
[3726] | 127 | frame->time = time; |
---|
| 128 | frame->mode = DEFAULT_ANIMATION_MODE; |
---|
[3727] | 129 | frame->object = this->workingObject; |
---|
[3573] | 130 | this->frames->add(frame); |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | |
---|
| 134 | /** |
---|
| 135 | \brief adds a keyframe with properties |
---|
| 136 | \param the point of the object |
---|
[3729] | 137 | \param and the direction of it |
---|
[3573] | 138 | \param at this time |
---|
| 139 | \param function of the velocity of the movement |
---|
| 140 | */ |
---|
[3729] | 141 | void SimpleAnimation::addKeyFrame(Vector* point, Quaternion* direction, float time, movementMode mode) |
---|
[3573] | 142 | { |
---|
[3727] | 143 | if( !this->bDescriptive) |
---|
| 144 | { |
---|
| 145 | PRINTF(1)("SimpleAnimation: executing animation code outside a AnimationBegin()/AnimationEnd() - ignoring\n"); |
---|
| 146 | return; |
---|
| 147 | } |
---|
[3726] | 148 | KeyFrame* frame = new KeyFrame; |
---|
| 149 | frame->position = point; |
---|
[3729] | 150 | frame->direction = direction; |
---|
[3726] | 151 | frame->time = time; |
---|
| 152 | frame->mode = mode; |
---|
[3727] | 153 | frame->object = this->workingObject; |
---|
[3573] | 154 | this->frames->add(frame); |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | /** |
---|
| 158 | \brief adds a already defined keyframe |
---|
| 159 | \param the keyframe to add |
---|
| 160 | */ |
---|
| 161 | void SimpleAnimation::addKeyFrame(KeyFrame* frame) |
---|
| 162 | { |
---|
[3727] | 163 | if( !this->bDescriptive) |
---|
| 164 | { |
---|
| 165 | PRINTF(1)("SimpleAnimation: executing animation code outside a AnimationBegin()/AnimationEnd() - ignoring\n"); |
---|
| 166 | return; |
---|
| 167 | } |
---|
| 168 | frame->object = this->workingObject; |
---|
| 169 | this->frames->add(frame); |
---|
[3573] | 170 | } |
---|
| 171 | |
---|
| 172 | |
---|
| 173 | /** |
---|
| 174 | \brief clear the list of keyframes, deleting all keyframes included |
---|
| 175 | */ |
---|
| 176 | void SimpleAnimation::reset() |
---|
| 177 | { |
---|
[3661] | 178 | tIterator<KeyFrame>* iterator = this->frames->getIterator(); |
---|
| 179 | KeyFrame* frame = iterator->nextElement(); |
---|
[3573] | 180 | while( frame != NULL) |
---|
| 181 | { |
---|
| 182 | delete frame; |
---|
[3661] | 183 | frame = iterator->nextElement(); |
---|
[3573] | 184 | } |
---|
[3661] | 185 | delete iterator; |
---|
[3573] | 186 | delete this->frames; |
---|
| 187 | |
---|
| 188 | this->frames = new tList<KeyFrame>(); |
---|
| 189 | this->localTime = 0; |
---|
[3719] | 190 | this->bRunning = false; |
---|
[3573] | 191 | |
---|
[3719] | 192 | this->currentFrame = NULL; |
---|
| 193 | this->lastFrame = NULL; |
---|
[3573] | 194 | } |
---|
| 195 | |
---|
| 196 | /** |
---|
| 197 | \brief starts the animation, therefore listens to tick signals |
---|
| 198 | */ |
---|
| 199 | void SimpleAnimation::start() |
---|
[3719] | 200 | { |
---|
| 201 | if( this->bRunning) |
---|
| 202 | { |
---|
| 203 | PRINTF(2)("SimpleAnimatin is already running. You are trying to start it again.\n"); |
---|
| 204 | return; |
---|
| 205 | } |
---|
| 206 | |
---|
| 207 | this->localTime = 0; |
---|
| 208 | this->lastFrame = this->frames->firstElement(); |
---|
| 209 | this->currentFrame = this->frames->nextElement(this->currentFrame); |
---|
| 210 | this->bRunning = true; |
---|
| 211 | } |
---|
[3573] | 212 | |
---|
| 213 | |
---|
| 214 | /** |
---|
| 215 | \brief stops the animation, immune to tick signals |
---|
| 216 | */ |
---|
| 217 | void SimpleAnimation::stop() |
---|
[3719] | 218 | { |
---|
| 219 | this->bRunning = false; |
---|
| 220 | } |
---|
[3573] | 221 | |
---|
| 222 | /** |
---|
| 223 | \brief stops and then starts the animation from begining |
---|
| 224 | */ |
---|
| 225 | void SimpleAnimation::restart() |
---|
| 226 | { |
---|
| 227 | this->localTime = 0; |
---|
[3719] | 228 | this->lastFrame = this->frames->firstElement(); |
---|
| 229 | this->currentFrame = this->frames->nextElement(this->currentFrame); |
---|
| 230 | this->bRunning = true; |
---|
[3573] | 231 | } |
---|
| 232 | |
---|
| 233 | /** |
---|
| 234 | \brief pauses the animation until resumed |
---|
| 235 | */ |
---|
| 236 | void SimpleAnimation::pause() |
---|
| 237 | { |
---|
[3719] | 238 | this->bRunning = false; |
---|
[3573] | 239 | } |
---|
| 240 | |
---|
| 241 | /** |
---|
| 242 | \brief resumes a pause, if not paused, no effect |
---|
| 243 | */ |
---|
| 244 | void SimpleAnimation::resume() |
---|
| 245 | { |
---|
[3719] | 246 | this->bRunning = true; |
---|
[3573] | 247 | } |
---|
| 248 | |
---|
| 249 | |
---|
| 250 | /** |
---|
| 251 | \brief heart beat, next animation step |
---|
| 252 | */ |
---|
| 253 | void SimpleAnimation::tick(float time) |
---|
| 254 | { |
---|
[3719] | 255 | if( !this->bRunning) |
---|
| 256 | return; |
---|
| 257 | |
---|
| 258 | this->localTime += time; |
---|
| 259 | /* first get the current frame via time-stamps */ |
---|
| 260 | while( this->localTime > this->currentFrame->time) |
---|
[3573] | 261 | { |
---|
[3729] | 262 | printf("SimpleAnimation::tick(...) - changing Frame\n"); |
---|
| 263 | this->localTime -= this->currentFrame->time; |
---|
| 264 | |
---|
[3730] | 265 | this->currentFrame->object->setRelCoor(this->currentFrame->position); |
---|
| 266 | *this->lastPosition = *this->currentFrame->position; |
---|
| 267 | |
---|
[3720] | 268 | this->lastFrame = this->currentFrame; |
---|
[3719] | 269 | this->currentFrame = this->frames->nextElement(this->currentFrame); |
---|
| 270 | } |
---|
[3730] | 271 | |
---|
[3719] | 272 | /* now animate it */ |
---|
| 273 | switch( this->mode) |
---|
| 274 | { |
---|
| 275 | case LINEAR: |
---|
[3720] | 276 | |
---|
[3726] | 277 | *this->tmpVect = *this->currentFrame->position - *this->lastFrame->position; |
---|
[3720] | 278 | *this->tmpVect = *this->tmpVect * this->localTime / this->currentFrame->time; |
---|
[3729] | 279 | this->currentFrame->object->shiftCoor(*this->tmpVect - *this->lastPosition); |
---|
| 280 | *this->lastPosition = *this->tmpVect; |
---|
[3719] | 281 | break; |
---|
| 282 | case EXP: |
---|
| 283 | |
---|
| 284 | break; |
---|
| 285 | case NEG_EXP: |
---|
[3726] | 286 | *this->tmpVect = *this->currentFrame->position - *this->lastFrame->position; |
---|
[3720] | 287 | *this->tmpVect = *this->tmpVect * (1 - exp(- this->localTime / this->currentFrame->time)); |
---|
[3719] | 288 | break; |
---|
| 289 | case SIN: |
---|
[3726] | 290 | *this->tmpVect = *this->currentFrame->position - *this->lastFrame->position; |
---|
[3720] | 291 | *this->tmpVect = *this->tmpVect * (1 - cos(- this->localTime / this->currentFrame->time)); |
---|
[3719] | 292 | break; |
---|
| 293 | case COS: |
---|
| 294 | |
---|
| 295 | break; |
---|
| 296 | case QUADRATIC: |
---|
[3726] | 297 | *this->tmpVect = *this->currentFrame->position - *this->lastFrame->position; |
---|
[3720] | 298 | *this->tmpVect = *this->tmpVect * 1/3 * ldexpf(this->localTime, 3); |
---|
[3719] | 299 | break; |
---|
| 300 | default: |
---|
| 301 | break; |
---|
[3573] | 302 | } |
---|
| 303 | } |
---|