ruby2d 0.9.4 → 0.11.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/assets/include/SDL2/SDL.h +4 -1
- data/assets/include/SDL2/SDL_assert.h +3 -1
- data/assets/include/SDL2/SDL_atomic.h +1 -1
- data/assets/include/SDL2/SDL_audio.h +1 -1
- data/assets/include/SDL2/SDL_bits.h +1 -1
- data/assets/include/SDL2/SDL_blendmode.h +4 -1
- data/assets/include/SDL2/SDL_clipboard.h +1 -1
- data/assets/include/SDL2/SDL_config.h +1 -1
- data/assets/include/SDL2/SDL_config_android.h +7 -3
- data/assets/include/SDL2/SDL_config_iphoneos.h +16 -10
- data/assets/include/SDL2/SDL_config_macosx.h +40 -14
- data/assets/include/SDL2/SDL_config_minimal.h +1 -1
- data/assets/include/SDL2/SDL_config_os2.h +26 -8
- data/assets/include/SDL2/SDL_config_pandora.h +4 -2
- data/assets/include/SDL2/SDL_config_psp.h +2 -1
- data/assets/include/SDL2/SDL_config_windows.h +40 -9
- data/assets/include/SDL2/SDL_config_winrt.h +5 -2
- data/assets/include/SDL2/SDL_config_wiz.h +5 -2
- data/assets/include/SDL2/SDL_copying.h +1 -1
- data/assets/include/SDL2/SDL_cpuinfo.h +32 -3
- data/assets/include/SDL2/SDL_egl.h +4 -1
- data/assets/include/SDL2/SDL_endian.h +6 -3
- data/assets/include/SDL2/SDL_error.h +38 -2
- data/assets/include/SDL2/SDL_events.h +64 -28
- data/assets/include/SDL2/SDL_filesystem.h +1 -1
- data/assets/include/SDL2/SDL_gamecontroller.h +160 -9
- data/assets/include/SDL2/SDL_gesture.h +1 -1
- data/assets/include/SDL2/SDL_haptic.h +10 -1
- data/assets/include/SDL2/SDL_hints.h +316 -4
- data/assets/include/SDL2/SDL_joystick.h +115 -24
- data/assets/include/SDL2/SDL_keyboard.h +1 -1
- data/assets/include/SDL2/SDL_keycode.h +11 -9
- data/assets/include/SDL2/SDL_loadso.h +1 -1
- data/assets/include/SDL2/SDL_locale.h +101 -0
- data/assets/include/SDL2/SDL_log.h +3 -3
- data/assets/include/SDL2/SDL_main.h +1 -1
- data/assets/include/SDL2/SDL_messagebox.h +6 -4
- data/assets/include/SDL2/SDL_metal.h +117 -0
- data/assets/include/SDL2/SDL_misc.h +75 -0
- data/assets/include/SDL2/SDL_mouse.h +1 -1
- data/assets/include/SDL2/SDL_mutex.h +1 -1
- data/assets/include/SDL2/SDL_name.h +1 -1
- data/assets/include/SDL2/SDL_opengl.h +1 -1
- data/assets/include/SDL2/SDL_opengl_glext.h +3 -0
- data/assets/include/SDL2/SDL_opengles.h +1 -1
- data/assets/include/SDL2/SDL_opengles2.h +1 -1
- data/assets/include/SDL2/SDL_pixels.h +25 -16
- data/assets/include/SDL2/SDL_platform.h +1 -1
- data/assets/include/SDL2/SDL_power.h +1 -1
- data/assets/include/SDL2/SDL_quit.h +1 -1
- data/assets/include/SDL2/SDL_rect.h +1 -1
- data/assets/include/SDL2/SDL_render.h +63 -3
- data/assets/include/SDL2/SDL_revision.h +2 -2
- data/assets/include/SDL2/SDL_rwops.h +2 -10
- data/assets/include/SDL2/SDL_scancode.h +1 -1
- data/assets/include/SDL2/SDL_sensor.h +25 -9
- data/assets/include/SDL2/SDL_shape.h +1 -1
- data/assets/include/SDL2/SDL_stdinc.h +44 -4
- data/assets/include/SDL2/SDL_surface.h +11 -2
- data/assets/include/SDL2/SDL_system.h +50 -4
- data/assets/include/SDL2/SDL_syswm.h +29 -2
- data/assets/include/SDL2/SDL_test.h +1 -1
- data/assets/include/SDL2/SDL_test_assert.h +1 -1
- data/assets/include/SDL2/SDL_test_common.h +14 -1
- data/assets/include/SDL2/SDL_test_compare.h +1 -1
- data/assets/include/SDL2/SDL_test_crc32.h +1 -1
- data/assets/include/SDL2/SDL_test_font.h +1 -1
- data/assets/include/SDL2/SDL_test_fuzzer.h +1 -1
- data/assets/include/SDL2/SDL_test_harness.h +1 -1
- data/assets/include/SDL2/SDL_test_images.h +1 -1
- data/assets/include/SDL2/SDL_test_log.h +1 -1
- data/assets/include/SDL2/SDL_test_md5.h +1 -1
- data/assets/include/SDL2/SDL_test_memory.h +1 -1
- data/assets/include/SDL2/SDL_test_random.h +1 -1
- data/assets/include/SDL2/SDL_thread.h +34 -11
- data/assets/include/SDL2/SDL_timer.h +1 -1
- data/assets/include/SDL2/SDL_touch.h +1 -1
- data/assets/include/SDL2/SDL_types.h +1 -1
- data/assets/include/SDL2/SDL_version.h +2 -2
- data/assets/include/SDL2/SDL_video.h +11 -5
- data/assets/include/SDL2/SDL_vulkan.h +6 -8
- data/assets/include/SDL2/begin_code.h +5 -9
- data/assets/include/SDL2/close_code.h +1 -1
- data/assets/macos/lib/libFLAC.a +0 -0
- data/assets/macos/lib/libSDL2.a +0 -0
- data/assets/macos/lib/libSDL2_image.a +0 -0
- data/assets/macos/lib/libSDL2_mixer.a +0 -0
- data/assets/macos/lib/libSDL2_ttf.a +0 -0
- data/assets/macos/lib/libfreetype.a +0 -0
- data/assets/macos/lib/libjpeg.a +0 -0
- data/assets/macos/lib/libmodplug.a +0 -0
- data/assets/macos/lib/libmpg123.a +0 -0
- data/assets/macos/lib/libogg.a +0 -0
- data/assets/macos/lib/libpng16.a +0 -0
- data/assets/macos/lib/libtiff.a +0 -0
- data/assets/macos/lib/libvorbis.a +0 -0
- data/assets/macos/lib/libvorbisfile.a +0 -0
- data/assets/macos/lib/libwebp.a +0 -0
- data/assets/mingw/bin/SDL2.dll +0 -0
- data/assets/mingw/lib/libSDL2.a +0 -0
- data/assets/mingw/lib/libSDL2.dll.a +0 -0
- data/assets/mingw/lib/libSDL2_test.a +0 -0
- data/assets/mingw/lib/libSDL2main.a +0 -0
- data/bin/ruby2d +1 -0
- data/{assets/linux/simple2d/src/simple2d.c → ext/ruby2d/common.c} +32 -32
- data/{assets/linux/simple2d/src → ext/ruby2d}/controllers.c +17 -17
- data/ext/ruby2d/extconf.rb +37 -59
- data/ext/ruby2d/font.c +35 -0
- data/{assets/linux/simple2d/src → ext/ruby2d}/gl.c +66 -98
- data/ext/ruby2d/gl2.c +86 -0
- data/{assets/linux/simple2d/src → ext/ruby2d}/gl3.c +69 -112
- data/{assets/linux/simple2d/src → ext/ruby2d}/gles.c +22 -81
- data/ext/ruby2d/image.c +58 -0
- data/{assets/linux/simple2d/src → ext/ruby2d}/input.c +8 -8
- data/{assets/linux/simple2d/src → ext/ruby2d}/music.c +30 -17
- data/ext/ruby2d/ruby2d.c +449 -439
- data/ext/ruby2d/ruby2d.h +652 -0
- data/{assets/linux/simple2d/src → ext/ruby2d}/shapes.c +18 -18
- data/{assets/linux/simple2d/src → ext/ruby2d}/sound.c +39 -14
- data/ext/ruby2d/text.c +22 -0
- data/{assets/linux/simple2d/src → ext/ruby2d}/window.c +63 -63
- data/lib/ruby2d/circle.rb +21 -3
- data/lib/ruby2d/cli/build.rb +4 -7
- data/lib/ruby2d/dsl.rb +16 -9
- data/lib/ruby2d/entity.rb +17 -0
- data/lib/ruby2d/font.rb +23 -3
- data/lib/ruby2d/image.rb +35 -7
- data/lib/ruby2d/line.rb +23 -1
- data/lib/ruby2d/music.rb +5 -0
- data/lib/ruby2d/pixel.rb +17 -0
- data/lib/ruby2d/quad.rb +21 -1
- data/lib/ruby2d/rectangle.rb +12 -3
- data/lib/ruby2d/renderable.rb +4 -20
- data/lib/ruby2d/sound.rb +30 -0
- data/lib/ruby2d/sprite.rb +57 -75
- data/lib/ruby2d/square.rb +10 -1
- data/lib/ruby2d/text.rb +55 -12
- data/lib/ruby2d/texture.rb +28 -0
- data/lib/ruby2d/tileset.rb +87 -0
- data/lib/ruby2d/triangle.rb +19 -1
- data/lib/ruby2d/version.rb +1 -1
- data/lib/ruby2d/vertices.rb +84 -0
- data/lib/ruby2d/window.rb +259 -22
- data/lib/ruby2d.rb +6 -1
- metadata +30 -26
- data/assets/include/simple2d.h +0 -757
- data/assets/linux/simple2d/Makefile +0 -260
- data/assets/linux/simple2d/bin/simple2d.sh +0 -1318
- data/assets/linux/simple2d/include/simple2d.h +0 -757
- data/assets/linux/simple2d/src/gl2.c +0 -146
- data/assets/linux/simple2d/src/image.c +0 -138
- data/assets/linux/simple2d/src/sprite.c +0 -147
- data/assets/linux/simple2d/src/text.c +0 -129
- data/assets/macos/lib/libsimple2d.a +0 -0
- data/assets/mingw/lib/libsimple2d.a +0 -0
@@ -1,6 +1,6 @@
|
|
1
1
|
// OpenGL 3.3+
|
2
2
|
|
3
|
-
#include "
|
3
|
+
#include "ruby2d.h"
|
4
4
|
|
5
5
|
// Skip this file if OpenGL ES
|
6
6
|
#if !GLES
|
@@ -10,18 +10,16 @@ static GLuint vboSize; // size of the VBO in bytes
|
|
10
10
|
static GLfloat *vboData; // pointer to the VBO data
|
11
11
|
static GLfloat *vboDataCurrent; // pointer to the data for the current vertices
|
12
12
|
static GLuint vboDataIndex = 0; // index of the current object being rendered
|
13
|
-
static GLuint vboObjCapacity =
|
13
|
+
static GLuint vboObjCapacity = 7500; // number of objects the VBO can store
|
14
|
+
static GLuint verticesTextureIds[7500]; // store the texture_id of each vertices
|
14
15
|
static GLuint shaderProgram; // triangle shader program
|
15
16
|
static GLuint texShaderProgram; // texture shader program
|
16
|
-
static GLuint indices[] = // indices for rendering textured quads
|
17
|
-
{ 0, 1, 2,
|
18
|
-
2, 3, 0 };
|
19
17
|
|
20
18
|
|
21
19
|
/*
|
22
20
|
* Applies the projection matrix
|
23
21
|
*/
|
24
|
-
void
|
22
|
+
void R2D_GL3_ApplyProjection(GLfloat orthoMatrix[16]) {
|
25
23
|
|
26
24
|
// Use the program object
|
27
25
|
glUseProgram(shaderProgram);
|
@@ -46,7 +44,7 @@ void S2D_GL3_ApplyProjection(GLfloat orthoMatrix[16]) {
|
|
46
44
|
/*
|
47
45
|
* Initalize OpenGL
|
48
46
|
*/
|
49
|
-
int
|
47
|
+
int R2D_GL3_Init() {
|
50
48
|
|
51
49
|
// Enable transparency
|
52
50
|
glEnable(GL_BLEND);
|
@@ -106,7 +104,7 @@ int S2D_GL3_Init() {
|
|
106
104
|
// Create a vertex buffer object and allocate data
|
107
105
|
glGenBuffers(1, &vbo);
|
108
106
|
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
109
|
-
vboSize = vboObjCapacity * sizeof(GLfloat) *
|
107
|
+
vboSize = vboObjCapacity * sizeof(GLfloat) * 8;
|
110
108
|
vboData = (GLfloat *) malloc(vboSize);
|
111
109
|
vboDataCurrent = vboData;
|
112
110
|
|
@@ -116,9 +114,9 @@ int S2D_GL3_Init() {
|
|
116
114
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
|
117
115
|
|
118
116
|
// Load the vertex and fragment shaders
|
119
|
-
GLuint vertexShader =
|
120
|
-
GLuint fragmentShader =
|
121
|
-
GLuint texFragmentShader =
|
117
|
+
GLuint vertexShader = R2D_GL_LoadShader( GL_VERTEX_SHADER, vertexSource, "GL3 Vertex");
|
118
|
+
GLuint fragmentShader = R2D_GL_LoadShader(GL_FRAGMENT_SHADER, fragmentSource, "GL3 Fragment");
|
119
|
+
GLuint texFragmentShader = R2D_GL_LoadShader(GL_FRAGMENT_SHADER, texFragmentSource, "GL3 Texture Fragment");
|
122
120
|
|
123
121
|
// Triangle Shader //
|
124
122
|
|
@@ -127,7 +125,7 @@ int S2D_GL3_Init() {
|
|
127
125
|
|
128
126
|
// Check if program was created successfully
|
129
127
|
if (shaderProgram == 0) {
|
130
|
-
|
128
|
+
R2D_GL_PrintError("Failed to create shader program");
|
131
129
|
return GL_FALSE;
|
132
130
|
}
|
133
131
|
|
@@ -142,7 +140,7 @@ int S2D_GL3_Init() {
|
|
142
140
|
glLinkProgram(shaderProgram);
|
143
141
|
|
144
142
|
// Check if linked
|
145
|
-
|
143
|
+
R2D_GL_CheckLinked(shaderProgram, "GL3 shader");
|
146
144
|
|
147
145
|
// Specify the layout of the position vertex data...
|
148
146
|
GLint posAttrib = glGetAttribLocation(shaderProgram, "position");
|
@@ -161,7 +159,7 @@ int S2D_GL3_Init() {
|
|
161
159
|
|
162
160
|
// Check if program was created successfully
|
163
161
|
if (texShaderProgram == 0) {
|
164
|
-
|
162
|
+
R2D_GL_PrintError("Failed to create shader program");
|
165
163
|
return GL_FALSE;
|
166
164
|
}
|
167
165
|
|
@@ -176,7 +174,7 @@ int S2D_GL3_Init() {
|
|
176
174
|
glLinkProgram(texShaderProgram);
|
177
175
|
|
178
176
|
// Check if linked
|
179
|
-
|
177
|
+
R2D_GL_CheckLinked(texShaderProgram, "GL3 texture shader");
|
180
178
|
|
181
179
|
// Specify the layout of the position vertex data...
|
182
180
|
posAttrib = glGetAttribLocation(texShaderProgram, "position");
|
@@ -206,18 +204,40 @@ int S2D_GL3_Init() {
|
|
206
204
|
/*
|
207
205
|
* Render the vertex buffer and reset it
|
208
206
|
*/
|
209
|
-
void
|
210
|
-
|
211
|
-
// Use the triangle shader program
|
212
|
-
glUseProgram(shaderProgram);
|
213
|
-
|
207
|
+
void R2D_GL3_FlushBuffers() {
|
214
208
|
// Bind to the vertex buffer object and update its data
|
215
209
|
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
210
|
+
|
216
211
|
glBufferData(GL_ARRAY_BUFFER, vboSize, NULL, GL_DYNAMIC_DRAW);
|
217
|
-
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(GLfloat) * vboDataIndex *
|
212
|
+
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(GLfloat) * vboDataIndex * 8, vboData);
|
213
|
+
|
214
|
+
GLuint verticesOffset = 0;
|
215
|
+
GLuint lastTextureId = verticesTextureIds[0];
|
216
|
+
|
217
|
+
for (GLuint i = 0; i <= vboDataIndex; i++) {
|
218
|
+
if(lastTextureId != verticesTextureIds[i] || i == vboDataIndex) {
|
219
|
+
// A texture ID of 0 represents no texture (render a triangle)
|
220
|
+
if(lastTextureId == 0) {
|
221
|
+
|
222
|
+
// Use the triangle shader program
|
223
|
+
glUseProgram(shaderProgram);
|
224
|
+
|
225
|
+
// A number other than 0 represents a texture_id
|
226
|
+
} else {
|
227
|
+
|
228
|
+
// Use the texture shader program
|
229
|
+
glUseProgram(texShaderProgram);
|
230
|
+
|
231
|
+
// Bind the texture using the provided ID
|
232
|
+
glBindTexture(GL_TEXTURE_2D, lastTextureId);
|
233
|
+
}
|
218
234
|
|
219
|
-
|
220
|
-
|
235
|
+
glDrawArrays(GL_TRIANGLES, verticesOffset, i - verticesOffset);
|
236
|
+
|
237
|
+
lastTextureId = verticesTextureIds[i];
|
238
|
+
verticesOffset = i;
|
239
|
+
}
|
240
|
+
}
|
221
241
|
|
222
242
|
// Reset the buffer object index and data pointer
|
223
243
|
vboDataIndex = 0;
|
@@ -228,7 +248,7 @@ void S2D_GL3_FlushBuffers() {
|
|
228
248
|
/*
|
229
249
|
* Draw triangle
|
230
250
|
*/
|
231
|
-
void
|
251
|
+
void R2D_GL3_DrawTriangle(GLfloat x1, GLfloat y1,
|
232
252
|
GLfloat r1, GLfloat g1, GLfloat b1, GLfloat a1,
|
233
253
|
GLfloat x2, GLfloat y2,
|
234
254
|
GLfloat r2, GLfloat g2, GLfloat b2, GLfloat a2,
|
@@ -236,10 +256,10 @@ void S2D_GL3_DrawTriangle(GLfloat x1, GLfloat y1,
|
|
236
256
|
GLfloat r3, GLfloat g3, GLfloat b3, GLfloat a3) {
|
237
257
|
|
238
258
|
// If buffer is full, flush it
|
239
|
-
if (vboDataIndex >= vboObjCapacity)
|
259
|
+
if (vboDataIndex + 3 >= vboObjCapacity) R2D_GL3_FlushBuffers();
|
240
260
|
|
241
261
|
// Set the triangle data into a formatted array
|
242
|
-
GLfloat vertices[] =
|
262
|
+
GLfloat vertices[24] =
|
243
263
|
{ x1, y1, r1, g1, b1, a1, 0, 0,
|
244
264
|
x2, y2, r2, g2, b2, a2, 0, 0,
|
245
265
|
x3, y3, r3, g3, b3, a3, 0, 0 };
|
@@ -248,101 +268,38 @@ void S2D_GL3_DrawTriangle(GLfloat x1, GLfloat y1,
|
|
248
268
|
memcpy(vboDataCurrent, vertices, sizeof(vertices));
|
249
269
|
|
250
270
|
// Increment the buffer object index and the vertex data pointer for next use
|
251
|
-
vboDataIndex
|
271
|
+
verticesTextureIds[vboDataIndex] = verticesTextureIds[vboDataIndex + 1] = verticesTextureIds[vboDataIndex + 2] = 0;
|
272
|
+
vboDataIndex += 3;
|
252
273
|
vboDataCurrent = (GLfloat *)((char *)vboDataCurrent + (sizeof(GLfloat) * 24));
|
253
274
|
}
|
254
275
|
|
255
276
|
|
256
277
|
/*
|
257
|
-
* Draw a texture
|
278
|
+
* Draw a texture (New method with vertices pre-calculated)
|
258
279
|
*/
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
S2D_GL_Point v4 = { .x = x, .y = y + h };
|
275
|
-
|
276
|
-
// Rotate vertices
|
277
|
-
if (angle != 0) {
|
278
|
-
v1 = S2D_RotatePoint(v1, angle, rx, ry);
|
279
|
-
v2 = S2D_RotatePoint(v2, angle, rx, ry);
|
280
|
-
v3 = S2D_RotatePoint(v3, angle, rx, ry);
|
281
|
-
v4 = S2D_RotatePoint(v4, angle, rx, ry);
|
282
|
-
}
|
283
|
-
|
284
|
-
// Set the textured quad data into a formatted array
|
285
|
-
GLfloat vertices[] =
|
286
|
-
// vertex coords | colors | x, y texture coords
|
287
|
-
{ v1.x, v1.y, r, g, b, a, tx1, ty1, // Top-left
|
288
|
-
v2.x, v2.y, r, g, b, a, tx2, ty2, // Top-right
|
289
|
-
v3.x, v3.y, r, g, b, a, tx3, ty3, // Bottom-right
|
290
|
-
v4.x, v4.y, r, g, b, a, tx4, ty4 }; // Bottom-left
|
291
|
-
|
292
|
-
// Use the texture shader program
|
293
|
-
glUseProgram(texShaderProgram);
|
294
|
-
|
295
|
-
// Bind the texture using the provided ID
|
296
|
-
glBindTexture(GL_TEXTURE_2D, texture_id);
|
297
|
-
|
298
|
-
// Create and Initialize the vertex data and array indices
|
299
|
-
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
300
|
-
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
|
301
|
-
|
302
|
-
// Render the textured quad
|
303
|
-
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
|
304
|
-
}
|
305
|
-
|
306
|
-
|
307
|
-
/*
|
308
|
-
* Draw image
|
309
|
-
*/
|
310
|
-
void S2D_GL3_DrawImage(S2D_Image *img) {
|
311
|
-
S2D_GL3_DrawTexture(
|
312
|
-
img->x, img->y, img->width, img->height,
|
313
|
-
img->rotate, img->rx, img->ry,
|
314
|
-
img->color.r, img->color.g, img->color.b, img->color.a,
|
315
|
-
0.f, 0.f, 1.f, 0.f, 1.f, 1.f, 0.f, 1.f,
|
316
|
-
img->texture_id
|
317
|
-
);
|
318
|
-
}
|
280
|
+
void R2D_GL3_DrawTexture(GLfloat coordinates[], GLfloat texture_coordinates[], GLfloat color[], int texture_id) {
|
281
|
+
// If buffer is full, flush it
|
282
|
+
if (vboDataIndex + 6 >= vboObjCapacity) R2D_GL3_FlushBuffers();
|
283
|
+
|
284
|
+
// There are 6 vertices for a square as we are rendering two Triangles to make up our square:
|
285
|
+
// Triangle one: Top left, Top right, Bottom right
|
286
|
+
// Triangle two: Bottom right, Bottom left, Top left
|
287
|
+
GLfloat vertices[48] = {
|
288
|
+
coordinates[0], coordinates[1], color[0], color[1], color[2], color[3], texture_coordinates[0], texture_coordinates[1],
|
289
|
+
coordinates[2], coordinates[3], color[0], color[1], color[2], color[3], texture_coordinates[2], texture_coordinates[3],
|
290
|
+
coordinates[4], coordinates[5], color[0], color[1], color[2], color[3], texture_coordinates[4], texture_coordinates[5],
|
291
|
+
coordinates[4], coordinates[5], color[0], color[1], color[2], color[3], texture_coordinates[4], texture_coordinates[5],
|
292
|
+
coordinates[6], coordinates[7], color[0], color[1], color[2], color[3], texture_coordinates[6], texture_coordinates[7],
|
293
|
+
coordinates[0], coordinates[1], color[0], color[1], color[2], color[3], texture_coordinates[0], texture_coordinates[1],
|
294
|
+
};
|
319
295
|
|
296
|
+
// Copy the vertex data into the current position of the buffer
|
297
|
+
memcpy(vboDataCurrent, vertices, sizeof(vertices));
|
320
298
|
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
void S2D_GL3_DrawSprite(S2D_Sprite *spr) {
|
325
|
-
S2D_GL3_DrawTexture(
|
326
|
-
spr->x, spr->y, spr->width, spr->height,
|
327
|
-
spr->rotate, spr->rx, spr->ry,
|
328
|
-
spr->color.r, spr->color.g, spr->color.b, spr->color.a,
|
329
|
-
spr->tx1, spr->ty1, spr->tx2, spr->ty2, spr->tx3, spr->ty3, spr->tx4, spr->ty4,
|
330
|
-
spr->img->texture_id
|
331
|
-
);
|
299
|
+
verticesTextureIds[vboDataIndex] = verticesTextureIds[vboDataIndex + 1] = verticesTextureIds[vboDataIndex + 2] = verticesTextureIds[vboDataIndex + 3] = verticesTextureIds[vboDataIndex + 4] = verticesTextureIds[vboDataIndex + 5] = texture_id;
|
300
|
+
vboDataIndex += 6;
|
301
|
+
vboDataCurrent = (GLfloat *)((char *)vboDataCurrent + (sizeof(GLfloat) * 48));
|
332
302
|
}
|
333
303
|
|
334
304
|
|
335
|
-
/*
|
336
|
-
* Draw text
|
337
|
-
*/
|
338
|
-
void S2D_GL3_DrawText(S2D_Text *txt) {
|
339
|
-
S2D_GL3_DrawTexture(
|
340
|
-
txt->x, txt->y, txt->width, txt->height,
|
341
|
-
txt->rotate, txt->rx, txt->ry,
|
342
|
-
txt->color.r, txt->color.g, txt->color.b, txt->color.a,
|
343
|
-
0.f, 0.f, 1.f, 0.f, 1.f, 1.f, 0.f, 1.f,
|
344
|
-
txt->texture_id
|
345
|
-
);
|
346
|
-
}
|
347
|
-
|
348
305
|
#endif
|
@@ -1,6 +1,6 @@
|
|
1
1
|
// OpenGL ES 2.0
|
2
2
|
|
3
|
-
#include "
|
3
|
+
#include "ruby2d.h"
|
4
4
|
|
5
5
|
#if GLES
|
6
6
|
|
@@ -24,7 +24,7 @@ static GLushort indices[] =
|
|
24
24
|
/*
|
25
25
|
* Applies the projection matrix
|
26
26
|
*/
|
27
|
-
void
|
27
|
+
void R2D_GLES_ApplyProjection(GLfloat orthoMatrix[16]) {
|
28
28
|
|
29
29
|
// Use the program object
|
30
30
|
glUseProgram(shaderProgram);
|
@@ -47,7 +47,7 @@ void S2D_GLES_ApplyProjection(GLfloat orthoMatrix[16]) {
|
|
47
47
|
/*
|
48
48
|
* Initalize OpenGL ES
|
49
49
|
*/
|
50
|
-
int
|
50
|
+
int R2D_GLES_Init() {
|
51
51
|
|
52
52
|
// Enable transparency
|
53
53
|
glEnable(GL_BLEND);
|
@@ -99,9 +99,9 @@ int S2D_GLES_Init() {
|
|
99
99
|
"}";
|
100
100
|
|
101
101
|
// Load the vertex and fragment shaders
|
102
|
-
GLuint vertexShader =
|
103
|
-
GLuint fragmentShader =
|
104
|
-
GLuint texFragmentShader =
|
102
|
+
GLuint vertexShader = R2D_GL_LoadShader( GL_VERTEX_SHADER, vertexSource, "GLES Vertex");
|
103
|
+
GLuint fragmentShader = R2D_GL_LoadShader(GL_FRAGMENT_SHADER, fragmentSource, "GLES Fragment");
|
104
|
+
GLuint texFragmentShader = R2D_GL_LoadShader(GL_FRAGMENT_SHADER, texFragmentSource, "GLES Texture Fragment");
|
105
105
|
|
106
106
|
// Triangle Shader //
|
107
107
|
|
@@ -110,7 +110,7 @@ int S2D_GLES_Init() {
|
|
110
110
|
|
111
111
|
// Check if program was created successfully
|
112
112
|
if (shaderProgram == 0) {
|
113
|
-
|
113
|
+
R2D_GL_PrintError("Failed to create shader program");
|
114
114
|
return GL_FALSE;
|
115
115
|
}
|
116
116
|
|
@@ -122,7 +122,7 @@ int S2D_GLES_Init() {
|
|
122
122
|
glLinkProgram(shaderProgram);
|
123
123
|
|
124
124
|
// Check if linked
|
125
|
-
|
125
|
+
R2D_GL_CheckLinked(shaderProgram, "GLES shader");
|
126
126
|
|
127
127
|
// Get the attribute locations
|
128
128
|
positionLocation = glGetAttribLocation(shaderProgram, "a_position");
|
@@ -135,7 +135,7 @@ int S2D_GLES_Init() {
|
|
135
135
|
|
136
136
|
// Check if program was created successfully
|
137
137
|
if (texShaderProgram == 0) {
|
138
|
-
|
138
|
+
R2D_GL_PrintError("Failed to create shader program");
|
139
139
|
return GL_FALSE;
|
140
140
|
}
|
141
141
|
|
@@ -147,7 +147,7 @@ int S2D_GLES_Init() {
|
|
147
147
|
glLinkProgram(texShaderProgram);
|
148
148
|
|
149
149
|
// Check if linked
|
150
|
-
|
150
|
+
R2D_GL_CheckLinked(texShaderProgram, "GLES texture shader");
|
151
151
|
|
152
152
|
// Get the attribute locations
|
153
153
|
texPositionLocation = glGetAttribLocation(texShaderProgram, "a_position");
|
@@ -169,7 +169,7 @@ int S2D_GLES_Init() {
|
|
169
169
|
/*
|
170
170
|
* Draw triangle
|
171
171
|
*/
|
172
|
-
void
|
172
|
+
void R2D_GLES_DrawTriangle(GLfloat x1, GLfloat y1,
|
173
173
|
GLfloat r1, GLfloat g1, GLfloat b1, GLfloat a1,
|
174
174
|
GLfloat x2, GLfloat y2,
|
175
175
|
GLfloat r2, GLfloat g2, GLfloat b2, GLfloat a2,
|
@@ -202,40 +202,22 @@ void S2D_GLES_DrawTriangle(GLfloat x1, GLfloat y1,
|
|
202
202
|
|
203
203
|
|
204
204
|
/*
|
205
|
-
* Draw a texture
|
205
|
+
* Draw a texture (New method with vertices pre-calculated)
|
206
206
|
*/
|
207
|
-
|
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
|
-
|
207
|
+
void R2D_GLES_DrawTexture(GLfloat coordinates[], GLfloat texture_coordinates[], GLfloat color[], int texture_id);
|
227
208
|
GLfloat vertices[] =
|
228
209
|
// x, y coords | x, y texture coords
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
210
|
+
{
|
211
|
+
coordinates[0], coordinates[1], 0.f, texture_coordinates[0], texture_coordinates[1],
|
212
|
+
coordinates[2], coordinates[3], 0.f, texture_coordinates[2], texture_coordinates[3],
|
213
|
+
coordinates[4], coordinates[5], 0.f, texture_coordinates[4], texture_coordinates[5],
|
214
|
+
coordinates[6], coordinates[7], 0.f, texture_coordinates[6], texture_coordinates[7] };
|
233
215
|
|
234
216
|
GLfloat colors[] =
|
235
|
-
{
|
236
|
-
|
237
|
-
|
238
|
-
|
217
|
+
{ color[0], color[1], color[2], color[3],
|
218
|
+
color[0], color[1], color[2], color[3],
|
219
|
+
color[0], color[1], color[2], color[3],
|
220
|
+
color[0], color[1], color[2], color[3] };
|
239
221
|
|
240
222
|
glUseProgram(texShaderProgram);
|
241
223
|
|
@@ -264,45 +246,4 @@ static void S2D_GLES_DrawTexture(int x, int y, int w, int h,
|
|
264
246
|
}
|
265
247
|
|
266
248
|
|
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
249
|
#endif
|
data/ext/ruby2d/image.c
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
// image.c
|
2
|
+
|
3
|
+
#include "ruby2d.h"
|
4
|
+
|
5
|
+
|
6
|
+
SDL_Surface *R2D_CreateImageSurface(const char *path) {
|
7
|
+
R2D_Init();
|
8
|
+
|
9
|
+
// Check if image file exists
|
10
|
+
if (!R2D_FileExists(path)) {
|
11
|
+
R2D_Error("R2D_CreateImageSurface", "Image file `%s` not found", path);
|
12
|
+
return NULL;
|
13
|
+
}
|
14
|
+
|
15
|
+
// Load image from file as SDL_Surface
|
16
|
+
SDL_Surface *surface = IMG_Load(path);
|
17
|
+
|
18
|
+
int bits_per_color = surface->format->Amask == 0 ?
|
19
|
+
surface->format->BitsPerPixel / 3 :
|
20
|
+
surface->format->BitsPerPixel / 4;
|
21
|
+
|
22
|
+
if (bits_per_color < 8) {
|
23
|
+
R2D_Log(R2D_WARN, "`%s` has less than 8 bits per color and will likely not render correctly", path, bits_per_color);
|
24
|
+
}
|
25
|
+
|
26
|
+
return surface;
|
27
|
+
}
|
28
|
+
|
29
|
+
void R2D_ImageConvertToRGB(SDL_Surface *surface) {
|
30
|
+
Uint32 r = surface->format->Rmask;
|
31
|
+
Uint32 g = surface->format->Gmask;
|
32
|
+
Uint32 a = surface->format->Amask;
|
33
|
+
|
34
|
+
if (r&0xFF000000 || r&0xFF0000) {
|
35
|
+
char *p = (char *)surface->pixels;
|
36
|
+
int bpp = surface->format->BytesPerPixel;
|
37
|
+
int w = surface->w;
|
38
|
+
int h = surface->h;
|
39
|
+
char tmp;
|
40
|
+
for (int i = 0; i < bpp * w * h; i += bpp) {
|
41
|
+
if (a&0xFF) {
|
42
|
+
tmp = p[i];
|
43
|
+
p[i] = p[i+3];
|
44
|
+
p[i+3] = tmp;
|
45
|
+
}
|
46
|
+
if (g&0xFF0000) {
|
47
|
+
tmp = p[i+1];
|
48
|
+
p[i+1] = p[i+2];
|
49
|
+
p[i+2] = tmp;
|
50
|
+
}
|
51
|
+
if (r&0xFF0000) {
|
52
|
+
tmp = p[i];
|
53
|
+
p[i] = p[i+2];
|
54
|
+
p[i+2] = tmp;
|
55
|
+
}
|
56
|
+
}
|
57
|
+
}
|
58
|
+
}
|
@@ -1,30 +1,30 @@
|
|
1
1
|
// input.c
|
2
2
|
|
3
|
-
#include "
|
3
|
+
#include "ruby2d.h"
|
4
4
|
|
5
5
|
|
6
6
|
/*
|
7
7
|
* Get the mouse coordinates relative to the viewport
|
8
8
|
*/
|
9
|
-
void
|
9
|
+
void R2D_GetMouseOnViewport(R2D_Window *window, int wx, int wy, int *x, int *y) {
|
10
10
|
|
11
11
|
double scale; // viewport scale factor
|
12
12
|
int w, h; // width and height of scaled viewport
|
13
13
|
|
14
14
|
switch (window->viewport.mode) {
|
15
15
|
|
16
|
-
case
|
16
|
+
case R2D_FIXED: case R2D_EXPAND:
|
17
17
|
*x = wx / (window->orig_width / (double)window->viewport.width);
|
18
18
|
*y = wy / (window->orig_height / (double)window->viewport.height);
|
19
19
|
break;
|
20
20
|
|
21
|
-
case
|
22
|
-
|
21
|
+
case R2D_SCALE:
|
22
|
+
R2D_GL_GetViewportScale(window, &w, &h, &scale);
|
23
23
|
*x = wx * 1 / scale - (window->width - w) / (2.0 * scale);
|
24
24
|
*y = wy * 1 / scale - (window->height - h) / (2.0 * scale);
|
25
25
|
break;
|
26
26
|
|
27
|
-
case
|
27
|
+
case R2D_STRETCH:
|
28
28
|
*x = wx * window->viewport.width / (double)window->width;
|
29
29
|
*y = wy * window->viewport.height / (double)window->height;
|
30
30
|
break;
|
@@ -35,7 +35,7 @@ void S2D_GetMouseOnViewport(S2D_Window *window, int wx, int wy, int *x, int *y)
|
|
35
35
|
/*
|
36
36
|
* Show the cursor over the window
|
37
37
|
*/
|
38
|
-
void
|
38
|
+
void R2D_ShowCursor() {
|
39
39
|
SDL_ShowCursor(SDL_ENABLE);
|
40
40
|
}
|
41
41
|
|
@@ -43,6 +43,6 @@ void S2D_ShowCursor() {
|
|
43
43
|
/*
|
44
44
|
* Hide the cursor over the window
|
45
45
|
*/
|
46
|
-
void
|
46
|
+
void R2D_HideCursor() {
|
47
47
|
SDL_ShowCursor(SDL_DISABLE);
|
48
48
|
}
|