1 /******************************************************************************
2  *  Warmux is a convivial mass murder game.
3  *  Copyright (C) 2001-2011 Warmux Team.
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
18  ******************************************************************************
19  * Sprite animation control.
20  ******************************************************************************
21  * 2005/09/21: Jean-Christophe Duberga (jcduberga@gmx.de)
22  *             Initial version
23  *****************************************************************************/
24 
25 #include "game/game_time.h"
26 #include "graphic/spriteanimation.h"
27 #include "graphic/sprite.h"
28 #include "network/randomsync.h"
29 
SpriteAnimation(Sprite & p_sprite)30 SpriteAnimation::SpriteAnimation(Sprite & p_sprite) :
31   sprite(p_sprite),
32   last_update(GameTime::GetInstance()->Read()),
33   speed_factor(1<<SPEED_BITS),
34   frame_delta(1),
35   loop_wait(0),
36   loop_wait_random(0),
37   finished(false),
38   show_on_finish(show_last_frame), //(enum)
39   loop(true),
40   pingpong(false)
41 {
42 }
43 
SpriteAnimation(const SpriteAnimation & other,Sprite & p_sprite)44 SpriteAnimation::SpriteAnimation(const SpriteAnimation & other, Sprite & p_sprite) :
45   sprite(p_sprite),
46   last_update(other.last_update),
47   speed_factor(other.speed_factor),
48   frame_delta(other.frame_delta),
49   loop_wait(other.loop_wait),
50   loop_wait_random(other.loop_wait_random),
51   finished(other.finished),
52   show_on_finish(other.show_on_finish), //(enum)
53   loop(other.loop),
54   pingpong(other.pingpong)
55 {
56 }
57 
Start()58 void SpriteAnimation::Start()
59 {
60    finished = false;
61    last_update = GameTime::GetInstance()->Read();
62 }
63 
Update()64 void SpriteAnimation::Update()
65 {
66   if (finished) {
67     return;
68   }
69 
70   if (GameTime::GetInstance()->Read() < last_update + sprite.GetCurrentDelay()) {
71     return;
72   }
73 
74   uint current_frame = sprite.GetCurrentFrame();
75   uint frame_count = sprite.GetFrameCount();
76 
77   //Delta to next frame used to enable frameskip
78   //if delay between 2 frame is < fps
79   int delta_to_next_f = (((GameTime::GetInstance()->Read() - last_update) / sprite.GetCurrentDelay()) * speed_factor)>>SPEED_BITS;
80   last_update += ((delta_to_next_f<<SPEED_BITS) * sprite.GetCurrentDelay()) / speed_factor;
81 
82   //Animation is finished, when last frame have been fully played
83   bool finish;
84 
85   int  delta = frame_delta * delta_to_next_f;
86   if (frame_delta < 0) {
87     finish = (int(current_frame) + delta <= -1);
88   } else {
89     finish = frame_count <= current_frame + delta;
90   }
91 
92   if (finish && !loop && (!pingpong || frame_delta < 0)) {
93      Finish();
94   } else {
95     uint next_frame = (current_frame + delta) % frame_count;
96 
97     if (pingpong) {
98       if (frame_delta>0 && current_frame + delta >= frame_count) {
99         next_frame = frame_count - next_frame -2;
100         frame_delta = - frame_delta;
101       } else if (frame_delta < 0 && int(current_frame) + delta <= -1) {
102         next_frame = (delta-(int)current_frame) % frame_count;
103         frame_delta = - frame_delta;
104         CalculateWait();
105       }
106     }
107 
108     if (next_frame != current_frame) {
109       if (next_frame >= frame_count) {
110         next_frame = 0;
111         CalculateWait();
112       }
113       sprite.SetCurrentFrame(next_frame);
114     }
115   }
116 }
117 
CalculateWait()118 void SpriteAnimation::CalculateWait()
119 {
120   MSG_DEBUG("eye", "CalculateWait stat   :  wait = %d , random = %d", loop_wait, loop_wait_random);
121   MSG_DEBUG("eye", "CalculateWait 1 : %d", last_update);
122 
123   if (loop_wait != 0) {
124     last_update += loop_wait;
125     if (loop_wait_random != 0) {
126       MSG_DEBUG("random.get","SpriteAnimation::CalculateWait()");
127       last_update +=  RandomSync().GetInt(0, loop_wait_random) - loop_wait_random/2;
128     }
129   }
130   MSG_DEBUG("eye", "CalculateWait 2 : %d", last_update);
131 }
132