-
Notifications
You must be signed in to change notification settings - Fork 2
/
.merge_file_a10224
120 lines (99 loc) · 2.58 KB
/
.merge_file_a10224
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
#include "Sprite.h"
unordered_map<string, SDL_Texture*> Sprite::assetTable;
Sprite::Sprite() {
texture = NULL;
scaleX = 1;
scaleY = 1;
frameCount = 1;
frameHeight = 1;
frameTime = 1;
currentFrame = 0;
currentHeight = 0;
}
Sprite::Sprite(string file, int frameCount, float frameTime, float frameHeight) : frameCount(frameCount), frameTime(frameTime), frameHeight(frameHeight) {
texture = NULL;
scaleX = 1;
scaleY = 1;
currentFrame = 0;
currentHeight = 0;
Open(file);
}
void Sprite::Update(float dt) {
timeElapsed += dt;
if (frameCount > 1){
if (timeElapsed >= frameTime){
if (currentFrame >= frameCount)
currentFrame = 0;
SetClip(GetWidth()*currentFrame, clipRect.h*currentHeight, clipRect.w, clipRect.h);
currentFrame++;
timeElapsed = 0;
}
}
if (frameCount == 1)
SetClip(0, 0, clipRect.w, clipRect.h);
}
void Sprite::SetFrame(int frame) {
float clipSet = 0;
clipSet = (frame-1)*GetWidth();
SetClip(clipSet, 0, dimensions.w / frameCount, dimensions.h);
}
void Sprite::SetFrameTime(float frameTime) {
this->frameTime = frameTime;
}
void Sprite::SetFrameCount(int frameCount) {
this->frameCount = frameCount;
}
int Sprite::GetTotalWidth() const {
return dimensions.w*scaleX;
}
Sprite :: ~Sprite() {}
void Sprite::Open(string file) {
if (assetTable.find(file) != assetTable.end()){
}
if (assetTable.find(file) == assetTable.end()){
texture = (SDL_Texture*)IMG_LoadTexture(Game::GetInstance().GetRenderer(), file.c_str());
assetTable.emplace(file, texture);
}
texture = assetTable.at(file);
SDL_QueryTexture(texture, NULL, NULL, &dimensions.w, &dimensions.h);
SetClip(0, 0, dimensions.w/frameCount, dimensions.h/frameHeight);
}
void Sprite::Clear() {
for (auto element = assetTable.begin(); element != assetTable.end(); element++) {
SDL_DestroyTexture(element->second);
}
}
void Sprite::SetClip(int x, int y, int w, int h) {
clipRect.x = x;
clipRect.y = y;
clipRect.w = w;
clipRect.h = h;
}
void Sprite::Render(int x, int y, float angle){
SDL_Rect dstrec;
dstrec = dimensions;
dstrec.x = x;
dstrec.y = y;
dstrec.w = clipRect.w*scaleX;
dstrec.h = clipRect.h*scaleY;
SDL_RenderCopyEx(Game::GetInstance().GetRenderer(), texture, &clipRect, &dstrec,angle,NULL,SDL_FLIP_NONE);
Update(Game::GetInstance().GetDeltaTime());
}
int Sprite::GetHeight() const {
return dimensions.h*scaleY/frameHeight;
}
int Sprite::GetWidth() const {
return dimensions.w*scaleX/frameCount;
}
void Sprite::SetScaleX(float scale) {
scaleX = scale;
}
void Sprite::SetScaleY(float scale) {
scaleY = scale;
}
bool Sprite::IsOpen() {
if (texture)
return true;
else
return false;
}