-
-
Notifications
You must be signed in to change notification settings - Fork 183
/
Copy pathSprite.h
231 lines (185 loc) · 5.6 KB
/
Sprite.h
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
* Copyright (c) scott.cgi All Rights Reserved.
*
* This source code belongs to project Mojoc, which is a pure C Game Engine hosted on GitHub.
* The Mojoc Game Engine is licensed under the MIT License, and will continue to be iterated with coding passion.
*
* License : https://github.com/scottcgi/Mojoc/blob/master/LICENSE
* GitHub : https://github.com/scottcgi/Mojoc
* CodeStyle: https://github.com/scottcgi/Mojoc/blob/master/Docs/CodeStyle.md
*
* Since : 2013-4-20
* Update : 2019-1-21
* Author : scott.cgi
*/
#ifndef SPRITE_H
#define SPRITE_H
#include "Engine/Graphics/Draw/Drawable.h"
#include "Engine/Graphics/Draw/Quad.h"
#include "Engine/Graphics/OpenGL/Texture.h"
#include "Engine/Toolkit/Utils/Array.h"
enum
{
/**
* Number of buffers.
*/
Sprite_BufferNum = 2,
/**
* Index of buffer vertex.
*/
Sprite_BufferVertex = 0,
/**
* Index of buffer index.
*/
Sprite_BufferIndex = 1,
/**
* One vertex position has x, y.
*/
Sprite_VertexPositionNum = 2,
/**
* One vertex has u, v.
*/
Sprite_VertexUVNum = 2,
/**
* One vertex size.
*/
Sprite_VertexNum = Sprite_VertexPositionNum + Sprite_VertexUVNum,
/**
* 2 (x, y) * 4 (sizeof float)
*/
Sprite_VertexPositionStride = Sprite_VertexPositionNum * sizeof(float),
/**
* 2 (u, v) * 4 (sizeof float)
*/
Sprite_VertexUVStride = Sprite_VertexUVNum * sizeof(float),
/**
* One vertex stride.
*/
Sprite_VertexStride = Sprite_VertexPositionStride + Sprite_VertexUVStride,
};
/**
* Render with texture by vertices.
* implement Drawable's render for render self.
*/
typedef struct
{
/**
* The base class for provide draw functions.
*/
Drawable drawable[1];
/**
* Render texture.
*/
Texture* texture;
/**
* The uv width in texture.
*/
float uvWidth;
/**
* The uv height in texture.
*/
float uvHeight;
/**
* If use VBO is NULL else buffer all vertex data.
* data model: [x, y, u, v...x, y, u, v...]
*/
Array(float)* vertexArr;
/**
* If use VBO is NULL else buffer all index data.
*/
Array(short)* indexArr;
/**
* If use VBO is the generated VBO ids else 0.
*/
GLuint vboIDs[Sprite_BufferNum];
/**
* If use VAO is the generated vao id else 0.
*/
GLuint vaoID;
//----------------------------------------------------------------------------------------------------------------------
/**
* Whether vertexArr has been deformed by ASprite Deform function.
* auto reset by Render function.
*/
bool isDeformed;
/**
* All vertices index count.
*/
int indexCount;
/**
* The vertex bytes data size.
*/
int vertexDataSize;
}
Sprite;
/**
* Control Sprite.
*/
struct ASprite
{
Sprite* (*Create) (Texture* texture);
void (*Init) (Texture* texture, Sprite* outSprite);
/**
* Create Sprite by resourceFilePath.
*
* resourceFilePath:
* Android: assets
* IOS : NSBundle
*/
Sprite* (*CreateWithFile) (const char* resourceFilePath);
/**
* Init Sprite by resourceFilePath.
*
* resourceFilePath:
* Android: assets
* IOS : NSBundle
*/
void (*InitWithFile) (const char* resourceFilePath, Sprite* outSprite);
Sprite* (*CreateWithQuad) (Texture* texture, Quad* quad);
void (*InitWithQuad) (Texture* texture, Quad* quad, Sprite* outSprite);
Sprite* (*CreateWithQuadArray)(Texture* texture, Array(Quad)* quadArr);
void (*InitWithQuadArray) (Texture* texture, Array(Quad)* quadArr, Sprite* outSprite);
void (*Release) (Sprite* sprite);
/**
* Deform Sprite vertex position and uv.
* the positionDeformArr will add each position (x y).
* the uvDeformArr will add each uv.
*
* the Sprite consists of quads,
* and each quad vertices order is from left-top counterclockwise to right-top.
*
* positionDeformArr: if NULL will not deform.
* the length must equals vertex positions number (the half of Sprite vertexArr length).
*
* uvDeformArr : if NULL will not deform.
* the length must equals vertex uvs number (the half of Sprite vertexArr length).
*/
void (*Deform) (Sprite* sprite, Array(float)* positionDeformArr, Array(float)* uvDeformArr);
/**
* Same as Deform function but deform position and uv by index.
*
* indexArr: the index of position and uv array, and each vertex position or uv will deform.
* the positionDeformArr or uvDeformArr length must equals indexArr length.
*
*/
void (*DeformByIndex) (
Sprite* sprite,
Array(float)* positionDeformArr,
Array(float)* uvDeformArr,
Array(int)* indexArr
);
/**
* The implementation of Drawable's render function for render Sprite.
*/
void (*Render) (Drawable* drawable);
};
extern struct ASprite ASprite[1];
/**
* Draw Sprite.
*/
static inline void ASprite_Draw(Sprite* sprite)
{
// indirect call ASprite->Render
ADrawable->Draw(sprite->drawable);
}
#endif