sfml3-rb 0.3.0 → 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 (117) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +25 -0
  3. data/README.md +36 -13
  4. data/TODO.md +18 -11
  5. data/ext/audio/music.c +6 -0
  6. data/ext/audio/sound.c +43 -4
  7. data/ext/audio/sound_buffer.c +8 -0
  8. data/ext/audio/sound_buffer_recorder.c +8 -3
  9. data/ext/audio/sound_recorder.c +21 -11
  10. data/ext/audio/sound_source_cone.c +18 -3
  11. data/ext/audio/sound_stream.c +25 -15
  12. data/ext/ext.c +4 -0
  13. data/ext/graphics/blend_mode.c +16 -8
  14. data/ext/graphics/circle.c +30 -13
  15. data/ext/graphics/color.c +17 -8
  16. data/ext/graphics/drawable.c +3 -2
  17. data/ext/graphics/font.c +6 -0
  18. data/ext/graphics/glyph.c +7 -0
  19. data/ext/graphics/image.c +25 -4
  20. data/ext/graphics/polygon.c +31 -11
  21. data/ext/graphics/rect.c +16 -8
  22. data/ext/graphics/rectangle.c +30 -11
  23. data/ext/graphics/render_state.c +8 -10
  24. data/ext/graphics/render_texture.c +27 -11
  25. data/ext/graphics/render_window.c +55 -0
  26. data/ext/graphics/render_window.h +10 -0
  27. data/ext/graphics/shader.c +6 -0
  28. data/ext/graphics/shape.c +23 -16
  29. data/ext/graphics/sprite.c +34 -10
  30. data/ext/graphics/stencil_mode.c +16 -8
  31. data/ext/graphics/target.c +60 -27
  32. data/ext/graphics/target.h +22 -8
  33. data/ext/graphics/text.c +33 -13
  34. data/ext/graphics/texture.c +34 -3
  35. data/ext/graphics/transformable.c +11 -20
  36. data/ext/graphics/vertex.c +18 -3
  37. data/ext/graphics/vertex_array.c +18 -9
  38. data/ext/graphics/vertex_buffer.c +30 -10
  39. data/ext/graphics/view.c +16 -10
  40. data/ext/network/ftp.c +30 -3
  41. data/ext/network/http.c +23 -6
  42. data/ext/network/ip_address.c +25 -14
  43. data/ext/network/packet.c +8 -3
  44. data/ext/network/socket_selector.c +8 -3
  45. data/ext/network/tcp_listener.c +8 -3
  46. data/ext/network/tcp_socket.c +8 -3
  47. data/ext/network/udp_socket.c +8 -3
  48. data/ext/system/buffer.c +13 -11
  49. data/ext/system/clock.c +11 -5
  50. data/ext/system/input_stream.c +21 -11
  51. data/ext/system/time.c +17 -13
  52. data/ext/system/vec2.c +17 -11
  53. data/ext/system/vec3.c +18 -11
  54. data/ext/window/context.c +14 -11
  55. data/ext/window/context_settings.c +16 -4
  56. data/ext/window/cursor.c +6 -0
  57. data/ext/window/event.c +15 -25
  58. data/ext/window/mouse.c +28 -11
  59. data/ext/window/touch.c +12 -10
  60. data/ext/window/video_mode.c +16 -25
  61. data/ext/window/window.c +110 -435
  62. data/ext/window/window.h +4 -4
  63. data/ext/window/window_base.c +251 -0
  64. data/ext/window/window_base.h +43 -0
  65. data/ext/window/window_base.inc +236 -0
  66. data/lib/sfml/version.rb +1 -1
  67. data/sig/audio/sound.rbs +1 -1
  68. data/sig/audio/sound_buffer_recorder.rbs +1 -1
  69. data/sig/audio/sound_recorder.rbs +1 -1
  70. data/sig/audio/sound_source_cone.rbs +1 -1
  71. data/sig/audio/sound_stream.rbs +1 -1
  72. data/sig/graphics/blend_mode.rbs +1 -1
  73. data/sig/graphics/circle.rbs +1 -1
  74. data/sig/graphics/color.rbs +5 -5
  75. data/sig/graphics/image.rbs +1 -1
  76. data/sig/graphics/polygon.rbs +1 -1
  77. data/sig/graphics/rect.rbs +5 -5
  78. data/sig/graphics/rectangle.rbs +1 -1
  79. data/sig/graphics/render_state.rbs +2 -2
  80. data/sig/graphics/render_target.rbs +18 -0
  81. data/sig/graphics/render_texture.rbs +4 -2
  82. data/sig/graphics/render_window.rbs +7 -0
  83. data/sig/graphics/shape.rbs +1 -1
  84. data/sig/graphics/sprite.rbs +2 -2
  85. data/sig/graphics/stencil_mode.rbs +2 -2
  86. data/sig/graphics/target.rbs +1 -3
  87. data/sig/graphics/text.rbs +4 -4
  88. data/sig/graphics/texture.rbs +1 -1
  89. data/sig/graphics/transformable.rbs +1 -1
  90. data/sig/graphics/vertex.rbs +4 -4
  91. data/sig/graphics/vertex_array.rbs +1 -1
  92. data/sig/graphics/vertex_buffer.rbs +4 -4
  93. data/sig/graphics/view.rbs +0 -1
  94. data/sig/network/ftp.rbs +1 -1
  95. data/sig/network/http.rbs +2 -2
  96. data/sig/network/ip_address.rbs +1 -1
  97. data/sig/network/packet.rbs +1 -1
  98. data/sig/network/socket_selector.rbs +1 -1
  99. data/sig/network/tcp_listener.rbs +1 -1
  100. data/sig/network/tcp_socket.rbs +1 -1
  101. data/sig/network/udp_socket.rbs +1 -1
  102. data/sig/sfml/version.rbs +3 -0
  103. data/sig/system/buffer.rbs +1 -1
  104. data/sig/system/clock.rbs +1 -1
  105. data/sig/system/input_stream.rbs +1 -1
  106. data/sig/system/time.rbs +4 -4
  107. data/sig/system/vec2.rbs +4 -4
  108. data/sig/system/vec3.rbs +4 -4
  109. data/sig/window/context.rbs +1 -1
  110. data/sig/window/context_settings.rbs +1 -1
  111. data/sig/window/event.rbs +1 -1
  112. data/sig/window/mouse.rbs +2 -2
  113. data/sig/window/touch.rbs +1 -1
  114. data/sig/window/video_mode.rbs +1 -1
  115. data/sig/window/window.rbs +2 -25
  116. data/sig/window/window_base.rbs +33 -0
  117. metadata +11 -2
