[3782] | 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 | |
---|
| 17 | #include "animation.h" |
---|
| 18 | #include "debug.h" |
---|
[3784] | 19 | #include "list.h" |
---|
[3782] | 20 | |
---|
| 21 | |
---|
| 22 | Anim::Anim(void) |
---|
| 23 | { |
---|
[3784] | 24 | // create a new List |
---|
| 25 | this->keyFrameList = new tList<AnimKeyFrame>(); |
---|
| 26 | |
---|
| 27 | // initialize a beginning KeyFrame, that will be deleted afterwards |
---|
| 28 | this->bHasKeys = false; |
---|
[3785] | 29 | AnimKeyFrame* tmpKeyFrame = new AnimKeyFrame; |
---|
| 30 | tmpKeyFrame->value = 0.0; |
---|
| 31 | tmpKeyFrame->duration = 1.0; |
---|
| 32 | keyFrameList->add(tmpKeyFrame); |
---|
[3784] | 33 | |
---|
[3785] | 34 | // setting default values |
---|
[3786] | 35 | this->localTime = 0.0; |
---|
[3787] | 36 | this->bRunning = true; |
---|
[3785] | 37 | this->animFunc = &Anim::linear; |
---|
| 38 | this->currentKeyFrame = tmpKeyFrame; |
---|
| 39 | this->nextKeyFrame = tmpKeyFrame; |
---|
[3782] | 40 | } |
---|
| 41 | |
---|
| 42 | |
---|
[3784] | 43 | Anim::~Anim(void) |
---|
| 44 | { |
---|
| 45 | // delete all the KeyFrames |
---|
| 46 | tIterator<AnimKeyFrame>* itKF = keyFrameList->getIterator(); |
---|
| 47 | AnimKeyFrame* enumKF = itKF->nextElement(); |
---|
| 48 | while (enumKF) |
---|
| 49 | { |
---|
| 50 | delete enumKF; |
---|
| 51 | enumKF = itKF->nextElement(); |
---|
| 52 | } |
---|
| 53 | delete itKF; |
---|
| 54 | delete this->keyFrameList; |
---|
| 55 | } |
---|
| 56 | |
---|
[3782] | 57 | tList<Anim>* Anim::animatorList = NULL; |
---|
[3784] | 58 | |
---|
| 59 | |
---|
| 60 | void Anim::addKeyFrame(float value, float duration, ANIM_FUNCTION animFunc) |
---|
| 61 | { |
---|
[3785] | 62 | // some small check |
---|
| 63 | if (duration <= 0.0) |
---|
| 64 | duration = 1.0; |
---|
| 65 | |
---|
| 66 | |
---|
| 67 | AnimKeyFrame* tmpKeyFrame; |
---|
| 68 | |
---|
| 69 | if (bHasKeys) |
---|
[3784] | 70 | { |
---|
[3785] | 71 | tmpKeyFrame = new AnimKeyFrame; |
---|
| 72 | if (this->currentKeyFrame == this->nextKeyFrame) |
---|
| 73 | this->nextKeyFrame = tmpKeyFrame; |
---|
[3786] | 74 | this->keyFrameList->add(tmpKeyFrame); |
---|
| 75 | |
---|
[3785] | 76 | } |
---|
| 77 | else |
---|
| 78 | { |
---|
| 79 | tmpKeyFrame = this->keyFrameList->firstElement(); |
---|
[3784] | 80 | bHasKeys = true; |
---|
| 81 | } |
---|
| 82 | tmpKeyFrame->value = value; |
---|
| 83 | tmpKeyFrame->duration = duration; |
---|
| 84 | tmpKeyFrame->animFunc = animFunc; |
---|
| 85 | |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | void Anim::setInfinity(ANIM_INFINITY preInfinity, ANIM_INFINITY postInfinity) |
---|
| 89 | { |
---|
| 90 | this->preInfinity = preInfinity; |
---|
| 91 | this->postInfinity = postInfinity; |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | void Anim::setAnimFunc(ANIM_FUNCTION animFunc) |
---|
| 95 | { |
---|
| 96 | switch (animFunc) |
---|
| 97 | { |
---|
| 98 | default: |
---|
| 99 | case ANIM_CONSTANT: |
---|
| 100 | this->animFunc = &Anim::constant; |
---|
| 101 | break; |
---|
| 102 | case ANIM_LINEAR: |
---|
| 103 | this->animFunc = &Anim::linear; |
---|
| 104 | break; |
---|
| 105 | case ANIM_RANDOM: |
---|
| 106 | this->animFunc = &Anim::random; |
---|
| 107 | break; |
---|
| 108 | case ANIM_SINE: |
---|
| 109 | this->animFunc = &Anim::sine; |
---|
| 110 | break; |
---|
| 111 | } |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | |
---|
| 115 | // animation functions |
---|
[3786] | 116 | float Anim::random(float timePassed) const |
---|
[3784] | 117 | { |
---|
| 118 | return (float)rand()/(float)RAND_MAX; |
---|
| 119 | } |
---|
| 120 | |
---|
[3786] | 121 | float Anim::constant(float timePassed) const |
---|
[3784] | 122 | { |
---|
[3785] | 123 | return this->currentKeyFrame->value; |
---|
[3784] | 124 | } |
---|
| 125 | |
---|
[3786] | 126 | float Anim::linear(float timePassed) const |
---|
[3784] | 127 | { |
---|
[3785] | 128 | return this->nextKeyFrame->value - (this->nextKeyFrame->value - this->currentKeyFrame->value) |
---|
[3786] | 129 | * (timePassed / this->currentKeyFrame->duration); |
---|
[3785] | 130 | // PRINTF(0)("value is %f, %p %p\n", val, this->currentKeyFrame, this->nextKeyFrame); |
---|
| 131 | // return val; |
---|
[3784] | 132 | } |
---|
| 133 | |
---|
[3786] | 134 | float Anim::sine(float timePassed) const |
---|
[3784] | 135 | { |
---|
[3786] | 136 | |
---|
[3784] | 137 | } |
---|