-
Notifications
You must be signed in to change notification settings - Fork 0
/
AnimatedSprite.cpp
69 lines (53 loc) · 1.55 KB
/
AnimatedSprite.cpp
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
//
// AnimatedSprite.cpp
// SpaceGame
//
// Created by Jonathan Wood on 08/09/2014.
// Copyright (c) 2014 Jonathan Wood. All rights reserved.
//
#include "AnimatedSprite.h"
double AnimatedSprite::dt=(1.0/60.0);
void AnimatedSprite::addFrame(sf::IntRect rect){
_frames.push_back(rect);
}
void AnimatedSprite::update(sf::RenderWindow& wind){
_counter+=dt;
if(_counter>=_frameSwitch)
{
if(_activeFrame+1<_frames.size())
_activeFrame+=1;
else
_activeFrame=0;
_counter=0;
}
_sprite.setTextureRect(_frames[_activeFrame]);
_sprite.setOrigin(_sprite.getTextureRect().width/2, _sprite.getTextureRect().height/2);
wind.draw(_sprite);
}
void AnimatedSprite::setPosition(sf::Vector2f pos){
_sprite.setPosition(pos);}
void AnimatedSprite::setPosition(float x, float y){
_sprite.setPosition(x, y);
}
AnimatedSprite::AnimatedSprite(sf::Texture& sheet, double fs){
_counter=0;
_activeFrame=0;
_frameSwitch=fs;
_sprite.setTexture(sheet);
}
AnimatedSprite::AnimatedSprite(unsigned int frameNumX, unsigned int frameNumY, sf::Texture& sheet, double fs){
_counter=0;
_activeFrame=0;
_frameSwitch=fs;
_sprite.setTexture(sheet);
int frameWidth=sheet.getSize().x/frameNumX;
int frameHeight=sheet.getSize().y/frameNumY;
for(int j=0 ; j<frameNumX ; j++)
{
for(int i=0 ; i<frameNumY ; i++)
{
addFrame(sf::IntRect(i*frameWidth, j*frameHeight, frameWidth, frameHeight));
}
}
}
AnimatedSprite::AnimatedSprite(){};