-
Notifications
You must be signed in to change notification settings - Fork 0
/
sprite.cpp
130 lines (101 loc) · 3 KB
/
sprite.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
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
#include "sprite.h"
namespace SSJ {
vector <Sprite *> Sprite::allSprites;
Sprite::Sprite()
{
this->defaultTexture = "null";
this->sprite = new sf::Sprite;
this->alphaMaksSet = false;
this->smooth = false;
activeAnimation = "*";
this->activeTexture = "*";
Sprite::allSprites.push_back(this);
}
void Sprite::Update(){
if(activeAnimation != "*"){
this->sprite->setTexture(*(this->animations.at(this->activeAnimation)->getCurrentFrame()));
}else if(this->activeTexture != "*"){
this->sprite->setTexture(*(this->textures.at(this->activeTexture)));
}
}
sf::Sprite* Sprite::getSprite()
{
return sprite;
}
void Sprite::setSprite(sf::Sprite sprite)
{
this->sprite = new sf::Sprite(sprite);
}
bool Sprite::getSmooth() const
{
return smooth;
}
void Sprite::setSmooth(bool value)
{
smooth = value;
}
sf::Color Sprite::getAlphaMaks() const
{
return alphaMaks;
}
void Sprite::setAlphaMaks(const sf::Color &value)
{
this->alphaMaksSet = true;
this->alphaMaks = value;
}
void Sprite::updateAllSprites()
{
/*for(int i = 0 ; i < Sprite::allSprites.size(); i++)
Sprite::allSprites.at(i)->Update();*/
}
void Sprite::AddTexture(string key, string path){
sf::Texture * tempTexture = new sf::Texture;
sf::Image img;
if(!img.loadFromFile(path)){
cout << "Failed load texture" + path << endl;
}
if(this->alphaMaksSet)
img.createMaskFromColor(this->getAlphaMaks(), 0);
tempTexture->loadFromImage(img);
tempTexture->setSmooth(this->smooth);
this->textures[key] = tempTexture;
if(this->textures.size() == 1){
this->sprite->setTexture(*tempTexture);
this->activeTexture = key;
this->defaultTexture = key;
this->AnyTextureActive = true;
}
}
void Sprite::AddTexture(string key, sf::Texture* texture, sf::IntRect clip){
this->textures[key] = texture;
if(this->textures.size() == 1){
this->sprite->setTexture(*texture);
this->sprite->setTextureRect(clip);
this->activeTexture = key;
this->defaultTexture = key;
this->AnyTextureActive = true;
}
}
void Sprite::AddAnimation(Animation * animation)
{
this->animations[animation->getAnimationName()] = animation;
}
void Sprite::ActiveAnimation(string name)
{
activeAnimation = name;
}
void Sprite::setTexture(string name)
{
this->AnyTextureActive = true;
this->activeTexture = name;
this->sprite->setTexture(*(this->textures.at(name)));
}
bool Sprite::isAnyTextureActive()
{
return this->AnyTextureActive;
}
Animation* Sprite::getAnimation(string name)
{
return this -> animations.at(name);
}
}