-
Notifications
You must be signed in to change notification settings - Fork 5
/
sprite.cpp
107 lines (92 loc) · 2.15 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
#include "sprite.h"
#include <glm/gtc/matrix_transform.hpp>
#include <cmath>
Sprite::Sprite(GLuint texture, int tW, int tH)
: dirty(true), texture(texture), scale(1.0f, 1.0f), angle(0.0f), position(0.0f, 0.0f, 0.0f),
r(255), g(255), b(255), a(255), textureWidth(tW), textureHeight(tH)
{
}
Sprite::~Sprite()
{
}
void Sprite::setPosition(float x, float y, float z)
{
position.x = x;
position.y = y;
position.z = z;
dirty = true;
}
void Sprite::setScale(float x, float y)
{
scale.x = x;
scale.y = y;
dirty = true;
}
void Sprite::setAngle(float a)
{
angle = a;
dirty = true;
}
void Sprite::setTextureRectangle(int x_, int y_, int w_, int h_)
{
x = x_;
y = y_;
w = w_;
h = h_;
}
void Sprite::setColor(unsigned char r_, unsigned char g_, unsigned char b_, unsigned char a_)
{
r = r_;
g = g_;
b = b_;
a = a_;
}
GLuint Sprite::getTexture() const
{
return texture;
}
const glm::mat4& Sprite::getModelMatrix()
{
if(dirty)
{
// Scale first, then rotate, then translate
model = glm::mat4();
// Translate last
model = glm::translate(model, position);
// Rotate second (around the origin)
model = glm::translate(model, glm::vec3(0.5f * scale.x, 0.5f * scale.y, 0.0f));
model = glm::rotate(model, glm::radians(angle), glm::vec3(0.0f, 0.0f, 1.0f));
model = glm::translate(model, glm::vec3(-0.5f * scale.x, -0.5f * scale.y, 0.0f));
// Scale first
model = glm::scale(model, glm::vec3(scale.x, scale.y, 1.0f));
dirty = false;
}
return model;
}
void Sprite::getTextureRectangle(int* x_, int* y_, int* w_, int* h_) const
{
*x_ = x;
*y_ = y;
*w_ = w;
*h_ = h;
}
bool Sprite::compare(const Sprite& other) const
{
if(texture == other.texture)
{
return position.z < other.position.z;
}
return texture < other.texture;
}
void Sprite::getColor(unsigned char* r_, unsigned char* g_, unsigned char* b_, unsigned char* a_) const
{
*r_ = r;
*g_ = g;
*b_ = b;
*a_ = a;
}
void Sprite::getTextureDimensions(int* tW, int* tH)
{
*tW = textureWidth;
*tH = textureHeight;
}