ruby2d 0.9.3 → 0.11.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (166) hide show
  1. checksums.yaml +4 -4
  2. data/assets/README.md +7 -9
  3. data/assets/Rakefile +85 -0
  4. data/assets/include/SDL2/SDL.h +4 -1
  5. data/assets/include/SDL2/SDL_assert.h +3 -1
  6. data/assets/include/SDL2/SDL_atomic.h +20 -2
  7. data/assets/include/SDL2/SDL_audio.h +47 -14
  8. data/assets/include/SDL2/SDL_bits.h +10 -1
  9. data/assets/include/SDL2/SDL_blendmode.h +10 -7
  10. data/assets/include/SDL2/SDL_clipboard.h +1 -1
  11. data/assets/include/SDL2/SDL_config.h +24 -390
  12. data/assets/include/SDL2/SDL_config_android.h +182 -0
  13. data/assets/include/SDL2/SDL_config_iphoneos.h +207 -0
  14. data/assets/include/SDL2/SDL_config_macosx.h +266 -0
  15. data/assets/include/SDL2/SDL_config_minimal.h +85 -0
  16. data/assets/include/SDL2/SDL_config_os2.h +188 -0
  17. data/assets/include/SDL2/SDL_config_pandora.h +135 -0
  18. data/assets/include/SDL2/SDL_config_psp.h +165 -0
  19. data/assets/include/SDL2/SDL_config_windows.h +288 -0
  20. data/assets/include/SDL2/SDL_config_winrt.h +243 -0
  21. data/assets/include/SDL2/SDL_config_wiz.h +149 -0
  22. data/assets/include/SDL2/SDL_copying.h +20 -0
  23. data/assets/include/SDL2/SDL_cpuinfo.h +119 -8
  24. data/assets/include/SDL2/SDL_egl.h +4 -1
  25. data/assets/include/SDL2/SDL_endian.h +6 -3
  26. data/assets/include/SDL2/SDL_error.h +38 -2
  27. data/assets/include/SDL2/SDL_events.h +67 -28
  28. data/assets/include/SDL2/SDL_filesystem.h +1 -1
  29. data/assets/include/SDL2/SDL_gamecontroller.h +160 -9
  30. data/assets/include/SDL2/SDL_gesture.h +1 -1
  31. data/assets/include/SDL2/SDL_haptic.h +10 -1
  32. data/assets/include/SDL2/SDL_hints.h +460 -17
  33. data/assets/include/SDL2/SDL_image.h +2 -2
  34. data/assets/include/SDL2/SDL_joystick.h +115 -24
  35. data/assets/include/SDL2/SDL_keyboard.h +1 -1
  36. data/assets/include/SDL2/SDL_keycode.h +11 -9
  37. data/assets/include/SDL2/SDL_loadso.h +1 -1
  38. data/assets/include/SDL2/SDL_locale.h +101 -0
  39. data/assets/include/SDL2/SDL_log.h +3 -3
  40. data/assets/include/SDL2/SDL_main.h +28 -16
  41. data/assets/include/SDL2/SDL_messagebox.h +6 -4
  42. data/assets/include/SDL2/SDL_metal.h +117 -0
  43. data/assets/include/SDL2/SDL_misc.h +75 -0
  44. data/assets/include/SDL2/SDL_mouse.h +1 -1
  45. data/assets/include/SDL2/SDL_mutex.h +1 -1
  46. data/assets/include/SDL2/SDL_name.h +1 -1
  47. data/assets/include/SDL2/SDL_opengl.h +1 -1
  48. data/assets/include/SDL2/SDL_opengl_glext.h +3 -0
  49. data/assets/include/SDL2/SDL_opengles.h +1 -1
  50. data/assets/include/SDL2/SDL_opengles2.h +1 -1
  51. data/assets/include/SDL2/SDL_pixels.h +27 -18
  52. data/assets/include/SDL2/SDL_platform.h +1 -1
  53. data/assets/include/SDL2/SDL_power.h +1 -1
  54. data/assets/include/SDL2/SDL_quit.h +1 -1
  55. data/assets/include/SDL2/SDL_rect.h +29 -3
  56. data/assets/include/SDL2/SDL_render.h +230 -3
  57. data/assets/include/SDL2/SDL_revision.h +2 -2
  58. data/assets/include/SDL2/SDL_rwops.h +51 -22
  59. data/assets/include/SDL2/SDL_scancode.h +2 -2
  60. data/assets/include/SDL2/SDL_sensor.h +28 -12
  61. data/assets/include/SDL2/SDL_shape.h +1 -1
  62. data/assets/include/SDL2/SDL_stdinc.h +44 -4
  63. data/assets/include/SDL2/SDL_surface.h +12 -2
  64. data/assets/include/SDL2/SDL_system.h +50 -4
  65. data/assets/include/SDL2/SDL_syswm.h +39 -9
  66. data/assets/include/SDL2/SDL_test.h +1 -1
  67. data/assets/include/SDL2/SDL_test_assert.h +1 -1
  68. data/assets/include/SDL2/SDL_test_common.h +32 -2
  69. data/assets/include/SDL2/SDL_test_compare.h +1 -1
  70. data/assets/include/SDL2/SDL_test_crc32.h +1 -1
  71. data/assets/include/SDL2/SDL_test_font.h +1 -1
  72. data/assets/include/SDL2/SDL_test_fuzzer.h +1 -1
  73. data/assets/include/SDL2/SDL_test_harness.h +1 -1
  74. data/assets/include/SDL2/SDL_test_images.h +1 -1
  75. data/assets/include/SDL2/SDL_test_log.h +1 -1
  76. data/assets/include/SDL2/SDL_test_md5.h +1 -1
  77. data/assets/include/SDL2/SDL_test_memory.h +3 -3
  78. data/assets/include/SDL2/SDL_test_random.h +1 -1
  79. data/assets/include/SDL2/SDL_thread.h +34 -11
  80. data/assets/include/SDL2/SDL_timer.h +1 -1
  81. data/assets/include/SDL2/SDL_touch.h +17 -1
  82. data/assets/include/SDL2/SDL_types.h +1 -1
  83. data/assets/include/SDL2/SDL_version.h +2 -2
  84. data/assets/include/SDL2/SDL_video.h +11 -5
  85. data/assets/include/SDL2/SDL_vulkan.h +9 -11
  86. data/assets/include/SDL2/begin_code.h +8 -9
  87. data/assets/include/SDL2/close_code.h +4 -1
  88. data/assets/macos/lib/libFLAC.a +0 -0
  89. data/assets/macos/lib/libSDL2.a +0 -0
  90. data/assets/macos/lib/libSDL2_image.a +0 -0
  91. data/assets/macos/lib/libSDL2_mixer.a +0 -0
  92. data/assets/macos/lib/libSDL2_ttf.a +0 -0
  93. data/assets/macos/lib/libfreetype.a +0 -0
  94. data/assets/macos/lib/libjpeg.a +0 -0
  95. data/assets/macos/lib/libmpg123.a +0 -0
  96. data/assets/macos/lib/libogg.a +0 -0
  97. data/assets/macos/lib/libpng16.a +0 -0
  98. data/assets/macos/lib/libtiff.a +0 -0
  99. data/assets/macos/lib/libvorbis.a +0 -0
  100. data/assets/macos/lib/libvorbisfile.a +0 -0
  101. data/assets/macos/lib/libwebp.a +0 -0
  102. data/assets/mingw/bin/SDL2.dll +0 -0
  103. data/assets/mingw/bin/SDL2_image.dll +0 -0
  104. data/assets/mingw/bin/libpng16-16.dll +0 -0
  105. data/assets/mingw/bin/libtiff-5.dll +0 -0
  106. data/assets/mingw/bin/libwebp-7.dll +0 -0
  107. data/assets/mingw/lib/libSDL2.a +0 -0
  108. data/assets/mingw/lib/libSDL2.dll.a +0 -0
  109. data/assets/mingw/lib/libSDL2_image.a +0 -0
  110. data/assets/mingw/lib/libSDL2_image.dll.a +0 -0
  111. data/assets/mingw/lib/libSDL2_test.a +0 -0
  112. data/assets/mingw/lib/libSDL2main.a +0 -0
  113. data/bin/ruby2d +1 -0
  114. data/{assets/linux/simple2d/src/simple2d.c → ext/ruby2d/common.c} +32 -32
  115. data/{assets/linux/simple2d/src → ext/ruby2d}/controllers.c +17 -17
  116. data/ext/ruby2d/extconf.rb +36 -58
  117. data/ext/ruby2d/font.c +35 -0
  118. data/{assets/linux/simple2d/src → ext/ruby2d}/gl.c +72 -100
  119. data/ext/ruby2d/gl2.c +86 -0
  120. data/ext/ruby2d/gl3.c +305 -0
  121. data/{assets/linux/simple2d/src → ext/ruby2d}/gles.c +22 -81
  122. data/ext/ruby2d/image.c +58 -0
  123. data/{assets/linux/simple2d/src → ext/ruby2d}/input.c +8 -8
  124. data/{assets/linux/simple2d/src → ext/ruby2d}/music.c +30 -17
  125. data/ext/ruby2d/ruby2d.c +449 -439
  126. data/ext/ruby2d/ruby2d.h +652 -0
  127. data/{assets/linux/simple2d/src → ext/ruby2d}/shapes.c +18 -18
  128. data/ext/ruby2d/sound.c +118 -0
  129. data/ext/ruby2d/text.c +22 -0
  130. data/{assets/linux/simple2d/src → ext/ruby2d}/window.c +73 -62
  131. data/lib/ruby2d/circle.rb +21 -3
  132. data/lib/ruby2d/cli/build.rb +4 -7
  133. data/lib/ruby2d/dsl.rb +16 -9
  134. data/lib/ruby2d/entity.rb +17 -0
  135. data/lib/ruby2d/font.rb +23 -3
  136. data/lib/ruby2d/image.rb +35 -7
  137. data/lib/ruby2d/line.rb +23 -1
  138. data/lib/ruby2d/music.rb +5 -0
  139. data/lib/ruby2d/pixel.rb +17 -0
  140. data/lib/ruby2d/quad.rb +21 -1
  141. data/lib/ruby2d/rectangle.rb +12 -3
  142. data/lib/ruby2d/renderable.rb +4 -20
  143. data/lib/ruby2d/sound.rb +30 -0
  144. data/lib/ruby2d/sprite.rb +57 -75
  145. data/lib/ruby2d/square.rb +10 -1
  146. data/lib/ruby2d/text.rb +55 -12
  147. data/lib/ruby2d/texture.rb +28 -0
  148. data/lib/ruby2d/tileset.rb +87 -0
  149. data/lib/ruby2d/triangle.rb +19 -1
  150. data/lib/ruby2d/version.rb +1 -1
  151. data/lib/ruby2d/vertices.rb +84 -0
  152. data/lib/ruby2d/window.rb +259 -22
  153. data/lib/ruby2d.rb +6 -1
  154. metadata +41 -26
  155. data/assets/include/simple2d.h +0 -735
  156. data/assets/linux/simple2d/Makefile +0 -257
  157. data/assets/linux/simple2d/bin/simple2d.sh +0 -1312
  158. data/assets/linux/simple2d/include/simple2d.h +0 -735
  159. data/assets/linux/simple2d/src/gl2.c +0 -146
  160. data/assets/linux/simple2d/src/gl3.c +0 -275
  161. data/assets/linux/simple2d/src/image.c +0 -138
  162. data/assets/linux/simple2d/src/sound.c +0 -56
  163. data/assets/linux/simple2d/src/sprite.c +0 -147
  164. data/assets/linux/simple2d/src/text.c +0 -129
  165. data/assets/macos/lib/libsimple2d.a +0 -0
  166. data/assets/mingw/lib/libsimple2d.a +0 -0
