ruby2d 0.9.0 → 0.9.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/assets/README.md +8 -8
- data/assets/Rakefile +85 -0
- data/assets/include/SDL2/SDL.h +1 -1
- data/assets/include/SDL2/SDL_assert.h +1 -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 +7 -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 +178 -0
- data/assets/include/SDL2/SDL_config_iphoneos.h +201 -0
- data/assets/include/SDL2/SDL_config_macosx.h +240 -0
- data/assets/include/SDL2/SDL_config_minimal.h +85 -0
- data/assets/include/SDL2/SDL_config_os2.h +170 -0
- data/assets/include/SDL2/SDL_config_pandora.h +133 -0
- data/assets/include/SDL2/SDL_config_psp.h +164 -0
- data/assets/include/SDL2/SDL_config_windows.h +257 -0
- data/assets/include/SDL2/SDL_config_winrt.h +240 -0
- data/assets/include/SDL2/SDL_config_wiz.h +146 -0
- data/assets/include/SDL2/SDL_copying.h +20 -0
- data/assets/include/SDL2/SDL_cpuinfo.h +90 -8
- data/assets/include/SDL2/SDL_egl.h +1 -1
- data/assets/include/SDL2/SDL_endian.h +1 -1
- data/assets/include/SDL2/SDL_error.h +1 -1
- data/assets/include/SDL2/SDL_events.h +4 -1
- data/assets/include/SDL2/SDL_filesystem.h +1 -1
- data/assets/include/SDL2/SDL_gamecontroller.h +1 -1
- data/assets/include/SDL2/SDL_gesture.h +1 -1
- data/assets/include/SDL2/SDL_haptic.h +1 -1
- data/assets/include/SDL2/SDL_hints.h +145 -14
- data/assets/include/SDL2/SDL_image.h +2 -2
- data/assets/include/SDL2/SDL_joystick.h +1 -1
- data/assets/include/SDL2/SDL_keyboard.h +1 -1
- data/assets/include/SDL2/SDL_keycode.h +1 -1
- data/assets/include/SDL2/SDL_loadso.h +1 -1
- data/assets/include/SDL2/SDL_log.h +1 -1
- data/assets/include/SDL2/SDL_main.h +28 -16
- data/assets/include/SDL2/SDL_messagebox.h +1 -1
- 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_opengles.h +1 -1
- data/assets/include/SDL2/SDL_opengles2.h +1 -1
- data/assets/include/SDL2/SDL_pixels.h +3 -3
- 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 +168 -1
- data/assets/include/SDL2/SDL_revision.h +2 -2
- data/assets/include/SDL2/SDL_rwops.h +50 -13
- data/assets/include/SDL2/SDL_scancode.h +2 -2
- data/assets/include/SDL2/SDL_sensor.h +4 -4
- data/assets/include/SDL2/SDL_shape.h +1 -1
- data/assets/include/SDL2/SDL_stdinc.h +1 -1
- data/assets/include/SDL2/SDL_surface.h +2 -1
- data/assets/include/SDL2/SDL_system.h +1 -1
- data/assets/include/SDL2/SDL_syswm.h +11 -8
- 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 +22 -5
- 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 +1 -1
- 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 +1 -1
- data/assets/include/SDL2/SDL_vulkan.h +3 -3
- data/assets/include/SDL2/begin_code.h +4 -1
- 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/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/ext/ruby2d/common.c +185 -0
- data/ext/ruby2d/controllers.c +110 -0
- data/ext/ruby2d/extconf.rb +97 -46
- data/ext/ruby2d/gl.c +430 -0
- data/ext/ruby2d/gl2.c +146 -0
- data/ext/ruby2d/gl3.c +348 -0
- data/ext/ruby2d/gles.c +308 -0
- data/ext/ruby2d/image.c +138 -0
- data/ext/ruby2d/input.c +48 -0
- data/ext/ruby2d/music.c +114 -0
- data/ext/ruby2d/ruby2d.c +125 -126
- data/{assets/include/simple2d.h → ext/ruby2d/ruby2d.h} +223 -201
- data/ext/ruby2d/shapes.c +154 -0
- data/ext/ruby2d/sound.c +93 -0
- data/ext/ruby2d/sprite.c +147 -0
- data/ext/ruby2d/text.c +129 -0
- data/ext/ruby2d/window.c +414 -0
- data/lib/ruby2d.rb +1 -1
- data/lib/ruby2d/cli/build.rb +3 -8
- data/lib/ruby2d/cli/colorize.rb +10 -0
- data/lib/ruby2d/version.rb +1 -1
- metadata +34 -10
- data/assets/macos/lib/libsimple2d.a +0 -0
- data/assets/mingw/lib/libsimple2d.a +0 -0
- data/lib/ruby2d/colorize.rb +0 -10
data/ext/ruby2d/gles.c
ADDED
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
// OpenGL ES 2.0
|
|
2
|
+
|
|
3
|
+
#include "ruby2d.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 R2D_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 R2D_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 = 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
|
+
|
|
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
|
+
R2D_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
|
+
R2D_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
|
+
R2D_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
|
+
R2D_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 R2D_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 R2D_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
|
+
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
|
+
|
|
219
|
+
// Rotate vertices
|
|
220
|
+
if (angle != 0) {
|
|
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
|
+
}
|
|
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 R2D_GLES_DrawImage(R2D_Image *img) {
|
|
271
|
+
R2D_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 R2D_GLES_DrawSprite(R2D_Sprite *spr) {
|
|
285
|
+
R2D_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 R2D_GLES_DrawText(R2D_Text *txt) {
|
|
299
|
+
R2D_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
|
data/ext/ruby2d/image.c
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
// image.c
|
|
2
|
+
|
|
3
|
+
#include "ruby2d.h"
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
/*
|
|
7
|
+
* Create an image, given a file path
|
|
8
|
+
*/
|
|
9
|
+
R2D_Image *R2D_CreateImage(const char *path) {
|
|
10
|
+
R2D_Init();
|
|
11
|
+
|
|
12
|
+
// Check if image file exists
|
|
13
|
+
if (!R2D_FileExists(path)) {
|
|
14
|
+
R2D_Error("R2D_CreateImage", "Image file `%s` not found", path);
|
|
15
|
+
return NULL;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// Allocate the image structure
|
|
19
|
+
R2D_Image *img = (R2D_Image *) malloc(sizeof(R2D_Image));
|
|
20
|
+
if (!img) {
|
|
21
|
+
R2D_Error("R2D_CreateImage", "Out of memory!");
|
|
22
|
+
return NULL;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// Load image from file as SDL_Surface
|
|
26
|
+
img->surface = IMG_Load(path);
|
|
27
|
+
if (!img->surface) {
|
|
28
|
+
R2D_Error("IMG_Load", IMG_GetError());
|
|
29
|
+
free(img);
|
|
30
|
+
return NULL;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
int bits_per_color = img->surface->format->Amask == 0 ?
|
|
34
|
+
img->surface->format->BitsPerPixel / 3 :
|
|
35
|
+
img->surface->format->BitsPerPixel / 4;
|
|
36
|
+
|
|
37
|
+
if (bits_per_color < 8) {
|
|
38
|
+
R2D_Log(R2D_WARN, "`%s` has less than 8 bits per color and will likely not render correctly", path, bits_per_color);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// Initialize values
|
|
42
|
+
img->path = path;
|
|
43
|
+
img->x = 0;
|
|
44
|
+
img->y = 0;
|
|
45
|
+
img->color.r = 1.f;
|
|
46
|
+
img->color.g = 1.f;
|
|
47
|
+
img->color.b = 1.f;
|
|
48
|
+
img->color.a = 1.f;
|
|
49
|
+
img->orig_width = img->surface->w;
|
|
50
|
+
img->orig_height = img->surface->h;
|
|
51
|
+
img->width = img->orig_width;
|
|
52
|
+
img->height = img->orig_height;
|
|
53
|
+
img->rotate = 0;
|
|
54
|
+
img->rx = 0;
|
|
55
|
+
img->ry = 0;
|
|
56
|
+
img->texture_id = 0;
|
|
57
|
+
|
|
58
|
+
// Detect image mode
|
|
59
|
+
img->format = GL_RGB;
|
|
60
|
+
if (img->surface->format->BytesPerPixel == 4) {
|
|
61
|
+
img->format = GL_RGBA;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// Flip image bits if BGA
|
|
65
|
+
|
|
66
|
+
Uint32 r = img->surface->format->Rmask;
|
|
67
|
+
Uint32 g = img->surface->format->Gmask;
|
|
68
|
+
Uint32 a = img->surface->format->Amask;
|
|
69
|
+
|
|
70
|
+
if (r&0xFF000000 || r&0xFF0000) {
|
|
71
|
+
char *p = (char *)img->surface->pixels;
|
|
72
|
+
int bpp = img->surface->format->BytesPerPixel;
|
|
73
|
+
int w = img->surface->w;
|
|
74
|
+
int h = img->surface->h;
|
|
75
|
+
char tmp;
|
|
76
|
+
for (int i = 0; i < bpp * w * h; i += bpp) {
|
|
77
|
+
if (a&0xFF) {
|
|
78
|
+
tmp = p[i];
|
|
79
|
+
p[i] = p[i+3];
|
|
80
|
+
p[i+3] = tmp;
|
|
81
|
+
}
|
|
82
|
+
if (g&0xFF0000) {
|
|
83
|
+
tmp = p[i+1];
|
|
84
|
+
p[i+1] = p[i+2];
|
|
85
|
+
p[i+2] = tmp;
|
|
86
|
+
}
|
|
87
|
+
if (r&0xFF0000) {
|
|
88
|
+
tmp = p[i];
|
|
89
|
+
p[i] = p[i+2];
|
|
90
|
+
p[i+2] = tmp;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return img;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
/*
|
|
100
|
+
* Rotate an image
|
|
101
|
+
*/
|
|
102
|
+
void R2D_RotateImage(R2D_Image *img, GLfloat angle, int position) {
|
|
103
|
+
|
|
104
|
+
R2D_GL_Point p = R2D_GetRectRotationPoint(
|
|
105
|
+
img->x, img->y, img->width, img->height, position
|
|
106
|
+
);
|
|
107
|
+
|
|
108
|
+
img->rotate = angle;
|
|
109
|
+
img->rx = p.x;
|
|
110
|
+
img->ry = p.y;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
/*
|
|
115
|
+
* Draw an image
|
|
116
|
+
*/
|
|
117
|
+
void R2D_DrawImage(R2D_Image *img) {
|
|
118
|
+
if (!img) return;
|
|
119
|
+
|
|
120
|
+
if (img->texture_id == 0) {
|
|
121
|
+
R2D_GL_CreateTexture(&img->texture_id, img->format,
|
|
122
|
+
img->orig_width, img->orig_height,
|
|
123
|
+
img->surface->pixels, GL_NEAREST);
|
|
124
|
+
SDL_FreeSurface(img->surface);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
R2D_GL_DrawImage(img);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
/*
|
|
132
|
+
* Free an image
|
|
133
|
+
*/
|
|
134
|
+
void R2D_FreeImage(R2D_Image *img) {
|
|
135
|
+
if (!img) return;
|
|
136
|
+
R2D_GL_FreeTexture(&img->texture_id);
|
|
137
|
+
free(img);
|
|
138
|
+
}
|
data/ext/ruby2d/input.c
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
// input.c
|
|
2
|
+
|
|
3
|
+
#include "ruby2d.h"
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
/*
|
|
7
|
+
* Get the mouse coordinates relative to the viewport
|
|
8
|
+
*/
|
|
9
|
+
void R2D_GetMouseOnViewport(R2D_Window *window, int wx, int wy, int *x, int *y) {
|
|
10
|
+
|
|
11
|
+
double scale; // viewport scale factor
|
|
12
|
+
int w, h; // width and height of scaled viewport
|
|
13
|
+
|
|
14
|
+
switch (window->viewport.mode) {
|
|
15
|
+
|
|
16
|
+
case R2D_FIXED: case R2D_EXPAND:
|
|
17
|
+
*x = wx / (window->orig_width / (double)window->viewport.width);
|
|
18
|
+
*y = wy / (window->orig_height / (double)window->viewport.height);
|
|
19
|
+
break;
|
|
20
|
+
|
|
21
|
+
case R2D_SCALE:
|
|
22
|
+
R2D_GL_GetViewportScale(window, &w, &h, &scale);
|
|
23
|
+
*x = wx * 1 / scale - (window->width - w) / (2.0 * scale);
|
|
24
|
+
*y = wy * 1 / scale - (window->height - h) / (2.0 * scale);
|
|
25
|
+
break;
|
|
26
|
+
|
|
27
|
+
case R2D_STRETCH:
|
|
28
|
+
*x = wx * window->viewport.width / (double)window->width;
|
|
29
|
+
*y = wy * window->viewport.height / (double)window->height;
|
|
30
|
+
break;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
/*
|
|
36
|
+
* Show the cursor over the window
|
|
37
|
+
*/
|
|
38
|
+
void R2D_ShowCursor() {
|
|
39
|
+
SDL_ShowCursor(SDL_ENABLE);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
/*
|
|
44
|
+
* Hide the cursor over the window
|
|
45
|
+
*/
|
|
46
|
+
void R2D_HideCursor() {
|
|
47
|
+
SDL_ShowCursor(SDL_DISABLE);
|
|
48
|
+
}
|