sfml3-rb 0.2.2 → 0.3.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 (148) hide show
  1. checksums.yaml +4 -4
  2. data/.yardopts +9 -0
  3. data/CHANGELOG.md +67 -16
  4. data/README.md +123 -32
  5. data/TODO.md +18 -11
  6. data/ext/audio/audio_enums.c +58 -20
  7. data/ext/audio/listener.c +89 -6
  8. data/ext/audio/music.c +212 -5
  9. data/ext/audio/sound.c +218 -9
  10. data/ext/audio/sound_buffer.c +197 -47
  11. data/ext/audio/sound_buffer_recorder.c +90 -18
  12. data/ext/audio/sound_recorder.c +188 -58
  13. data/ext/audio/sound_source_cone.c +127 -33
  14. data/ext/audio/sound_stream.c +204 -21
  15. data/ext/core/exceptions.c +10 -8
  16. data/ext/core/unicode.c +12 -2
  17. data/ext/ext.c +8 -0
  18. data/ext/graphics/blend_mode.c +218 -60
  19. data/ext/graphics/circle.c +300 -16
  20. data/ext/graphics/color.c +253 -94
  21. data/ext/graphics/drawable.c +16 -4
  22. data/ext/graphics/font.c +144 -25
  23. data/ext/graphics/glyph.c +38 -8
  24. data/ext/graphics/image.c +205 -41
  25. data/ext/graphics/polygon.c +317 -30
  26. data/ext/graphics/rect.c +225 -60
  27. data/ext/graphics/rectangle.c +307 -35
  28. data/ext/graphics/render_state.c +124 -12
  29. data/ext/graphics/render_texture.c +191 -14
  30. data/ext/graphics/render_window.c +55 -0
  31. data/ext/graphics/render_window.h +10 -0
  32. data/ext/graphics/shader.c +289 -17
  33. data/ext/graphics/shape.c +265 -32
  34. data/ext/graphics/sprite.c +245 -30
  35. data/ext/graphics/stencil_mode.c +163 -46
  36. data/ext/graphics/target.c +121 -43
  37. data/ext/graphics/target.h +31 -17
  38. data/ext/graphics/text.c +339 -19
  39. data/ext/graphics/texture.c +291 -6
  40. data/ext/graphics/transform.c +178 -5
  41. data/ext/graphics/transformable.c +127 -12
  42. data/ext/graphics/vertex.c +98 -6
  43. data/ext/graphics/vertex_array.c +142 -21
  44. data/ext/graphics/vertex_buffer.c +154 -12
  45. data/ext/graphics/view.c +140 -6
  46. data/ext/network/ftp.c +278 -12
  47. data/ext/network/http.c +179 -44
  48. data/ext/network/ip_address.c +127 -35
  49. data/ext/network/network_enums.c +248 -141
  50. data/ext/network/packet.c +280 -41
  51. data/ext/network/socket_selector.c +98 -21
  52. data/ext/network/tcp_listener.c +82 -18
  53. data/ext/network/tcp_socket.c +135 -27
  54. data/ext/network/udp_socket.c +129 -28
  55. data/ext/ports.rb +7 -0
  56. data/ext/system/buffer.c +47 -16
  57. data/ext/system/clock.c +77 -12
  58. data/ext/system/input_stream.c +113 -51
  59. data/ext/system/sleep.c +10 -2
  60. data/ext/system/time.c +182 -40
  61. data/ext/system/vec2.c +166 -39
  62. data/ext/system/vec3.c +177 -39
  63. data/ext/window/clipboard.c +47 -17
  64. data/ext/window/context.c +66 -13
  65. data/ext/window/context_settings.c +132 -42
  66. data/ext/window/cursor.c +45 -9
  67. data/ext/window/event.c +164 -53
  68. data/ext/window/joystick.c +69 -3
  69. data/ext/window/keyboard.c +168 -116
  70. data/ext/window/mouse.c +64 -13
  71. data/ext/window/sensor.c +28 -2
  72. data/ext/window/touch.c +32 -12
  73. data/ext/window/video_mode.c +93 -31
  74. data/ext/window/vulkan.c +34 -6
  75. data/ext/window/window.c +228 -229
  76. data/ext/window/window.h +4 -4
  77. data/ext/window/window_base.c +251 -0
  78. data/ext/window/window_base.h +43 -0
  79. data/ext/window/window_base.inc +236 -0
  80. data/lib/sfml/version.rb +1 -1
  81. data/sig/audio/audio_enums.rbs +30 -0
  82. data/sig/audio/listener.rbs +16 -0
  83. data/sig/audio/music.rbs +56 -0
  84. data/sig/audio/sound.rbs +51 -0
  85. data/sig/audio/sound_buffer.rbs +17 -0
  86. data/sig/audio/sound_buffer_recorder.rbs +14 -0
  87. data/sig/audio/sound_recorder.rbs +17 -0
  88. data/sig/audio/sound_source_cone.rbs +14 -0
  89. data/sig/audio/sound_stream.rbs +51 -0
  90. data/sig/graphics/blend_mode.rbs +29 -0
  91. data/sig/graphics/circle.rbs +44 -0
  92. data/sig/graphics/color.rbs +39 -0
  93. data/sig/graphics/drawable.rbs +5 -0
  94. data/sig/graphics/font.rbs +20 -0
  95. data/sig/graphics/glyph.rbs +7 -0
  96. data/sig/graphics/image.rbs +22 -0
  97. data/sig/graphics/polygon.rbs +42 -0
  98. data/sig/graphics/rect.rbs +33 -0
  99. data/sig/graphics/rectangle.rbs +42 -0
  100. data/sig/graphics/render_state.rbs +21 -0
  101. data/sig/graphics/render_target.rbs +18 -0
  102. data/sig/graphics/render_texture.rbs +39 -0
  103. data/sig/graphics/render_window.rbs +7 -0
  104. data/sig/graphics/shader.rbs +39 -0
  105. data/sig/graphics/shape.rbs +38 -0
  106. data/sig/graphics/sprite.rbs +34 -0
  107. data/sig/graphics/stencil_mode.rbs +18 -0
  108. data/sig/graphics/target.rbs +8 -0
  109. data/sig/graphics/text.rbs +48 -0
  110. data/sig/graphics/texture.rbs +34 -0
  111. data/sig/graphics/transform.rbs +37 -0
  112. data/sig/graphics/transformable.rbs +25 -0
  113. data/sig/graphics/vertex.rbs +17 -0
  114. data/sig/graphics/vertex_array.rbs +17 -0
  115. data/sig/graphics/vertex_buffer.rbs +22 -0
  116. data/sig/graphics/view.rbs +23 -0
  117. data/sig/network/ftp.rbs +46 -0
  118. data/sig/network/http.rbs +27 -0
  119. data/sig/network/ip_address.rbs +22 -0
  120. data/sig/network/network_enums.rbs +95 -0
  121. data/sig/network/packet.rbs +40 -0
  122. data/sig/network/socket_selector.rbs +14 -0
  123. data/sig/network/tcp_listener.rbs +13 -0
  124. data/sig/network/tcp_socket.rbs +18 -0
  125. data/sig/network/udp_socket.rbs +17 -0
  126. data/sig/sfml/version.rbs +3 -0
  127. data/sig/system/buffer.rbs +11 -0
  128. data/sig/system/clock.rbs +13 -0
  129. data/sig/system/input_stream.rbs +7 -0
  130. data/sig/system/sleep.rbs +3 -0
  131. data/sig/system/time.rbs +28 -0
  132. data/sig/system/vec2.rbs +26 -0
  133. data/sig/system/vec3.rbs +27 -0
  134. data/sig/window/clipboard.rbs +8 -0
  135. data/sig/window/context.rbs +11 -0
  136. data/sig/window/context_settings.rbs +20 -0
  137. data/sig/window/cursor.rbs +6 -0
  138. data/sig/window/event.rbs +20 -0
  139. data/sig/window/joystick.rbs +15 -0
  140. data/sig/window/keyboard.rbs +10 -0
  141. data/sig/window/mouse.rbs +9 -0
  142. data/sig/window/sensor.rbs +8 -0
  143. data/sig/window/touch.rbs +6 -0
  144. data/sig/window/video_mode.rbs +16 -0
  145. data/sig/window/vulkan.rbs +7 -0
  146. data/sig/window/window.rbs +34 -0
  147. data/sig/window/window_base.rbs +33 -0
  148. metadata +76 -3
