File: spriteanimation.cpp

package info (click to toggle)
warmux 1%3A11.04.1%2Brepack2-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, sid
  • size: 126,388 kB
  • sloc: cpp: 186,040; xml: 8,909; sh: 3,358; makefile: 1,052; ansic: 713
file content (131 lines) | stat: -rw-r--r-- 4,276 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/******************************************************************************
 *  Warmux is a convivial mass murder game.
 *  Copyright (C) 2001-2011 Warmux Team.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 ******************************************************************************
 * Sprite animation control.
 ******************************************************************************
 * 2005/09/21: Jean-Christophe Duberga (jcduberga@gmx.de)
 *             Initial version
 *****************************************************************************/

#include "game/game_time.h"
#include "graphic/spriteanimation.h"
#include "graphic/sprite.h"
#include "network/randomsync.h"

SpriteAnimation::SpriteAnimation(Sprite & p_sprite) :
  sprite(p_sprite),
  last_update(GameTime::GetInstance()->Read()),
  speed_factor(1<<SPEED_BITS),
  frame_delta(1),
  loop_wait(0),
  loop_wait_random(0),
  finished(false),
  show_on_finish(show_last_frame), //(enum)
  loop(true),
  pingpong(false)
{
}

SpriteAnimation::SpriteAnimation(const SpriteAnimation & other, Sprite & p_sprite) :
  sprite(p_sprite),
  last_update(other.last_update),
  speed_factor(other.speed_factor),
  frame_delta(other.frame_delta),
  loop_wait(other.loop_wait),
  loop_wait_random(other.loop_wait_random),
  finished(other.finished),
  show_on_finish(other.show_on_finish), //(enum)
  loop(other.loop),
  pingpong(other.pingpong)
{
}

void SpriteAnimation::Start()
{
   finished = false;
   last_update = GameTime::GetInstance()->Read();
}

void SpriteAnimation::Update()
{
  if (finished) { 
    return;
  }

  if (GameTime::GetInstance()->Read() < last_update + sprite.GetCurrentDelay()) {
    return;
  }

  uint current_frame = sprite.GetCurrentFrame();
  uint frame_count = sprite.GetFrameCount();

  //Delta to next frame used to enable frameskip
  //if delay between 2 frame is < fps
  int delta_to_next_f = (((GameTime::GetInstance()->Read() - last_update) / sprite.GetCurrentDelay()) * speed_factor)>>SPEED_BITS;
  last_update += ((delta_to_next_f<<SPEED_BITS) * sprite.GetCurrentDelay()) / speed_factor;

  //Animation is finished, when last frame have been fully played
  bool finish;

  int  delta = frame_delta * delta_to_next_f;
  if (frame_delta < 0) {
    finish = (int(current_frame) + delta <= -1);
  } else {
    finish = frame_count <= current_frame + delta;
  }

  if (finish && !loop && (!pingpong || frame_delta < 0)) {
     Finish();
  } else {
    uint next_frame = (current_frame + delta) % frame_count;

    if (pingpong) {
      if (frame_delta>0 && current_frame + delta >= frame_count) {
        next_frame = frame_count - next_frame -2;
        frame_delta = - frame_delta;
      } else if (frame_delta < 0 && int(current_frame) + delta <= -1) {
        next_frame = (delta-(int)current_frame) % frame_count;
        frame_delta = - frame_delta;
        CalculateWait();
      }
    }

    if (next_frame != current_frame) {
      if (next_frame >= frame_count) {
        next_frame = 0;
        CalculateWait();
      }
      sprite.SetCurrentFrame(next_frame);
    }
  }
}

void SpriteAnimation::CalculateWait()
{
  MSG_DEBUG("eye", "CalculateWait stat   :  wait = %d , random = %d", loop_wait, loop_wait_random);
  MSG_DEBUG("eye", "CalculateWait 1 : %d", last_update);

  if (loop_wait != 0) {
    last_update += loop_wait;
    if (loop_wait_random != 0) {
      MSG_DEBUG("random.get","SpriteAnimation::CalculateWait()");
      last_update +=  RandomSync().GetInt(0, loop_wait_random) - loop_wait_random/2;
    }
  }
  MSG_DEBUG("eye", "CalculateWait 2 : %d", last_update);
}