ruby2d 0.9.0 → 0.9.5
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 +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/gl2.c
ADDED
@@ -0,0 +1,146 @@
|
|
1
|
+
// OpenGL 2.1
|
2
|
+
|
3
|
+
#include "ruby2d.h"
|
4
|
+
|
5
|
+
#if !GLES
|
6
|
+
|
7
|
+
|
8
|
+
/*
|
9
|
+
* Applies the projection matrix
|
10
|
+
*/
|
11
|
+
void R2D_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 R2D_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
|
+
R2D_GL_PrintError("OpenGL initialization failed");
|
41
|
+
return 1;
|
42
|
+
} else {
|
43
|
+
return 0;
|
44
|
+
}
|
45
|
+
}
|
46
|
+
|
47
|
+
|
48
|
+
/*
|
49
|
+
* Draw triangle
|
50
|
+
*/
|
51
|
+
void R2D_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 R2D_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
|
+
R2D_GL_Point v1 = { .x = x, .y = y };
|
77
|
+
R2D_GL_Point v2 = { .x = x + w, .y = y };
|
78
|
+
R2D_GL_Point v3 = { .x = x + w, .y = y + h };
|
79
|
+
R2D_GL_Point v4 = { .x = x, .y = y + h };
|
80
|
+
|
81
|
+
// Rotate vertices
|
82
|
+
if (angle != 0) {
|
83
|
+
v1 = R2D_RotatePoint(v1, angle, rx, ry);
|
84
|
+
v2 = R2D_RotatePoint(v2, angle, rx, ry);
|
85
|
+
v3 = R2D_RotatePoint(v3, angle, rx, ry);
|
86
|
+
v4 = R2D_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 R2D_GL2_DrawImage(R2D_Image *img) {
|
109
|
+
R2D_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 R2D_GL2_DrawSprite(R2D_Sprite *spr) {
|
123
|
+
R2D_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 R2D_GL2_DrawText(R2D_Text *txt) {
|
137
|
+
R2D_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
|
data/ext/ruby2d/gl3.c
ADDED
@@ -0,0 +1,348 @@
|
|
1
|
+
// OpenGL 3.3+
|
2
|
+
|
3
|
+
#include "ruby2d.h"
|
4
|
+
|
5
|
+
// Skip this file if OpenGL ES
|
6
|
+
#if !GLES
|
7
|
+
|
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
|
17
|
+
{ 0, 1, 2,
|
18
|
+
2, 3, 0 };
|
19
|
+
|
20
|
+
|
21
|
+
/*
|
22
|
+
* Applies the projection matrix
|
23
|
+
*/
|
24
|
+
void R2D_GL3_ApplyProjection(GLfloat orthoMatrix[16]) {
|
25
|
+
|
26
|
+
// Use the program object
|
27
|
+
glUseProgram(shaderProgram);
|
28
|
+
|
29
|
+
// Apply the projection matrix to the triangle shader
|
30
|
+
glUniformMatrix4fv(
|
31
|
+
glGetUniformLocation(shaderProgram, "u_mvpMatrix"),
|
32
|
+
1, GL_FALSE, orthoMatrix
|
33
|
+
);
|
34
|
+
|
35
|
+
// Use the texture program object
|
36
|
+
glUseProgram(texShaderProgram);
|
37
|
+
|
38
|
+
// Apply the projection matrix to the texture shader
|
39
|
+
glUniformMatrix4fv(
|
40
|
+
glGetUniformLocation(texShaderProgram, "u_mvpMatrix"),
|
41
|
+
1, GL_FALSE, orthoMatrix
|
42
|
+
);
|
43
|
+
}
|
44
|
+
|
45
|
+
|
46
|
+
/*
|
47
|
+
* Initalize OpenGL
|
48
|
+
*/
|
49
|
+
int R2D_GL3_Init() {
|
50
|
+
|
51
|
+
// Enable transparency
|
52
|
+
glEnable(GL_BLEND);
|
53
|
+
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
54
|
+
|
55
|
+
// Vertex shader source string
|
56
|
+
GLchar vertexSource[] =
|
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
|
+
|
70
|
+
"void main() {"
|
71
|
+
// Send the color and texture coordinates right through to the fragment shader
|
72
|
+
" Color = color;"
|
73
|
+
" Texcoord = texcoord;"
|
74
|
+
// Transform the vertex position using the projection matrix
|
75
|
+
" gl_Position = u_mvpMatrix * position;"
|
76
|
+
"}";
|
77
|
+
|
78
|
+
// Fragment shader source string
|
79
|
+
GLchar fragmentSource[] =
|
80
|
+
"#version 150 core\n" // shader version
|
81
|
+
"in vec4 Color;" // input color from vertex shader
|
82
|
+
"out vec4 outColor;" // output fragment color
|
83
|
+
|
84
|
+
"void main() {"
|
85
|
+
" outColor = Color;" // pass the color right through
|
86
|
+
"}";
|
87
|
+
|
88
|
+
// Fragment shader source string for textures
|
89
|
+
GLchar texFragmentSource[] =
|
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
|
+
|
96
|
+
"void main() {"
|
97
|
+
// Apply the texture unit, texture coordinates, and color
|
98
|
+
" outColor = texture(tex, Texcoord) * Color;"
|
99
|
+
"}";
|
100
|
+
|
101
|
+
// Create a vertex array object
|
102
|
+
GLuint vao;
|
103
|
+
glGenVertexArrays(1, &vao);
|
104
|
+
glBindVertexArray(vao);
|
105
|
+
|
106
|
+
// Create a vertex buffer object and allocate data
|
107
|
+
glGenBuffers(1, &vbo);
|
108
|
+
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
109
|
+
vboSize = vboObjCapacity * sizeof(GLfloat) * 24;
|
110
|
+
vboData = (GLfloat *) malloc(vboSize);
|
111
|
+
vboDataCurrent = vboData;
|
112
|
+
|
113
|
+
// Create an element buffer object
|
114
|
+
GLuint ebo;
|
115
|
+
glGenBuffers(1, &ebo);
|
116
|
+
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
|
117
|
+
|
118
|
+
// Load the vertex and fragment shaders
|
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 //
|
124
|
+
|
125
|
+
// Create the shader program object
|
126
|
+
shaderProgram = glCreateProgram();
|
127
|
+
|
128
|
+
// Check if program was created successfully
|
129
|
+
if (shaderProgram == 0) {
|
130
|
+
R2D_GL_PrintError("Failed to create shader program");
|
131
|
+
return GL_FALSE;
|
132
|
+
}
|
133
|
+
|
134
|
+
// Attach the shader objects to the program object
|
135
|
+
glAttachShader(shaderProgram, vertexShader);
|
136
|
+
glAttachShader(shaderProgram, fragmentShader);
|
137
|
+
|
138
|
+
// Bind the output color variable to the fragment shader color number
|
139
|
+
glBindFragDataLocation(shaderProgram, 0, "outColor");
|
140
|
+
|
141
|
+
// Link the shader program
|
142
|
+
glLinkProgram(shaderProgram);
|
143
|
+
|
144
|
+
// Check if linked
|
145
|
+
R2D_GL_CheckLinked(shaderProgram, "GL3 shader");
|
146
|
+
|
147
|
+
// Specify the layout of the position vertex data...
|
148
|
+
GLint posAttrib = glGetAttribLocation(shaderProgram, "position");
|
149
|
+
glEnableVertexAttribArray(posAttrib);
|
150
|
+
glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), 0);
|
151
|
+
|
152
|
+
// ...and the color vertex data
|
153
|
+
GLint colAttrib = glGetAttribLocation(shaderProgram, "color");
|
154
|
+
glEnableVertexAttribArray(colAttrib);
|
155
|
+
glVertexAttribPointer(colAttrib, 4, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (void*)(2 * sizeof(GLfloat)));
|
156
|
+
|
157
|
+
// Texture Shader //
|
158
|
+
|
159
|
+
// Create the texture shader program object
|
160
|
+
texShaderProgram = glCreateProgram();
|
161
|
+
|
162
|
+
// Check if program was created successfully
|
163
|
+
if (texShaderProgram == 0) {
|
164
|
+
R2D_GL_PrintError("Failed to create shader program");
|
165
|
+
return GL_FALSE;
|
166
|
+
}
|
167
|
+
|
168
|
+
// Attach the shader objects to the program object
|
169
|
+
glAttachShader(texShaderProgram, vertexShader);
|
170
|
+
glAttachShader(texShaderProgram, texFragmentShader);
|
171
|
+
|
172
|
+
// Bind the output color variable to the fragment shader color number
|
173
|
+
glBindFragDataLocation(texShaderProgram, 0, "outColor");
|
174
|
+
|
175
|
+
// Link the shader program
|
176
|
+
glLinkProgram(texShaderProgram);
|
177
|
+
|
178
|
+
// Check if linked
|
179
|
+
R2D_GL_CheckLinked(texShaderProgram, "GL3 texture shader");
|
180
|
+
|
181
|
+
// Specify the layout of the position vertex data...
|
182
|
+
posAttrib = glGetAttribLocation(texShaderProgram, "position");
|
183
|
+
glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), 0);
|
184
|
+
glEnableVertexAttribArray(posAttrib);
|
185
|
+
|
186
|
+
// ...and the color vertex data...
|
187
|
+
colAttrib = glGetAttribLocation(texShaderProgram, "color");
|
188
|
+
glVertexAttribPointer(colAttrib, 4, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (void*)(2 * sizeof(GLfloat)));
|
189
|
+
glEnableVertexAttribArray(colAttrib);
|
190
|
+
|
191
|
+
// ...and the texture coordinates
|
192
|
+
GLint texAttrib = glGetAttribLocation(texShaderProgram, "texcoord");
|
193
|
+
glVertexAttribPointer(texAttrib, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (void*)(6 * sizeof(GLfloat)));
|
194
|
+
glEnableVertexAttribArray(texAttrib);
|
195
|
+
|
196
|
+
// Clean up
|
197
|
+
glDeleteShader(vertexShader);
|
198
|
+
glDeleteShader(fragmentShader);
|
199
|
+
glDeleteShader(texFragmentShader);
|
200
|
+
|
201
|
+
// If successful, return true
|
202
|
+
return GL_TRUE;
|
203
|
+
}
|
204
|
+
|
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
|
+
|
228
|
+
/*
|
229
|
+
* Draw triangle
|
230
|
+
*/
|
231
|
+
void R2D_GL3_DrawTriangle(GLfloat x1, GLfloat y1,
|
232
|
+
GLfloat r1, GLfloat g1, GLfloat b1, GLfloat a1,
|
233
|
+
GLfloat x2, GLfloat y2,
|
234
|
+
GLfloat r2, GLfloat g2, GLfloat b2, GLfloat a2,
|
235
|
+
GLfloat x3, GLfloat y3,
|
236
|
+
GLfloat r3, GLfloat g3, GLfloat b3, GLfloat a3) {
|
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
|
242
|
+
GLfloat vertices[] =
|
243
|
+
{ x1, y1, r1, g1, b1, a1, 0, 0,
|
244
|
+
x2, y2, r2, g2, b2, a2, 0, 0,
|
245
|
+
x3, y3, r3, g3, b3, a3, 0, 0 };
|
246
|
+
|
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));
|
253
|
+
}
|
254
|
+
|
255
|
+
|
256
|
+
/*
|
257
|
+
* Draw a texture
|
258
|
+
*/
|
259
|
+
static void R2D_GL3_DrawTexture(int x, int y, int w, int h,
|
260
|
+
GLfloat angle, GLfloat rx, GLfloat ry,
|
261
|
+
GLfloat r, GLfloat g, GLfloat b, GLfloat a,
|
262
|
+
GLfloat tx1, GLfloat ty1, GLfloat tx2, GLfloat ty2,
|
263
|
+
GLfloat tx3, GLfloat ty3, GLfloat tx4, GLfloat ty4,
|
264
|
+
GLuint texture_id) {
|
265
|
+
|
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 };
|
275
|
+
|
276
|
+
// Rotate vertices
|
277
|
+
if (angle != 0) {
|
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);
|
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 R2D_GL3_DrawImage(R2D_Image *img) {
|
311
|
+
R2D_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
|
+
}
|
319
|
+
|
320
|
+
|
321
|
+
/*
|
322
|
+
* Draw sprite
|
323
|
+
*/
|
324
|
+
void R2D_GL3_DrawSprite(R2D_Sprite *spr) {
|
325
|
+
R2D_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
|
+
);
|
332
|
+
}
|
333
|
+
|
334
|
+
|
335
|
+
/*
|
336
|
+
* Draw text
|
337
|
+
*/
|
338
|
+
void R2D_GL3_DrawText(R2D_Text *txt) {
|
339
|
+
R2D_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
|
+
#endif
|