@@ -9,13 +9,13 @@
9
9
  #include "network/packet.h"
10
10
 
11
11
  typedef struct {
12
- sfUdpSocket *handle;
12
+ sfUdpSocket* handle;
13
13
  } UdpSocket;
14
14
 
15
15
  static VALUE rb_cUdpSocket;
16
16
 
17
- static void UdpSocket_free(void *ptr) {
18
- UdpSocket *socket = ptr;
17
+ static void UdpSocket_free(void* ptr) {
18
+ UdpSocket* socket = ptr;
19
19
 
20
20
  if (socket->handle != NULL) {
21
21
  sfUdpSocket_destroy(socket->handle);
@@ -27,11 +27,10 @@ static void UdpSocket_free(void *ptr) {
27
27
  static const rb_data_type_t UdpSocket_data_type = {
28
28
  .wrap_struct_name = "SFML::UdpSocket",
29
29
  .function = {.dmark = NULL, .dfree = UdpSocket_free, .dsize = NULL},
30
- .flags = RUBY_TYPED_FREE_IMMEDIATELY
31
- };
30
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY};
32
31
 
33
- static VALUE UdpSocket_wrap(VALUE klass, sfUdpSocket *handle) {
34
- UdpSocket *ptr;
32
+ static VALUE UdpSocket_wrap(VALUE klass, sfUdpSocket* handle) {
33
+ UdpSocket* ptr;
35
34
 
36
35
  if (handle == NULL) {
37
36
  rb_raise(rb_eRuntimeError, "failed to create UDP socket");
@@ -43,60 +42,135 @@ static VALUE UdpSocket_wrap(VALUE klass, sfUdpSocket *handle) {
43
42
  return TypedData_Wrap_Struct(klass, &UdpSocket_data_type, ptr);
44
43
  }
45
44
 
46
- static VALUE UdpSocket_new(VALUE klass) {
45
+ static VALUE UdpSocket_alloc(VALUE klass) {
47
46
  return UdpSocket_wrap(klass, sfUdpSocket_create());
48
47
  }
49
48
 
49
+ /* call-seq:
50
+ * UdpSocket.new -> UdpSocket
51
+ *
52
+ * Creates a new, unbound UDP socket.
53
+ *
54
+ * @return [UdpSocket] a new, unbound socket
55
+ */
56
+ static VALUE UdpSocket_initialize(VALUE self) {
57
+ return self;
58
+ }
59
+
60
+ /* call-seq:
61
+ * UdpSocket.any_port -> Integer
62
+ *
63
+ * Returns the port value that makes #bind bind to an OS-chosen port.
64
+ *
65
+ * @return [Integer] a port value that #bind interprets as "let the OS pick
66
+ * an available port"
67
+ */
50
68
  static VALUE UdpSocket_any_port(VALUE klass) {
51
69
  return UINT2NUM(sfUdpSocket_anyPort());
52
70
  }
53
71
 
72
+ /* call-seq:
73
+ * UdpSocket.max_datagram_size -> Integer
74
+ *
75
+ * Returns the maximum size, in bytes, of a single UDP datagram.
76
+ *
77
+ * @return [Integer] the maximum number of bytes that can be sent in a
78
+ * single UDP datagram (65507)
79
+ */
54
80
  static VALUE UdpSocket_max_datagram_size(VALUE klass) {
55
81
  return UINT2NUM(sfUdpSocket_maxDatagramSize());
56
82
  }
57
83
 
84
+ /* call-seq: blocking? -> true or false
85
+ *
86
+ * Returns +true+ if the socket is in blocking mode.
87
+ *
88
+ * @return [Boolean]
89
+ */
58
90
  static VALUE UdpSocket_blocking(VALUE self) {
59
91
  return BOOL2RB(sfUdpSocket_isBlocking(Get_UdpSocket_Struct(self)));
60
92
  }
61
93
 
94
+ /* call-seq:
95
+ * blocking=(value) -> value
96
+ *
97
+ * Enables or disables blocking mode according to +value+.
98
+ *
99
+ * @return [Boolean] +value+
100
+ */
62
101
  static VALUE UdpSocket_set_blocking(VALUE self, VALUE rb_value) {
63
102
  sfUdpSocket_setBlocking(Get_UdpSocket_Struct(self), RTEST(rb_value));
64
103
  return rb_value;
65
104
  }
66
105
 
106
+ /* call-seq: local_port -> Integer
107
+ *
108
+ * Returns the local port the socket is bound to.
109
+ *
110
+ * @return [Integer] the port the socket is bound to, or 0 if not bound
111
+ */
67
112
  static VALUE UdpSocket_local_port(VALUE self) {
68
113
  return UINT2NUM(sfUdpSocket_getLocalPort(Get_UdpSocket_Struct(self)));
69
114
  }
70
115
 
71
- static VALUE UdpSocket_bind(int argc, VALUE *argv, VALUE self) {
116
+ /* call-seq:
117
+ * bind(port, address = IpAddress::ANY) -> Symbol
118
+ *
119
+ * Binds the socket to a local port, optionally restricted to a specific
120
+ * local network interface via +address+, so it can receive datagrams.
121
+ *
122
+ * @return [Symbol] a SocketStatus name, +:done+ on success
123
+ */
124
+ static VALUE UdpSocket_bind(int argc, VALUE* argv, VALUE self) {
72
125
  VALUE rb_port, rb_address;
73
126
 
74
127
  rb_scan_args(argc, argv, "11", &rb_port, &rb_address);
75
128
 
76
- return ID2SYM(rb_intern(socket_status_name(sfUdpSocket_bind(
77
- Get_UdpSocket_Struct(self), (unsigned short) NUM2INT(rb_port),
78
- ip_address_from_rb(rb_address, sfIpAddress_Any)))));
129
+ return ID2SYM(rb_intern(socket_status_name(
130
+ sfUdpSocket_bind(Get_UdpSocket_Struct(self), (unsigned short)NUM2INT(rb_port),
131
+ ip_address_from_rb(rb_address, sfIpAddress_Any)))));
79
132
  }
80
133
 
134
+ /* call-seq: unbind -> self
135
+ *
136
+ * Unbinds the socket from its local port.
137
+ *
138
+ * @return [self]
139
+ */
81
140
  static VALUE UdpSocket_unbind(VALUE self) {
82
141
  sfUdpSocket_unbind(Get_UdpSocket_Struct(self));
83
142
  return self;
84
143
  }
85
144
 
145
+ /* call-seq:
146
+ * send(data, address, port) -> Symbol
147
+ *
148
+ * Sends a single datagram to a remote peer. +data+ must be no larger than
149
+ * .max_datagram_size bytes.
150
+ *
151
+ * @return [Symbol] a SocketStatus name, +:done+ on success
152
+ */
86
153
  static VALUE UdpSocket_send(VALUE self, VALUE rb_data, VALUE rb_address, VALUE rb_port) {
87
154
  StringValue(rb_data);
88
155
 
89
156
  return ID2SYM(rb_intern(socket_status_name(sfUdpSocket_send(
90
- Get_UdpSocket_Struct(self), RSTRING_PTR(rb_data), (size_t) RSTRING_LEN(rb_data),
91
- ip_address_from_rb(rb_address, sfIpAddress_None), (unsigned short) NUM2INT(rb_port)))));
157
+ Get_UdpSocket_Struct(self), RSTRING_PTR(rb_data), (size_t)RSTRING_LEN(rb_data),
158
+ ip_address_from_rb(rb_address, sfIpAddress_None), (unsigned short)NUM2INT(rb_port)))));
92
159
  }
93
160
 
94
- /* Returns [data, IpAddress, port, status]; the address and port are those of
95
- the sender. */
96
- static VALUE UdpSocket_receive(int argc, VALUE *argv, VALUE self) {
161
+ /* call-seq:
162
+ * receive(max_length = 1024) -> [String, IpAddress, Integer, Symbol]
163
+ *
164
+ * Returns [data, IpAddress, port, status]; the address and port are those of
165
+ * the sender.
166
+ *
167
+ * @return [Array(String, IpAddress, Integer, Symbol)]
168
+ * @raise [ArgumentError] if +max_length+ isn't positive
169
+ */
170
+ static VALUE UdpSocket_receive(int argc, VALUE* argv, VALUE self) {
97
171
  VALUE rb_max_length;
98
172
  long max_length = 1024;
99
- char *buffer;
173
+ char* buffer;
100
174
  size_t received = 0;
101
175
  sfIpAddress remote_address = sfIpAddress_None;
102
176
  unsigned short remote_port = 0;
@@ -113,13 +187,17 @@ static VALUE UdpSocket_receive(int argc, VALUE *argv, VALUE self) {
113
187
  rb_raise(rb_eArgError, "maximum length must be positive");
114
188
  }
115
189
 
116
- buffer = malloc((size_t) max_length);
190
+ buffer = malloc((size_t)max_length);
191
+
192
+ if (buffer == NULL) {
193
+ rb_raise(rb_eNoMemError, "failed to allocate receive buffer");
194
+ }
117
195
 
118
- status = sfUdpSocket_receive(Get_UdpSocket_Struct(self), buffer, (size_t) max_length, &received,
196
+ status = sfUdpSocket_receive(Get_UdpSocket_Struct(self), buffer, (size_t)max_length, &received,
119
197
  &remote_address, &remote_port);
120
198
 
121
199
  rb_result = rb_ary_new_capa(4);
122
- rb_ary_push(rb_result, rb_str_new(buffer, (long) received));
200
+ rb_ary_push(rb_result, rb_str_new(buffer, (long)received));
123
201
  rb_ary_push(rb_result, ip_address_to_rb(remote_address));
124
202
  rb_ary_push(rb_result, UINT2NUM(remote_port));
125
203
  rb_ary_push(rb_result, ID2SYM(rb_intern(socket_status_name(status))));
@@ -129,6 +207,14 @@ static VALUE UdpSocket_receive(int argc, VALUE *argv, VALUE self) {
129
207
  return rb_result;
130
208
  }
131
209
 
210
+ /* call-seq:
211
+ * send_packet(packet, address, port) -> Symbol
212
+ *
213
+ * Sends +packet+ as a datagram to the given +address+ and +port+.
214
+ *
215
+ * @return [Symbol] a SocketStatus name, +:done+ on success
216
+ * @raise [TypeError] if +packet+ is not an SFML::Packet
217
+ */
132
218
  static VALUE UdpSocket_send_packet(VALUE self, VALUE rb_packet, VALUE rb_address, VALUE rb_port) {
133
219
  if (!rb_obj_is_kind_of(rb_packet, Get_Klass_Packet())) {
134
220
  rb_raise(rb_eTypeError, "expected an SFML::Packet");
@@ -136,10 +222,19 @@ static VALUE UdpSocket_send_packet(VALUE self, VALUE rb_packet, VALUE rb_address
136
222
 
137
223
  return ID2SYM(rb_intern(socket_status_name(sfUdpSocket_sendPacket(
138
224
  Get_UdpSocket_Struct(self), Get_Packet_Struct(rb_packet),
139
- ip_address_from_rb(rb_address, sfIpAddress_None), (unsigned short) NUM2INT(rb_port)))));
225
+ ip_address_from_rb(rb_address, sfIpAddress_None), (unsigned short)NUM2INT(rb_port)))));
140
226
  }
141
227
 
142
- /* Returns [IpAddress, port, status]. */
228
+ /* call-seq:
229
+ * receive_packet(packet) -> [IpAddress, Integer, Symbol]
230
+ *
231
+ * Fills +packet+ (replacing its previous contents) with the next datagram
232
+ * received. Returns [IpAddress, port, status], the address and port of the
233
+ * sender.
234
+ *
235
+ * @return [Array(IpAddress, Integer, Symbol)]
236
+ * @raise [TypeError] if +packet+ is not an SFML::Packet
237
+ */
143
238
  static VALUE UdpSocket_receive_packet(VALUE self, VALUE rb_packet) {
144
239
  sfIpAddress remote_address = sfIpAddress_None;
145
240
  unsigned short remote_port = 0;
@@ -161,10 +256,16 @@ static VALUE UdpSocket_receive_packet(VALUE self, VALUE rb_packet) {
161
256
  return rb_result;
162
257
  }
163
258
 
164
- void Init_UdpSocket(VALUE rb_module) {
165
- rb_cUdpSocket = rb_define_class_under(rb_module, "UdpSocket", rb_cObject);
259
+ /* Document-class: SFML::UdpSocket
260
+ * A connectionless, unreliable, datagram-oriented socket for UDP
261
+ * communication. Datagrams may be lost, duplicated or arrive out of order,
262
+ * and are capped at .max_datagram_size bytes.
263
+ */
264
+ void Init_UdpSocket(VALUE rb_mSFML) {
265
+ rb_cUdpSocket = rb_define_class_under(rb_mSFML, "UdpSocket", rb_cObject);
266
+ rb_define_alloc_func(rb_cUdpSocket, UdpSocket_alloc);
166
267
 
167
- rb_define_singleton_method(rb_cUdpSocket, "new", UdpSocket_new, 0);
268
+ rb_define_method(rb_cUdpSocket, "initialize", UdpSocket_initialize, 0);
168
269
  rb_define_singleton_method(rb_cUdpSocket, "any_port", UdpSocket_any_port, 0);
169
270
  rb_define_singleton_method(rb_cUdpSocket, "max_datagram_size", UdpSocket_max_datagram_size, 0);
170
271
 
@@ -183,8 +284,8 @@ VALUE Get_Klass_UdpSocket(void) {
183
284
  return rb_cUdpSocket;
184
285
  }
185
286
 
186
- void *Get_UdpSocket_Struct(VALUE self) {
187
- UdpSocket *ptr;
287
+ void* Get_UdpSocket_Struct(VALUE self) {
288
+ UdpSocket* ptr;
188
289
  TypedData_Get_Struct(self, UdpSocket, &UdpSocket_data_type, ptr);
189
290
  return ptr->handle;
190
291
  }
data/ext/ports.rb CHANGED
@@ -34,10 +34,17 @@ module Ports
34
34
  'x86-linux-gnu' => { triple: 'i686-linux-gnu', multiarch: 'i386-linux-gnu',
35
35
  os: :linux, cpu: 'i686' },
36
36
  'aarch64-linux-gnu' => { triple: 'aarch64-linux-gnu', os: :linux, cpu: 'aarch64' },
37
+ 'arm-linux-gnu' => { triple: 'arm-linux-gnueabihf', os: :linux, cpu: 'arm' },
37
38
  'x86_64-linux-musl' => { triple: 'x86_64-unknown-linux-musl', os: :linux, cpu: 'x86_64' },
38
39
  'x86-linux-musl' => { triple: 'i686-unknown-linux-musl', os: :linux, cpu: 'i686' },
40
+ # These two deliberately omit the -unknown- Debian-musl entries above use:
41
+ # the triple must match musl-cross-make's TARGET exactly, since that string
42
+ # is both the compiler prefix and the sysroot path (see musl_sysroot).
43
+ 'aarch64-linux-musl' => { triple: 'aarch64-linux-musl', os: :linux, cpu: 'aarch64' },
44
+ 'arm-linux-musl' => { triple: 'arm-linux-musleabihf', os: :linux, cpu: 'arm' },
39
45
  'x64-mingw-ucrt' => { triple: 'x86_64-w64-mingw32', os: :windows, cpu: 'x86_64' },
40
46
  'x86-mingw32' => { triple: 'i686-w64-mingw32', os: :windows, cpu: 'i686' },
47
+ 'aarch64-mingw-ucrt' => { triple: 'aarch64-w64-mingw32', os: :windows, cpu: 'arm64' },
41
48
  'x86_64-darwin' => { triple: 'x86_64-apple-darwin', os: :darwin, cpu: 'x86_64' },
42
49
  'arm64-darwin' => { triple: 'aarch64-apple-darwin', os: :darwin, cpu: 'arm64' }
43
50
  }.freeze
data/ext/system/buffer.c CHANGED
@@ -7,57 +7,88 @@
7
7
 
8
8
  static VALUE rb_cBuffer;
9
9
 
10
- static void Buffer_free(void *ptr) {
10
+ static void Buffer_free(void* ptr) {
11
11
  sfBuffer_destroy(ptr);
12
12
  }
13
13
 
14
14
  static const rb_data_type_t Buffer_data_type = {
15
15
  .wrap_struct_name = "SFML::Buffer",
16
16
  .function = {.dmark = NULL, .dfree = Buffer_free, .dsize = NULL},
17
- .flags = RUBY_TYPED_FREE_IMMEDIATELY
18
- };
17
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY};
19
18
 
20
- static VALUE Buffer_wrap(VALUE klass, sfBuffer *buffer) {
19
+ static VALUE Buffer_wrap(VALUE klass, sfBuffer* buffer) {
21
20
  return TypedData_Wrap_Struct(klass, &Buffer_data_type, buffer);
22
21
  }
23
22
 
24
- static VALUE Buffer_new(VALUE klass) {
25
- VALUE self;
26
- sfBuffer *buffer = sfBuffer_create();
23
+ static VALUE Buffer_alloc(VALUE klass) {
24
+ sfBuffer* buffer = sfBuffer_create();
27
25
 
28
26
  if (buffer == NULL) {
29
27
  rb_raise(rb_eNoMemError, "could not create buffer");
30
28
  }
31
29
 
32
- self = Buffer_wrap(klass, buffer);
30
+ return Buffer_wrap(klass, buffer);
31
+ }
33
32
 
33
+ /* call-seq:
34
+ * Buffer.new -> Buffer
35
+ *
36
+ * Creates a new, empty byte buffer.
37
+ *
38
+ * @return [Buffer] a new, empty buffer
39
+ * @raise [NoMemoryError] if the underlying buffer could not be allocated
40
+ */
41
+ static VALUE Buffer_initialize(VALUE self) {
34
42
  return self;
35
43
  }
36
44
 
45
+ /* call-seq: size -> Integer
46
+ *
47
+ * Returns the size of the buffer in bytes.
48
+ *
49
+ * @return [Integer] size in bytes
50
+ */
37
51
  static VALUE Buffer_get_size(VALUE self) {
38
52
  return SIZET2NUM(sfBuffer_getSize(Get_Buffer_Struct(self)));
39
53
  }
40
54
 
55
+ /* call-seq: data -> String
56
+ *
57
+ * Returns the raw bytes held by the buffer.
58
+ *
59
+ * @return [String] the raw bytes, as a binary String
60
+ */
41
61
  static VALUE Buffer_get_data(VALUE self) {
42
- sfBuffer *buffer = Get_Buffer_Struct(self);
62
+ sfBuffer* buffer = Get_Buffer_Struct(self);
43
63
  size_t size = sfBuffer_getSize(buffer);
44
- const uint8_t *data = sfBuffer_getData(buffer);
64
+ const uint8_t* data = sfBuffer_getData(buffer);
45
65
 
46
66
  if (data == NULL || size == 0) {
47
67
  return rb_str_new("", 0);
48
68
  }
49
69
 
50
- return rb_str_new((const char *) data, (long) size);
70
+ return rb_str_new((const char*)data, (long)size);
51
71
  }
52
72
 
73
+ /* call-seq: empty? -> true or false
74
+ *
75
+ * Returns +true+ if the buffer holds no bytes.
76
+ *
77
+ * @return [Boolean]
78
+ */
53
79
  static VALUE Buffer_is_empty(VALUE self) {
54
80
  return BOOL2RB(sfBuffer_getSize(Get_Buffer_Struct(self)) == 0);
55
81
  }
56
82
 
57
- void Init_Buffer(VALUE rb_module) {
58
- rb_cBuffer = rb_define_class_under(rb_module, "Buffer", rb_cObject);
83
+ /* Document-class: SFML::Buffer
84
+ * A raw byte buffer, as used for encoded/decoded audio and image data
85
+ * elsewhere in the library.
86
+ */
87
+ void Init_Buffer(VALUE rb_mSFML) {
88
+ rb_cBuffer = rb_define_class_under(rb_mSFML, "Buffer", rb_cObject);
59
89
 
60
- rb_define_singleton_method(rb_cBuffer, "new", Buffer_new, 0);
90
+ rb_define_alloc_func(rb_cBuffer, Buffer_alloc);
91
+ rb_define_method(rb_cBuffer, "initialize", Buffer_initialize, 0);
61
92
 
62
93
  rb_define_method(rb_cBuffer, "size", Buffer_get_size, 0);
63
94
  rb_define_method(rb_cBuffer, "length", Buffer_get_size, 0);
@@ -70,8 +101,8 @@ VALUE Get_Klass_Buffer(void) {
70
101
  return rb_cBuffer;
71
102
  }
72
103
 
73
- void *Get_Buffer_Struct(VALUE self) {
74
- sfBuffer *ptr;
104
+ void* Get_Buffer_Struct(VALUE self) {
105
+ sfBuffer* ptr;
75
106
  TypedData_Get_Struct(self, sfBuffer, &Buffer_data_type, ptr);
76
107
  return ptr;
77
108
  }
data/ext/system/clock.c CHANGED
@@ -9,23 +9,26 @@
9
9
 
10
10
  static VALUE rb_cClock;
11
11
 
12
- static sfClock *Clock_create() {
12
+ static sfClock* Clock_create() {
13
13
  return sfClock_create();
14
14
  }
15
15
 
16
- static void Clock_free(void *ptr) {
16
+ static void Clock_free(void* ptr) {
17
17
  sfClock_destroy(ptr);
18
18
  }
19
19
 
20
20
  static const rb_data_type_t Clock_data_type = {
21
21
  .wrap_struct_name = "SFML::Clock",
22
22
  .function = {.dmark = NULL, .dfree = Clock_free, .dsize = NULL},
23
- .flags = RUBY_TYPED_FREE_IMMEDIATELY
24
- };
23
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY};
25
24
 
26
- static VALUE Clock_new_from(VALUE klass, sfClock *clock) {
25
+ static VALUE Clock_new_from(VALUE klass, sfClock* clock) {
27
26
  VALUE self;
28
27
 
28
+ if (clock == NULL) {
29
+ rb_raise(rb_eNoMemError, "failed to allocate clock");
30
+ }
31
+
29
32
  self = TypedData_Wrap_Struct(klass, &Clock_data_type, clock);
30
33
 
31
34
  rb_obj_call_init(self, 0, NULL);
@@ -33,48 +36,110 @@ static VALUE Clock_new_from(VALUE klass, sfClock *clock) {
33
36
  return self;
34
37
  }
35
38
 
36
- static VALUE Clock_new(VALUE klass) {
37
- return Clock_new_from(klass, Clock_create());
39
+ static VALUE Clock_alloc(VALUE klass) {
40
+ sfClock* clock = Clock_create();
41
+
42
+ if (clock == NULL) {
43
+ rb_raise(rb_eNoMemError, "failed to allocate clock");
44
+ }
45
+
46
+ return TypedData_Wrap_Struct(klass, &Clock_data_type, clock);
38
47
  }
39
48
 
49
+ /* call-seq:
50
+ * Clock.new -> Clock
51
+ *
52
+ * Creates a new clock and starts it immediately.
53
+ *
54
+ * @return [Clock]
55
+ */
40
56
  static VALUE Clock_init(VALUE self) {
41
57
  return self;
42
58
  }
43
59
 
60
+ /* call-seq: elapsed_time -> Time
61
+ *
62
+ * Returns the time elapsed on the clock.
63
+ *
64
+ * @return [Time] time elapsed since the clock was created, started, or last
65
+ * restarted/reset, whichever is most recent
66
+ */
44
67
  static VALUE Clock_get_elapsed_time(VALUE self) {
45
68
  return time_to_rb(sfClock_getElapsedTime(Get_Clock_Struct(self)));
46
69
  }
47
70
 
71
+ /* call-seq: restart! -> Time
72
+ *
73
+ * Restarts the clock (equivalent to #reset! followed by #start!) and
74
+ * returns the time elapsed before restarting.
75
+ *
76
+ * @return [Time]
77
+ */
48
78
  static VALUE Clock_restart(VALUE self) {
49
79
  return time_to_rb(sfClock_restart(Get_Clock_Struct(self)));
50
80
  }
51
81
 
82
+ /* call-seq: reset! -> Time
83
+ *
84
+ * Stops the clock and resets its elapsed time to zero, returning the time
85
+ * elapsed before resetting.
86
+ *
87
+ * @return [Time]
88
+ */
52
89
  static VALUE Clock_reset(VALUE self) {
53
90
  return time_to_rb(sfClock_reset(Get_Clock_Struct(self)));
54
91
  }
55
92
 
93
+ /* call-seq: running? -> true or false
94
+ *
95
+ * Returns +true+ while the clock is running, +false+ once it is stopped.
96
+ *
97
+ * @return [Boolean]
98
+ */
56
99
  static VALUE Clock_is_running(VALUE self) {
57
100
  return BOOL2RB(sfClock_isRunning(Get_Clock_Struct(self)));
58
101
  }
59
102
 
103
+ /* call-seq: start! -> self
104
+ *
105
+ * Resumes a stopped clock without resetting its elapsed time.
106
+ *
107
+ * @return [self]
108
+ */
60
109
  static VALUE Clock_start(VALUE self) {
61
110
  sfClock_start(Get_Clock_Struct(self));
62
111
  return self;
63
112
  }
64
113
 
114
+ /* call-seq: stop! -> self
115
+ *
116
+ * Pauses the clock; #elapsed_time keeps returning the time at which it
117
+ * was stopped until #start! or #restart! is called.
118
+ *
119
+ * @return [self]
120
+ */
65
121
  static VALUE Clock_stop(VALUE self) {
66
122
  sfClock_stop(Get_Clock_Struct(self));
67
123
  return self;
68
124
  }
69
125
 
126
+ /* call-seq: copy -> Clock
127
+ *
128
+ * Returns an independent copy of the clock.
129
+ *
130
+ * @return [Clock] an independent copy with the same elapsed/running state
131
+ */
70
132
  static VALUE Clock_copy(VALUE self) {
71
133
  return Clock_new_from(Get_Klass_Clock(), sfClock_copy(Get_Clock_Struct(self)));
72
134
  }
73
135
 
74
- void Init_Clock(VALUE rb_module) {
75
- rb_cClock = rb_define_class_under(rb_module, "Clock", rb_cObject);
136
+ /* Document-class: SFML::Clock
137
+ * A stopwatch for measuring elapsed time.
138
+ */
139
+ void Init_Clock(VALUE rb_mSFML) {
140
+ rb_cClock = rb_define_class_under(rb_mSFML, "Clock", rb_cObject);
76
141
 
77
- rb_define_singleton_method(rb_cClock, "new", Clock_new, 0);
142
+ rb_define_alloc_func(rb_cClock, Clock_alloc);
78
143
 
79
144
  // methods
80
145
  rb_define_method(rb_cClock, "initialize", Clock_init, 0);
@@ -89,8 +154,8 @@ void Init_Clock(VALUE rb_module) {
89
154
  rb_define_method(rb_cClock, "running?", Clock_is_running, 0);
90
155
  }
91
156
 
92
- void *Get_Clock_Struct(VALUE self) {
93
- sfClock *clock;
157
+ void* Get_Clock_Struct(VALUE self) {
158
+ sfClock* clock;
94
159
  TypedData_Get_Struct(self, sfClock, &Clock_data_type, clock);
95
160
  return clock;
96
161
  }