ruby2d 0.9.0 → 0.9.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,146 @@
1
+ // OpenGL 2.1
2
+
3
+ #include "../include/simple2d.h"
4
+
5
+ #if !GLES
6
+
7
+
8
+ /*
9
+ * Applies the projection matrix
10
+ */
11
+ void S2D_GL2_ApplyProjection(int w, int h) {
12
+
13
+ // Initialize the projection matrix
14
+ glMatrixMode(GL_PROJECTION);
15
+ glLoadIdentity();
16
+
17
+ // Multiply the current matrix with the orthographic matrix
18
+ glOrtho(0.f, w, h, 0.f, -1.f, 1.f);
19
+
20
+ // Initialize the model-view matrix
21
+ glMatrixMode(GL_MODELVIEW);
22
+ glLoadIdentity();
23
+ }
24
+
25
+
26
+ /*
27
+ * Initalize OpenGL
28
+ */
29
+ int S2D_GL2_Init() {
30
+
31
+ GLenum error = GL_NO_ERROR;
32
+
33
+ // Enable transparency
34
+ glEnable(GL_BLEND);
35
+ glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
36
+
37
+ // Check for errors
38
+ error = glGetError();
39
+ if (error != GL_NO_ERROR) {
40
+ S2D_GL_PrintError("OpenGL initialization failed");
41
+ return 1;
42
+ } else {
43
+ return 0;
44
+ }
45
+ }
46
+
47
+
48
+ /*
49
+ * Draw triangle
50
+ */
51
+ void S2D_GL2_DrawTriangle(GLfloat x1, GLfloat y1,
52
+ GLfloat r1, GLfloat g1, GLfloat b1, GLfloat a1,
53
+ GLfloat x2, GLfloat y2,
54
+ GLfloat r2, GLfloat g2, GLfloat b2, GLfloat a2,
55
+ GLfloat x3, GLfloat y3,
56
+ GLfloat r3, GLfloat g3, GLfloat b3, GLfloat a3) {
57
+
58
+ glBegin(GL_TRIANGLES);
59
+ glColor4f(r1, g1, b1, a1); glVertex2f(x1, y1);
60
+ glColor4f(r2, g2, b2, a2); glVertex2f(x2, y2);
61
+ glColor4f(r3, g3, b3, a3); glVertex2f(x3, y3);
62
+ glEnd();
63
+ }
64
+
65
+
66
+ /*
67
+ * Draw texture
68
+ */
69
+ static void S2D_GL2_DrawTexture(int x, int y, int w, int h,
70
+ GLfloat angle, GLfloat rx, GLfloat ry,
71
+ GLfloat r, GLfloat g, GLfloat b, GLfloat a,
72
+ GLfloat tx1, GLfloat ty1, GLfloat tx2, GLfloat ty2,
73
+ GLfloat tx3, GLfloat ty3, GLfloat tx4, GLfloat ty4,
74
+ GLuint texture_id) {
75
+
76
+ S2D_GL_Point v1 = { .x = x, .y = y };
77
+ S2D_GL_Point v2 = { .x = x + w, .y = y };
78
+ S2D_GL_Point v3 = { .x = x + w, .y = y + h };
79
+ S2D_GL_Point v4 = { .x = x, .y = y + h };
80
+
81
+ // Rotate vertices
82
+ if (angle != 0) {
83
+ v1 = S2D_RotatePoint(v1, angle, rx, ry);
84
+ v2 = S2D_RotatePoint(v2, angle, rx, ry);
85
+ v3 = S2D_RotatePoint(v3, angle, rx, ry);
86
+ v4 = S2D_RotatePoint(v4, angle, rx, ry);
87
+ }
88
+
89
+ glEnable(GL_TEXTURE_2D);
90
+
91
+ glBindTexture(GL_TEXTURE_2D, texture_id);
92
+
93
+ glBegin(GL_QUADS);
94
+ glColor4f(r, g, b, a);
95
+ glTexCoord2f(tx1, ty1); glVertex2f(v1.x, v1.y);
96
+ glTexCoord2f(tx2, ty2); glVertex2f(v2.x, v2.y);
97
+ glTexCoord2f(tx3, ty3); glVertex2f(v3.x, v3.y);
98
+ glTexCoord2f(tx4, ty4); glVertex2f(v4.x, v4.y);
99
+ glEnd();
100
+
101
+ glDisable(GL_TEXTURE_2D);
102
+ }
103
+
104
+
105
+ /*
106
+ * Draw image
107
+ */
108
+ void S2D_GL2_DrawImage(S2D_Image *img) {
109
+ S2D_GL2_DrawTexture(
110
+ img->x, img->y, img->width, img->height,
111
+ img->rotate, img->rx, img->ry,
112
+ img->color.r, img->color.g, img->color.b, img->color.a,
113
+ 0.f, 0.f, 1.f, 0.f, 1.f, 1.f, 0.f, 1.f,
114
+ img->texture_id
115
+ );
116
+ }
117
+
118
+
119
+ /*
120
+ * Draw sprite
121
+ */
122
+ void S2D_GL2_DrawSprite(S2D_Sprite *spr) {
123
+ S2D_GL2_DrawTexture(
124
+ spr->x, spr->y, spr->width, spr->height,
125
+ spr->rotate, spr->rx, spr->ry,
126
+ spr->color.r, spr->color.g, spr->color.b, spr->color.a,
127
+ spr->tx1, spr->ty1, spr->tx2, spr->ty2, spr->tx3, spr->ty3, spr->tx4, spr->ty4,
128
+ spr->img->texture_id
129
+ );
130
+ }
131
+
132
+
133
+ /*
134
+ * Draw text
135
+ */
136
+ void S2D_GL2_DrawText(S2D_Text *txt) {
137
+ S2D_GL2_DrawTexture(
138
+ txt->x, txt->y, txt->width, txt->height,
139
+ txt->rotate, txt->rx, txt->ry,
140
+ txt->color.r, txt->color.g, txt->color.b, txt->color.a,
141
+ 0.f, 0.f, 1.f, 0.f, 1.f, 1.f, 0.f, 1.f,
142
+ txt->texture_id
143
+ );
144
+ }
145
+
146
+ #endif
@@ -0,0 +1,275 @@
1
+ // OpenGL 3.3
2
+
3
+ #include "../include/simple2d.h"
4
+
5
+ #if !GLES
6
+
7
+ static GLuint shaderProgram;
8
+ static GLuint texShaderProgram;
9
+ static GLuint indices[] =
10
+ { 0, 1, 2,
11
+ 2, 3, 0 };
12
+
13
+
14
+ /*
15
+ * Applies the projection matrix
16
+ */
17
+ void S2D_GL3_ApplyProjection(GLfloat orthoMatrix[16]) {
18
+
19
+ // Use the program object
20
+ glUseProgram(shaderProgram);
21
+
22
+ glUniformMatrix4fv(
23
+ glGetUniformLocation(shaderProgram, "u_mvpMatrix"),
24
+ 1, GL_FALSE, orthoMatrix
25
+ );
26
+
27
+ // Use the texture program object
28
+ glUseProgram(texShaderProgram);
29
+
30
+ glUniformMatrix4fv(
31
+ glGetUniformLocation(texShaderProgram, "u_mvpMatrix"),
32
+ 1, GL_FALSE, orthoMatrix
33
+ );
34
+ }
35
+
36
+
37
+ /*
38
+ * Initalize OpenGL
39
+ */
40
+ int S2D_GL3_Init() {
41
+
42
+ // Enable transparency
43
+ glEnable(GL_BLEND);
44
+ glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
45
+
46
+ // Vertex shader source string
47
+ GLchar vertexSource[] =
48
+ "#version 150 core\n"
49
+ "uniform mat4 u_mvpMatrix;"
50
+ "in vec4 position;"
51
+ "in vec4 color;"
52
+ "in vec2 texcoord;"
53
+ "out vec4 Color;"
54
+ "out vec2 Texcoord;"
55
+ "void main() {"
56
+ " Color = color;"
57
+ " Texcoord = texcoord;"
58
+ " gl_Position = u_mvpMatrix * position;"
59
+ "}";
60
+
61
+ // Fragment shader source string
62
+ GLchar fragmentSource[] =
63
+ "#version 150 core\n"
64
+ "in vec4 Color;"
65
+ "out vec4 outColor;"
66
+ "void main() {"
67
+ " outColor = Color;"
68
+ "}";
69
+
70
+ // Fragment shader source string for textures
71
+ GLchar texFragmentSource[] =
72
+ "#version 150 core\n"
73
+ "in vec4 Color;"
74
+ "in vec2 Texcoord;"
75
+ "out vec4 outColor;"
76
+ "uniform sampler2D tex;"
77
+ "void main() {"
78
+ " outColor = texture(tex, Texcoord) * Color;"
79
+ "}";
80
+
81
+ // Create Vertex Array Object
82
+ GLuint vao;
83
+ glGenVertexArrays(1, &vao);
84
+ glBindVertexArray(vao);
85
+
86
+ // Create Vertex Buffer Object
87
+ GLuint vbo;
88
+ glGenBuffers(1, &vbo);
89
+ glBindBuffer(GL_ARRAY_BUFFER, vbo);
90
+
91
+ // Create an element array
92
+ GLuint ebo;
93
+ glGenBuffers(1, &ebo);
94
+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
95
+
96
+ // Load the vertex and fragment shaders
97
+ GLuint vertexShader = S2D_GL_LoadShader( GL_VERTEX_SHADER, vertexSource, "GL3 Vertex");
98
+ GLuint fragmentShader = S2D_GL_LoadShader(GL_FRAGMENT_SHADER, fragmentSource, "GL3 Fragment");
99
+ GLuint texFragmentShader = S2D_GL_LoadShader(GL_FRAGMENT_SHADER, texFragmentSource, "GL3 Texture Fragment");
100
+
101
+ // Create the shader program object
102
+ shaderProgram = glCreateProgram();
103
+
104
+ // Check if program was created successfully
105
+ if (shaderProgram == 0) {
106
+ S2D_GL_PrintError("Failed to create shader program");
107
+ return GL_FALSE;
108
+ }
109
+
110
+ // Attach the shader objects to the program object
111
+ glAttachShader(shaderProgram, vertexShader);
112
+ glAttachShader(shaderProgram, fragmentShader);
113
+
114
+ // Bind the varying out variables to the fragment shader color number
115
+ glBindFragDataLocation(shaderProgram, 0, "outColor");
116
+
117
+ // Link the shader program
118
+ glLinkProgram(shaderProgram);
119
+
120
+ // Check if linked
121
+ S2D_GL_CheckLinked(shaderProgram, "GL3 shader");
122
+
123
+ // Specify the layout of the vertex data
124
+ GLint posAttrib = glGetAttribLocation(shaderProgram, "position");
125
+ glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), 0);
126
+ glEnableVertexAttribArray(posAttrib);
127
+
128
+ GLint colAttrib = glGetAttribLocation(shaderProgram, "color");
129
+ glVertexAttribPointer(colAttrib, 4, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (void*)(2 * sizeof(GLfloat)));
130
+ glEnableVertexAttribArray(colAttrib);
131
+
132
+ // Create the texture shader program object
133
+ texShaderProgram = glCreateProgram();
134
+
135
+ // Check if program was created successfully
136
+ if (texShaderProgram == 0) {
137
+ S2D_GL_PrintError("Failed to create shader program");
138
+ return GL_FALSE;
139
+ }
140
+
141
+ glAttachShader(texShaderProgram, vertexShader);
142
+ glAttachShader(texShaderProgram, texFragmentShader);
143
+
144
+ glBindFragDataLocation(texShaderProgram, 0, "outColor");
145
+
146
+ glLinkProgram(texShaderProgram);
147
+
148
+ // Check if linked
149
+ S2D_GL_CheckLinked(texShaderProgram, "GL3 texture shader");
150
+
151
+ // Specify the layout of the vertex data
152
+ posAttrib = glGetAttribLocation(texShaderProgram, "position");
153
+ glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), 0);
154
+ glEnableVertexAttribArray(posAttrib);
155
+
156
+ colAttrib = glGetAttribLocation(texShaderProgram, "color");
157
+ glVertexAttribPointer(colAttrib, 4, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (void*)(2 * sizeof(GLfloat)));
158
+ glEnableVertexAttribArray(colAttrib);
159
+
160
+ GLint texAttrib = glGetAttribLocation(texShaderProgram, "texcoord");
161
+ glVertexAttribPointer(texAttrib, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (void*)(6 * sizeof(GLfloat)));
162
+ glEnableVertexAttribArray(texAttrib);
163
+
164
+ // Clean up
165
+ glDeleteShader(vertexShader);
166
+ glDeleteShader(fragmentShader);
167
+ glDeleteShader(texFragmentShader);
168
+
169
+ return GL_TRUE;
170
+ }
171
+
172
+
173
+ /*
174
+ * Draw triangle
175
+ */
176
+ void S2D_GL3_DrawTriangle(GLfloat x1, GLfloat y1,
177
+ GLfloat r1, GLfloat g1, GLfloat b1, GLfloat a1,
178
+ GLfloat x2, GLfloat y2,
179
+ GLfloat r2, GLfloat g2, GLfloat b2, GLfloat a2,
180
+ GLfloat x3, GLfloat y3,
181
+ GLfloat r3, GLfloat g3, GLfloat b3, GLfloat a3) {
182
+
183
+ GLfloat vertices[] =
184
+ { x1, y1, r1, g1, b1, a1, 0, 0,
185
+ x2, y2, r2, g2, b2, a2, 0, 0,
186
+ x3, y3, r3, g3, b3, a3, 0, 0 };
187
+
188
+ glUseProgram(shaderProgram);
189
+ glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
190
+ glDrawArrays(GL_TRIANGLES, 0, 3);
191
+ }
192
+
193
+
194
+ /*
195
+ * Draw a texture
196
+ */
197
+ static void S2D_GL3_DrawTexture(int x, int y, int w, int h,
198
+ GLfloat angle, GLfloat rx, GLfloat ry,
199
+ GLfloat r, GLfloat g, GLfloat b, GLfloat a,
200
+ GLfloat tx1, GLfloat ty1, GLfloat tx2, GLfloat ty2,
201
+ GLfloat tx3, GLfloat ty3, GLfloat tx4, GLfloat ty4,
202
+ GLuint texture_id) {
203
+
204
+ S2D_GL_Point v1 = { .x = x, .y = y };
205
+ S2D_GL_Point v2 = { .x = x + w, .y = y };
206
+ S2D_GL_Point v3 = { .x = x + w, .y = y + h };
207
+ S2D_GL_Point v4 = { .x = x, .y = y + h };
208
+
209
+ // Rotate vertices
210
+ if (angle != 0) {
211
+ v1 = S2D_RotatePoint(v1, angle, rx, ry);
212
+ v2 = S2D_RotatePoint(v2, angle, rx, ry);
213
+ v3 = S2D_RotatePoint(v3, angle, rx, ry);
214
+ v4 = S2D_RotatePoint(v4, angle, rx, ry);
215
+ }
216
+
217
+ GLfloat vertices[] =
218
+ // vertex coords | colors | x, y texture coords
219
+ { v1.x, v1.y, r, g, b, a, tx1, ty1, // Top-left
220
+ v2.x, v2.y, r, g, b, a, tx2, ty2, // Top-right
221
+ v3.x, v3.y, r, g, b, a, tx3, ty3, // Bottom-right
222
+ v4.x, v4.y, r, g, b, a, tx4, ty4 }; // Bottom-left
223
+
224
+ glUseProgram(texShaderProgram);
225
+ glBindTexture(GL_TEXTURE_2D, texture_id);
226
+
227
+ glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
228
+ glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
229
+
230
+ glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
231
+ }
232
+
233
+
234
+ /*
235
+ * Draw image
236
+ */
237
+ void S2D_GL3_DrawImage(S2D_Image *img) {
238
+ S2D_GL3_DrawTexture(
239
+ img->x, img->y, img->width, img->height,
240
+ img->rotate, img->rx, img->ry,
241
+ img->color.r, img->color.g, img->color.b, img->color.a,
242
+ 0.f, 0.f, 1.f, 0.f, 1.f, 1.f, 0.f, 1.f,
243
+ img->texture_id
244
+ );
245
+ }
246
+
247
+
248
+ /*
249
+ * Draw sprite
250
+ */
251
+ void S2D_GL3_DrawSprite(S2D_Sprite *spr) {
252
+ S2D_GL3_DrawTexture(
253
+ spr->x, spr->y, spr->width, spr->height,
254
+ spr->rotate, spr->rx, spr->ry,
255
+ spr->color.r, spr->color.g, spr->color.b, spr->color.a,
256
+ spr->tx1, spr->ty1, spr->tx2, spr->ty2, spr->tx3, spr->ty3, spr->tx4, spr->ty4,
257
+ spr->img->texture_id
258
+ );
259
+ }
260
+
261
+
262
+ /*
263
+ * Draw text
264
+ */
265
+ void S2D_GL3_DrawText(S2D_Text *txt) {
266
+ S2D_GL3_DrawTexture(
267
+ txt->x, txt->y, txt->width, txt->height,
268
+ txt->rotate, txt->rx, txt->ry,
269
+ txt->color.r, txt->color.g, txt->color.b, txt->color.a,
270
+ 0.f, 0.f, 1.f, 0.f, 1.f, 1.f, 0.f, 1.f,
271
+ txt->texture_id
272
+ );
273
+ }
274
+
275
+ #endif
@@ -0,0 +1,308 @@
1
+ // OpenGL ES 2.0
2
+
3
+ #include "../include/simple2d.h"
4
+
5
+ #if GLES
6
+
7
+ // Triangle shader
8
+ static GLuint shaderProgram;
9
+ static GLuint positionLocation;
10
+ static GLuint colorLocation;
11
+
12
+ // Texture shader
13
+ static GLuint texShaderProgram;
14
+ static GLuint texPositionLocation;
15
+ static GLuint texColorLocation;
16
+ static GLuint texCoordLocation;
17
+ static GLuint samplerLocation;
18
+
19
+ static GLushort indices[] =
20
+ { 0, 1, 2,
21
+ 2, 3, 0 };
22
+
23
+
24
+ /*
25
+ * Applies the projection matrix
26
+ */
27
+ void S2D_GLES_ApplyProjection(GLfloat orthoMatrix[16]) {
28
+
29
+ // Use the program object
30
+ glUseProgram(shaderProgram);
31
+
32
+ glUniformMatrix4fv(
33
+ glGetUniformLocation(shaderProgram, "u_mvpMatrix"),
34
+ 1, GL_FALSE, orthoMatrix
35
+ );
36
+
37
+ // Use the texture program object
38
+ glUseProgram(texShaderProgram);
39
+
40
+ glUniformMatrix4fv(
41
+ glGetUniformLocation(texShaderProgram, "u_mvpMatrix"),
42
+ 1, GL_FALSE, orthoMatrix
43
+ );
44
+ }
45
+
46
+
47
+ /*
48
+ * Initalize OpenGL ES
49
+ */
50
+ int S2D_GLES_Init() {
51
+
52
+ // Enable transparency
53
+ glEnable(GL_BLEND);
54
+ glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
55
+
56
+ // Vertex shader source string
57
+ GLchar vertexSource[] =
58
+ // uniforms used by the vertex shader
59
+ "uniform mat4 u_mvpMatrix;" // projection matrix
60
+
61
+ // attributes input to the vertex shader
62
+ "attribute vec4 a_position;" // position value
63
+ "attribute vec4 a_color;" // input vertex color
64
+ "attribute vec2 a_texcoord;" // input texture
65
+
66
+ // varying variables, input to the fragment shader
67
+ "varying vec4 v_color;" // output vertex color
68
+ "varying vec2 v_texcoord;" // output texture
69
+
70
+ "void main()"
71
+ "{"
72
+ " v_color = a_color;"
73
+ " v_texcoord = a_texcoord;"
74
+ " gl_Position = u_mvpMatrix * a_position;"
75
+ "}";
76
+
77
+ // Fragment shader source string
78
+ GLchar fragmentSource[] =
79
+ "precision mediump float;"
80
+ // input vertex color from vertex shader
81
+ "varying vec4 v_color;"
82
+
83
+ "void main()"
84
+ "{"
85
+ " gl_FragColor = v_color;"
86
+ "}";
87
+
88
+ // Fragment shader source string for textures
89
+ GLchar texFragmentSource[] =
90
+ "precision mediump float;"
91
+ // input vertex color from vertex shader
92
+ "varying vec4 v_color;"
93
+ "varying vec2 v_texcoord;"
94
+ "uniform sampler2D s_texture;"
95
+
96
+ "void main()"
97
+ "{"
98
+ " gl_FragColor = texture2D(s_texture, v_texcoord) * v_color;"
99
+ "}";
100
+
101
+ // Load the vertex and fragment shaders
102
+ GLuint vertexShader = S2D_GL_LoadShader( GL_VERTEX_SHADER, vertexSource, "GLES Vertex");
103
+ GLuint fragmentShader = S2D_GL_LoadShader(GL_FRAGMENT_SHADER, fragmentSource, "GLES Fragment");
104
+ GLuint texFragmentShader = S2D_GL_LoadShader(GL_FRAGMENT_SHADER, texFragmentSource, "GLES Texture Fragment");
105
+
106
+ // Triangle Shader //
107
+
108
+ // Create the shader program object
109
+ shaderProgram = glCreateProgram();
110
+
111
+ // Check if program was created successfully
112
+ if (shaderProgram == 0) {
113
+ S2D_GL_PrintError("Failed to create shader program");
114
+ return GL_FALSE;
115
+ }
116
+
117
+ // Attach the shader objects to the program object
118
+ glAttachShader(shaderProgram, vertexShader);
119
+ glAttachShader(shaderProgram, fragmentShader);
120
+
121
+ // Link the shader program
122
+ glLinkProgram(shaderProgram);
123
+
124
+ // Check if linked
125
+ S2D_GL_CheckLinked(shaderProgram, "GLES shader");
126
+
127
+ // Get the attribute locations
128
+ positionLocation = glGetAttribLocation(shaderProgram, "a_position");
129
+ colorLocation = glGetAttribLocation(shaderProgram, "a_color");
130
+
131
+ // Texture Shader //
132
+
133
+ // Create the texture shader program object
134
+ texShaderProgram = glCreateProgram();
135
+
136
+ // Check if program was created successfully
137
+ if (texShaderProgram == 0) {
138
+ S2D_GL_PrintError("Failed to create shader program");
139
+ return GL_FALSE;
140
+ }
141
+
142
+ // Attach the shader objects to the program object
143
+ glAttachShader(texShaderProgram, vertexShader);
144
+ glAttachShader(texShaderProgram, texFragmentShader);
145
+
146
+ // Link the shader program
147
+ glLinkProgram(texShaderProgram);
148
+
149
+ // Check if linked
150
+ S2D_GL_CheckLinked(texShaderProgram, "GLES texture shader");
151
+
152
+ // Get the attribute locations
153
+ texPositionLocation = glGetAttribLocation(texShaderProgram, "a_position");
154
+ texColorLocation = glGetAttribLocation(texShaderProgram, "a_color");
155
+ texCoordLocation = glGetAttribLocation(texShaderProgram, "a_texcoord");
156
+
157
+ // Get the sampler location
158
+ samplerLocation = glGetUniformLocation(texShaderProgram, "s_texture");
159
+
160
+ // Clean up
161
+ glDeleteShader(vertexShader);
162
+ glDeleteShader(fragmentShader);
163
+ glDeleteShader(texFragmentShader);
164
+
165
+ return GL_TRUE;
166
+ }
167
+
168
+
169
+ /*
170
+ * Draw triangle
171
+ */
172
+ void S2D_GLES_DrawTriangle(GLfloat x1, GLfloat y1,
173
+ GLfloat r1, GLfloat g1, GLfloat b1, GLfloat a1,
174
+ GLfloat x2, GLfloat y2,
175
+ GLfloat r2, GLfloat g2, GLfloat b2, GLfloat a2,
176
+ GLfloat x3, GLfloat y3,
177
+ GLfloat r3, GLfloat g3, GLfloat b3, GLfloat a3) {
178
+
179
+ GLfloat vertices[] =
180
+ { x1, y1, 0.f,
181
+ x2, y2, 0.f,
182
+ x3, y3, 0.f };
183
+
184
+ GLfloat colors[] =
185
+ { r1, g1, b1, a1,
186
+ r2, g2, b2, a2,
187
+ r3, g3, b3, a3 };
188
+
189
+ glUseProgram(shaderProgram);
190
+
191
+ // Load the vertex position
192
+ glVertexAttribPointer(positionLocation, 3, GL_FLOAT, GL_FALSE, 0, vertices);
193
+ glEnableVertexAttribArray(positionLocation);
194
+
195
+ // Load the colors
196
+ glVertexAttribPointer(colorLocation, 4, GL_FLOAT, GL_FALSE, 0, colors);
197
+ glEnableVertexAttribArray(colorLocation);
198
+
199
+ // draw
200
+ glDrawArrays(GL_TRIANGLES, 0, 3);
201
+ }
202
+
203
+
204
+ /*
205
+ * Draw a texture
206
+ */
207
+ static void S2D_GLES_DrawTexture(int x, int y, int w, int h,
208
+ GLfloat angle, GLfloat rx, GLfloat ry,
209
+ GLfloat r, GLfloat g, GLfloat b, GLfloat a,
210
+ GLfloat tx1, GLfloat ty1, GLfloat tx2, GLfloat ty2,
211
+ GLfloat tx3, GLfloat ty3, GLfloat tx4, GLfloat ty4,
212
+ GLuint texture_id) {
213
+
214
+ S2D_GL_Point v1 = { .x = x, .y = y };
215
+ S2D_GL_Point v2 = { .x = x + w, .y = y };
216
+ S2D_GL_Point v3 = { .x = x + w, .y = y + h };
217
+ S2D_GL_Point v4 = { .x = x, .y = y + h };
218
+
219
+ // Rotate vertices
220
+ if (angle != 0) {
221
+ v1 = S2D_RotatePoint(v1, angle, rx, ry);
222
+ v2 = S2D_RotatePoint(v2, angle, rx, ry);
223
+ v3 = S2D_RotatePoint(v3, angle, rx, ry);
224
+ v4 = S2D_RotatePoint(v4, angle, rx, ry);
225
+ }
226
+
227
+ GLfloat vertices[] =
228
+ // x, y coords | x, y texture coords
229
+ { v1.x, v1.y, 0.f, tx1, ty1,
230
+ v2.x, v2.y, 0.f, tx2, ty2,
231
+ v3.x, v3.y, 0.f, tx3, ty3,
232
+ v4.x, v4.y, 0.f, tx4, ty4 };
233
+
234
+ GLfloat colors[] =
235
+ { r, g, b, a,
236
+ r, g, b, a,
237
+ r, g, b, a,
238
+ r, g, b, a };
239
+
240
+ glUseProgram(texShaderProgram);
241
+
242
+ // Load the vertex position
243
+ glVertexAttribPointer(texPositionLocation, 3, GL_FLOAT, GL_FALSE,
244
+ 5 * sizeof(GLfloat), vertices);
245
+ glEnableVertexAttribArray(texPositionLocation);
246
+
247
+ // Load the colors
248
+ glVertexAttribPointer(texColorLocation, 4, GL_FLOAT, GL_FALSE, 0, colors);
249
+ glEnableVertexAttribArray(texColorLocation);
250
+
251
+ // Load the texture coordinate
252
+ glVertexAttribPointer(texCoordLocation, 2, GL_FLOAT, GL_FALSE,
253
+ 5 * sizeof(GLfloat), &vertices[3]);
254
+ glEnableVertexAttribArray(texCoordLocation);
255
+
256
+ // Bind the texture
257
+ glActiveTexture(GL_TEXTURE0);
258
+ glBindTexture(GL_TEXTURE_2D, texture_id);
259
+
260
+ // Set the sampler texture unit to 0
261
+ glUniform1i(samplerLocation, 0);
262
+
263
+ glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices);
264
+ }
265
+
266
+
267
+ /*
268
+ * Draw image
269
+ */
270
+ void S2D_GLES_DrawImage(S2D_Image *img) {
271
+ S2D_GLES_DrawTexture(
272
+ img->x, img->y, img->width, img->height,
273
+ img->rotate, img->rx, img->ry,
274
+ img->color.r, img->color.g, img->color.b, img->color.a,
275
+ 0.f, 0.f, 1.f, 0.f, 1.f, 1.f, 0.f, 1.f,
276
+ img->texture_id
277
+ );
278
+ }
279
+
280
+
281
+ /*
282
+ * Draw sprite
283
+ */
284
+ void S2D_GLES_DrawSprite(S2D_Sprite *spr) {
285
+ S2D_GLES_DrawTexture(
286
+ spr->x, spr->y, spr->width, spr->height,
287
+ spr->rotate, spr->rx, spr->ry,
288
+ spr->color.r, spr->color.g, spr->color.b, spr->color.a,
289
+ spr->tx1, spr->ty1, spr->tx2, spr->ty2, spr->tx3, spr->ty3, spr->tx4, spr->ty4,
290
+ spr->img->texture_id
291
+ );
292
+ }
293
+
294
+
295
+ /*
296
+ * Draw text
297
+ */
298
+ void S2D_GLES_DrawText(S2D_Text *txt) {
299
+ S2D_GLES_DrawTexture(
300
+ txt->x, txt->y, txt->width, txt->height,
301
+ txt->rotate, txt->rx, txt->ry,
302
+ txt->color.r, txt->color.g, txt->color.b, txt->color.a,
303
+ 0.f, 0.f, 1.f, 0.f, 1.f, 1.f, 0.f, 1.f,
304
+ txt->texture_id
305
+ );
306
+ }
307
+
308
+ #endif