sfml3-rb 0.0.1 → 0.2.1

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 +321 -0
  3. data/README.md +44 -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 +88 -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
data/ext/audio/sound.c ADDED
@@ -0,0 +1,139 @@
1
+ #include "audio/sound.h"
2
+
3
+ #include <ruby.h>
4
+ #include <ruby/thread.h>
5
+ #include <stdlib.h>
6
+
7
+ #include "audio/effect_processor.h"
8
+ #include "audio/sound_buffer.h"
9
+ #include "audio/sound_source.h"
10
+ #include "core/exceptions.h"
11
+
12
+ typedef struct {
13
+ SoundSource source;
14
+ VALUE rb_buffer;
15
+ } Sound;
16
+
17
+ static VALUE rb_cSound;
18
+
19
+ static void Sound_mark(void* ptr) {
20
+ Sound* sound = ptr;
21
+
22
+ rb_gc_mark(sound->rb_buffer);
23
+ }
24
+
25
+ /* sfSound_destroy() calls Sound::stop(), which can block waiting on the audio
26
+ thread (see the matching comment on SS_METHOD(stop) in sound_source.inc);
27
+ release the GVL for the same reason. */
28
+ static void* Sound_destroy_without_gvl(void* handle) {
29
+ sfSound_destroy(handle);
30
+ return NULL;
31
+ }
32
+
33
+ static void Sound_free(void* ptr) {
34
+ Sound* sound = ptr;
35
+
36
+ effect_processor_release(sound->source.effect_slot);
37
+ rb_thread_call_without_gvl(Sound_destroy_without_gvl, sound->source.handle, RUBY_UBF_IO, NULL);
38
+ free(sound);
39
+ }
40
+
41
+ static const rb_data_type_t Sound_data_type = {
42
+ .wrap_struct_name = "SFML::Sound",
43
+ .function = {.dmark = Sound_mark, .dfree = Sound_free, .dsize = NULL},
44
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY};
45
+
46
+ static VALUE Sound_wrap(VALUE klass, sfSound* handle, VALUE rb_buffer) {
47
+ Sound* ptr;
48
+
49
+ if (handle == NULL) {
50
+ rb_raise(rb_eRuntimeError, "failed to create sound");
51
+ }
52
+
53
+ ptr = malloc(sizeof(Sound));
54
+ ptr->source.handle = handle;
55
+ ptr->source.effect_slot = -1;
56
+ ptr->rb_buffer = rb_buffer;
57
+
58
+ return TypedData_Wrap_Struct(klass, &Sound_data_type, ptr);
59
+ }
60
+
61
+ static VALUE Sound_new(VALUE klass, VALUE rb_buffer) {
62
+ if (!rb_obj_is_kind_of(rb_buffer, Get_Klass_SoundBuffer())) {
63
+ raise_invalid_argument_class(Get_Klass_SoundBuffer());
64
+ }
65
+
66
+ return Sound_wrap(klass, sfSound_create(Get_SoundBuffer_Struct(rb_buffer)), rb_buffer);
67
+ }
68
+
69
+ static VALUE Sound_copy(VALUE self) {
70
+ Sound* sound = (Sound*)Get_Sound_Struct(self);
71
+
72
+ return Sound_wrap(Get_Klass_Sound(), sfSound_copy((const sfSound*)sound->source.handle),
73
+ sound->rb_buffer);
74
+ }
75
+
76
+ static VALUE Sound_get_buffer(VALUE self) {
77
+ return ((Sound*)Get_Sound_Struct(self))->rb_buffer;
78
+ }
79
+
80
+ typedef struct {
81
+ sfSound* handle;
82
+ const sfSoundBuffer* buffer;
83
+ } SoundSetBufferArgs;
84
+
85
+ /* sfSound_setBuffer() calls Sound::stop() when a buffer is already attached,
86
+ which can block waiting on the audio thread; release the GVL for the same
87
+ reason as Sound_free/SS_METHOD(stop). */
88
+ static void* Sound_set_buffer_without_gvl(void* raw) {
89
+ SoundSetBufferArgs* args = raw;
90
+
91
+ sfSound_setBuffer(args->handle, args->buffer);
92
+
93
+ return NULL;
94
+ }
95
+
96
+ static VALUE Sound_set_buffer(VALUE self, VALUE rb_buffer) {
97
+ Sound* sound = (Sound*)Get_Sound_Struct(self);
98
+ SoundSetBufferArgs args;
99
+
100
+ if (!rb_obj_is_kind_of(rb_buffer, Get_Klass_SoundBuffer())) {
101
+ raise_invalid_argument_class(Get_Klass_SoundBuffer());
102
+ }
103
+
104
+ args.handle = sound->source.handle;
105
+ args.buffer = Get_SoundBuffer_Struct(rb_buffer);
106
+
107
+ rb_thread_call_without_gvl(Sound_set_buffer_without_gvl, &args, RUBY_UBF_IO, NULL);
108
+ sound->rb_buffer = rb_buffer;
109
+
110
+ return rb_buffer;
111
+ }
112
+
113
+ #define SS_FN(name) sfSound_##name
114
+ #define SS_METHOD(name) Sound_##name
115
+ #include "audio/sound_source.inc"
116
+ #undef SS_FN
117
+ #undef SS_METHOD
118
+
119
+ void Init_Sound(VALUE rb_module) {
120
+ rb_cSound = rb_define_class_under(rb_module, "Sound", rb_cObject);
121
+
122
+ rb_define_singleton_method(rb_cSound, "new", Sound_new, 1);
123
+
124
+ rb_define_method(rb_cSound, "copy", Sound_copy, 0);
125
+ rb_define_method(rb_cSound, "buffer", Sound_get_buffer, 0);
126
+ rb_define_method(rb_cSound, "buffer=", Sound_set_buffer, 1);
127
+
128
+ Sound_define_sound_source_methods(rb_cSound);
129
+ }
130
+
131
+ VALUE Get_Klass_Sound(void) {
132
+ return rb_cSound;
133
+ }
134
+
135
+ void* Get_Sound_Struct(VALUE self) {
136
+ Sound* ptr;
137
+ TypedData_Get_Struct(self, Sound, &Sound_data_type, ptr);
138
+ return ptr;
139
+ }
data/ext/audio/sound.h ADDED
@@ -0,0 +1,14 @@
1
+ #ifndef SFML_RB_AUDIO_SOUND_H
2
+ #define SFML_RB_AUDIO_SOUND_H
3
+
4
+ #include <ruby.h>
5
+
6
+ #include "core/sfml.h"
7
+
8
+ void Init_Sound(VALUE rb_module);
9
+
10
+ VALUE Get_Klass_Sound(void);
11
+
12
+ void *Get_Sound_Struct(VALUE self);
13
+
14
+ #endif //SFML_RB_AUDIO_SOUND_H
@@ -0,0 +1,274 @@
1
+ #include "audio/sound_buffer.h"
2
+
3
+ #include <ruby.h>
4
+ #include <stdint.h>
5
+ #include <stdlib.h>
6
+ #include <string.h>
7
+
8
+ #include "audio/audio_enums.h"
9
+ #include "core/exceptions.h"
10
+ #include "core/macros.h"
11
+ #include "system/input_stream.h"
12
+ #include "system/time.h"
13
+
14
+ typedef struct {
15
+ sfSoundBuffer *buffer;
16
+ bool owns;
17
+ } SoundBuffer;
18
+
19
+ static VALUE rb_cSoundBuffer;
20
+
21
+ static void SoundBuffer_free(void *ptr) {
22
+ SoundBuffer *sound_buffer = ptr;
23
+
24
+ if (sound_buffer->owns) {
25
+ sfSoundBuffer_destroy(sound_buffer->buffer);
26
+ }
27
+
28
+ free(sound_buffer);
29
+ }
30
+
31
+ static const rb_data_type_t SoundBuffer_data_type = {
32
+ .wrap_struct_name = "SFML::SoundBuffer",
33
+ .function = {.dmark = NULL, .dfree = SoundBuffer_free, .dsize = NULL},
34
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY
35
+ };
36
+
37
+ static VALUE SoundBuffer_wrap(VALUE klass, sfSoundBuffer *buffer) {
38
+ SoundBuffer *ptr;
39
+
40
+ if (buffer == NULL) {
41
+ rb_raise(rb_eRuntimeError, "failed to create sound buffer");
42
+ }
43
+
44
+ ptr = malloc(sizeof(SoundBuffer));
45
+ ptr->buffer = buffer;
46
+ ptr->owns = true;
47
+
48
+ return TypedData_Wrap_Struct(klass, &SoundBuffer_data_type, ptr);
49
+ }
50
+
51
+ VALUE sound_buffer_from_borrowed(const sfSoundBuffer *buffer) {
52
+ SoundBuffer *ptr;
53
+
54
+ if (buffer == NULL) {
55
+ return Qnil;
56
+ }
57
+
58
+ ptr = malloc(sizeof(SoundBuffer));
59
+ ptr->buffer = (sfSoundBuffer *) buffer;
60
+ ptr->owns = false;
61
+
62
+ return TypedData_Wrap_Struct(rb_cSoundBuffer, &SoundBuffer_data_type, ptr);
63
+ }
64
+
65
+ static VALUE SoundBuffer_from_file(VALUE klass, VALUE rb_path) {
66
+ return SoundBuffer_wrap(klass, sfSoundBuffer_createFromFile(StringValueCStr(rb_path)));
67
+ }
68
+
69
+ static VALUE SoundBuffer_from_memory(VALUE klass, VALUE rb_data) {
70
+ StringValue(rb_data);
71
+
72
+ return SoundBuffer_wrap(klass, sfSoundBuffer_createFromMemory(RSTRING_PTR(rb_data),
73
+ (size_t) RSTRING_LEN(rb_data)));
74
+ }
75
+
76
+ static VALUE SoundBuffer_from_stream(VALUE klass, VALUE rb_stream) {
77
+ VALUE holder = Qnil;
78
+ sfInputStream *stream = input_stream_from_rb(rb_stream, &holder);
79
+
80
+ (void) holder;
81
+
82
+ return SoundBuffer_wrap(klass, sfSoundBuffer_createFromStream(stream));
83
+ }
84
+
85
+ /* SFML rejects a buffer whose channel map does not have exactly one entry per
86
+ channel, so an omitted map is filled with the conventional layout for 1 and
87
+ 2 channels and left Unspecified beyond that. */
88
+ static void SoundBuffer_default_channel_map(unsigned int channel_count, sfSoundChannel *map) {
89
+ static const sfSoundChannel surround[] = {
90
+ sfSoundChannelFrontLeft, sfSoundChannelFrontRight, sfSoundChannelFrontCenter,
91
+ sfSoundChannelLowFrequencyEffects, sfSoundChannelBackLeft, sfSoundChannelBackRight,
92
+ sfSoundChannelSideLeft, sfSoundChannelSideRight, sfSoundChannelTopCenter
93
+ };
94
+ unsigned int i;
95
+
96
+ for (i = 0; i < channel_count; i++) {
97
+ if (i < sizeof(surround) / sizeof(surround[0])) {
98
+ map[i] = surround[i];
99
+ } else {
100
+ map[i] = sfSoundChannelUnspecified;
101
+ }
102
+ }
103
+
104
+ if (channel_count == 1) {
105
+ map[0] = sfSoundChannelMono;
106
+ }
107
+ }
108
+
109
+ /* Copies the samples into a contiguous int16 buffer: an Array is converted
110
+ element by element, a String is memcpy'd out of (possibly unaligned) Ruby
111
+ storage. The channel map, when given, is translated symbol by symbol. */
112
+ static VALUE SoundBuffer_from_samples(int argc, VALUE *argv, VALUE klass) {
113
+ VALUE rb_samples, rb_channel_count, rb_sample_rate, rb_channel_map;
114
+ int16_t *samples;
115
+ sfSoundChannel *channel_map;
116
+ unsigned int channel_count;
117
+ uint64_t sample_count;
118
+ sfSoundBuffer *buffer;
119
+
120
+ rb_scan_args(argc, argv, "31", &rb_samples, &rb_channel_count, &rb_sample_rate,
121
+ &rb_channel_map);
122
+
123
+ channel_count = (unsigned int) NUM2INT(rb_channel_count);
124
+
125
+ if (channel_count == 0) {
126
+ rb_raise(rb_eArgError, "channel count must be positive");
127
+ }
128
+
129
+ if (RB_TYPE_P(rb_samples, T_STRING)) {
130
+ size_t bytes = (size_t) RSTRING_LEN(rb_samples);
131
+
132
+ if (bytes % sizeof(int16_t) != 0) {
133
+ rb_raise(rb_eArgError, "sample string length must be a multiple of 2 bytes");
134
+ }
135
+
136
+ sample_count = bytes / sizeof(int16_t);
137
+ samples = malloc(bytes > 0 ? bytes : 1);
138
+
139
+ if (bytes > 0) {
140
+ memcpy(samples, RSTRING_PTR(rb_samples), bytes);
141
+ }
142
+ } else if (RB_TYPE_P(rb_samples, T_ARRAY)) {
143
+ long i;
144
+
145
+ sample_count = (uint64_t) RARRAY_LEN(rb_samples);
146
+ samples = malloc(sizeof(int16_t) * ((size_t) sample_count + 1));
147
+
148
+ for (i = 0; i < (long) sample_count; i++) {
149
+ samples[i] = (int16_t) NUM2INT(rb_ary_entry(rb_samples, i));
150
+ }
151
+ } else {
152
+ rb_raise(rb_eArgError, "samples must be an Array of integers or a String");
153
+ return Qnil;
154
+ }
155
+
156
+ channel_map = malloc(sizeof(sfSoundChannel) * channel_count);
157
+
158
+ if (NIL_P(rb_channel_map)) {
159
+ SoundBuffer_default_channel_map(channel_count, channel_map);
160
+ } else {
161
+ long i;
162
+
163
+ if (!RB_TYPE_P(rb_channel_map, T_ARRAY) ||
164
+ (unsigned long) RARRAY_LEN(rb_channel_map) != channel_count) {
165
+ free(samples);
166
+ free(channel_map);
167
+ rb_raise(rb_eArgError, "channel map must have exactly one entry per channel");
168
+ }
169
+
170
+ for (i = 0; i < (long) channel_count; i++) {
171
+ channel_map[i] = sound_channel_from_rb(rb_ary_entry(rb_channel_map, i));
172
+ }
173
+ }
174
+
175
+ buffer = sfSoundBuffer_createFromSamples(samples, sample_count, channel_count,
176
+ (unsigned int) NUM2INT(rb_sample_rate), channel_map,
177
+ channel_count);
178
+
179
+ free(samples);
180
+ free(channel_map);
181
+
182
+ return SoundBuffer_wrap(klass, buffer);
183
+ }
184
+
185
+ static VALUE SoundBuffer_copy(VALUE self) {
186
+ return SoundBuffer_wrap(Get_Klass_SoundBuffer(),
187
+ sfSoundBuffer_copy(Get_SoundBuffer_Struct(self)));
188
+ }
189
+
190
+ static VALUE SoundBuffer_save_to_file(VALUE self, VALUE rb_path) {
191
+ return BOOL2RB(sfSoundBuffer_saveToFile(Get_SoundBuffer_Struct(self), StringValueCStr(rb_path)));
192
+ }
193
+
194
+ static VALUE SoundBuffer_samples(VALUE self) {
195
+ const int16_t *samples = sfSoundBuffer_getSamples(Get_SoundBuffer_Struct(self));
196
+ uint64_t count = sfSoundBuffer_getSampleCount(Get_SoundBuffer_Struct(self));
197
+ VALUE rb_array;
198
+ uint64_t i;
199
+
200
+ if (samples == NULL) {
201
+ return rb_ary_new();
202
+ }
203
+
204
+ rb_array = rb_ary_new_capa((long) count);
205
+
206
+ for (i = 0; i < count; i++) {
207
+ rb_ary_push(rb_array, INT2NUM(samples[i]));
208
+ }
209
+
210
+ return rb_array;
211
+ }
212
+
213
+ static VALUE SoundBuffer_sample_count(VALUE self) {
214
+ return ULL2NUM(sfSoundBuffer_getSampleCount(Get_SoundBuffer_Struct(self)));
215
+ }
216
+
217
+ static VALUE SoundBuffer_sample_rate(VALUE self) {
218
+ return UINT2NUM(sfSoundBuffer_getSampleRate(Get_SoundBuffer_Struct(self)));
219
+ }
220
+
221
+ static VALUE SoundBuffer_channel_count(VALUE self) {
222
+ return UINT2NUM(sfSoundBuffer_getChannelCount(Get_SoundBuffer_Struct(self)));
223
+ }
224
+
225
+ static VALUE SoundBuffer_channel_map(VALUE self) {
226
+ size_t count = 0;
227
+ sfSoundChannel *map = sfSoundBuffer_getChannelMap(Get_SoundBuffer_Struct(self), &count);
228
+ VALUE rb_array;
229
+ size_t i;
230
+
231
+ if (map == NULL) {
232
+ return rb_ary_new();
233
+ }
234
+
235
+ rb_array = rb_ary_new_capa((long) count);
236
+
237
+ for (i = 0; i < count; i++) {
238
+ rb_ary_push(rb_array, ID2SYM(rb_intern(sound_channel_name(map[i]))));
239
+ }
240
+
241
+ return rb_array;
242
+ }
243
+
244
+ static VALUE SoundBuffer_duration(VALUE self) {
245
+ return time_to_rb(sfSoundBuffer_getDuration(Get_SoundBuffer_Struct(self)));
246
+ }
247
+
248
+ void Init_SoundBuffer(VALUE rb_module) {
249
+ rb_cSoundBuffer = rb_define_class_under(rb_module, "SoundBuffer", rb_cObject);
250
+
251
+ rb_define_singleton_method(rb_cSoundBuffer, "from_file", SoundBuffer_from_file, 1);
252
+ rb_define_singleton_method(rb_cSoundBuffer, "from_memory", SoundBuffer_from_memory, 1);
253
+ rb_define_singleton_method(rb_cSoundBuffer, "from_stream", SoundBuffer_from_stream, 1);
254
+ rb_define_singleton_method(rb_cSoundBuffer, "from_samples", SoundBuffer_from_samples, -1);
255
+
256
+ rb_define_method(rb_cSoundBuffer, "copy", SoundBuffer_copy, 0);
257
+ rb_define_method(rb_cSoundBuffer, "save_to_file", SoundBuffer_save_to_file, 1);
258
+ rb_define_method(rb_cSoundBuffer, "samples", SoundBuffer_samples, 0);
259
+ rb_define_method(rb_cSoundBuffer, "sample_count", SoundBuffer_sample_count, 0);
260
+ rb_define_method(rb_cSoundBuffer, "sample_rate", SoundBuffer_sample_rate, 0);
261
+ rb_define_method(rb_cSoundBuffer, "channel_count", SoundBuffer_channel_count, 0);
262
+ rb_define_method(rb_cSoundBuffer, "channel_map", SoundBuffer_channel_map, 0);
263
+ rb_define_method(rb_cSoundBuffer, "duration", SoundBuffer_duration, 0);
264
+ }
265
+
266
+ VALUE Get_Klass_SoundBuffer(void) {
267
+ return rb_cSoundBuffer;
268
+ }
269
+
270
+ void *Get_SoundBuffer_Struct(VALUE self) {
271
+ SoundBuffer *ptr;
272
+ TypedData_Get_Struct(self, SoundBuffer, &SoundBuffer_data_type, ptr);
273
+ return ptr->buffer;
274
+ }
@@ -0,0 +1,18 @@
1
+ #ifndef SFML_RB_AUDIO_SOUND_BUFFER_H
2
+ #define SFML_RB_AUDIO_SOUND_BUFFER_H
3
+
4
+ #include <ruby.h>
5
+
6
+ #include "core/sfml.h"
7
+
8
+ void Init_SoundBuffer(VALUE rb_module);
9
+
10
+ VALUE Get_Klass_SoundBuffer(void);
11
+
12
+ void *Get_SoundBuffer_Struct(VALUE self);
13
+
14
+ /* Wraps a buffer owned by someone else (a SoundBufferRecorder) without taking
15
+ ownership, so freeing the wrapper must not destroy it. */
16
+ VALUE sound_buffer_from_borrowed(const sfSoundBuffer *buffer);
17
+
18
+ #endif //SFML_RB_AUDIO_SOUND_BUFFER_H
@@ -0,0 +1,109 @@
1
+ #include "audio/sound_buffer_recorder.h"
2
+
3
+ #include <ruby.h>
4
+ #include <stdlib.h>
5
+
6
+ #include "audio/audio_enums.h"
7
+ #include "audio/sound_buffer.h"
8
+ #include "core/macros.h"
9
+
10
+ typedef struct {
11
+ sfSoundBufferRecorder *handle;
12
+ } SoundBufferRecorder;
13
+
14
+ static VALUE rb_cSoundBufferRecorder;
15
+
16
+ static void SoundBufferRecorder_free(void *ptr) {
17
+ SoundBufferRecorder *recorder = ptr;
18
+
19
+ if (recorder->handle != NULL) {
20
+ sfSoundBufferRecorder_destroy(recorder->handle);
21
+ }
22
+
23
+ free(recorder);
24
+ }
25
+
26
+ static const rb_data_type_t SoundBufferRecorder_data_type = {
27
+ .wrap_struct_name = "SFML::SoundBufferRecorder",
28
+ .function = {.dmark = NULL, .dfree = SoundBufferRecorder_free, .dsize = NULL},
29
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY
30
+ };
31
+
32
+ static VALUE SoundBufferRecorder_wrap(VALUE klass, sfSoundBufferRecorder *handle) {
33
+ SoundBufferRecorder *ptr;
34
+
35
+ if (handle == NULL) {
36
+ rb_raise(rb_eRuntimeError, "failed to create sound buffer recorder (no capture device?)");
37
+ }
38
+
39
+ ptr = malloc(sizeof(SoundBufferRecorder));
40
+ ptr->handle = handle;
41
+
42
+ return TypedData_Wrap_Struct(klass, &SoundBufferRecorder_data_type, ptr);
43
+ }
44
+
45
+ static VALUE SoundBufferRecorder_new(VALUE klass) {
46
+ return SoundBufferRecorder_wrap(klass, sfSoundBufferRecorder_create());
47
+ }
48
+
49
+ static VALUE SoundBufferRecorder_start(VALUE self, VALUE rb_sample_rate) {
50
+ return BOOL2RB(sfSoundBufferRecorder_start(Get_SoundBufferRecorder_Struct(self),
51
+ (unsigned int) NUM2INT(rb_sample_rate)));
52
+ }
53
+
54
+ static VALUE SoundBufferRecorder_stop(VALUE self) {
55
+ sfSoundBufferRecorder_stop(Get_SoundBufferRecorder_Struct(self));
56
+ return self;
57
+ }
58
+
59
+ static VALUE SoundBufferRecorder_sample_rate(VALUE self) {
60
+ return UINT2NUM(sfSoundBufferRecorder_getSampleRate(Get_SoundBufferRecorder_Struct(self)));
61
+ }
62
+
63
+ static VALUE SoundBufferRecorder_buffer(VALUE self) {
64
+ return sound_buffer_from_borrowed(sfSoundBufferRecorder_getBuffer(Get_SoundBufferRecorder_Struct(self)));
65
+ }
66
+
67
+ static VALUE SoundBufferRecorder_device(VALUE self) {
68
+ return rb_str_new_cstr(sfSoundBufferRecorder_getDevice(Get_SoundBufferRecorder_Struct(self)));
69
+ }
70
+
71
+ static VALUE SoundBufferRecorder_set_device(VALUE self, VALUE rb_name) {
72
+ return BOOL2RB(sfSoundBufferRecorder_setDevice(Get_SoundBufferRecorder_Struct(self),
73
+ StringValueCStr(rb_name)));
74
+ }
75
+
76
+ static VALUE SoundBufferRecorder_channel_count(VALUE self) {
77
+ return UINT2NUM(sfSoundBufferRecorder_getChannelCount(Get_SoundBufferRecorder_Struct(self)));
78
+ }
79
+
80
+ static VALUE SoundBufferRecorder_set_channel_count(VALUE self, VALUE rb_count) {
81
+ sfSoundBufferRecorder_setChannelCount(Get_SoundBufferRecorder_Struct(self),
82
+ (unsigned int) NUM2INT(rb_count));
83
+ return rb_count;
84
+ }
85
+
86
+ void Init_SoundBufferRecorder(VALUE rb_module) {
87
+ rb_cSoundBufferRecorder = rb_define_class_under(rb_module, "SoundBufferRecorder", rb_cObject);
88
+
89
+ rb_define_singleton_method(rb_cSoundBufferRecorder, "new", SoundBufferRecorder_new, 0);
90
+
91
+ rb_define_method(rb_cSoundBufferRecorder, "start", SoundBufferRecorder_start, 1);
92
+ rb_define_method(rb_cSoundBufferRecorder, "stop", SoundBufferRecorder_stop, 0);
93
+ rb_define_method(rb_cSoundBufferRecorder, "sample_rate", SoundBufferRecorder_sample_rate, 0);
94
+ rb_define_method(rb_cSoundBufferRecorder, "buffer", SoundBufferRecorder_buffer, 0);
95
+ rb_define_method(rb_cSoundBufferRecorder, "device", SoundBufferRecorder_device, 0);
96
+ rb_define_method(rb_cSoundBufferRecorder, "device=", SoundBufferRecorder_set_device, 1);
97
+ rb_define_method(rb_cSoundBufferRecorder, "channel_count", SoundBufferRecorder_channel_count, 0);
98
+ rb_define_method(rb_cSoundBufferRecorder, "channel_count=", SoundBufferRecorder_set_channel_count, 1);
99
+ }
100
+
101
+ VALUE Get_Klass_SoundBufferRecorder(void) {
102
+ return rb_cSoundBufferRecorder;
103
+ }
104
+
105
+ void *Get_SoundBufferRecorder_Struct(VALUE self) {
106
+ SoundBufferRecorder *ptr;
107
+ TypedData_Get_Struct(self, SoundBufferRecorder, &SoundBufferRecorder_data_type, ptr);
108
+ return ptr->handle;
109
+ }
@@ -0,0 +1,14 @@
1
+ #ifndef SFML_RB_AUDIO_SOUND_BUFFER_RECORDER_H
2
+ #define SFML_RB_AUDIO_SOUND_BUFFER_RECORDER_H
3
+
4
+ #include <ruby.h>
5
+
6
+ #include "core/sfml.h"
7
+
8
+ void Init_SoundBufferRecorder(VALUE rb_module);
9
+
10
+ VALUE Get_Klass_SoundBufferRecorder(void);
11
+
12
+ void *Get_SoundBufferRecorder_Struct(VALUE self);
13
+
14
+ #endif //SFML_RB_AUDIO_SOUND_BUFFER_RECORDER_H