sfml3-rb 0.0.1 → 0.2.2

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.
Files changed (191) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +342 -0
  3. data/README.md +51 -14
  4. data/TODO.md +199 -0
  5. data/ext/audio/audio_enums.c +131 -0
  6. data/ext/audio/audio_enums.h +18 -0
  7. data/ext/audio/effect_processor.c +233 -0
  8. data/ext/audio/effect_processor.h +25 -0
  9. data/ext/audio/listener.c +83 -0
  10. data/ext/audio/listener.h +8 -0
  11. data/ext/audio/music.c +174 -0
  12. data/ext/audio/music.h +14 -0
  13. data/ext/audio/sound.c +139 -0
  14. data/ext/audio/sound.h +14 -0
  15. data/ext/audio/sound_buffer.c +274 -0
  16. data/ext/audio/sound_buffer.h +18 -0
  17. data/ext/audio/sound_buffer_recorder.c +109 -0
  18. data/ext/audio/sound_buffer_recorder.h +14 -0
  19. data/ext/audio/sound_recorder.c +268 -0
  20. data/ext/audio/sound_recorder.h +14 -0
  21. data/ext/audio/sound_source.h +13 -0
  22. data/ext/audio/sound_source.inc +271 -0
  23. data/ext/audio/sound_source_cone.c +138 -0
  24. data/ext/audio/sound_source_cone.h +20 -0
  25. data/ext/audio/sound_stream.c +294 -0
  26. data/ext/audio/sound_stream.h +14 -0
  27. data/ext/{exceptions.c → core/exceptions.c} +10 -8
  28. data/ext/{ext → core}/exceptions.h +3 -3
  29. data/ext/core/foreign_thread.c +149 -0
  30. data/ext/core/foreign_thread.h +31 -0
  31. data/ext/core/macros.h +8 -0
  32. data/ext/{ext → core}/sfml.h +5 -3
  33. data/ext/core/unicode.c +46 -0
  34. data/ext/core/unicode.h +25 -0
  35. data/ext/ext.c +125 -14
  36. data/ext/extconf.rb +51 -6
  37. data/ext/graphics/blend_mode.c +261 -0
  38. data/ext/graphics/blend_mode.h +18 -0
  39. data/ext/graphics/circle.c +319 -0
  40. data/ext/{ext/klass/drawable → graphics}/circle.h +3 -3
  41. data/ext/graphics/color.c +318 -0
  42. data/ext/graphics/color.h +37 -0
  43. data/ext/{drawable.c → graphics/drawable.c} +11 -11
  44. data/ext/graphics/drawable.h +10 -0
  45. data/ext/graphics/enums.c +123 -0
  46. data/ext/graphics/enums.h +20 -0
  47. data/ext/graphics/font.c +148 -0
  48. data/ext/graphics/font.h +14 -0
  49. data/ext/graphics/glyph.c +60 -0
  50. data/ext/graphics/glyph.h +14 -0
  51. data/ext/graphics/image.c +205 -0
  52. data/ext/graphics/image.h +16 -0
  53. data/ext/graphics/polygon.c +300 -0
  54. data/ext/graphics/polygon.h +10 -0
  55. data/ext/graphics/rect.c +287 -0
  56. data/ext/graphics/rect.h +30 -0
  57. data/ext/graphics/rectangle.c +302 -0
  58. data/ext/graphics/rectangle.h +10 -0
  59. data/ext/graphics/render_state.c +204 -0
  60. data/ext/graphics/render_state.h +18 -0
  61. data/ext/graphics/render_target.inc +181 -0
  62. data/ext/graphics/render_texture.c +182 -0
  63. data/ext/graphics/render_texture.h +14 -0
  64. data/ext/graphics/shader.c +540 -0
  65. data/ext/graphics/shader.h +14 -0
  66. data/ext/graphics/shape.c +311 -0
  67. data/ext/graphics/shape.h +10 -0
  68. data/ext/graphics/sprite.c +249 -0
  69. data/ext/graphics/sprite.h +10 -0
  70. data/ext/graphics/stencil_mode.c +211 -0
  71. data/ext/graphics/stencil_mode.h +18 -0
  72. data/ext/graphics/target.c +133 -0
  73. data/ext/graphics/target.h +40 -0
  74. data/ext/graphics/text.c +344 -0
  75. data/ext/graphics/text.h +10 -0
  76. data/ext/graphics/texture.c +379 -0
  77. data/ext/graphics/texture.h +16 -0
  78. data/ext/graphics/transform.c +372 -0
  79. data/ext/graphics/transform.h +35 -0
  80. data/ext/{transformable.c → graphics/transformable.c} +168 -144
  81. data/ext/{ext/klass → graphics}/transformable.h +3 -3
  82. data/ext/graphics/vertex.c +179 -0
  83. data/ext/graphics/vertex.h +23 -0
  84. data/ext/graphics/vertex_array.c +127 -0
  85. data/ext/graphics/vertex_array.h +10 -0
  86. data/ext/graphics/vertex_buffer.c +211 -0
  87. data/ext/graphics/vertex_buffer.h +14 -0
  88. data/ext/{view.c → graphics/view.c} +168 -169
  89. data/ext/{ext/klass → graphics}/view.h +3 -3
  90. data/ext/network/ftp.c +405 -0
  91. data/ext/network/ftp.h +20 -0
  92. data/ext/network/http.c +243 -0
  93. data/ext/network/http.h +18 -0
  94. data/ext/network/ip_address.c +164 -0
  95. data/ext/network/ip_address.h +20 -0
  96. data/ext/network/network_enums.c +298 -0
  97. data/ext/network/network_enums.h +26 -0
  98. data/ext/network/packet.c +189 -0
  99. data/ext/network/packet.h +14 -0
  100. data/ext/network/socket_selector.c +142 -0
  101. data/ext/network/socket_selector.h +14 -0
  102. data/ext/network/tcp_listener.c +115 -0
  103. data/ext/network/tcp_listener.h +14 -0
  104. data/ext/network/tcp_socket.c +199 -0
  105. data/ext/network/tcp_socket.h +17 -0
  106. data/ext/network/udp_socket.c +190 -0
  107. data/ext/network/udp_socket.h +14 -0
  108. data/ext/ports.rb +97 -21
  109. data/ext/system/buffer.c +77 -0
  110. data/ext/system/buffer.h +14 -0
  111. data/ext/system/clock.c +100 -0
  112. data/ext/{ext/klass → system}/clock.h +3 -3
  113. data/ext/system/input_stream.c +229 -0
  114. data/ext/system/input_stream.h +23 -0
  115. data/ext/system/sleep.c +16 -0
  116. data/ext/system/sleep.h +8 -0
  117. data/ext/system/time.c +218 -0
  118. data/ext/system/time.h +18 -0
  119. data/ext/system/vec2.c +251 -0
  120. data/ext/system/vec2.h +55 -0
  121. data/ext/system/vec3.c +219 -0
  122. data/ext/system/vec3.h +18 -0
  123. data/ext/window/clipboard.c +89 -0
  124. data/ext/window/clipboard.h +8 -0
  125. data/ext/window/context.c +70 -0
  126. data/ext/window/context.h +8 -0
  127. data/ext/window/context_settings.c +220 -0
  128. data/ext/window/context_settings.h +18 -0
  129. data/ext/window/cursor.c +56 -0
  130. data/ext/window/cursor.h +14 -0
  131. data/ext/window/event.c +216 -0
  132. data/ext/window/event.h +14 -0
  133. data/ext/{event_name.c → window/event_name.c} +2 -2
  134. data/ext/window/event_name.h +8 -0
  135. data/ext/window/input_enums.c +261 -0
  136. data/ext/window/input_enums.h +36 -0
  137. data/ext/window/joystick.c +60 -0
  138. data/ext/window/joystick.h +8 -0
  139. data/ext/{keyboard.c → window/keyboard.c} +74 -2
  140. data/ext/window/keyboard.h +12 -0
  141. data/ext/window/mouse.c +70 -0
  142. data/ext/window/mouse.h +8 -0
  143. data/ext/window/sensor.c +30 -0
  144. data/ext/window/sensor.h +8 -0
  145. data/ext/window/touch.c +49 -0
  146. data/ext/window/touch.h +8 -0
  147. data/ext/window/video_mode.c +133 -0
  148. data/ext/window/video_mode.h +14 -0
  149. data/ext/window/vulkan.c +44 -0
  150. data/ext/window/vulkan.h +8 -0
  151. data/ext/window/window.c +414 -0
  152. data/ext/{ext/klass → window}/window.h +3 -3
  153. data/lib/sfml/version.rb +4 -2
  154. data/lib/sfml.rb +2 -0
  155. metadata +163 -56
  156. data/ext/circle.c +0 -198
  157. data/ext/clock.c +0 -65
  158. data/ext/color.c +0 -43
  159. data/ext/event.c +0 -148
  160. data/ext/ext/klass/color.h +0 -26
  161. data/ext/ext/klass/drawable/image.h +0 -4
  162. data/ext/ext/klass/drawable/polygon.h +0 -4
  163. data/ext/ext/klass/drawable/rectangle.h +0 -4
  164. data/ext/ext/klass/drawable/sprite.h +0 -4
  165. data/ext/ext/klass/drawable/texture.h +0 -4
  166. data/ext/ext/klass/event/event_name.h +0 -8
  167. data/ext/ext/klass/event.h +0 -12
  168. data/ext/ext/klass/keyboard.h +0 -8
  169. data/ext/ext/klass/render_state.h +0 -16
  170. data/ext/ext/klass/target.h +0 -18
  171. data/ext/ext/klass/transform.h +0 -17
  172. data/ext/ext/klass/video_mode.h +0 -12
  173. data/ext/ext/module/drawable/drawable.h +0 -10
  174. data/ext/ext/module/transform.h +0 -18
  175. data/ext/ext/module.h +0 -10
  176. data/ext/ext/rect.h +0 -16
  177. data/ext/ext/style_name.h +0 -11
  178. data/ext/ext/vec2.h +0 -38
  179. data/ext/image.c +0 -5
  180. data/ext/polygon.c +0 -5
  181. data/ext/rect.c +0 -26
  182. data/ext/rectangle.c +0 -5
  183. data/ext/render_state.c +0 -109
  184. data/ext/sprite.c +0 -5
  185. data/ext/style_name.c +0 -15
  186. data/ext/target.c +0 -106
  187. data/ext/texture.c +0 -5
  188. data/ext/transform.c +0 -107
  189. data/ext/vec2.c +0 -42
  190. data/ext/video_mode.c +0 -78
  191. data/ext/window.c +0 -274
