[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 | |
---|
[4421] | 36 | this->first = this->current = NULL; |
---|
[3365] | 37 | } |
---|
[1853] | 38 | |
---|
| 39 | |
---|
[3245] | 40 | /** |
---|
| 41 | \brief standard deconstructor |
---|
[1853] | 42 | |
---|
[3245] | 43 | */ |
---|
[4421] | 44 | QuickAnimation::~QuickAnimation (void) |
---|
[3543] | 45 | { |
---|
[4415] | 46 | this->current = this->first; |
---|
| 47 | QuickKeyFrame delKF; |
---|
[4597] | 48 | |
---|
[4415] | 49 | while (this->current != NULL) |
---|
| 50 | { |
---|
| 51 | this->first = this->current->next; |
---|
| 52 | delete this->current; |
---|
| 53 | this->current = this->first; |
---|
| 54 | } |
---|
[3543] | 55 | } |
---|
[4415] | 56 | |
---|
[4421] | 57 | /** |
---|
| 58 | \brief adds a new entry to the list of keyframes |
---|
| 59 | \param position the position to add the key to |
---|
| 60 | \param value the Value to set for the position |
---|
| 61 | \returns false if the key existed already for a given position |
---|
| 62 | */ |
---|
| 63 | bool QuickAnimation::addEntry(float position, float value) |
---|
[4415] | 64 | { |
---|
| 65 | this->current = this->first; |
---|
[4421] | 66 | while (this->current != NULL) |
---|
[4415] | 67 | { |
---|
[4421] | 68 | // if it is between some keyframes |
---|
| 69 | if ((!this->current->next && this->current->position < position) |
---|
[4597] | 70 | || (this->current->position < position && this->current->next->position > position)) |
---|
| 71 | break; |
---|
[4421] | 72 | // if it is the same as an already existing keyframe |
---|
| 73 | else if (this->current->position == position) |
---|
[4597] | 74 | return false; |
---|
[4421] | 75 | this->current = this->current->next; |
---|
| 76 | } |
---|
[4415] | 77 | |
---|
[4421] | 78 | QuickKeyFrame* newKey = new QuickKeyFrame; |
---|
| 79 | if (this->first == NULL) |
---|
| 80 | { |
---|
| 81 | this->first = newKey; |
---|
| 82 | this->current = newKey; |
---|
| 83 | newKey->next = NULL; |
---|
[4415] | 84 | } |
---|
[4421] | 85 | else |
---|
[4415] | 86 | { |
---|
[4421] | 87 | newKey->next = this->current->next; |
---|
| 88 | this->current->next = newKey; |
---|
[4415] | 89 | } |
---|
[4421] | 90 | newKey->value = value; |
---|
| 91 | newKey->position = position; |
---|
[4597] | 92 | |
---|
[4421] | 93 | this->current = this->first; |
---|
[4425] | 94 | |
---|
| 95 | return true; |
---|
[4415] | 96 | } |
---|
| 97 | |
---|
[4421] | 98 | /** |
---|
[4425] | 99 | \brief changes an entry in the region of position |
---|
| 100 | \param position the Position of an existing keyframe |
---|
| 101 | \param region a deviation of the existing keyframe (like a delta in witch to search for |
---|
| 102 | \param value the new Value |
---|
| 103 | */ |
---|
| 104 | bool QuickAnimation::changeEntry(float position, float value, float region) |
---|
| 105 | { |
---|
| 106 | this->current = this->first; |
---|
| 107 | while (this->current) |
---|
| 108 | { |
---|
| 109 | if (this->current->position < position+region && this->current->position > position-region) |
---|
[4597] | 110 | { |
---|
| 111 | this->current->value = value; |
---|
| 112 | return true; |
---|
| 113 | } |
---|
| 114 | this->current = this->current->next; |
---|
[4425] | 115 | } |
---|
| 116 | this->current = this->first; |
---|
| 117 | return false; |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | /* |
---|
| 121 | \param position The position where to find the Node to kill |
---|
[4597] | 122 | |
---|
[4425] | 123 | bool QuickAnimation::removeEntry(float position) |
---|
| 124 | { |
---|
| 125 | this->current = this->first; |
---|
[4597] | 126 | QuickKeyFrame* last = |
---|
| 127 | |
---|
[4425] | 128 | while (this->current) |
---|
| 129 | { |
---|
| 130 | if (this->current->position == position) |
---|
| 131 | { |
---|
[4597] | 132 | |
---|
| 133 | |
---|
[4425] | 134 | } |
---|
[4597] | 135 | this->current = this->current->next; |
---|
[4425] | 136 | } |
---|
| 137 | this->current = this->first; |
---|
| 138 | } |
---|
| 139 | */ |
---|
| 140 | |
---|
| 141 | /** |
---|
[4421] | 142 | \brief returns the value of the animation at a certain position |
---|
| 143 | \param position the position to get the value from :) |
---|
| 144 | */ |
---|
[4597] | 145 | float QuickAnimation::getValue(float position) |
---|
[4415] | 146 | { |
---|
[4421] | 147 | if (unlikely(this->first == NULL)) |
---|
| 148 | return 0.0; |
---|
| 149 | else if (unlikely (this->first->next == NULL)) |
---|
| 150 | return this->first->value; |
---|
| 151 | else |
---|
[4415] | 152 | { |
---|
[4421] | 153 | if (unlikely(position < this->current->position)) |
---|
[4597] | 154 | { |
---|
| 155 | if (position <= this->first->position) |
---|
| 156 | return this->first->value; |
---|
| 157 | this->current = this->first; |
---|
| 158 | } |
---|
[4421] | 159 | while (likely(this->current->next != NULL && position > this->current->next->position)) |
---|
[4597] | 160 | this->current = this->current->next; |
---|
[4421] | 161 | if (this->current->next == NULL) |
---|
[4597] | 162 | return this->current->value; |
---|
| 163 | |
---|
[4421] | 164 | return this->current->value + (this->current->next->value - this->current->value) |
---|
[4597] | 165 | * ((position-this->current->position) / (this->current->next->position -this->current->position)); |
---|
[4415] | 166 | } |
---|
| 167 | } |
---|
| 168 | |
---|
[4479] | 169 | /** |
---|
| 170 | \brief outputs some nice information about this class |
---|
| 171 | */ |
---|
[4421] | 172 | void QuickAnimation::debug(void) |
---|
| 173 | { |
---|
| 174 | this->current = this->first; |
---|
| 175 | |
---|
| 176 | PRINT(0)("QuickAnim:: (position, value)"); |
---|
| 177 | while(this->current) |
---|
| 178 | { |
---|
| 179 | PRINT(0)("->(%f, %f)", this->current->position, this->current->value); |
---|
| 180 | this->current = this->current->next; |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | PRINT(0)("\n"); |
---|
| 184 | this->current = this->first; |
---|
| 185 | } |
---|