ruby2d 0.9.2 → 0.10.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/assets/README.md +7 -8
- data/assets/Rakefile +85 -0
- data/assets/include/SDL2/SDL.h +4 -1
- data/assets/include/SDL2/SDL_assert.h +3 -1
- data/assets/include/SDL2/SDL_atomic.h +20 -2
- data/assets/include/SDL2/SDL_audio.h +47 -14
- data/assets/include/SDL2/SDL_bits.h +10 -1
- data/assets/include/SDL2/SDL_blendmode.h +10 -7
- data/assets/include/SDL2/SDL_clipboard.h +1 -1
- data/assets/include/SDL2/SDL_config.h +24 -390
- data/assets/include/SDL2/SDL_config_android.h +182 -0
- data/assets/include/SDL2/SDL_config_iphoneos.h +207 -0
- data/assets/include/SDL2/SDL_config_macosx.h +266 -0
- data/assets/include/SDL2/SDL_config_minimal.h +85 -0
- data/assets/include/SDL2/SDL_config_os2.h +188 -0
- data/assets/include/SDL2/SDL_config_pandora.h +135 -0
- data/assets/include/SDL2/SDL_config_psp.h +165 -0
- data/assets/include/SDL2/SDL_config_windows.h +288 -0
- data/assets/include/SDL2/SDL_config_winrt.h +243 -0
- data/assets/include/SDL2/SDL_config_wiz.h +149 -0
- data/assets/include/SDL2/SDL_copying.h +20 -0
- data/assets/include/SDL2/SDL_cpuinfo.h +119 -8
- 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 +67 -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 +460 -17
- data/assets/include/SDL2/SDL_image.h +2 -2
- 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 +28 -16
- 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 +27 -18
- 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 +29 -3
- data/assets/include/SDL2/SDL_render.h +230 -3
- data/assets/include/SDL2/SDL_revision.h +2 -2
- data/assets/include/SDL2/SDL_rwops.h +51 -22
- data/assets/include/SDL2/SDL_scancode.h +2 -2
- data/assets/include/SDL2/SDL_sensor.h +28 -12
- 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 +12 -2
- data/assets/include/SDL2/SDL_system.h +50 -4
- data/assets/include/SDL2/SDL_syswm.h +39 -9
- 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 +32 -2
- 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 +3 -3
- 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 +17 -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 +9 -11
- data/assets/include/SDL2/begin_code.h +8 -9
- data/assets/include/SDL2/close_code.h +4 -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/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/bin/SDL2_image.dll +0 -0
- data/assets/mingw/bin/libpng16-16.dll +0 -0
- data/assets/mingw/bin/libtiff-5.dll +0 -0
- data/assets/mingw/bin/libwebp-7.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_image.a +0 -0
- data/assets/mingw/lib/libSDL2_image.dll.a +0 -0
- data/assets/mingw/lib/libSDL2_test.a +0 -0
- data/assets/mingw/lib/libSDL2main.a +0 -0
- data/bin/ruby2d +2 -1
- 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 +6 -36
- data/{assets/linux/simple2d/src → ext/ruby2d}/gl.c +106 -85
- data/{assets/linux/simple2d/src → ext/ruby2d}/gl2.c +35 -20
- data/{assets/linux/simple2d/src → ext/ruby2d}/gl3.c +146 -58
- data/{assets/linux/simple2d/src → ext/ruby2d}/gles.c +41 -26
- data/{assets/linux/simple2d/src → ext/ruby2d}/image.c +16 -16
- 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 +427 -290
- data/ext/ruby2d/ruby2d.h +789 -0
- data/{assets/linux/simple2d/src → ext/ruby2d}/shapes.c +18 -18
- data/ext/ruby2d/sound.c +118 -0
- data/{assets/linux/simple2d/src → ext/ruby2d}/sprite.c +16 -16
- data/{assets/linux/simple2d/src → ext/ruby2d}/text.c +22 -22
- data/ext/ruby2d/tileset.c +30 -0
- data/{assets/linux/simple2d/src → ext/ruby2d}/window.c +71 -60
- data/lib/ruby2d.rb +5 -2
- data/lib/ruby2d/circle.rb +18 -2
- data/lib/ruby2d/cli/build.rb +3 -8
- data/lib/ruby2d/cli/colorize.rb +10 -0
- data/lib/ruby2d/dsl.rb +16 -9
- data/lib/ruby2d/entity.rb +17 -0
- data/lib/ruby2d/font.rb +4 -3
- data/lib/ruby2d/image.rb +24 -1
- data/lib/ruby2d/line.rb +20 -0
- data/lib/ruby2d/music.rb +5 -0
- data/lib/ruby2d/pixel.rb +17 -0
- data/lib/ruby2d/quad.rb +18 -0
- data/lib/ruby2d/rectangle.rb +11 -2
- data/lib/ruby2d/renderable.rb +4 -8
- data/lib/ruby2d/sound.rb +5 -0
- data/lib/ruby2d/sprite.rb +35 -3
- data/lib/ruby2d/square.rb +9 -0
- data/lib/ruby2d/text.rb +22 -1
- data/lib/ruby2d/tileset.rb +69 -0
- data/lib/ruby2d/triangle.rb +16 -0
- data/lib/ruby2d/version.rb +1 -1
- data/lib/ruby2d/window.rb +249 -22
- metadata +41 -27
- data/assets/include/simple2d.h +0 -735
- data/assets/linux/simple2d/Makefile +0 -250
- data/assets/linux/simple2d/bin/simple2d.sh +0 -1249
- data/assets/linux/simple2d/include/simple2d.h +0 -735
- data/assets/linux/simple2d/src/sound.c +0 -56
- data/assets/macos/lib/libsimple2d.a +0 -0
- data/assets/mingw/lib/libsimple2d.a +0 -0
- data/lib/ruby2d/colorize.rb +0 -10
@@ -1,12 +1,19 @@
|
|
1
|
-
// OpenGL 3.3
|
1
|
+
// OpenGL 3.3+
|
2
2
|
|
3
|
-
#include "
|
3
|
+
#include "ruby2d.h"
|
4
4
|
|
5
|
+
// Skip this file if OpenGL ES
|
5
6
|
#if !GLES
|
6
7
|
|
7
|
-
static GLuint
|
8
|
-
static GLuint
|
9
|
-
static
|
8
|
+
static GLuint vbo; // our primary vertex buffer object (VBO)
|
9
|
+
static GLuint vboSize; // size of the VBO in bytes
|
10
|
+
static GLfloat *vboData; // pointer to the VBO data
|
11
|
+
static GLfloat *vboDataCurrent; // pointer to the data for the current vertices
|
12
|
+
static GLuint vboDataIndex = 0; // index of the current object being rendered
|
13
|
+
static GLuint vboObjCapacity = 2500; // number of objects the VBO can store
|
14
|
+
static GLuint shaderProgram; // triangle shader program
|
15
|
+
static GLuint texShaderProgram; // texture shader program
|
16
|
+
static GLuint indices[] = // indices for rendering textured quads
|
10
17
|
{ 0, 1, 2,
|
11
18
|
2, 3, 0 };
|
12
19
|
|
@@ -14,11 +21,12 @@ static GLuint indices[] =
|
|
14
21
|
/*
|
15
22
|
* Applies the projection matrix
|
16
23
|
*/
|
17
|
-
void
|
24
|
+
void R2D_GL3_ApplyProjection(GLfloat orthoMatrix[16]) {
|
18
25
|
|
19
26
|
// Use the program object
|
20
27
|
glUseProgram(shaderProgram);
|
21
28
|
|
29
|
+
// Apply the projection matrix to the triangle shader
|
22
30
|
glUniformMatrix4fv(
|
23
31
|
glGetUniformLocation(shaderProgram, "u_mvpMatrix"),
|
24
32
|
1, GL_FALSE, orthoMatrix
|
@@ -27,6 +35,7 @@ void S2D_GL3_ApplyProjection(GLfloat orthoMatrix[16]) {
|
|
27
35
|
// Use the texture program object
|
28
36
|
glUseProgram(texShaderProgram);
|
29
37
|
|
38
|
+
// Apply the projection matrix to the texture shader
|
30
39
|
glUniformMatrix4fv(
|
31
40
|
glGetUniformLocation(texShaderProgram, "u_mvpMatrix"),
|
32
41
|
1, GL_FALSE, orthoMatrix
|
@@ -37,7 +46,7 @@ void S2D_GL3_ApplyProjection(GLfloat orthoMatrix[16]) {
|
|
37
46
|
/*
|
38
47
|
* Initalize OpenGL
|
39
48
|
*/
|
40
|
-
int
|
49
|
+
int R2D_GL3_Init() {
|
41
50
|
|
42
51
|
// Enable transparency
|
43
52
|
glEnable(GL_BLEND);
|
@@ -45,65 +54,80 @@ int S2D_GL3_Init() {
|
|
45
54
|
|
46
55
|
// Vertex shader source string
|
47
56
|
GLchar vertexSource[] =
|
48
|
-
"#version 150 core\n"
|
49
|
-
|
50
|
-
"
|
51
|
-
|
52
|
-
|
53
|
-
"
|
54
|
-
"
|
57
|
+
"#version 150 core\n" // shader version
|
58
|
+
|
59
|
+
"uniform mat4 u_mvpMatrix;" // projection matrix
|
60
|
+
|
61
|
+
// Input attributes to the vertex shader
|
62
|
+
"in vec4 position;" // position value
|
63
|
+
"in vec4 color;" // vertex color
|
64
|
+
"in vec2 texcoord;" // texture coordinates
|
65
|
+
|
66
|
+
// Outputs to the fragment shader
|
67
|
+
"out vec4 Color;" // vertex color
|
68
|
+
"out vec2 Texcoord;" // texture coordinates
|
69
|
+
|
55
70
|
"void main() {"
|
71
|
+
// Send the color and texture coordinates right through to the fragment shader
|
56
72
|
" Color = color;"
|
57
73
|
" Texcoord = texcoord;"
|
74
|
+
// Transform the vertex position using the projection matrix
|
58
75
|
" gl_Position = u_mvpMatrix * position;"
|
59
76
|
"}";
|
60
77
|
|
61
78
|
// Fragment shader source string
|
62
79
|
GLchar fragmentSource[] =
|
63
|
-
"#version 150 core\n"
|
64
|
-
"in vec4 Color;"
|
65
|
-
"out vec4 outColor;"
|
80
|
+
"#version 150 core\n" // shader version
|
81
|
+
"in vec4 Color;" // input color from vertex shader
|
82
|
+
"out vec4 outColor;" // output fragment color
|
83
|
+
|
66
84
|
"void main() {"
|
67
|
-
" outColor = Color;"
|
85
|
+
" outColor = Color;" // pass the color right through
|
68
86
|
"}";
|
69
87
|
|
70
88
|
// Fragment shader source string for textures
|
71
89
|
GLchar texFragmentSource[] =
|
72
|
-
"#version 150 core\n"
|
73
|
-
"in vec4 Color;"
|
74
|
-
"in vec2 Texcoord;"
|
75
|
-
"out vec4 outColor;"
|
76
|
-
"uniform sampler2D tex;"
|
90
|
+
"#version 150 core\n" // shader version
|
91
|
+
"in vec4 Color;" // input color from vertex shader
|
92
|
+
"in vec2 Texcoord;" // input texture coordinates
|
93
|
+
"out vec4 outColor;" // output fragment color
|
94
|
+
"uniform sampler2D tex;" // 2D texture unit
|
95
|
+
|
77
96
|
"void main() {"
|
97
|
+
// Apply the texture unit, texture coordinates, and color
|
78
98
|
" outColor = texture(tex, Texcoord) * Color;"
|
79
99
|
"}";
|
80
100
|
|
81
|
-
// Create
|
101
|
+
// Create a vertex array object
|
82
102
|
GLuint vao;
|
83
103
|
glGenVertexArrays(1, &vao);
|
84
104
|
glBindVertexArray(vao);
|
85
105
|
|
86
|
-
// Create
|
87
|
-
GLuint vbo;
|
106
|
+
// Create a vertex buffer object and allocate data
|
88
107
|
glGenBuffers(1, &vbo);
|
89
108
|
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
109
|
+
vboSize = vboObjCapacity * sizeof(GLfloat) * 24;
|
110
|
+
vboData = (GLfloat *) malloc(vboSize);
|
111
|
+
vboDataCurrent = vboData;
|
90
112
|
|
91
|
-
// Create an element
|
113
|
+
// Create an element buffer object
|
92
114
|
GLuint ebo;
|
93
115
|
glGenBuffers(1, &ebo);
|
94
116
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
|
95
117
|
|
96
118
|
// Load the vertex and fragment shaders
|
97
|
-
GLuint vertexShader =
|
98
|
-
GLuint fragmentShader =
|
99
|
-
GLuint texFragmentShader =
|
119
|
+
GLuint vertexShader = R2D_GL_LoadShader( GL_VERTEX_SHADER, vertexSource, "GL3 Vertex");
|
120
|
+
GLuint fragmentShader = R2D_GL_LoadShader(GL_FRAGMENT_SHADER, fragmentSource, "GL3 Fragment");
|
121
|
+
GLuint texFragmentShader = R2D_GL_LoadShader(GL_FRAGMENT_SHADER, texFragmentSource, "GL3 Texture Fragment");
|
122
|
+
|
123
|
+
// Triangle Shader //
|
100
124
|
|
101
125
|
// Create the shader program object
|
102
126
|
shaderProgram = glCreateProgram();
|
103
127
|
|
104
128
|
// Check if program was created successfully
|
105
129
|
if (shaderProgram == 0) {
|
106
|
-
|
130
|
+
R2D_GL_PrintError("Failed to create shader program");
|
107
131
|
return GL_FALSE;
|
108
132
|
}
|
109
133
|
|
@@ -111,52 +135,60 @@ int S2D_GL3_Init() {
|
|
111
135
|
glAttachShader(shaderProgram, vertexShader);
|
112
136
|
glAttachShader(shaderProgram, fragmentShader);
|
113
137
|
|
114
|
-
// Bind the
|
138
|
+
// Bind the output color variable to the fragment shader color number
|
115
139
|
glBindFragDataLocation(shaderProgram, 0, "outColor");
|
116
140
|
|
117
141
|
// Link the shader program
|
118
142
|
glLinkProgram(shaderProgram);
|
119
143
|
|
120
144
|
// Check if linked
|
121
|
-
|
145
|
+
R2D_GL_CheckLinked(shaderProgram, "GL3 shader");
|
122
146
|
|
123
|
-
// Specify the layout of the vertex data
|
147
|
+
// Specify the layout of the position vertex data...
|
124
148
|
GLint posAttrib = glGetAttribLocation(shaderProgram, "position");
|
125
|
-
glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), 0);
|
126
149
|
glEnableVertexAttribArray(posAttrib);
|
150
|
+
glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), 0);
|
127
151
|
|
152
|
+
// ...and the color vertex data
|
128
153
|
GLint colAttrib = glGetAttribLocation(shaderProgram, "color");
|
129
|
-
glVertexAttribPointer(colAttrib, 4, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (void*)(2 * sizeof(GLfloat)));
|
130
154
|
glEnableVertexAttribArray(colAttrib);
|
155
|
+
glVertexAttribPointer(colAttrib, 4, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (void*)(2 * sizeof(GLfloat)));
|
156
|
+
|
157
|
+
// Texture Shader //
|
131
158
|
|
132
159
|
// Create the texture shader program object
|
133
160
|
texShaderProgram = glCreateProgram();
|
134
161
|
|
135
162
|
// Check if program was created successfully
|
136
163
|
if (texShaderProgram == 0) {
|
137
|
-
|
164
|
+
R2D_GL_PrintError("Failed to create shader program");
|
138
165
|
return GL_FALSE;
|
139
166
|
}
|
140
167
|
|
168
|
+
// Attach the shader objects to the program object
|
141
169
|
glAttachShader(texShaderProgram, vertexShader);
|
142
170
|
glAttachShader(texShaderProgram, texFragmentShader);
|
143
171
|
|
172
|
+
// Bind the output color variable to the fragment shader color number
|
144
173
|
glBindFragDataLocation(texShaderProgram, 0, "outColor");
|
145
174
|
|
175
|
+
// Link the shader program
|
146
176
|
glLinkProgram(texShaderProgram);
|
147
177
|
|
148
178
|
// Check if linked
|
149
|
-
|
179
|
+
R2D_GL_CheckLinked(texShaderProgram, "GL3 texture shader");
|
150
180
|
|
151
|
-
// Specify the layout of the vertex data
|
181
|
+
// Specify the layout of the position vertex data...
|
152
182
|
posAttrib = glGetAttribLocation(texShaderProgram, "position");
|
153
183
|
glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), 0);
|
154
184
|
glEnableVertexAttribArray(posAttrib);
|
155
185
|
|
186
|
+
// ...and the color vertex data...
|
156
187
|
colAttrib = glGetAttribLocation(texShaderProgram, "color");
|
157
188
|
glVertexAttribPointer(colAttrib, 4, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (void*)(2 * sizeof(GLfloat)));
|
158
189
|
glEnableVertexAttribArray(colAttrib);
|
159
190
|
|
191
|
+
// ...and the texture coordinates
|
160
192
|
GLint texAttrib = glGetAttribLocation(texShaderProgram, "texcoord");
|
161
193
|
glVertexAttribPointer(texAttrib, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (void*)(6 * sizeof(GLfloat)));
|
162
194
|
glEnableVertexAttribArray(texAttrib);
|
@@ -166,54 +198,90 @@ int S2D_GL3_Init() {
|
|
166
198
|
glDeleteShader(fragmentShader);
|
167
199
|
glDeleteShader(texFragmentShader);
|
168
200
|
|
201
|
+
// If successful, return true
|
169
202
|
return GL_TRUE;
|
170
203
|
}
|
171
204
|
|
172
205
|
|
206
|
+
/*
|
207
|
+
* Render the vertex buffer and reset it
|
208
|
+
*/
|
209
|
+
void R2D_GL3_FlushBuffers() {
|
210
|
+
|
211
|
+
// Use the triangle shader program
|
212
|
+
glUseProgram(shaderProgram);
|
213
|
+
|
214
|
+
// Bind to the vertex buffer object and update its data
|
215
|
+
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
216
|
+
glBufferData(GL_ARRAY_BUFFER, vboSize, NULL, GL_DYNAMIC_DRAW);
|
217
|
+
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(GLfloat) * vboDataIndex * 24, vboData);
|
218
|
+
|
219
|
+
// Render all the triangles in the buffer
|
220
|
+
glDrawArrays(GL_TRIANGLES, 0, (GLsizei)(vboDataIndex * 3));
|
221
|
+
|
222
|
+
// Reset the buffer object index and data pointer
|
223
|
+
vboDataIndex = 0;
|
224
|
+
vboDataCurrent = vboData;
|
225
|
+
}
|
226
|
+
|
227
|
+
|
173
228
|
/*
|
174
229
|
* Draw triangle
|
175
230
|
*/
|
176
|
-
void
|
231
|
+
void R2D_GL3_DrawTriangle(GLfloat x1, GLfloat y1,
|
177
232
|
GLfloat r1, GLfloat g1, GLfloat b1, GLfloat a1,
|
178
233
|
GLfloat x2, GLfloat y2,
|
179
234
|
GLfloat r2, GLfloat g2, GLfloat b2, GLfloat a2,
|
180
235
|
GLfloat x3, GLfloat y3,
|
181
236
|
GLfloat r3, GLfloat g3, GLfloat b3, GLfloat a3) {
|
182
237
|
|
238
|
+
// If buffer is full, flush it
|
239
|
+
if (vboDataIndex >= vboObjCapacity) R2D_GL3_FlushBuffers();
|
240
|
+
|
241
|
+
// Set the triangle data into a formatted array
|
183
242
|
GLfloat vertices[] =
|
184
243
|
{ x1, y1, r1, g1, b1, a1, 0, 0,
|
185
244
|
x2, y2, r2, g2, b2, a2, 0, 0,
|
186
245
|
x3, y3, r3, g3, b3, a3, 0, 0 };
|
187
246
|
|
188
|
-
|
189
|
-
|
190
|
-
|
247
|
+
// Copy the vertex data into the current position of the buffer
|
248
|
+
memcpy(vboDataCurrent, vertices, sizeof(vertices));
|
249
|
+
|
250
|
+
// Increment the buffer object index and the vertex data pointer for next use
|
251
|
+
vboDataIndex++;
|
252
|
+
vboDataCurrent = (GLfloat *)((char *)vboDataCurrent + (sizeof(GLfloat) * 24));
|
191
253
|
}
|
192
254
|
|
193
255
|
|
194
256
|
/*
|
195
257
|
* Draw a texture
|
196
258
|
*/
|
197
|
-
static void
|
259
|
+
static void R2D_GL3_DrawTexture(int x, int y, int w, int h,
|
198
260
|
GLfloat angle, GLfloat rx, GLfloat ry,
|
199
261
|
GLfloat r, GLfloat g, GLfloat b, GLfloat a,
|
200
262
|
GLfloat tx1, GLfloat ty1, GLfloat tx2, GLfloat ty2,
|
201
263
|
GLfloat tx3, GLfloat ty3, GLfloat tx4, GLfloat ty4,
|
202
264
|
GLuint texture_id) {
|
203
265
|
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
266
|
+
// Currently, textures are not buffered, so we have to flush all buffers so
|
267
|
+
// textures get rendered in the correct Z order
|
268
|
+
R2D_GL3_FlushBuffers();
|
269
|
+
|
270
|
+
// Set up the vertex points
|
271
|
+
R2D_GL_Point v1 = { .x = x, .y = y };
|
272
|
+
R2D_GL_Point v2 = { .x = x + w, .y = y };
|
273
|
+
R2D_GL_Point v3 = { .x = x + w, .y = y + h };
|
274
|
+
R2D_GL_Point v4 = { .x = x, .y = y + h };
|
208
275
|
|
209
276
|
// Rotate vertices
|
210
277
|
if (angle != 0) {
|
211
|
-
v1 =
|
212
|
-
v2 =
|
213
|
-
v3 =
|
214
|
-
v4 =
|
278
|
+
v1 = R2D_RotatePoint(v1, angle, rx, ry);
|
279
|
+
v2 = R2D_RotatePoint(v2, angle, rx, ry);
|
280
|
+
v3 = R2D_RotatePoint(v3, angle, rx, ry);
|
281
|
+
v4 = R2D_RotatePoint(v4, angle, rx, ry);
|
215
282
|
}
|
216
283
|
|
284
|
+
// Set the textured quad data into a formatted array
|
217
285
|
GLfloat vertices[] =
|
218
286
|
// vertex coords | colors | x, y texture coords
|
219
287
|
{ v1.x, v1.y, r, g, b, a, tx1, ty1, // Top-left
|
@@ -221,12 +289,17 @@ static void S2D_GL3_DrawTexture(int x, int y, int w, int h,
|
|
221
289
|
v3.x, v3.y, r, g, b, a, tx3, ty3, // Bottom-right
|
222
290
|
v4.x, v4.y, r, g, b, a, tx4, ty4 }; // Bottom-left
|
223
291
|
|
292
|
+
// Use the texture shader program
|
224
293
|
glUseProgram(texShaderProgram);
|
294
|
+
|
295
|
+
// Bind the texture using the provided ID
|
225
296
|
glBindTexture(GL_TEXTURE_2D, texture_id);
|
226
297
|
|
298
|
+
// Create and Initialize the vertex data and array indices
|
227
299
|
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
228
300
|
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
|
229
301
|
|
302
|
+
// Render the textured quad
|
230
303
|
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
|
231
304
|
}
|
232
305
|
|
@@ -234,8 +307,8 @@ static void S2D_GL3_DrawTexture(int x, int y, int w, int h,
|
|
234
307
|
/*
|
235
308
|
* Draw image
|
236
309
|
*/
|
237
|
-
void
|
238
|
-
|
310
|
+
void R2D_GL3_DrawImage(R2D_Image *img) {
|
311
|
+
R2D_GL3_DrawTexture(
|
239
312
|
img->x, img->y, img->width, img->height,
|
240
313
|
img->rotate, img->rx, img->ry,
|
241
314
|
img->color.r, img->color.g, img->color.b, img->color.a,
|
@@ -248,8 +321,8 @@ void S2D_GL3_DrawImage(S2D_Image *img) {
|
|
248
321
|
/*
|
249
322
|
* Draw sprite
|
250
323
|
*/
|
251
|
-
void
|
252
|
-
|
324
|
+
void R2D_GL3_DrawSprite(R2D_Sprite *spr) {
|
325
|
+
R2D_GL3_DrawTexture(
|
253
326
|
spr->x, spr->y, spr->width, spr->height,
|
254
327
|
spr->rotate, spr->rx, spr->ry,
|
255
328
|
spr->color.r, spr->color.g, spr->color.b, spr->color.a,
|
@@ -259,11 +332,26 @@ void S2D_GL3_DrawSprite(S2D_Sprite *spr) {
|
|
259
332
|
}
|
260
333
|
|
261
334
|
|
335
|
+
/*
|
336
|
+
* Draw a tile
|
337
|
+
*/
|
338
|
+
void R2D_GL3_DrawTile(R2D_Image *img, int x, int y, int tw, int th, GLfloat tx1, GLfloat ty1, GLfloat tx2,
|
339
|
+
GLfloat ty2, GLfloat tx3, GLfloat ty3, GLfloat tx4, GLfloat ty4) {
|
340
|
+
R2D_GL3_DrawTexture(
|
341
|
+
x, y, tw, th,
|
342
|
+
img->rotate, img->rx, img->ry,
|
343
|
+
img->color.r, img->color.g, img->color.b, img->color.a,
|
344
|
+
tx1, ty1, tx2, ty2, tx3, ty3, tx4, ty4,
|
345
|
+
img->texture_id
|
346
|
+
);
|
347
|
+
}
|
348
|
+
|
349
|
+
|
262
350
|
/*
|
263
351
|
* Draw text
|
264
352
|
*/
|
265
|
-
void
|
266
|
-
|
353
|
+
void R2D_GL3_DrawText(R2D_Text *txt) {
|
354
|
+
R2D_GL3_DrawTexture(
|
267
355
|
txt->x, txt->y, txt->width, txt->height,
|
268
356
|
txt->rotate, txt->rx, txt->ry,
|
269
357
|
txt->color.r, txt->color.g, txt->color.b, txt->color.a,
|
@@ -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,
|
@@ -204,24 +204,24 @@ void S2D_GLES_DrawTriangle(GLfloat x1, GLfloat y1,
|
|
204
204
|
/*
|
205
205
|
* Draw a texture
|
206
206
|
*/
|
207
|
-
static void
|
207
|
+
static void R2D_GLES_DrawTexture(int x, int y, int w, int h,
|
208
208
|
GLfloat angle, GLfloat rx, GLfloat ry,
|
209
209
|
GLfloat r, GLfloat g, GLfloat b, GLfloat a,
|
210
210
|
GLfloat tx1, GLfloat ty1, GLfloat tx2, GLfloat ty2,
|
211
211
|
GLfloat tx3, GLfloat ty3, GLfloat tx4, GLfloat ty4,
|
212
212
|
GLuint texture_id) {
|
213
213
|
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
214
|
+
R2D_GL_Point v1 = { .x = x, .y = y };
|
215
|
+
R2D_GL_Point v2 = { .x = x + w, .y = y };
|
216
|
+
R2D_GL_Point v3 = { .x = x + w, .y = y + h };
|
217
|
+
R2D_GL_Point v4 = { .x = x, .y = y + h };
|
218
218
|
|
219
219
|
// Rotate vertices
|
220
220
|
if (angle != 0) {
|
221
|
-
v1 =
|
222
|
-
v2 =
|
223
|
-
v3 =
|
224
|
-
v4 =
|
221
|
+
v1 = R2D_RotatePoint(v1, angle, rx, ry);
|
222
|
+
v2 = R2D_RotatePoint(v2, angle, rx, ry);
|
223
|
+
v3 = R2D_RotatePoint(v3, angle, rx, ry);
|
224
|
+
v4 = R2D_RotatePoint(v4, angle, rx, ry);
|
225
225
|
}
|
226
226
|
|
227
227
|
GLfloat vertices[] =
|
@@ -267,8 +267,8 @@ static void S2D_GLES_DrawTexture(int x, int y, int w, int h,
|
|
267
267
|
/*
|
268
268
|
* Draw image
|
269
269
|
*/
|
270
|
-
void
|
271
|
-
|
270
|
+
void R2D_GLES_DrawImage(R2D_Image *img) {
|
271
|
+
R2D_GLES_DrawTexture(
|
272
272
|
img->x, img->y, img->width, img->height,
|
273
273
|
img->rotate, img->rx, img->ry,
|
274
274
|
img->color.r, img->color.g, img->color.b, img->color.a,
|
@@ -281,8 +281,8 @@ void S2D_GLES_DrawImage(S2D_Image *img) {
|
|
281
281
|
/*
|
282
282
|
* Draw sprite
|
283
283
|
*/
|
284
|
-
void
|
285
|
-
|
284
|
+
void R2D_GLES_DrawSprite(R2D_Sprite *spr) {
|
285
|
+
R2D_GLES_DrawTexture(
|
286
286
|
spr->x, spr->y, spr->width, spr->height,
|
287
287
|
spr->rotate, spr->rx, spr->ry,
|
288
288
|
spr->color.r, spr->color.g, spr->color.b, spr->color.a,
|
@@ -292,11 +292,26 @@ void S2D_GLES_DrawSprite(S2D_Sprite *spr) {
|
|
292
292
|
}
|
293
293
|
|
294
294
|
|
295
|
+
/*
|
296
|
+
* Draw a tile
|
297
|
+
*/
|
298
|
+
void R2D_GLES_DrawTile(R2D_Image *img, int x, int y, int tw, int th, GLfloat tx1, GLfloat ty1, GLfloat tx2,
|
299
|
+
GLfloat ty2, GLfloat tx3, GLfloat ty3, GLfloat tx4, GLfloat ty4) {
|
300
|
+
R2D_GLES_DrawTexture(
|
301
|
+
x, y, tw, th,
|
302
|
+
img->rotate, img->rx, img->ry,
|
303
|
+
img->color.r, img->color.g, img->color.b, img->color.a,
|
304
|
+
tx1, ty1, tx2, ty2, tx3, ty3, tx4, ty4,
|
305
|
+
img->texture_id
|
306
|
+
);
|
307
|
+
}
|
308
|
+
|
309
|
+
|
295
310
|
/*
|
296
311
|
* Draw text
|
297
312
|
*/
|
298
|
-
void
|
299
|
-
|
313
|
+
void R2D_GLES_DrawText(R2D_Text *txt) {
|
314
|
+
R2D_GLES_DrawTexture(
|
300
315
|
txt->x, txt->y, txt->width, txt->height,
|
301
316
|
txt->rotate, txt->rx, txt->ry,
|
302
317
|
txt->color.r, txt->color.g, txt->color.b, txt->color.a,
|