@@ -0,0 +1,372 @@
1
+ #include "graphics/transform.h"
2
+
3
+ #include <ruby.h>
4
+ #include <stdio.h>
5
+
6
+ #include "system/vec2.h"
7
+ #include "graphics/rect.h"
8
+ #include "core/exceptions.h"
9
+ #include "core/macros.h"
10
+ #include "core/sfml.h"
11
+
12
+ static VALUE rb_cTransform;
13
+
14
+ static void Transform_free(void* ptr) {
15
+ xfree(ptr);
16
+ }
17
+
18
+ static size_t Transform_size(const void* ptr) {
19
+ (void)ptr;
20
+ return sizeof(sfTransform);
21
+ }
22
+
23
+ static const rb_data_type_t Transform_data_type = {
24
+ .wrap_struct_name = "SFML::Transform",
25
+ .function = {.dmark = NULL, .dfree = Transform_free, .dsize = Transform_size},
26
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY};
27
+
28
+ VALUE Get_Klass_Transform(void) {
29
+ return rb_cTransform;
30
+ }
31
+
32
+ static sfTransform* Get_Transform_Struct(VALUE self) {
33
+ sfTransform* ptr;
34
+ TypedData_Get_Struct(self, sfTransform, &Transform_data_type, ptr);
35
+ return ptr;
36
+ }
37
+
38
+ VALUE Transform_wrap(sfTransform c_transform) {
39
+ sfTransform* transform = ALLOC(sfTransform);
40
+
41
+ *transform = c_transform;
42
+
43
+ return TypedData_Wrap_Struct(rb_cTransform, &Transform_data_type, transform);
44
+ }
45
+
46
+ /* Mutating in place on a caller's object, so the frozen check is not optional:
47
+ IDENTITY is a frozen shared constant and must stay the identity. */
48
+ static sfTransform* Transform_writable(VALUE self) {
49
+ rb_check_frozen(self);
50
+ return Get_Transform_Struct(self);
51
+ }
52
+
53
+ /* -------------------------------------------------------------------------- */
54
+ /* Construction */
55
+ /* -------------------------------------------------------------------------- */
56
+
57
+ static VALUE Transform_alloc(VALUE klass) {
58
+ sfTransform* transform = ALLOC(sfTransform);
59
+
60
+ *transform = sfTransform_Identity;
61
+
62
+ return TypedData_Wrap_Struct(klass, &Transform_data_type, transform);
63
+ }
64
+
65
+ /* new -> identity
66
+ new(array_of_9) -> from that matrix
67
+ new(a00, a01, ... a22) -> from those nine elements */
68
+ static VALUE Transform_init(int argc, VALUE* argv, VALUE self) {
69
+ sfTransform* transform = Get_Transform_Struct(self);
70
+ float matrix[MATRIX_LENGTH];
71
+
72
+ if (argc == 0) {
73
+ *transform = sfTransform_Identity;
74
+ return self;
75
+ }
76
+
77
+ if (argc == 1) {
78
+ Transform_ArrayToMatrix(argv[0], matrix);
79
+ } else if (argc == MATRIX_LENGTH) {
80
+ size_t i;
81
+
82
+ for (i = 0; i < MATRIX_LENGTH; i++) {
83
+ matrix[i] = (float)NUM2DBL(argv[i]);
84
+ }
85
+ } else {
86
+ raise_invalid_arguments_excepted(MATRIX_LENGTH, (size_t)argc);
87
+ return self;
88
+ }
89
+
90
+ *transform = sfTransform_fromMatrix(matrix[0], matrix[1], matrix[2], matrix[3], matrix[4],
91
+ matrix[5], matrix[6], matrix[7], matrix[8]);
92
+
93
+ return self;
94
+ }
95
+
96
+ static VALUE Transform_s_identity(VALUE klass) {
97
+ (void)klass;
98
+ return Transform_wrap(sfTransform_Identity);
99
+ }
100
+
101
+ static VALUE Transform_s_from_a(VALUE klass, VALUE rb_matrix) {
102
+ (void)klass;
103
+ return Transform_wrap(Transform_ArrayToTransform(rb_matrix));
104
+ }
105
+
106
+ /* -------------------------------------------------------------------------- */
107
+ /* Matrix access */
108
+ /* -------------------------------------------------------------------------- */
109
+
110
+ static VALUE Transform_to_a(VALUE self) {
111
+ return Transform_MatrixToArray(Get_Transform_Struct(self)->matrix);
112
+ }
113
+
114
+ /* The 16-float 4x4 that sfTransform_getMatrix() fills, ready for glLoadMatrixf.
115
+ Deliberately not called #matrix: that name already means the 3x3 everywhere
116
+ else in this binding (Sprite#matrix, Text#matrix, RenderState#matrix). */
117
+ static VALUE Transform_gl_matrix(VALUE self) {
118
+ float matrix[GL_MATRIX_LENGTH];
119
+ VALUE rb_arr;
120
+ size_t i;
121
+
122
+ sfTransform_getMatrix(Get_Transform_Struct(self), matrix);
123
+
124
+ rb_arr = rb_ary_new_capa(GL_MATRIX_LENGTH);
125
+
126
+ for (i = 0; i < GL_MATRIX_LENGTH; i++) {
127
+ rb_ary_push(rb_arr, DBL2NUM(matrix[i]));
128
+ }
129
+
130
+ return rb_arr;
131
+ }
132
+
133
+ static VALUE Transform_eql(VALUE self, VALUE rb_other) {
134
+ sfTransform other;
135
+
136
+ if (!rb_obj_is_kind_of(rb_other, rb_cTransform) && !RB_TYPE_P(rb_other, T_ARRAY)) {
137
+ return Qfalse;
138
+ }
139
+
140
+ other = Transform_ArrayToTransform(rb_other);
141
+
142
+ return BOOL2RB(sfTransform_equal(Get_Transform_Struct(self), &other));
143
+ }
144
+
145
+ static VALUE Transform_to_s(VALUE self) {
146
+ const float* m = Get_Transform_Struct(self)->matrix;
147
+
148
+ return rb_sprintf("#<SFML::Transform [%g, %g, %g] [%g, %g, %g] [%g, %g, %g]>", m[0], m[1], m[2],
149
+ m[3], m[4], m[5], m[6], m[7], m[8]);
150
+ }
151
+
152
+ /* -------------------------------------------------------------------------- */
153
+ /* Application */
154
+ /* -------------------------------------------------------------------------- */
155
+
156
+ static VALUE Transform_transform_point(VALUE self, VALUE rb_point) {
157
+ sfVector2f point =
158
+ sfTransform_transformPoint(Get_Transform_Struct(self), vec2f_from_rb(rb_point));
159
+
160
+ return vec2f_to_rb(point);
161
+ }
162
+
163
+ static VALUE Transform_transform_rect(VALUE self, VALUE rb_rect) {
164
+ sfFloatRect rect = sfTransform_transformRect(Get_Transform_Struct(self), rect_from_rb(rb_rect));
165
+
166
+ return rect_to_rb(rect);
167
+ }
168
+
169
+ static VALUE Transform_get_inverse(VALUE self) {
170
+ return Transform_wrap(sfTransform_getInverse(Get_Transform_Struct(self)));
171
+ }
172
+
173
+ static VALUE Transform_copy(VALUE self) {
174
+ return Transform_wrap(*Get_Transform_Struct(self));
175
+ }
176
+
177
+ /* -------------------------------------------------------------------------- */
178
+ /* Mutators -- each returns self so they chain */
179
+ /* -------------------------------------------------------------------------- */
180
+
181
+ static VALUE Transform_combine_bang(VALUE self, VALUE rb_other) {
182
+ sfTransform other = Transform_ArrayToTransform(rb_other);
183
+
184
+ sfTransform_combine(Transform_writable(self), &other);
185
+
186
+ return self;
187
+ }
188
+
189
+ static VALUE Transform_mul(VALUE self, VALUE rb_other) {
190
+ sfTransform combined = *Get_Transform_Struct(self);
191
+ sfTransform other = Transform_ArrayToTransform(rb_other);
192
+
193
+ sfTransform_combine(&combined, &other);
194
+
195
+ return Transform_wrap(combined);
196
+ }
197
+
198
+ static VALUE Transform_translate_bang(VALUE self, VALUE rb_offset) {
199
+ sfTransform_translate(Transform_writable(self), vec2f_from_rb(rb_offset));
200
+ return self;
201
+ }
202
+
203
+ static VALUE Transform_rotate_bang(int argc, VALUE* argv, VALUE self) {
204
+ VALUE rb_angle, rb_center;
205
+ sfTransform* transform;
206
+
207
+ rb_scan_args(argc, argv, "11", &rb_angle, &rb_center);
208
+ transform = Transform_writable(self);
209
+
210
+ if (NIL_P(rb_center)) {
211
+ sfTransform_rotate(transform, (float)NUM2DBL(rb_angle));
212
+ } else {
213
+ sfTransform_rotateWithCenter(transform, (float)NUM2DBL(rb_angle), vec2f_from_rb(rb_center));
214
+ }
215
+
216
+ return self;
217
+ }
218
+
219
+ static VALUE Transform_scale_bang(int argc, VALUE* argv, VALUE self) {
220
+ VALUE rb_factors, rb_center;
221
+ sfTransform* transform;
222
+
223
+ rb_scan_args(argc, argv, "11", &rb_factors, &rb_center);
224
+ transform = Transform_writable(self);
225
+
226
+ if (NIL_P(rb_center)) {
227
+ sfTransform_scale(transform, vec2f_from_rb(rb_factors));
228
+ } else {
229
+ sfTransform_scaleWithCenter(transform, vec2f_from_rb(rb_factors), vec2f_from_rb(rb_center));
230
+ }
231
+
232
+ return self;
233
+ }
234
+
235
+ /* Non-mutating forms: copy, then apply. */
236
+ static VALUE Transform_translate(VALUE self, VALUE rb_offset) {
237
+ return Transform_translate_bang(Transform_copy(self), rb_offset);
238
+ }
239
+
240
+ static VALUE Transform_rotate(int argc, VALUE* argv, VALUE self) {
241
+ return Transform_rotate_bang(argc, argv, Transform_copy(self));
242
+ }
243
+
244
+ static VALUE Transform_scale(int argc, VALUE* argv, VALUE self) {
245
+ return Transform_scale_bang(argc, argv, Transform_copy(self));
246
+ }
247
+
248
+ /* -------------------------------------------------------------------------- */
249
+ /* Deprecated module-function API */
250
+ /* -------------------------------------------------------------------------- */
251
+
252
+ /* Transform used to be a module whose two functions took and returned plain
253
+ 9-element Arrays. Both are kept, Array-in/Array-out, so existing callers are
254
+ unaffected by the promotion to a class. */
255
+ static VALUE Transform_s_combine(VALUE klass, VALUE rb_arr_a, VALUE rb_arr_b) {
256
+ sfTransform transform_a = Transform_ArrayToTransform(rb_arr_a);
257
+ sfTransform transform_b = Transform_ArrayToTransform(rb_arr_b);
258
+
259
+ (void)klass;
260
+
261
+ sfTransform_combine(&transform_a, &transform_b);
262
+
263
+ return Transform_MatrixToArray(transform_a.matrix);
264
+ }
265
+
266
+ static VALUE Transform_s_inverse(VALUE klass, VALUE rb_matrix) {
267
+ sfTransform transform = Transform_ArrayToTransform(rb_matrix);
268
+
269
+ (void)klass;
270
+
271
+ return Transform_MatrixToArray(sfTransform_getInverse(&transform).matrix);
272
+ }
273
+
274
+ void Init_Transform(VALUE rb_module) {
275
+ rb_cTransform = rb_define_class_under(rb_module, "Transform", rb_cObject);
276
+
277
+ rb_define_alloc_func(rb_cTransform, Transform_alloc);
278
+ rb_define_method(rb_cTransform, "initialize", Transform_init, -1);
279
+
280
+ rb_define_singleton_method(rb_cTransform, "identity", Transform_s_identity, 0);
281
+ rb_define_singleton_method(rb_cTransform, "from_a", Transform_s_from_a, 1);
282
+ rb_define_singleton_method(rb_cTransform, "combine", Transform_s_combine, 2);
283
+ rb_define_singleton_method(rb_cTransform, "inverse", Transform_s_inverse, 1);
284
+
285
+ rb_define_method(rb_cTransform, "to_a", Transform_to_a, 0);
286
+ rb_define_method(rb_cTransform, "to_ary", Transform_to_a, 0);
287
+ rb_define_method(rb_cTransform, "matrix", Transform_to_a, 0);
288
+ rb_define_method(rb_cTransform, "gl_matrix", Transform_gl_matrix, 0);
289
+ rb_define_method(rb_cTransform, "==", Transform_eql, 1);
290
+ rb_define_method(rb_cTransform, "to_s", Transform_to_s, 0);
291
+ rb_define_method(rb_cTransform, "inspect", Transform_to_s, 0);
292
+
293
+ rb_define_method(rb_cTransform, "copy", Transform_copy, 0);
294
+ rb_define_method(rb_cTransform, "inverse", Transform_get_inverse, 0);
295
+ rb_define_method(rb_cTransform, "transform_point", Transform_transform_point, 1);
296
+ rb_define_method(rb_cTransform, "transform_rect", Transform_transform_rect, 1);
297
+
298
+ rb_define_method(rb_cTransform, "*", Transform_mul, 1);
299
+ rb_define_method(rb_cTransform, "combine", Transform_mul, 1);
300
+ rb_define_method(rb_cTransform, "combine!", Transform_combine_bang, 1);
301
+ rb_define_method(rb_cTransform, "translate", Transform_translate, 1);
302
+ rb_define_method(rb_cTransform, "translate!", Transform_translate_bang, 1);
303
+ rb_define_method(rb_cTransform, "rotate", Transform_rotate, -1);
304
+ rb_define_method(rb_cTransform, "rotate!", Transform_rotate_bang, -1);
305
+ rb_define_method(rb_cTransform, "scale", Transform_scale, -1);
306
+ rb_define_method(rb_cTransform, "scale!", Transform_scale_bang, -1);
307
+
308
+ rb_define_const(rb_cTransform, "IDENTITY", rb_obj_freeze(Transform_wrap(sfTransform_Identity)));
309
+ }
310
+
311
+ /* -------------------------------------------------------------------------- */
312
+ /* Conversion helpers, shared with every class that exposes a transform */
313
+ /* -------------------------------------------------------------------------- */
314
+
315
+ VALUE Transform_MatrixToArray(float* c_matrix) {
316
+ VALUE rb_arr;
317
+ size_t i;
318
+
319
+ if (c_matrix == NULL) {
320
+ return Qnil;
321
+ }
322
+
323
+ rb_arr = rb_ary_new_capa(MATRIX_LENGTH);
324
+
325
+ for (i = 0; i < MATRIX_LENGTH; i++) {
326
+ rb_ary_push(rb_arr, DBL2NUM(c_matrix[i]));
327
+ }
328
+
329
+ return rb_arr;
330
+ }
331
+
332
+ void Transform_ArrayToMatrix(VALUE rb_matrix, float* c_matrix) {
333
+ size_t i;
334
+
335
+ if (c_matrix == NULL) {
336
+ return;
337
+ }
338
+
339
+ if (rb_obj_is_kind_of(rb_matrix, rb_cTransform)) {
340
+ const float* source = Get_Transform_Struct(rb_matrix)->matrix;
341
+
342
+ for (i = 0; i < MATRIX_LENGTH; i++) {
343
+ c_matrix[i] = source[i];
344
+ }
345
+
346
+ return;
347
+ }
348
+
349
+ if (!RB_TYPE_P(rb_matrix, T_ARRAY)) {
350
+ raise_invalid_argument_class(rb_cTransform);
351
+ }
352
+
353
+ if (rb_array_len(rb_matrix) != MATRIX_LENGTH) {
354
+ raise_invalid_array_length(MATRIX_LENGTH);
355
+ }
356
+
357
+ for (i = 0; i < MATRIX_LENGTH; i++) {
358
+ c_matrix[i] = (float)NUM2DBL(rb_ary_entry(rb_matrix, (long)i));
359
+ }
360
+ }
361
+
362
+ sfTransform Transform_ArrayToTransform(VALUE rb_matrix) {
363
+ sfTransform transform;
364
+
365
+ Transform_ArrayToMatrix(rb_matrix, transform.matrix);
366
+
367
+ return transform;
368
+ }
369
+
370
+ VALUE Transform_TransformToArray(sfTransform c_transform) {
371
+ return Transform_MatrixToArray(c_transform.matrix);
372
+ }
@@ -0,0 +1,35 @@
1
+ #ifndef SFML_RB_GRAPHICS_TRANSFORM_H
2
+ #define SFML_RB_GRAPHICS_TRANSFORM_H
3
+
4
+ #include <ruby.h>
5
+
6
+ #include "core/sfml.h"
7
+
8
+ /* The 3x3 affine matrix SFML stores in sfTransform. Distinct from the 16-float
9
+ 4x4 OpenGL matrix that sfTransform_getMatrix() fills -- see Transform#gl_matrix. */
10
+ #define MATRIX_LENGTH 9 // 3x3
11
+
12
+ /* The 4x4 column-major matrix sfTransform_getMatrix() writes, for glLoadMatrixf. */
13
+ #define GL_MATRIX_LENGTH 16 // 4x4
14
+
15
+ void Init_Transform(VALUE rb_module);
16
+
17
+ VALUE Get_Klass_Transform(void);
18
+
19
+ /* Every class that exposes #transform / #inverse_transform returns the 3x3 as a
20
+ plain Array through this, which is why those getters are Arrays and not
21
+ Transform instances. */
22
+ VALUE Transform_MatrixToArray(float* c_matrix);
23
+
24
+ void Transform_ArrayToMatrix(VALUE rb_matrix, float* c_matrix);
25
+
26
+ /* Accepts either a Transform instance or a 9-element Array, so anything that
27
+ takes a transform from the caller accepts both forms. */
28
+ sfTransform Transform_ArrayToTransform(VALUE rb_matrix);
29
+
30
+ VALUE Transform_TransformToArray(sfTransform c_transform);
31
+
32
+ /* Wraps a transform by value into a new Transform instance. */
33
+ VALUE Transform_wrap(sfTransform c_transform);
34
+
35
+ #endif // SFML_RB_GRAPHICS_TRANSFORM_H