@@ -1,146 +0,0 @@
1
- // OpenGL 2.1
2
-
3
- #include "../include/simple2d.h"
4
-
5
- #if !GLES
6
-
7
-
8
- /*
9
- * Applies the projection matrix
10
- */
11
- void S2D_GL2_ApplyProjection(int w, int h) {
12
-
13
- // Initialize the projection matrix
14
- glMatrixMode(GL_PROJECTION);
15
- glLoadIdentity();
16
-
17
- // Multiply the current matrix with the orthographic matrix
18
- glOrtho(0.f, w, h, 0.f, -1.f, 1.f);
19
-
20
- // Initialize the model-view matrix
21
- glMatrixMode(GL_MODELVIEW);
22
- glLoadIdentity();
23
- }
24
-
25
-
26
- /*
27
- * Initalize OpenGL
28
- */
29
- int S2D_GL2_Init() {
30
-
31
- GLenum error = GL_NO_ERROR;
32
-
33
- // Enable transparency
34
- glEnable(GL_BLEND);
35
- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
36
-
37
- // Check for errors
38
- error = glGetError();
39
- if (error != GL_NO_ERROR) {
40
- S2D_GL_PrintError("OpenGL initialization failed");
41
- return 1;
42
- } else {
43
- return 0;
44
- }
45
- }
46
-
47
-
48
- /*
49
- * Draw triangle
50
- */
51
- void S2D_GL2_DrawTriangle(GLfloat x1, GLfloat y1,
52
- GLfloat r1, GLfloat g1, GLfloat b1, GLfloat a1,
53
- GLfloat x2, GLfloat y2,
54
- GLfloat r2, GLfloat g2, GLfloat b2, GLfloat a2,
55
- GLfloat x3, GLfloat y3,
56
- GLfloat r3, GLfloat g3, GLfloat b3, GLfloat a3) {
57
-
58
- glBegin(GL_TRIANGLES);
59
- glColor4f(r1, g1, b1, a1); glVertex2f(x1, y1);
60
- glColor4f(r2, g2, b2, a2); glVertex2f(x2, y2);
61
- glColor4f(r3, g3, b3, a3); glVertex2f(x3, y3);
62
- glEnd();
63
- }
64
-
65
-
66
- /*
67
- * Draw texture
68
- */
69
- static void S2D_GL2_DrawTexture(int x, int y, int w, int h,
70
- GLfloat angle, GLfloat rx, GLfloat ry,
71
- GLfloat r, GLfloat g, GLfloat b, GLfloat a,
72
- GLfloat tx1, GLfloat ty1, GLfloat tx2, GLfloat ty2,
73
- GLfloat tx3, GLfloat ty3, GLfloat tx4, GLfloat ty4,
74
- GLuint texture_id) {
75
-
76
- S2D_GL_Point v1 = { .x = x, .y = y };
77
- S2D_GL_Point v2 = { .x = x + w, .y = y };
78
- S2D_GL_Point v3 = { .x = x + w, .y = y + h };
79
- S2D_GL_Point v4 = { .x = x, .y = y + h };
80
-
81
- // Rotate vertices
82
- if (angle != 0) {
83
- v1 = S2D_RotatePoint(v1, angle, rx, ry);
84
- v2 = S2D_RotatePoint(v2, angle, rx, ry);
85
- v3 = S2D_RotatePoint(v3, angle, rx, ry);
86
- v4 = S2D_RotatePoint(v4, angle, rx, ry);
87
- }
88
-
89
- glEnable(GL_TEXTURE_2D);
90
-
91
- glBindTexture(GL_TEXTURE_2D, texture_id);
92
-
93
- glBegin(GL_QUADS);
94
- glColor4f(r, g, b, a);
95
- glTexCoord2f(tx1, ty1); glVertex2f(v1.x, v1.y);
96
- glTexCoord2f(tx2, ty2); glVertex2f(v2.x, v2.y);
97
- glTexCoord2f(tx3, ty3); glVertex2f(v3.x, v3.y);
98
- glTexCoord2f(tx4, ty4); glVertex2f(v4.x, v4.y);
99
- glEnd();
100
-
101
- glDisable(GL_TEXTURE_2D);
102
- }
103
-
104
-
105
- /*
106
- * Draw image
107
- */
108
- void S2D_GL2_DrawImage(S2D_Image *img) {
109
- S2D_GL2_DrawTexture(
110
- img->x, img->y, img->width, img->height,
111
- img->rotate, img->rx, img->ry,
112
- img->color.r, img->color.g, img->color.b, img->color.a,
113
- 0.f, 0.f, 1.f, 0.f, 1.f, 1.f, 0.f, 1.f,
114
- img->texture_id
115
- );
116
- }
117
-
118
-
119
- /*
120
- * Draw sprite
121
- */
122
- void S2D_GL2_DrawSprite(S2D_Sprite *spr) {
123
- S2D_GL2_DrawTexture(
124
- spr->x, spr->y, spr->width, spr->height,
125
- spr->rotate, spr->rx, spr->ry,
126
- spr->color.r, spr->color.g, spr->color.b, spr->color.a,
127
- spr->tx1, spr->ty1, spr->tx2, spr->ty2, spr->tx3, spr->ty3, spr->tx4, spr->ty4,
128
- spr->img->texture_id
129
- );
130
- }
131
-
132
-
133
- /*
134
- * Draw text
135
- */
136
- void S2D_GL2_DrawText(S2D_Text *txt) {
137
- S2D_GL2_DrawTexture(
138
- txt->x, txt->y, txt->width, txt->height,
139
- txt->rotate, txt->rx, txt->ry,
140
- txt->color.r, txt->color.g, txt->color.b, txt->color.a,
141
- 0.f, 0.f, 1.f, 0.f, 1.f, 1.f, 0.f, 1.f,
142
- txt->texture_id
143
- );
144
- }
145
-
146
- #endif
@@ -1,275 +0,0 @@
1
- // OpenGL 3.3
2
-
3
- #include "../include/simple2d.h"
4
-
5
- #if !GLES
6
-
7
- static GLuint shaderProgram;
8
- static GLuint texShaderProgram;
9
- static GLuint indices[] =
10
- { 0, 1, 2,
11
- 2, 3, 0 };
12
-
13
-
14
- /*
15
- * Applies the projection matrix
16
- */
17
- void S2D_GL3_ApplyProjection(GLfloat orthoMatrix[16]) {
18
-
19
- // Use the program object
20
- glUseProgram(shaderProgram);
21
-
22
- glUniformMatrix4fv(
23
- glGetUniformLocation(shaderProgram, "u_mvpMatrix"),
24
- 1, GL_FALSE, orthoMatrix
25
- );
26
-
27
- // Use the texture program object
28
- glUseProgram(texShaderProgram);
29
-
30
- glUniformMatrix4fv(
31
- glGetUniformLocation(texShaderProgram, "u_mvpMatrix"),
32
- 1, GL_FALSE, orthoMatrix
33
- );
34
- }
35
-
36
-
37
- /*
38
- * Initalize OpenGL
39
- */
40
- int S2D_GL3_Init() {
41
-
42
- // Enable transparency
43
- glEnable(GL_BLEND);
44
- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
45
-
46
- // Vertex shader source string
47
- GLchar vertexSource[] =
48
- "#version 150 core\n"
49
- "uniform mat4 u_mvpMatrix;"
50
- "in vec4 position;"
51
- "in vec4 color;"
52
- "in vec2 texcoord;"
53
- "out vec4 Color;"
54
- "out vec2 Texcoord;"
55
- "void main() {"
56
- " Color = color;"
57
- " Texcoord = texcoord;"
58
- " gl_Position = u_mvpMatrix * position;"
59
- "}";
60
-
61
- // Fragment shader source string
62
- GLchar fragmentSource[] =
63
- "#version 150 core\n"
64
- "in vec4 Color;"
65
- "out vec4 outColor;"
66
- "void main() {"
67
- " outColor = Color;"
68
- "}";
69
-
70
- // Fragment shader source string for textures
71
- GLchar texFragmentSource[] =
72
- "#version 150 core\n"
73
- "in vec4 Color;"
74
- "in vec2 Texcoord;"
75
- "out vec4 outColor;"
76
- "uniform sampler2D tex;"
77
- "void main() {"
78
- " outColor = texture(tex, Texcoord) * Color;"
79
- "}";
80
-
81
- // Create Vertex Array Object
82
- GLuint vao;
83
- glGenVertexArrays(1, &vao);
84
- glBindVertexArray(vao);
85
-
86
- // Create Vertex Buffer Object
87
- GLuint vbo;
88
- glGenBuffers(1, &vbo);
89
- glBindBuffer(GL_ARRAY_BUFFER, vbo);
90
-
91
- // Create an element array
92
- GLuint ebo;
93
- glGenBuffers(1, &ebo);
94
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
95
-
96
- // Load the vertex and fragment shaders
97
- GLuint vertexShader = S2D_GL_LoadShader( GL_VERTEX_SHADER, vertexSource, "GL3 Vertex");
98
- GLuint fragmentShader = S2D_GL_LoadShader(GL_FRAGMENT_SHADER, fragmentSource, "GL3 Fragment");
99
- GLuint texFragmentShader = S2D_GL_LoadShader(GL_FRAGMENT_SHADER, texFragmentSource, "GL3 Texture Fragment");
100
-
101
- // Create the shader program object
102
- shaderProgram = glCreateProgram();
103
-
104
- // Check if program was created successfully
105
- if (shaderProgram == 0) {
106
- S2D_GL_PrintError("Failed to create shader program");
107
- return GL_FALSE;
108
- }
109
-
110
- // Attach the shader objects to the program object
111
- glAttachShader(shaderProgram, vertexShader);
112
- glAttachShader(shaderProgram, fragmentShader);
113
-
114
- // Bind the varying out variables to the fragment shader color number
115
- glBindFragDataLocation(shaderProgram, 0, "outColor");
116
-
117
- // Link the shader program
118
- glLinkProgram(shaderProgram);
119
-
120
- // Check if linked
121
- S2D_GL_CheckLinked(shaderProgram, "GL3 shader");
122
-
123
- // Specify the layout of the vertex data
124
- GLint posAttrib = glGetAttribLocation(shaderProgram, "position");
125
- glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), 0);
126
- glEnableVertexAttribArray(posAttrib);
127
-
128
- GLint colAttrib = glGetAttribLocation(shaderProgram, "color");
129
- glVertexAttribPointer(colAttrib, 4, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (void*)(2 * sizeof(GLfloat)));
130
- glEnableVertexAttribArray(colAttrib);
131
-
132
- // Create the texture shader program object
133
- texShaderProgram = glCreateProgram();
134
-
135
- // Check if program was created successfully
136
- if (texShaderProgram == 0) {
137
- S2D_GL_PrintError("Failed to create shader program");
138
- return GL_FALSE;
139
- }
140
-
141
- glAttachShader(texShaderProgram, vertexShader);
142
- glAttachShader(texShaderProgram, texFragmentShader);
143
-
144
- glBindFragDataLocation(texShaderProgram, 0, "outColor");
145
-
146
- glLinkProgram(texShaderProgram);
147
-
148
- // Check if linked
149
- S2D_GL_CheckLinked(texShaderProgram, "GL3 texture shader");
150
-
151
- // Specify the layout of the vertex data
152
- posAttrib = glGetAttribLocation(texShaderProgram, "position");
153
- glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), 0);
154
- glEnableVertexAttribArray(posAttrib);
155
-
156
- colAttrib = glGetAttribLocation(texShaderProgram, "color");
157
- glVertexAttribPointer(colAttrib, 4, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (void*)(2 * sizeof(GLfloat)));
158
- glEnableVertexAttribArray(colAttrib);
159
-
160
- GLint texAttrib = glGetAttribLocation(texShaderProgram, "texcoord");
161
- glVertexAttribPointer(texAttrib, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (void*)(6 * sizeof(GLfloat)));
162
- glEnableVertexAttribArray(texAttrib);
163
-
164
- // Clean up
165
- glDeleteShader(vertexShader);
166
- glDeleteShader(fragmentShader);
167
- glDeleteShader(texFragmentShader);
168
-
169
- return GL_TRUE;
170
- }
171
-
172
-
173
- /*
174
- * Draw triangle
175
- */
176
- void S2D_GL3_DrawTriangle(GLfloat x1, GLfloat y1,
177
- GLfloat r1, GLfloat g1, GLfloat b1, GLfloat a1,
178
- GLfloat x2, GLfloat y2,
179
- GLfloat r2, GLfloat g2, GLfloat b2, GLfloat a2,
180
- GLfloat x3, GLfloat y3,
181
- GLfloat r3, GLfloat g3, GLfloat b3, GLfloat a3) {
182
-
183
- GLfloat vertices[] =
184
- { x1, y1, r1, g1, b1, a1, 0, 0,
185
- x2, y2, r2, g2, b2, a2, 0, 0,
186
- x3, y3, r3, g3, b3, a3, 0, 0 };
187
-
188
- glUseProgram(shaderProgram);
189
- glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
190
- glDrawArrays(GL_TRIANGLES, 0, 3);
191
- }
192
-
193
-
194
- /*
195
- * Draw a texture
196
- */
197
- static void S2D_GL3_DrawTexture(int x, int y, int w, int h,
198
- GLfloat angle, GLfloat rx, GLfloat ry,
199
- GLfloat r, GLfloat g, GLfloat b, GLfloat a,
200
- GLfloat tx1, GLfloat ty1, GLfloat tx2, GLfloat ty2,
201
- GLfloat tx3, GLfloat ty3, GLfloat tx4, GLfloat ty4,
202
- GLuint texture_id) {
203
-
204
- S2D_GL_Point v1 = { .x = x, .y = y };
205
- S2D_GL_Point v2 = { .x = x + w, .y = y };
206
- S2D_GL_Point v3 = { .x = x + w, .y = y + h };
207
- S2D_GL_Point v4 = { .x = x, .y = y + h };
208
-
209
- // Rotate vertices
210
- if (angle != 0) {
211
- v1 = S2D_RotatePoint(v1, angle, rx, ry);
212
- v2 = S2D_RotatePoint(v2, angle, rx, ry);
213
- v3 = S2D_RotatePoint(v3, angle, rx, ry);
214
- v4 = S2D_RotatePoint(v4, angle, rx, ry);
215
- }
216
-
217
- GLfloat vertices[] =
218
- // vertex coords | colors | x, y texture coords
219
- { v1.x, v1.y, r, g, b, a, tx1, ty1, // Top-left
220
- v2.x, v2.y, r, g, b, a, tx2, ty2, // Top-right
221
- v3.x, v3.y, r, g, b, a, tx3, ty3, // Bottom-right
222
- v4.x, v4.y, r, g, b, a, tx4, ty4 }; // Bottom-left
223
-
224
- glUseProgram(texShaderProgram);
225
- glBindTexture(GL_TEXTURE_2D, texture_id);
226
-
227
- glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
228
- glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
229
-
230
- glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
231
- }
232
-
233
-
234
- /*
235
- * Draw image
236
- */
237
- void S2D_GL3_DrawImage(S2D_Image *img) {
238
- S2D_GL3_DrawTexture(
239
- img->x, img->y, img->width, img->height,
240
- img->rotate, img->rx, img->ry,
241
- img->color.r, img->color.g, img->color.b, img->color.a,
242
- 0.f, 0.f, 1.f, 0.f, 1.f, 1.f, 0.f, 1.f,
243
- img->texture_id
244
- );
245
- }
246
-
247
-
248
- /*
249
- * Draw sprite
250
- */
251
- void S2D_GL3_DrawSprite(S2D_Sprite *spr) {
252
- S2D_GL3_DrawTexture(
253
- spr->x, spr->y, spr->width, spr->height,
254
- spr->rotate, spr->rx, spr->ry,
255
- spr->color.r, spr->color.g, spr->color.b, spr->color.a,
256
- spr->tx1, spr->ty1, spr->tx2, spr->ty2, spr->tx3, spr->ty3, spr->tx4, spr->ty4,
257
- spr->img->texture_id
258
- );
259
- }
260
-
261
-
262
- /*
263
- * Draw text
264
- */
265
- void S2D_GL3_DrawText(S2D_Text *txt) {
266
- S2D_GL3_DrawTexture(
267
- txt->x, txt->y, txt->width, txt->height,
268
- txt->rotate, txt->rx, txt->ry,
269
- txt->color.r, txt->color.g, txt->color.b, txt->color.a,
270
- 0.f, 0.f, 1.f, 0.f, 1.f, 1.f, 0.f, 1.f,
271
- txt->texture_id
272
- );
273
- }
274
-
275
- #endif
@@ -1,138 +0,0 @@
1
- // image.c
2
-
3
- #include "../include/simple2d.h"
4
-
5
-
6
- /*
7
- * Create an image, given a file path
8
- */
9
- S2D_Image *S2D_CreateImage(const char *path) {
10
- S2D_Init();
11
-
12
- // Check if image file exists
13
- if (!S2D_FileExists(path)) {
14
- S2D_Error("S2D_CreateImage", "Image file `%s` not found", path);
15
- return NULL;
16
- }
17
-
18
- // Allocate the image structure
19
- S2D_Image *img = (S2D_Image *) malloc(sizeof(S2D_Image));
20
- if (!img) {
21
- S2D_Error("S2D_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
- S2D_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
- S2D_Log(S2D_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 S2D_RotateImage(S2D_Image *img, GLfloat angle, int position) {
103
-
104
- S2D_GL_Point p = S2D_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 S2D_DrawImage(S2D_Image *img) {
118
- if (!img) return;
119
-
120
- if (img->texture_id == 0) {
121
- S2D_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
- S2D_GL_DrawImage(img);
128
- }
129
-
130
-
131
- /*
132
- * Free an image
133
- */
134
- void S2D_FreeImage(S2D_Image *img) {
135
- if (!img) return;
136
- S2D_GL_FreeTexture(&img->texture_id);
137
- free(img);
138
- }