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