Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/simple_animation.cc @ 3727

Last change on this file since 3727 was 3727, checked in by patrick, 21 years ago

orxonox/trunk: added a third debug level, made SimpleAnimation singleton and restructured it to be more opengl command style

File size: 6.8 KB
Line 
1
2
3/*
4   orxonox - the future of 3D-vertical-scrollers
5
6   Copyright (C) 2004 orx
7
8   This program is free software; you can redistribute it and/or modify
9   it under the terms of the GNU General Public License as published by
10   the Free Software Foundation; either version 2, or (at your option)
11   any later version.
12
13   ### File Specific:
14   main-programmer: Patrick Boenzli
15   co-programmer: ...
16*/
17
18
19#include "simple_animation.h"
20#include "stdincl.h"
21#include "p_node.h"
22#include "vector.h"
23
24using namespace std;
25
26
27
28SimpleAnimation* SimpleAnimation::singletonRef = 0;
29/**
30   \brief gets the singleton instance
31   \returns singleton instance
32*/
33SimpleAnimation* SimpleAnimation::getInstance()
34{
35  if( singletonRef == NULL)
36    singletonRef = new SimpleAnimation();
37  return singletonRef;
38}
39
40/**
41   \brief standard constructor
42*/
43SimpleAnimation::SimpleAnimation () 
44{
45   this->setClassName ("SimpleAnimation");
46   this->frames = new tList<KeyFrame>();
47   this->localTime = 0;
48   this->bRunning = false;
49   this->currentFrame = NULL;
50   this->lastFrame = NULL;
51
52   this->tmpVect = new Vector();
53}
54
55
56/**
57   \brief standard deconstructor
58
59*/
60SimpleAnimation::~SimpleAnimation () 
61{
62  tIterator<KeyFrame>* iterator = this->frames->getIterator();
63  KeyFrame* frame = iterator->nextElement(); 
64  while( frame != NULL) 
65    { 
66      delete frame;
67      frame = iterator->nextElement();
68    }
69  delete iterator;
70  delete this->frames;
71}
72
73
74/**
75   \brief this determines the start of an Animator Describtion
76
77   this can then be followed by different commands like addKeyFrame(..) etc. and
78   will be closed with AnimatiorEnd()
79*/
80void SimpleAnimation::AnimatorBegin()
81{
82  this->bDescriptive = true;
83}
84
85
86/**
87   \brief this determines the end of an Animator Describtion
88
89   this can then be followed by different commands like addKeyFrame(..) etc. and
90   will be closed with AnimatiorEnd()
91*/
92void SimpleAnimation::AnimatorEnd()
93{
94  this->workingObject = NULL;
95  this->bDescriptive = false;
96}
97
98
99/**
100   \brief select an object to work on by using this function
101   \param object wo work on
102*/
103void SimpleAnimation::selectObject(WorldEntity* entity)
104{
105  this->workingObject = entity;
106}
107
108
109
110/**
111   \brief adds a keyframe with properties
112   \param the point of the object
113   \param and the orientation of it
114   \param at this time
115*/
116void SimpleAnimation::addKeyFrame(Vector* point, Quaternion* orientation, float time)
117{
118  if( !this->bDescriptive)
119    {
120      PRINTF(1)("SimpleAnimation: executing animation code outside a AnimationBegin()/AnimationEnd() - ignoring\n");
121      return;
122    }
123  KeyFrame* frame = new KeyFrame;
124  frame->position = point;
125  frame->orientation = orientation;
126  frame->time = time;
127  frame->mode = DEFAULT_ANIMATION_MODE;
128  frame->object = this->workingObject;
129  this->frames->add(frame);
130}
131
132
133/**
134   \brief adds a keyframe with properties
135   \param the point of the object
136   \param and the orientation of it
137   \param at this time
138   \param function of the velocity of the movement
139*/
140void SimpleAnimation::addKeyFrame(Vector* point, Quaternion* orientation, float time, movementMode mode)
141{
142  if( !this->bDescriptive)
143    {
144      PRINTF(1)("SimpleAnimation: executing animation code outside a AnimationBegin()/AnimationEnd() - ignoring\n");
145      return;
146    }
147  KeyFrame* frame = new KeyFrame;
148  frame->position = point;
149  frame->orientation = orientation;
150  frame->time = time;
151  frame->mode = mode;
152  frame->object = this->workingObject;
153  this->frames->add(frame);
154}
155
156/**
157   \brief adds a already defined keyframe
158   \param the keyframe to add
159*/
160void SimpleAnimation::addKeyFrame(KeyFrame* frame)
161{
162  if( !this->bDescriptive)
163    {
164      PRINTF(1)("SimpleAnimation: executing animation code outside a AnimationBegin()/AnimationEnd() - ignoring\n");
165      return;
166    }
167  frame->object = this->workingObject;
168  this->frames->add(frame);
169}
170
171
172/**
173   \brief clear the list of keyframes, deleting all keyframes included
174*/
175void SimpleAnimation::reset()
176{
177  tIterator<KeyFrame>* iterator = this->frames->getIterator();
178  KeyFrame* frame = iterator->nextElement(); 
179  while( frame != NULL) 
180    { 
181      delete frame;
182      frame = iterator->nextElement();
183    }
184  delete iterator;
185  delete this->frames;
186
187  this->frames = new tList<KeyFrame>();
188  this->localTime = 0;
189  this->bRunning = false;
190
191  this->currentFrame = NULL;
192  this->lastFrame = NULL;
193}
194
195/**
196   \brief starts the animation, therefore listens to tick signals
197*/
198void SimpleAnimation::start()
199{
200  if( this->bRunning)
201    {
202      PRINTF(2)("SimpleAnimatin is already running. You are trying to start it again.\n");
203    return;
204    }
205 
206  this->localTime = 0;
207  this->lastFrame = this->frames->firstElement();
208  this->currentFrame = this->frames->nextElement(this->currentFrame);
209  this->bRunning = true;
210}
211
212
213/**
214   \brief stops the animation, immune to tick signals
215*/
216void SimpleAnimation::stop()
217{
218  this->bRunning = false;
219}
220
221/**
222   \brief stops and then starts the animation from begining
223*/
224void SimpleAnimation::restart()
225{
226  this->localTime = 0;
227  this->lastFrame = this->frames->firstElement();
228  this->currentFrame = this->frames->nextElement(this->currentFrame);
229  this->bRunning = true;
230}
231
232/**
233   \brief pauses the animation until resumed
234*/
235void SimpleAnimation::pause()
236{
237  this->bRunning = false;
238}
239
240/**
241   \brief resumes a pause, if not paused, no effect
242*/
243void SimpleAnimation::resume()
244{
245  this->bRunning = true;
246}
247
248
249/**
250   \brief heart beat, next animation step
251*/
252void SimpleAnimation::tick(float time)
253{
254  if( !this->bRunning)
255    return;
256
257  this->localTime += time;
258  /* first get the current frame via time-stamps */ 
259  while( this->localTime > this->currentFrame->time)
260    {
261      printf("SimpleAnimation::tick(...) - changing Frame");
262      this->lastFrame = this->currentFrame;
263      this->currentFrame = this->frames->nextElement(this->currentFrame);
264      this->localTime -= this->currentFrame->time;
265    }
266 
267 
268  /* now animate it */
269  switch( this->mode)
270    {
271    case LINEAR:
272
273      *this->tmpVect = *this->currentFrame->position - *this->lastFrame->position;
274      *this->tmpVect = *this->tmpVect * this->localTime / this->currentFrame->time;
275      //this->setAbsCoordinate(this->tmpVect);
276      break;
277    case EXP:
278     
279      break;
280    case NEG_EXP:
281      *this->tmpVect = *this->currentFrame->position - *this->lastFrame->position;
282      *this->tmpVect = *this->tmpVect * (1 - exp(- this->localTime / this->currentFrame->time));     
283      break;
284    case SIN:
285      *this->tmpVect = *this->currentFrame->position - *this->lastFrame->position;
286      *this->tmpVect = *this->tmpVect * (1 - cos(- this->localTime / this->currentFrame->time));     
287      break;
288    case COS:
289     
290      break;
291    case QUADRATIC:
292      *this->tmpVect = *this->currentFrame->position - *this->lastFrame->position;
293      *this->tmpVect = *this->tmpVect * 1/3 * ldexpf(this->localTime, 3);
294      break;
295    default:
296      break;
297    }
298}
Note: See TracBrowser for help on using the repository browser.