[4597] | 1 | /* |
---|
[1853] | 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. |
---|
[1855] | 10 | |
---|
| 11 | ### File Specific: |
---|
| 12 | main-programmer: ... |
---|
| 13 | co-programmer: ... |
---|
[1853] | 14 | */ |
---|
| 15 | |
---|
[3955] | 16 | //#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_ |
---|
[1853] | 17 | |
---|
[4415] | 18 | #include "quick_animation.h" |
---|
[1853] | 19 | |
---|
[4415] | 20 | #include "compiler.h" |
---|
| 21 | #include "debug.h" |
---|
| 22 | #ifndef NULL |
---|
| 23 | #define NULL 0 |
---|
| 24 | #endif |
---|
| 25 | |
---|
| 26 | |
---|
[1856] | 27 | using namespace std; |
---|
[1853] | 28 | |
---|
[3245] | 29 | /** |
---|
| 30 | \brief standard constructor |
---|
| 31 | */ |
---|
[4421] | 32 | QuickAnimation::QuickAnimation (void) |
---|
[3365] | 33 | { |
---|
[4597] | 34 | this->setClassID(CL_QUICK_ANIMATION, "QuickAnimation"); |
---|
[4320] | 35 | |
---|
[4654] | 36 | // initialize the First KeyFrame (will be deleted if a new one gets created) |
---|
| 37 | this->count = 0; |
---|
| 38 | this->first = this->current = new QuickKeyFrame; |
---|
| 39 | this->first->next = this->first->prev = this->first; |
---|
| 40 | this->first->position = 0.0; |
---|
| 41 | this->first->value = 0.0; |
---|
[3365] | 42 | } |
---|
[1853] | 43 | |
---|
[3245] | 44 | /** |
---|
[4654] | 45 | \brief deletes all the deconstructor stuff |
---|
[3245] | 46 | */ |
---|
[4421] | 47 | QuickAnimation::~QuickAnimation (void) |
---|
[3543] | 48 | { |
---|
[4415] | 49 | this->current = this->first; |
---|
[4654] | 50 | QuickKeyFrame* delKF = this->first; |
---|
[4597] | 51 | |
---|
[4654] | 52 | do |
---|
| 53 | { |
---|
| 54 | delKF = this->current; |
---|
| 55 | this->current = this->current->next; |
---|
| 56 | delete delKF; |
---|
| 57 | } while (this->current != this->first); |
---|
| 58 | |
---|
[3543] | 59 | } |
---|
[4415] | 60 | |
---|
[4421] | 61 | /** |
---|
| 62 | \brief adds a new entry to the list of keyframes |
---|
| 63 | \param position the position to add the key to |
---|
| 64 | \param value the Value to set for the position |
---|
| 65 | \returns false if the key existed already for a given position |
---|
| 66 | */ |
---|
[4654] | 67 | void QuickAnimation::addEntry(float position, float value) |
---|
[4415] | 68 | { |
---|
[4654] | 69 | if (this->getName()) |
---|
| 70 | { |
---|
| 71 | printf("ADDED KEYFRAME %d\n", this->count); |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | // create the new KeyFrame |
---|
| 75 | QuickKeyFrame* newKey = new QuickKeyFrame; |
---|
| 76 | newKey->position = position; |
---|
| 77 | newKey->value = value; |
---|
| 78 | |
---|
| 79 | // if we add a KeyFrame for the first Time: |
---|
| 80 | if (unlikely (this->count == 0)) |
---|
| 81 | { |
---|
| 82 | delete this->first; |
---|
| 83 | |
---|
| 84 | newKey->next = newKey; |
---|
| 85 | newKey->prev = newKey; |
---|
| 86 | this->first = newKey; |
---|
| 87 | this->current = newKey; |
---|
| 88 | } |
---|
| 89 | // if the KeyFrame is in front of the FIRST keyFrame |
---|
| 90 | else if (this->first->position > position) |
---|
| 91 | { |
---|
| 92 | newKey->next = this->first; |
---|
| 93 | newKey->prev = this->first->prev; |
---|
| 94 | newKey->prev->next = newKey; |
---|
| 95 | newKey->next->prev = newKey; |
---|
| 96 | |
---|
| 97 | this->first = newKey; // the new Key becomes the FIRST. |
---|
| 98 | } |
---|
| 99 | // if the KeyFrame is at the End of the Animation |
---|
| 100 | else if (this->first->prev->position < position) |
---|
| 101 | { |
---|
| 102 | newKey->next = this->first; |
---|
| 103 | newKey->prev = this->first->prev; |
---|
| 104 | newKey->prev->next = newKey; |
---|
| 105 | newKey->next->prev = newKey; |
---|
| 106 | } |
---|
| 107 | // if the Key is between two frames. |
---|
| 108 | else |
---|
| 109 | { |
---|
| 110 | this->current = this->first; |
---|
| 111 | do |
---|
[4415] | 112 | { |
---|
[4654] | 113 | if (this->current->position < position && this->current->next->position > position) |
---|
[4597] | 114 | break; |
---|
[4421] | 115 | // if it is the same as an already existing keyframe |
---|
| 116 | else if (this->current->position == position) |
---|
[4654] | 117 | { |
---|
| 118 | this->current->value = value; |
---|
| 119 | delete newKey; |
---|
| 120 | return; |
---|
| 121 | } |
---|
[4421] | 122 | this->current = this->current->next; |
---|
[4654] | 123 | } while (this->current != this->first); |
---|
[4415] | 124 | |
---|
[4654] | 125 | newKey->next = this->current->next; |
---|
| 126 | newKey->prev = this->current; |
---|
| 127 | newKey->next->prev = newKey; |
---|
| 128 | newKey->prev->next = newKey; |
---|
| 129 | } |
---|
[4421] | 130 | this->current = this->first; |
---|
[4654] | 131 | ++this->count; |
---|
[4415] | 132 | } |
---|
| 133 | |
---|
[4421] | 134 | /** |
---|
[4425] | 135 | \brief changes an entry in the region of position |
---|
| 136 | \param position the Position of an existing keyframe |
---|
| 137 | \param region a deviation of the existing keyframe (like a delta in witch to search for |
---|
| 138 | \param value the new Value |
---|
[4654] | 139 | |
---|
| 140 | \todo rimplement |
---|
[4425] | 141 | */ |
---|
[4654] | 142 | void QuickAnimation::changeEntry(float position, float value, float region) |
---|
[4425] | 143 | { |
---|
[4654] | 144 | // this->current = this->first; |
---|
| 145 | // do |
---|
| 146 | // { |
---|
| 147 | // if (this->current->position < position+region && this->current->position > position-region) |
---|
| 148 | // { |
---|
| 149 | // this->current->value = value; |
---|
| 150 | // return; |
---|
| 151 | // } |
---|
| 152 | // this->current = this->current->next; |
---|
| 153 | // } while (this->current != this->first); |
---|
| 154 | // |
---|
| 155 | // this->current = this->first; |
---|
[4649] | 156 | |
---|
| 157 | this->addEntry(position, value); |
---|
[4425] | 158 | } |
---|
| 159 | |
---|
| 160 | /* |
---|
| 161 | \param position The position where to find the Node to kill |
---|
[4597] | 162 | |
---|
[4425] | 163 | bool QuickAnimation::removeEntry(float position) |
---|
| 164 | { |
---|
| 165 | this->current = this->first; |
---|
[4597] | 166 | QuickKeyFrame* last = |
---|
| 167 | |
---|
[4425] | 168 | while (this->current) |
---|
| 169 | { |
---|
| 170 | if (this->current->position == position) |
---|
| 171 | { |
---|
[4597] | 172 | |
---|
| 173 | |
---|
[4425] | 174 | } |
---|
[4597] | 175 | this->current = this->current->next; |
---|
[4425] | 176 | } |
---|
| 177 | this->current = this->first; |
---|
| 178 | } |
---|
| 179 | */ |
---|
| 180 | |
---|
| 181 | /** |
---|
[4421] | 182 | \brief returns the value of the animation at a certain position |
---|
| 183 | \param position the position to get the value from :) |
---|
| 184 | */ |
---|
[4597] | 185 | float QuickAnimation::getValue(float position) |
---|
[4415] | 186 | { |
---|
[4654] | 187 | int counter = 0; |
---|
| 188 | while (true) |
---|
| 189 | { |
---|
| 190 | counter++; |
---|
| 191 | if (counter >= 10) |
---|
[4415] | 192 | { |
---|
[4654] | 193 | printf("FUCK!!! %f\n", position); |
---|
| 194 | this->debug(); |
---|
| 195 | break; |
---|
[4597] | 196 | |
---|
[4654] | 197 | } |
---|
| 198 | // if we have a match |
---|
| 199 | if (likely(this->current->position <= position && this->current->next->position >= position)) |
---|
[4421] | 200 | return this->current->value + (this->current->next->value - this->current->value) |
---|
[4654] | 201 | * ((position-this->current->position) / (this->current->next->position -this->current->position)); |
---|
| 202 | |
---|
| 203 | else if (unlikely(this->first->prev->position < position)) |
---|
| 204 | return this->first->prev->value; |
---|
| 205 | else if (unlikely(this->first->position > position)) |
---|
| 206 | return this->first->value; |
---|
| 207 | else if(likely(this->current->next->position < position)) |
---|
| 208 | this->current = this->current->next; |
---|
| 209 | else if (likely(this->current->position > position)) |
---|
| 210 | this->current = this->current->prev; |
---|
| 211 | } |
---|
[4415] | 212 | } |
---|
| 213 | |
---|
[4479] | 214 | /** |
---|
| 215 | \brief outputs some nice information about this class |
---|
| 216 | */ |
---|
[4421] | 217 | void QuickAnimation::debug(void) |
---|
| 218 | { |
---|
| 219 | this->current = this->first; |
---|
| 220 | |
---|
[4654] | 221 | PRINT(0)("QuickAnim(entries:%d)::(position, value)", this->count); |
---|
| 222 | do |
---|
[4421] | 223 | { |
---|
| 224 | PRINT(0)("->(%f, %f)", this->current->position, this->current->value); |
---|
| 225 | this->current = this->current->next; |
---|
[4654] | 226 | } while(this->current != this->first); |
---|
[4421] | 227 | |
---|
| 228 | PRINT(0)("\n"); |
---|
| 229 | this->current = this->first; |
---|
| 230 | } |
---|