forked from lenLRX/win32gl1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sprite.cpp
210 lines (177 loc) · 3.25 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include "stdafx.h"
#include "sprite.h"
#include <vector>
#include "globalvar.h"
#include "printText.h"
using namespace std;
Sprite::Sprite(string pic, int layer) \
:_layer(layer), rotation(0.0f), scaleX(1.0f), scaleY(1.0f), flipX(false), flipY(false)
{
_anchorPoint.x = 0.5;
_anchorPoint.y = 0.5;
bool HitCache = false;
//看一看我们是否已经储存了同名的图片
for (vector<Texture>::iterator it = Textures.begin();it!=Textures.end();it++)
{
if ((*it).name == pic)
{
HitCache = true; //我们碰到了同名的图片
(*it).RefCount++;
Tex=(*it).Tex; //设置纹理
}
}
//如果没有同名图片,我们就创建一个纹理
if (!HitCache)
{
Texture temp;
temp.name = pic.c_str();
temp.RefCount = 1;
glGenTextures(1,&temp.Tex);
InitTexture(temp.Tex, temp.name.c_str(), PNG);
Tex = temp.Tex;
Textures.push_back(temp);
}
}
Sprite::Sprite(GLuint _Texture, int layer) \
: _layer(layer), rotation(0.0f), scaleX(1.0f), scaleY(1.0f), flipX(false), flipY(false)
{
Tex = _Texture;
}
Sprite::~Sprite()
{
;
}
//获取绘图优先级
int Sprite::getlayer() const
{
return _layer;
}
void Sprite::setlayer(int layer)
{
_layer = layer;
}
void Sprite::setpos(Point pos)
{
_pos = pos;
}
Point Sprite::getpos()
{
return _pos;
}
void Sprite::setpos(float x, float y)
{
_pos.x = x;
_pos.y = y;
}
Point Sprite::getAnchorPoint()
{
return _anchorPoint;
}
void Sprite::setAnchorPoint(Point anchorPoint)
{
_anchorPoint = anchorPoint;
}
void Sprite::setAnchorPoint(float x, float y)
{
_anchorPoint.x = x;
_anchorPoint.y = y;
}
Size Sprite::getsize()
{
return _size;
}
void Sprite::setsize(Size size)
{
_size = size;
}
void Sprite::setsize(float x,float y)
{
_size.width=x;
_size.height = y;
}
void Sprite::setrotation(float rot)
{
rotation = rot;
}
float Sprite::getrotation()
{
return rotation;
}
float Sprite::getscaleX()
{
return scaleX;
}
void Sprite::setscaleX(float X)
{
scaleX = X;
}
float Sprite::getscaleY()
{
return scaleY;
}
void Sprite::setscaleY(float Y)
{
scaleY = Y;
}
void Sprite::setflipX(bool b)
{
flipX = b;
}
bool Sprite::getflipX()
{
return flipX;
}
void Sprite::setflipY(bool b)
{
flipY = b;
}
bool Sprite::getflipY()
{
return flipY;
}
void Sprite::setTex(GLuint t)
{
Tex = t;
}
GLuint Sprite::getTex()
{
return Tex;
}
void Sprite::setid(int id)
{
_id = id;
}
int Sprite::getid()
{
return _id;
}
Point Sprite::convertToLocalSpace(Point pt)
{
Point returnPT;
glLoadIdentity();
glRotatef(90.0f + rotation / M_PI * 180, 0.0f, 0.0f, 1.0f);
float mat[16];
glGetFloatv(GL_MODELVIEW_MATRIX, mat); //利用opengl的投影矩阵来计算
/*
glRasterPos2f(0.65f, 0.8f);
glPrint("\n%f,%f,%f,%f", mat[0], mat[1], mat[2], mat[3] );
glRasterPos2f(0.6f, 0.8f);
glPrint("%f,%f,%f,%f", mat[4], mat[5], mat[6], mat[7]);
glRasterPos2f(0.55f, 0.8f);
glPrint("%f,%f,%f,%f", mat[8], mat[9], mat[10], mat[11]);
glRasterPos2f(0.5f, 0.8f);
glPrint("%f,%f,%f,%f", mat[12], mat[13], mat[14], mat[15]);
*/
returnPT.x = mat[4] * (_pos.x - pt.x) + mat[5] * (_pos.y - pt.y);
returnPT.y = mat[0] * (_pos.x - pt.x) + mat[1] * (_pos.y - pt.y) ;
returnPT.y *= -1;
return returnPT;
}
bool Sprite::mouseEventCallBack(mouseEvent _event)
{
return false;
}
bool Sprite::operator< (const Sprite& cp)
{
return this->_layer < cp.getlayer();
}