-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSprite.cpp
188 lines (150 loc) · 4.06 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
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
#include "Sprite.h"
Sprite::Sprite(Vec2 position, string src, double scale)
: Node(position), box({ 0,0,0,0 })// box(Polygon())
{
collider = Polygon2D({ Vec2(0,0),Vec2(0,0) ,Vec2(0,0) ,Vec2(0,0) }, position);
anchor = Vec2(0.5, 0.5);
if (src != "") {
open(src);
}
setScale(scale);
updateFunc = [&](Sprite* self) {};
collidedFunc = [&](Sprite* self, Vec2 dir) {};
};
Sprite::Sprite(Sprite& sp) {
name = sp.name;
img = sp.img;
box = sp.box;
collider = sp.collider;
collider.parent = this;
//cout << (collider.getAllPoints().size()) << endl;
anchor = sp.anchor;
enableCollider = sp.enableCollider;
drawCollider = sp.drawCollider;
position = sp.position;
color = sp.color;
updateFunc = sp.updateFunc;
collidedFunc = sp.collidedFunc;
}
Sprite* Sprite::clone() {
return new Sprite(*this);
}
void Sprite::open(string imgPath) {
defaultImg = Tool::openRescource(imgPath);
img = defaultImg;
if (img == nullptr)
cout << "[Error]Failed to load image:" << imgPath << IMG_GetError() << endl;
SDL_Rect pos;
pos.x = position.x;
pos.y = position.y;
SDL_QueryTexture(img, NULL, NULL, &pos.w, &pos.h); //»ñÈ¡¿í¸ßÊôÐÔ
Vec2 offset(pos.w / 2, pos.h/2);
vector<Vec2> ps;
position = position - offset;
ps.push_back(position + Vec2(0, 0));
ps.push_back(position + Vec2(0 + pos.w, 0));
ps.push_back(position + Vec2(0 + pos.w, 0 + pos.h));
ps.push_back(position + Vec2(0, 0 + pos.h));
box = pos;
collider = Polygon2D(ps, collider.getPosition());
collider.parent = this;
//Vec2 realPos(pos.x - pos.w * anchor.x, pos.y - pos.h * anchor.y);
//setPosition(realPos);
//setPosition(position);
collider.setColor(Color(0, 0, 255, 255));
}
Sprite::~Sprite() {
SDL_DestroyTexture(img);
}
SDL_Texture* Sprite::getTexture() {
return img;
}
void Sprite::setPolygonCollider(Polygon2D& c) {
collider = c;
collider.parent = this;
}
void Sprite::setName(string newName) {
collider.name = newName;
name = newName;
}
void Sprite::setPosition(Vec2 p) {
if (p != position) {
collider.setPosition(p);
position = p;
box.x = p.x;
box.y = p.y;
}
}
void Sprite::setBox(SDL_Rect r) {
box = r;
setPosition(Vec2(r.x, r.y));
}
void Sprite::setAnchor(Vec2 a) {
if (a.x < eps || a.y < eps || a.x > 1 || a.y > 1) {
return;
}
anchor = a;
}
void Sprite::setScale(double k) {
if (k == 1.0) return;
collider.scale(k);
box.w *= k;
box.h *= k;
scale.x = scale.y = k;
}
void Sprite::draw() {
draw(box);
/*Draw::SetColor(0,0,255,255);
SDL_RenderDrawLine(Draw::ren, position.x, position.y + 3, position.x, position.y - 3);
SDL_RenderDrawLine(Draw::ren, position.x + 3, position.y, position.x - 3, position.y);*/
}
void Sprite::draw(SDL_Rect targetRect) {
if (!visable || img == NULL) return;
targetRect.x -= anchor.x * box.w;
targetRect.y -= anchor.y * box.h;
SDL_RendererFlip flip1 = flipX ? SDL_FLIP_HORIZONTAL : SDL_FLIP_NONE;
SDL_RendererFlip flip2 = flipY ? SDL_FLIP_VERTICAL : SDL_FLIP_NONE;
SDL_RenderCopyEx(Draw::ren, img, NULL, &targetRect, rotation, NULL, flip1);
if (drawCollider)
getPolygonCollider()->draw();
}
void Sprite::runAnimation(Animation *anime) {
if (anime == curAnimation) return;
curAnimation = anime;
curAnimation->setCurrentFrame(0);
}
void Sprite::onUpdate(double dt) {
if (curAnimation != NULL && !curAnimation->checkEnd()) {
img = curAnimation->getCurrentClip();
curAnimation->next();
}
if (simulateGravity) {
velocity = velocity.moveToword(Vec2(0, 500), 0, gravity * dt);
}
else {
velocity.y = 0;
}
if (velocity.y) {
move(velocity * dt);
}
updateFunc(this);
}
void Sprite::onCollided(Node* other, Vec2 dir) {
collidedFunc(this, dir);
}
void Sprite::rotateTo(double angle, double speed) {
angle = Tool::Clamp(angle, -180, 180);
double sub = Tool::Clamp(angle - rotation, -180, 180);
if (fabs(sub) <= angle * speed) {
getPolygonCollider()->rotateBy(sub);
rotation = angle;
//cout << rotation << endl;
return;
}
getPolygonCollider()->rotateBy((sub) * speed);
rotation = rotation + (sub) * speed;
}
void Sprite::setRotation(double angle) {
rotation = angle;
getPolygonCollider()->rotateBy(0 - getPolygonCollider()->getRotation());
}