-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEffect.cpp
More file actions
193 lines (167 loc) · 5.94 KB
/
Copy pathEffect.cpp
File metadata and controls
193 lines (167 loc) · 5.94 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include "Effect.hpp"
Effect::Effect() {
activated = true;
automatic = true;
timeCounter = 0;
actualAnimation = 0;
timeBetweenAnimations = 1;
}
Effect::~Effect() { }
int Effect::loadAnimation(std::string name, int qtty, std::string ext) {
animation.clear();
int errorPos = -1;
for(int i = 0; i < qtty; ++i){
std::stringstream s;
s << name << i;
std::string path = s.str();
if(!loadFrame(path+"."+ext)) return errorPos = i;
}
return errorPos;//return -1;
}
bool Effect::loadAnimation(std::vector<sf::Texture>& vector) {
animation.clear();
animation = vector;
return true;
}
bool Effect::loadHorizontalSpriteSheet(std::string name, float qtty){
sf::Image spriteSheetImage;
if(! spriteSheetImage.loadFromFile(name)) return false;
else {
float height = spriteSheetImage.getSize().y;
float width = spriteSheetImage.getSize().x/qtty;
sf::Texture auxText;
for(int i = 0; i < qtty; ++i){
if(i*width > spriteSheetImage.getSize().x) return false;
auxText.loadFromImage(spriteSheetImage, sf::IntRect(i*width,0,width, height));
animation.push_back(auxText);
}
}
return true;
}
bool Effect::loadHorizontalSpriteSheet(sf::Image image, float qtty){
float height = image.getSize().y;
float width = image.getSize().x/qtty;
sf::Texture auxText;
for(int i = 0; i < qtty; ++i){
if(i*width > image.getSize().x) return false;
auxText.loadFromImage(image, sf::IntRect(i*width,0,width, height));
animation.push_back(auxText);
}
return true;
}
bool Effect::loadVerticalSpriteSheet(std::string name,float qtty){
sf::Image spriteSheetImage;
if(! spriteSheetImage.loadFromFile(name)) return false;
else {
float width = spriteSheetImage.getSize().x;
float height = spriteSheetImage.getSize().y/qtty;
sf::Texture auxText;
for(int i = 0; i < qtty; ++i){
if(i*height > spriteSheetImage.getSize().y) return false;
auxText.loadFromImage(spriteSheetImage, sf::IntRect(0,i*height,width, height));
animation.push_back(auxText);
}
}
return true;
}
bool Effect::loadVerticalSpriteSheet(sf::Image image,float qtty){
float width = image.getSize().x;
float height = image.getSize().y/qtty;
sf::Texture auxText;
for(int i = 0; i < qtty; ++i){
if(i*height > image.getSize().y) return false;
auxText.loadFromImage(image, sf::IntRect(0,i*height,width, height));
animation.push_back(auxText);
}
return true;
}
bool Effect::loadSpriteSheet(std::string name, float rows, float columns, int lastRowQtty){
int lastRow = lastRowQtty;
if(lastRowQtty < 0) lastRow = columns;
sf::Image spriteSheetImage;
if(! spriteSheetImage.loadFromFile(name)) return false;
else {
float width = spriteSheetImage.getSize().x/columns;
float height = spriteSheetImage.getSize().y/rows;
sf::Texture auxText;
int qttyColumns = columns;
for(int k = 0; k < rows; ++k){
if(k*height > spriteSheetImage.getSize().y) return false;
if(k == rows -1) qttyColumns = lastRow;
for(int i = 0; i < qttyColumns; ++i){
if(i*width > spriteSheetImage.getSize().x) return false;
auxText.loadFromImage(spriteSheetImage, sf::IntRect(i*width,k*height,width, height));
animation.push_back(auxText);
}
}
}
return true;
}
bool Effect::loadSpriteSheet(sf::Image image, float rows, float columns, int lastRowQtty){
int lastRow = lastRowQtty;
if(lastRowQtty < 0) lastRow = columns;
float width = image.getSize().x/columns;
float height = image.getSize().y/rows;
sf::Texture auxText;
int qttyColumns = columns;
for(int k = 0; k < rows; ++k){
if(k*height > image.getSize().y) return false;
if(k == rows -1) qttyColumns = lastRow;
for(int i = 0; i < qttyColumns; ++i){
if(i*width > image.getSize().x) return false;
auxText.loadFromImage(image, sf::IntRect(i*width,k*height,width, height));
animation.push_back(auxText);
}
}
return true;
}
bool Effect::loadFrame(std::string name, int position) {
if(position >= animation.size()) return false;
return animation[position].loadFromFile(name);
}
bool Effect::loadFrame(std::string name) {
sf::Texture text;
if(! text.loadFromFile(name)) return false;
else animation.push_back(text);
return true;
}
bool Effect::loadFrame(sf::Texture & text, int position) {
if(position >= animation.size()) return false;
animation[position] = sf::Texture(text);
return true;
}
bool Effect::loadFrame(sf::Texture & text) {
animation.push_back(text);
return true;
}
void Effect::updateAnimation(float deltatime) {
timeCounter += deltatime;
if(timeCounter > timeBetweenAnimations){
timeCounter = 0;
if(automatic){
inc_actualAnim();
this->setTexture(animation[actualAnimation], false);
}
}
}
void Effect::drawEffect(sf::RenderTarget& target) {
if(activated){
target.draw((*this));
}
}
void Effect::setActualAnimation(int newPosition) {
actualAnimation = newPosition%animation.size();
}
void Effect::inc_actualAnim() {
actualAnimation = (actualAnimation+1)%animation.size();
}
void Effect::dec_actualAnim() {
actualAnimation = (actualAnimation-1)%animation.size();
}
void Effect::active() { activated = true; }
void Effect::inactive() { activated = false; }
void Effect::enableAutomaticAnimation() { automatic = true; }
void Effect::disableAutomaticAnimation() { automatic = false; }
int Effect::getActualAnimation() { return actualAnimation; }
float Effect::getTimeBetweenAnimations() { return timeBetweenAnimations; }
void Effect::setTimeBetweenAnimations(float time) { timeBetweenAnimations = time; }