[1853] | 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. |
---|
[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 | |
---|
[1856] | 29 | |
---|
[3245] | 30 | /** |
---|
| 31 | \brief standard constructor |
---|
| 32 | */ |
---|
[4421] | 33 | QuickAnimation::QuickAnimation (void) |
---|
[3365] | 34 | { |
---|
[4415] | 35 | this->setClassName("QuickAnimation"); |
---|
[4320] | 36 | |
---|
[4421] | 37 | this->first = this->current = NULL; |
---|
[3365] | 38 | } |
---|
[1853] | 39 | |
---|
| 40 | |
---|
[3245] | 41 | /** |
---|
| 42 | \brief standard deconstructor |
---|
[1853] | 43 | |
---|
[3245] | 44 | */ |
---|
[4421] | 45 | QuickAnimation::~QuickAnimation (void) |
---|
[3543] | 46 | { |
---|
[4415] | 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 | |
---|
[3543] | 57 | } |
---|
[4415] | 58 | |
---|
[4421] | 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) |
---|
[4415] | 66 | { |
---|
| 67 | this->current = this->first; |
---|
[4421] | 68 | while (this->current != NULL) |
---|
[4415] | 69 | { |
---|
[4421] | 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 | } |
---|
[4415] | 79 | |
---|
[4421] | 80 | QuickKeyFrame* newKey = new QuickKeyFrame; |
---|
| 81 | if (this->first == NULL) |
---|
| 82 | { |
---|
| 83 | this->first = newKey; |
---|
| 84 | this->current = newKey; |
---|
| 85 | newKey->next = NULL; |
---|
[4415] | 86 | } |
---|
[4421] | 87 | else |
---|
[4415] | 88 | { |
---|
[4421] | 89 | newKey->next = this->current->next; |
---|
| 90 | this->current->next = newKey; |
---|
[4415] | 91 | } |
---|
[4421] | 92 | newKey->value = value; |
---|
| 93 | newKey->position = position; |
---|
| 94 | |
---|
| 95 | this->current = this->first; |
---|
[4415] | 96 | } |
---|
| 97 | |
---|
[4421] | 98 | /** |
---|
| 99 | \brief returns the value of the animation at a certain position |
---|
| 100 | \param position the position to get the value from :) |
---|
| 101 | */ |
---|
[4415] | 102 | float QuickAnimation::getValue(float position) |
---|
| 103 | { |
---|
[4421] | 104 | if (unlikely(this->first == NULL)) |
---|
| 105 | return 0.0; |
---|
| 106 | else if (unlikely (this->first->next == NULL)) |
---|
| 107 | return this->first->value; |
---|
| 108 | else |
---|
[4415] | 109 | { |
---|
[4421] | 110 | if (unlikely(position < this->current->position)) |
---|
[4415] | 111 | { |
---|
[4421] | 112 | if (position <= this->first->position) |
---|
| 113 | return this->first->value; |
---|
[4415] | 114 | this->current = this->first; |
---|
| 115 | } |
---|
[4421] | 116 | while (likely(this->current->next != NULL && position > this->current->next->position)) |
---|
| 117 | this->current = this->current->next; |
---|
| 118 | if (this->current->next == NULL) |
---|
| 119 | return this->current->value; |
---|
| 120 | |
---|
| 121 | return this->current->value + (this->current->next->value - this->current->value) |
---|
| 122 | * ((position-this->current->position) / (this->current->next->position -this->current->position)); |
---|
[4415] | 123 | } |
---|
| 124 | } |
---|
| 125 | |
---|
[4421] | 126 | |
---|
| 127 | void QuickAnimation::debug(void) |
---|
| 128 | { |
---|
| 129 | this->current = this->first; |
---|
| 130 | |
---|
| 131 | PRINT(0)("QuickAnim:: (position, value)"); |
---|
| 132 | while(this->current) |
---|
| 133 | { |
---|
| 134 | PRINT(0)("->(%f, %f)", this->current->position, this->current->value); |
---|
| 135 | this->current = this->current->next; |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | PRINT(0)("\n"); |
---|
| 139 | this->current = this->first; |
---|
| 140 | } |
---|