data/ext/system/clock.c CHANGED
@@ -36,6 +36,16 @@ static VALUE Clock_new_from(VALUE klass, sfClock* clock) {
36
36
  return self;
37
37
  }
38
38
 
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);
47
+ }
48
+
39
49
  /* call-seq:
40
50
  * Clock.new -> Clock
41
51
  *
@@ -43,10 +53,6 @@ static VALUE Clock_new_from(VALUE klass, sfClock* clock) {
43
53
  *
44
54
  * @return [Clock]
45
55
  */
46
- static VALUE Clock_new(VALUE klass) {
47
- return Clock_new_from(klass, Clock_create());
48
- }
49
-
50
56
  static VALUE Clock_init(VALUE self) {
51
57
  return self;
52
58
  }
@@ -133,7 +139,7 @@ static VALUE Clock_copy(VALUE self) {
133
139
  void Init_Clock(VALUE rb_mSFML) {
134
140
  rb_cClock = rb_define_class_under(rb_mSFML, "Clock", rb_cObject);
135
141
 
136
- rb_define_singleton_method(rb_cClock, "new", Clock_new, 0);
142
+ rb_define_alloc_func(rb_cClock, Clock_alloc);
137
143
 
138
144
  // methods
139
145
  rb_define_method(rb_cClock, "initialize", Clock_init, 0);
@@ -190,6 +190,23 @@ static int64_t InputStream_get_size(void* userData) {
190
190
  return ctx.result;
191
191
  }
192
192
 
193
+ static VALUE InputStream_alloc(VALUE klass) {
194
+ InputStream* stream = malloc(sizeof(InputStream));
195
+
196
+ if (stream == NULL) {
197
+ rb_raise(rb_eNoMemError, "failed to allocate input stream");
198
+ }
199
+
200
+ stream->rb_io = Qnil;
201
+ stream->stream.read = InputStream_read;
202
+ stream->stream.seek = InputStream_seek;
203
+ stream->stream.tell = InputStream_tell;
204
+ stream->stream.getSize = InputStream_get_size;
205
+ stream->stream.userData = stream;
206
+
207
+ return TypedData_Wrap_Struct(klass, &InputStream_data_type, stream);
208
+ }
209
+
193
210
  /* call-seq:
194
211
  * InputStream.new(io) -> InputStream
195
212
  *
@@ -200,24 +217,16 @@ static int64_t InputStream_get_size(void* userData) {
200
217
  * @return [InputStream]
201
218
  * @raise [ArgumentError] if +io+ does not respond to +#read+
202
219
  */
203
- static VALUE InputStream_new(VALUE klass, VALUE rb_io) {
204
- VALUE self;
220
+ static VALUE InputStream_initialize(VALUE self, VALUE rb_io) {
205
221
  InputStream* stream;
206
222
 
207
223
  if (!rb_respond_to(rb_io, rb_intern("read"))) {
208
224
  rb_raise(rb_eArgError, "stream object must respond to #read");
209
225
  }
210
226
 
211
- stream = malloc(sizeof(InputStream));
227
+ TypedData_Get_Struct(self, InputStream, &InputStream_data_type, stream);
212
228
 
213
229
  stream->rb_io = rb_io;
214
- stream->stream.read = InputStream_read;
215
- stream->stream.seek = InputStream_seek;
216
- stream->stream.tell = InputStream_tell;
217
- stream->stream.getSize = InputStream_get_size;
218
- stream->stream.userData = stream;
219
-
220
- self = TypedData_Wrap_Struct(klass, &InputStream_data_type, stream);
221
230
 
222
231
  return self;
223
232
  }
@@ -240,7 +249,8 @@ static VALUE InputStream_get_io(VALUE self) {
240
249
  void Init_InputStream(VALUE rb_mSFML) {
241
250
  rb_cInputStream = rb_define_class_under(rb_mSFML, "InputStream", rb_cObject);
242
251
 
243
- rb_define_singleton_method(rb_cInputStream, "new", InputStream_new, 1);
252
+ rb_define_alloc_func(rb_cInputStream, InputStream_alloc);
253
+ rb_define_method(rb_cInputStream, "initialize", InputStream_initialize, 1);
244
254
 
245
255
  rb_define_method(rb_cInputStream, "io", InputStream_get_io, 0);
246
256
  }
data/ext/system/time.c CHANGED
@@ -33,6 +33,18 @@ static VALUE Time_wrap(sfTime time) {
33
33
  return TypedData_Wrap_Struct(rb_cSFTime, &Time_data_type, ptr);
34
34
  }
35
35
 
36
+ static VALUE Time_alloc(VALUE klass) {
37
+ Time* ptr = malloc(sizeof(Time));
38
+
39
+ if (ptr == NULL) {
40
+ rb_raise(rb_eNoMemError, "failed to allocate time");
41
+ }
42
+
43
+ ptr->time = sfTime_Zero;
44
+
45
+ return TypedData_Wrap_Struct(klass, &Time_data_type, ptr);
46
+ }
47
+
36
48
  /* call-seq:
37
49
  * Time.new -> Time(0)
38
50
  * Time.new(seconds) -> Time
@@ -46,9 +58,7 @@ static VALUE Time_wrap(sfTime time) {
46
58
  * @return [Time]
47
59
  * @raise [ArgumentError] if given more than one argument
48
60
  */
49
- static VALUE Time_new(int argc, VALUE* argv, VALUE klass) {
50
- VALUE self;
51
- Time* ptr;
61
+ static VALUE Time_initialize(int argc, VALUE* argv, VALUE self) {
52
62
  sfTime time = sfTime_Zero;
53
63
 
54
64
  if (argc == 1 && rb_obj_is_kind_of(argv[0], rb_cSFTime)) {
@@ -61,15 +71,7 @@ static VALUE Time_new(int argc, VALUE* argv, VALUE klass) {
61
71
  raise_invalid_arguments_excepted(1, argc);
62
72
  }
63
73
 
64
- ptr = malloc(sizeof(Time));
65
-
66
- if (ptr == NULL) {
67
- rb_raise(rb_eNoMemError, "failed to allocate time");
68
- }
69
-
70
- ptr->time = time;
71
-
72
- self = TypedData_Wrap_Struct(klass, &Time_data_type, ptr);
74
+ ((Time*)Get_Time_Struct(self))->time = time;
73
75
 
74
76
  return self;
75
77
  }
@@ -302,7 +304,9 @@ void Init_Time(VALUE rb_mSFML) {
302
304
 
303
305
  rb_include_module(rb_cSFTime, rb_mComparable);
304
306
 
305
- rb_define_singleton_method(rb_cSFTime, "new", Time_new, -1);
307
+ rb_define_alloc_func(rb_cSFTime, Time_alloc);
308
+ rb_define_method(rb_cSFTime, "initialize", Time_initialize, -1);
309
+
306
310
  rb_define_singleton_method(rb_cSFTime, "seconds", Time_seconds, 1);
307
311
  rb_define_singleton_method(rb_cSFTime, "milliseconds", Time_milliseconds, 1);
308
312
  rb_define_singleton_method(rb_cSFTime, "microseconds", Time_microseconds, 1);
data/ext/system/vec2.c CHANGED
@@ -22,6 +22,19 @@ static const rb_data_type_t Vector2_data_type = {
22
22
  .function = {.dmark = NULL, .dfree = Vector2_free, .dsize = NULL},
23
23
  .flags = RUBY_TYPED_FREE_IMMEDIATELY};
24
24
 
25
+ static VALUE Vector2_alloc(VALUE klass) {
26
+ Vector2* ptr = malloc(sizeof(Vector2));
27
+
28
+ if (ptr == NULL) {
29
+ rb_raise(rb_eNoMemError, "failed to allocate vector");
30
+ }
31
+
32
+ ptr->vec.x = 0;
33
+ ptr->vec.y = 0;
34
+
35
+ return TypedData_Wrap_Struct(klass, &Vector2_data_type, ptr);
36
+ }
37
+
25
38
  /* call-seq:
26
39
  * Vector2.new -> Vector2(0, 0)
27
40
  * Vector2.new(x, y) -> Vector2(x, y)
@@ -35,8 +48,7 @@ static const rb_data_type_t Vector2_data_type = {
35
48
  * @raise [ArgumentError] if given an Array shorter than 2 elements, or an
36
49
  * argument count other than 0, 1 or 2
37
50
  */
38
- static VALUE Vector2_new(int argc, VALUE* argv, VALUE klass) {
39
- VALUE self;
51
+ static VALUE Vector2_initialize(int argc, VALUE* argv, VALUE self) {
40
52
  Vector2* ptr;
41
53
  float x = 0;
42
54
  float y = 0;
@@ -59,17 +71,10 @@ static VALUE Vector2_new(int argc, VALUE* argv, VALUE klass) {
59
71
  raise_invalid_arguments_excepted(2, argc);
60
72
  }
61
73
 
62
- ptr = malloc(sizeof(Vector2));
63
-
64
- if (ptr == NULL) {
65
- rb_raise(rb_eNoMemError, "failed to allocate vector");
66
- }
67
-
74
+ ptr = Get_Vector2_Struct(self);
68
75
  ptr->vec.x = x;
69
76
  ptr->vec.y = y;
70
77
 
71
- self = TypedData_Wrap_Struct(klass, &Vector2_data_type, ptr);
72
-
73
78
  return self;
74
79
  }
75
80
 
@@ -266,7 +271,8 @@ void Init_Vector2(VALUE rb_mSFML) {
266
271
 
267
272
  rb_include_module(rb_cVector2, rb_mEnumerable);
268
273
 
269
- rb_define_singleton_method(rb_cVector2, "new", Vector2_new, -1);
274
+ rb_define_alloc_func(rb_cVector2, Vector2_alloc);
275
+ rb_define_method(rb_cVector2, "initialize", Vector2_initialize, -1);
270
276
 
271
277
  rb_define_method(rb_cVector2, "x", Vector2_get_x, 0);
272
278
  rb_define_method(rb_cVector2, "y", Vector2_get_y, 0);
data/ext/system/vec3.c CHANGED
@@ -21,6 +21,20 @@ static const rb_data_type_t Vector3_data_type = {
21
21
  .function = {.dmark = NULL, .dfree = Vector3_free, .dsize = NULL},
22
22
  .flags = RUBY_TYPED_FREE_IMMEDIATELY};
23
23
 
24
+ static VALUE Vector3_alloc(VALUE klass) {
25
+ Vector3* ptr = malloc(sizeof(Vector3));
26
+
27
+ if (ptr == NULL) {
28
+ rb_raise(rb_eNoMemError, "failed to allocate vector");
29
+ }
30
+
31
+ ptr->vec.x = 0;
32
+ ptr->vec.y = 0;
33
+ ptr->vec.z = 0;
34
+
35
+ return TypedData_Wrap_Struct(klass, &Vector3_data_type, ptr);
36
+ }
37
+
24
38
  /* call-seq:
25
39
  * Vector3.new -> Vector3(0, 0, 0)
26
40
  * Vector3.new(x, y, z) -> Vector3(x, y, z)
@@ -34,8 +48,7 @@ static const rb_data_type_t Vector3_data_type = {
34
48
  * @raise [ArgumentError] if given an Array shorter than 3 elements, or an
35
49
  * argument count other than 0, 1 or 3
36
50
  */
37
- static VALUE Vector3_new(int argc, VALUE* argv, VALUE klass) {
38
- VALUE self;
51
+ static VALUE Vector3_initialize(int argc, VALUE* argv, VALUE self) {
39
52
  Vector3* ptr;
40
53
  float x = 0;
41
54
  float y = 0;
@@ -62,18 +75,11 @@ static VALUE Vector3_new(int argc, VALUE* argv, VALUE klass) {
62
75
  raise_invalid_arguments_excepted(3, argc);
63
76
  }
64
77
 
65
- ptr = malloc(sizeof(Vector3));
66
-
67
- if (ptr == NULL) {
68
- rb_raise(rb_eNoMemError, "failed to allocate vector");
69
- }
70
-
78
+ ptr = Get_Vector3_Struct(self);
71
79
  ptr->vec.x = x;
72
80
  ptr->vec.y = y;
73
81
  ptr->vec.z = z;
74
82
 
75
- self = TypedData_Wrap_Struct(klass, &Vector3_data_type, ptr);
76
-
77
83
  return self;
78
84
  }
79
85
 
@@ -286,7 +292,8 @@ void Init_Vector3(VALUE rb_mSFML) {
286
292
 
287
293
  rb_include_module(rb_cVector3, rb_mEnumerable);
288
294
 
289
- rb_define_singleton_method(rb_cVector3, "new", Vector3_new, -1);
295
+ rb_define_alloc_func(rb_cVector3, Vector3_alloc);
296
+ rb_define_method(rb_cVector3, "initialize", Vector3_initialize, -1);
290
297
 
291
298
  rb_define_method(rb_cVector3, "x", Vector3_get_x, 0);
292
299
  rb_define_method(rb_cVector3, "y", Vector3_get_y, 0);
data/ext/window/context.c CHANGED
@@ -23,6 +23,16 @@ static sfContext* Get_Context_Struct(VALUE self) {
23
23
  return ptr;
24
24
  }
25
25
 
26
+ static VALUE Context_alloc(VALUE klass) {
27
+ sfContext* context = sfContext_create();
28
+
29
+ if (context == NULL) {
30
+ rb_raise(rb_eRuntimeError, "failed to create context");
31
+ }
32
+
33
+ return TypedData_Wrap_Struct(klass, &Context_data_type, context);
34
+ }
35
+
26
36
  /* call-seq:
27
37
  * Context.new -> Context
28
38
  *
@@ -31,16 +41,7 @@ static sfContext* Get_Context_Struct(VALUE self) {
31
41
  * @return [Context]
32
42
  * @raise [RuntimeError] if context creation fails
33
43
  */
34
- static VALUE Context_new(VALUE klass) {
35
- sfContext* context = sfContext_create();
36
- VALUE self;
37
-
38
- if (context == NULL) {
39
- rb_raise(rb_eRuntimeError, "failed to create context");
40
- }
41
-
42
- self = TypedData_Wrap_Struct(klass, &Context_data_type, context);
43
-
44
+ static VALUE Context_initialize(VALUE self) {
44
45
  return self;
45
46
  }
46
47
 
@@ -110,7 +111,9 @@ static VALUE Context_active_context_id(VALUE klass) {
110
111
  void Init_Context(VALUE rb_mSFML) {
111
112
  rb_cContext = rb_define_class_under(rb_mSFML, "Context", rb_cObject);
112
113
 
113
- rb_define_singleton_method(rb_cContext, "new", Context_new, 0);
114
+ rb_define_alloc_func(rb_cContext, Context_alloc);
115
+ rb_define_method(rb_cContext, "initialize", Context_initialize, 0);
116
+
114
117
  rb_define_singleton_method(rb_cContext, "extension_available?", Context_extension_available, 1);
115
118
  rb_define_singleton_method(rb_cContext, "function", Context_get_function, 1);
116
119
  rb_define_singleton_method(rb_cContext, "active_context_id", Context_active_context_id, 0);
@@ -42,6 +42,18 @@ static const char* context_flag_name(VALUE value) {
42
42
  return StringValueCStr(value);
43
43
  }
44
44
 
45
+ static VALUE ContextSettings_alloc(VALUE klass) {
46
+ ContextSettings* ptr = malloc(sizeof(ContextSettings));
47
+
48
+ if (ptr == NULL) {
49
+ rb_raise(rb_eNoMemError, "failed to allocate context settings");
50
+ }
51
+
52
+ ptr->settings = (sfContextSettings){0, 0, 0, 1, 1, sfContextDefault, false};
53
+
54
+ return TypedData_Wrap_Struct(klass, &ContextSettings_data_type, ptr);
55
+ }
56
+
45
57
  /* call-seq:
46
58
  * ContextSettings.new(depth_bits = 0, stencil_bits = 0, antialiasing_level = 0, major_version =
47
59
  * 1, minor_version = 1, flags = :default, srgb_capable = false) -> ContextSettings
@@ -49,10 +61,9 @@ static const char* context_flag_name(VALUE value) {
49
61
  * @return [ContextSettings]
50
62
  * @raise [ArgumentError] if +flags+ contains an unknown attribute name
51
63
  */
52
- static VALUE ContextSettings_new(int argc, VALUE* argv, VALUE klass) {
64
+ static VALUE ContextSettings_initialize(int argc, VALUE* argv, VALUE self) {
53
65
  sfContextSettings settings = {0, 0, 0, 1, 1, sfContextDefault, false};
54
66
  VALUE rb_depth, rb_stencil, rb_antialiasing, rb_major, rb_minor, rb_flags, rb_srgb;
55
- VALUE self;
56
67
 
57
68
  rb_scan_args(argc, argv, "07", &rb_depth, &rb_stencil, &rb_antialiasing, &rb_major, &rb_minor,
58
69
  &rb_flags, &rb_srgb);
@@ -108,7 +119,7 @@ static VALUE ContextSettings_new(int argc, VALUE* argv, VALUE klass) {
108
119
  settings.sRgbCapable = RTEST(rb_srgb);
109
120
  }
110
121
 
111
- self = ContextSettings_wrap(settings);
122
+ ((ContextSettings*)Get_ContextSettings_Struct(self))->settings = settings;
112
123
 
113
124
  return self;
114
125
  }
@@ -248,7 +259,8 @@ static VALUE ContextSettings_set_srgb(VALUE self, VALUE rb_value) {
248
259
  void Init_ContextSettings(VALUE rb_mSFML) {
249
260
  rb_cContextSettings = rb_define_class_under(rb_mSFML, "ContextSettings", rb_cObject);
250
261
 
251
- rb_define_singleton_method(rb_cContextSettings, "new", ContextSettings_new, -1);
262
+ rb_define_alloc_func(rb_cContextSettings, ContextSettings_alloc);
263
+ rb_define_method(rb_cContextSettings, "initialize", ContextSettings_initialize, -1);
252
264
 
253
265
  rb_define_method(rb_cContextSettings, "depth_bits", ContextSettings_get_depth_bits, 0);
254
266
  rb_define_method(rb_cContextSettings, "stencil_bits", ContextSettings_get_stencil_bits, 0);
data/ext/window/cursor.c CHANGED
@@ -64,12 +64,18 @@ static VALUE Cursor_from_system(VALUE klass, VALUE rb_type) {
64
64
  return Cursor_wrap(sfCursor_createFromSystem(cursor_type_from_rb(rb_type)));
65
65
  }
66
66
 
67
+ static VALUE Cursor_alloc(VALUE klass) {
68
+ (void)klass;
69
+ rb_raise(rb_eNotImpError, "use Cursor.from_system or Cursor.from_pixels");
70
+ }
71
+
67
72
  /* Document-class: SFML::Cursor
68
73
  * A native mouse cursor, either built from raw pixel data or one of the
69
74
  * platform's built-in system cursors. Assign to Window#cursor= to apply.
70
75
  */
71
76
  void Init_Cursor(VALUE rb_mSFML) {
72
77
  rb_cCursor = rb_define_class_under(rb_mSFML, "Cursor", rb_cObject);
78
+ rb_define_alloc_func(rb_cCursor, Cursor_alloc);
73
79
 
74
80
  rb_define_singleton_method(rb_cCursor, "from_pixels", Cursor_from_pixels, 3);
75
81
  rb_define_singleton_method(rb_cCursor, "from_system", Cursor_from_system, 1);
data/ext/window/event.c CHANGED
@@ -14,16 +14,6 @@
14
14
 
15
15
  static VALUE rb_cEvent;
16
16
 
17
- static sfEvent* Event_create() {
18
- sfEvent* event = malloc(sizeof(sfEvent));
19
-
20
- if (event == NULL) {
21
- rb_raise(rb_eNoMemError, "failed to allocate event");
22
- }
23
-
24
- return event;
25
- }
26
-
27
17
  static void Event_free(void* ptr) {
28
18
  free(ptr);
29
19
  }
@@ -33,6 +23,18 @@ static const rb_data_type_t Event_data_type = {
33
23
  .function = {.dmark = NULL, .dfree = Event_free, .dsize = NULL},
34
24
  .flags = RUBY_TYPED_FREE_IMMEDIATELY};
35
25
 
26
+ static VALUE Event_alloc(VALUE klass) {
27
+ sfEvent* event = malloc(sizeof(sfEvent));
28
+
29
+ if (event == NULL) {
30
+ rb_raise(rb_eNoMemError, "failed to allocate event");
31
+ }
32
+
33
+ *event = (sfEvent){0};
34
+
35
+ return TypedData_Wrap_Struct(klass, &Event_data_type, event);
36
+ }
37
+
36
38
  /* call-seq:
37
39
  * Event.new -> Event
38
40
  *
@@ -40,17 +42,6 @@ static const rb_data_type_t Event_data_type = {
40
42
  *
41
43
  * @return [Event]
42
44
  */
43
- static VALUE Event_new(VALUE klass) {
44
- VALUE self;
45
- sfEvent* event;
46
-
47
- event = Event_create();
48
- self = TypedData_Wrap_Struct(klass, &Event_data_type, event);
49
-
50
- rb_obj_call_init(self, 0, NULL);
51
-
52
- return self;
53
- }
54
45
 
55
46
  /* call-seq: initialize -> self
56
47
  *
@@ -60,7 +51,7 @@ static VALUE Event_new(VALUE klass) {
60
51
  * internally (e.g. by #poll_event!).
61
52
  * @return [self]
62
53
  */
63
- static VALUE Event_init(VALUE self) {
54
+ static VALUE Event_initialize(VALUE self) {
64
55
  return self;
65
56
  }
66
57
 
@@ -306,9 +297,8 @@ static VALUE Event_get_sensor(VALUE self) {
306
297
  void Init_Event(VALUE rb_mSFML) {
307
298
  rb_cEvent = rb_define_class_under(rb_mSFML, "Event", rb_cObject);
308
299
 
309
- rb_define_singleton_method(rb_cEvent, "new", Event_new, 0);
310
-
311
- rb_define_method(rb_cEvent, "initialize", Event_init, 0);
300
+ rb_define_alloc_func(rb_cEvent, Event_alloc);
301
+ rb_define_method(rb_cEvent, "initialize", Event_initialize, 0);
312
302
 
313
303
  rb_define_method(rb_cEvent, "type", Event_type, 0);
314
304
  rb_define_method(rb_cEvent, "size", Event_get_size, 0);
data/ext/window/mouse.c CHANGED
@@ -4,23 +4,41 @@
4
4
 
5
5
  #include "window/input_enums.h"
6
6
  #include "window/window.h"
7
+ #include "window/window_base.h"
7
8
  #include "system/vec2.h"
8
9
  #include "core/exceptions.h"
9
10
  #include "core/macros.h"
10
11
  #include "core/sfml.h"
11
12
 
12
- /* nil means desktop-relative. CSFML gives sfRenderWindow its own entry points,
13
- so a window argument never has to be cast down to sfWindowBase. */
14
- static const sfRenderWindow* Mouse_relative_window(VALUE rb_window) {
15
- if (NIL_P(rb_window)) {
16
- return NULL;
13
+ /* A RenderWindow and a Window both wrap an sfRenderWindow; a WindowBase wraps
14
+ an sfWindowBase. CSFML ships a separate entry point for each, so pick the one
15
+ matching the handle -- casting an sfRenderWindow* down to sfWindowBase* would
16
+ rely on the base being at offset 0, which CSFML does not guarantee. */
17
+ static sfVector2i Mouse_get_relative_position(VALUE rb_window) {
18
+ if (rb_obj_is_kind_of(rb_window, Get_Klass_Window())) {
19
+ return sfMouse_getPositionRenderWindow(Get_Window_Struct(rb_window));
20
+ }
21
+
22
+ if (rb_obj_is_kind_of(rb_window, Get_Klass_WindowBase())) {
23
+ return sfMouse_getPositionWindowBase(Get_WindowBase_Struct(rb_window));
24
+ }
25
+
26
+ raise_invalid_argument_class(Get_Klass_WindowBase());
27
+ return (sfVector2i){0, 0};
28
+ }
29
+
30
+ static void Mouse_set_relative_position(VALUE rb_window, sfVector2i position) {
31
+ if (rb_obj_is_kind_of(rb_window, Get_Klass_Window())) {
32
+ sfMouse_setPositionRenderWindow(position, Get_Window_Struct(rb_window));
33
+ return;
17
34
  }
18
35
 
19
- if (!rb_obj_is_kind_of(rb_window, Get_Klass_Window())) {
20
- raise_invalid_argument_class(Get_Klass_Window());
36
+ if (rb_obj_is_kind_of(rb_window, Get_Klass_WindowBase())) {
37
+ sfMouse_setPositionWindowBase(position, Get_WindowBase_Struct(rb_window));
38
+ return;
21
39
  }
22
40
 
23
- return Get_Window_Struct(rb_window);
41
+ raise_invalid_argument_class(Get_Klass_WindowBase());
24
42
  }
25
43
 
26
44
  /* call-seq:
@@ -51,7 +69,7 @@ static VALUE Mouse_get_position(int argc, VALUE* argv, VALUE module) {
51
69
  if (NIL_P(rb_window)) {
52
70
  position = sfMouse_getPosition(NULL);
53
71
  } else {
54
- position = sfMouse_getPositionRenderWindow(Mouse_relative_window(rb_window));
72
+ position = Mouse_get_relative_position(rb_window);
55
73
  }
56
74
 
57
75
  return vec2f_to_rb((sfVector2f){(float)position.x, (float)position.y});
@@ -72,8 +90,7 @@ static VALUE Mouse_set_position(int argc, VALUE* argv, VALUE module) {
72
90
  if (NIL_P(rb_window)) {
73
91
  sfMouse_setPosition(vec2i_from_rb(rb_position), NULL);
74
92
  } else {
75
- sfMouse_setPositionRenderWindow(vec2i_from_rb(rb_position),
76
- Mouse_relative_window(rb_window));
93
+ Mouse_set_relative_position(rb_window, vec2i_from_rb(rb_position));
77
94
  }
78
95
 
79
96
  return rb_position;
data/ext/window/touch.c CHANGED
@@ -3,23 +3,26 @@
3
3
  #include <ruby.h>
4
4
 
5
5
  #include "window/window.h"
6
+ #include "window/window_base.h"
6
7
  #include "system/vec2.h"
7
8
  #include "core/exceptions.h"
8
9
  #include "core/macros.h"
9
10
  #include "core/sfml.h"
10
11
 
11
- /* nil means desktop-relative. CSFML gives sfRenderWindow its own entry point,
12
- so a window argument never has to be cast down to sfWindowBase. */
13
- static const sfRenderWindow* Touch_relative_window(VALUE rb_window) {
14
- if (NIL_P(rb_window)) {
15
- return NULL;
12
+ /* A RenderWindow and a Window both wrap an sfRenderWindow; a WindowBase wraps
13
+ an sfWindowBase. CSFML ships a separate entry point for each, so pick the one
14
+ matching the handle rather than casting between them. */
15
+ static sfVector2i Touch_relative_position(unsigned int finger, VALUE rb_window) {
16
+ if (rb_obj_is_kind_of(rb_window, Get_Klass_Window())) {
17
+ return sfTouch_getPositionRenderWindow(finger, Get_Window_Struct(rb_window));
16
18
  }
17
19
 
18
- if (!rb_obj_is_kind_of(rb_window, Get_Klass_Window())) {
19
- raise_invalid_argument_class(Get_Klass_Window());
20
+ if (rb_obj_is_kind_of(rb_window, Get_Klass_WindowBase())) {
21
+ return sfTouch_getPositionWindowBase(finger, Get_WindowBase_Struct(rb_window));
20
22
  }
21
23
 
22
- return Get_Window_Struct(rb_window);
24
+ raise_invalid_argument_class(Get_Klass_WindowBase());
25
+ return (sfVector2i){0, 0};
23
26
  }
24
27
 
25
28
  /* call-seq:
@@ -49,8 +52,7 @@ static VALUE Touch_get_position(int argc, VALUE* argv, VALUE module) {
49
52
  if (NIL_P(rb_window)) {
50
53
  position = sfTouch_getPosition(NUM2UINT(rb_finger), NULL);
51
54
  } else {
52
- position =
53
- sfTouch_getPositionRenderWindow(NUM2UINT(rb_finger), Touch_relative_window(rb_window));
55
+ position = Touch_relative_position(NUM2UINT(rb_finger), rb_window);
54
56
  }
55
57
 
56
58
  return vec2f_to_rb((sfVector2f){(float)position.x, (float)position.y});
@@ -9,7 +9,7 @@
9
9
 
10
10
  static VALUE rb_cMode;
11
11
 
12
- static sfVideoMode* VideoMode_alloc(unsigned width, unsigned height, unsigned bits, int* created) {
12
+ static sfVideoMode* VideoMode_create(unsigned width, unsigned height, unsigned bits, int* created) {
13
13
  sfVideoMode* mode = malloc(sizeof(sfVideoMode));
14
14
 
15
15
  if (mode == NULL) {
@@ -36,8 +36,14 @@ static const rb_data_type_t VideoMode_data_type = {
36
36
  .function = {.dmark = NULL, .dfree = VideoMode_free, .dsize = NULL},
37
37
  .flags = RUBY_TYPED_FREE_IMMEDIATELY};
38
38
 
39
+ static VALUE VideoMode_alloc(VALUE klass) {
40
+ sfVideoMode* ptr = VideoMode_create(0, 0, 0, NULL);
41
+
42
+ return TypedData_Wrap_Struct(klass, &VideoMode_data_type, ptr);
43
+ }
44
+
39
45
  static VALUE VideoMode_from_c(sfVideoMode mode) {
40
- sfVideoMode* ptr = VideoMode_alloc(mode.size.x, mode.size.y, mode.bitsPerPixel, NULL);
46
+ sfVideoMode* ptr = VideoMode_create(mode.size.x, mode.size.y, mode.bitsPerPixel, NULL);
41
47
  VALUE self = TypedData_Wrap_Struct(rb_cMode, &VideoMode_data_type, ptr);
42
48
 
43
49
  rb_iv_set(self, "@width", UINT2NUM(mode.size.x));
@@ -54,28 +60,13 @@ static VALUE VideoMode_from_c(sfVideoMode mode) {
54
60
  *
55
61
  * @return [VideoMode]
56
62
  */
57
- static VALUE VideoMode_new(VALUE klass, VALUE rb_width, VALUE rb_height, VALUE rb_bits) {
58
- VALUE self;
59
- VALUE argv[] = {rb_width, rb_height, rb_bits};
60
-
61
- self = VideoMode_from_c(
62
- (sfVideoMode){{(unsigned)NUM2UINT(rb_width), (unsigned)NUM2UINT(rb_height)},
63
- (unsigned)NUM2UINT(rb_bits)});
63
+ static VALUE VideoMode_initialize(VALUE self, VALUE rb_width, VALUE rb_height, VALUE rb_bits) {
64
+ sfVideoMode* mode = Get_Mode_Struct(self);
64
65
 
65
- rb_obj_call_init(self, 3, argv);
66
+ mode->size.x = (unsigned)NUM2UINT(rb_width);
67
+ mode->size.y = (unsigned)NUM2UINT(rb_height);
68
+ mode->bitsPerPixel = (unsigned)NUM2UINT(rb_bits);
66
69
 
67
- return self;
68
- }
69
-
70
- /* call-seq: initialize(width, height, bits) -> self
71
- *
72
- * Stores the mode's dimensions; called internally by .new.
73
- *
74
- * @private Sets the ivars backing the #width/#height/#bits readers; called
75
- * internally by .new.
76
- * @return [self]
77
- */
78
- static VALUE VideoMode_init(VALUE self, VALUE rb_width, VALUE rb_height, VALUE rb_bits) {
79
70
  rb_iv_set(self, "@width", rb_width);
80
71
  rb_iv_set(self, "@height", rb_height);
81
72
  rb_iv_set(self, "@bits", rb_bits);
@@ -179,11 +170,11 @@ static VALUE VideoMode_eql(VALUE self, VALUE rb_other) {
179
170
  void Init_VideoMode(VALUE rb_mSFML) {
180
171
  rb_cMode = rb_define_class_under(rb_mSFML, "VideoMode", rb_cObject);
181
172
 
182
- rb_define_singleton_method(rb_cMode, "new", VideoMode_new, 3);
173
+ rb_define_alloc_func(rb_cMode, VideoMode_alloc);
174
+ rb_define_method(rb_cMode, "initialize", VideoMode_initialize, 3);
175
+
183
176
  rb_define_singleton_method(rb_cMode, "desktop_mode", VideoMode_desktop_mode, 0);
184
177
  rb_define_singleton_method(rb_cMode, "fullscreen_modes", VideoMode_fullscreen_modes, 0);
185
-
186
- rb_define_method(rb_cMode, "initialize", VideoMode_init, 3);
187
178
  rb_define_method(rb_cMode, "available?", VideoMode_is_available, 0);
188
179
  rb_define_method(rb_cMode, "valid?", VideoMode_is_available, 0);
189
180
  rb_define_method(rb_cMode, "size", VideoMode_get_size, 0);