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
@@ -5,41 +5,61 @@
5
5
 
6
6
  #include "core/sfml.h"
7
7
 
8
- static void unicode_char_to_utf8(sfChar32 code, char buffer[4], int *length) {
8
+ static void unicode_char_to_utf8(sfChar32 code, char buffer[4], int* length) {
9
9
  if (code < 0x80) {
10
- buffer[0] = (char) code;
10
+ buffer[0] = (char)code;
11
11
  *length = 1;
12
12
  } else if (code < 0x800) {
13
- buffer[0] = (char) (0xC0 | (code >> 6));
14
- buffer[1] = (char) (0x80 | (code & 0x3F));
13
+ buffer[0] = (char)(0xC0 | (code >> 6));
14
+ buffer[1] = (char)(0x80 | (code & 0x3F));
15
15
  *length = 2;
16
16
  } else if (code < 0x10000) {
17
- buffer[0] = (char) (0xE0 | (code >> 12));
18
- buffer[1] = (char) (0x80 | ((code >> 6) & 0x3F));
19
- buffer[2] = (char) (0x80 | (code & 0x3F));
17
+ buffer[0] = (char)(0xE0 | (code >> 12));
18
+ buffer[1] = (char)(0x80 | ((code >> 6) & 0x3F));
19
+ buffer[2] = (char)(0x80 | (code & 0x3F));
20
20
  *length = 3;
21
21
  } else {
22
- buffer[0] = (char) (0xF0 | (code >> 18));
23
- buffer[1] = (char) (0x80 | ((code >> 12) & 0x3F));
24
- buffer[2] = (char) (0x80 | ((code >> 6) & 0x3F));
25
- buffer[3] = (char) (0x80 | (code & 0x3F));
22
+ buffer[0] = (char)(0xF0 | (code >> 18));
23
+ buffer[1] = (char)(0x80 | ((code >> 12) & 0x3F));
24
+ buffer[2] = (char)(0x80 | ((code >> 6) & 0x3F));
25
+ buffer[3] = (char)(0x80 | (code & 0x3F));
26
26
  *length = 4;
27
27
  }
28
28
  }
29
29
 
30
+ /* call-seq: string -> String
31
+ *
32
+ * Returns the current system clipboard contents as a plain String.
33
+ *
34
+ * @return [String] the current system clipboard contents
35
+ */
30
36
  static VALUE Clipboard_get_string(VALUE module) {
31
- const char *string = sfClipboard_getString();
37
+ const char* string = sfClipboard_getString();
32
38
 
33
39
  return rb_utf8_str_new_cstr(string != NULL ? string : "");
34
40
  }
35
41
 
42
+ /* call-seq:
43
+ * string=(value) -> String
44
+ *
45
+ * Sets the system clipboard to +value+.
46
+ *
47
+ * @return [String] +value+
48
+ */
36
49
  static VALUE Clipboard_set_string(VALUE module, VALUE rb_text) {
37
50
  sfClipboard_setString(StringValueCStr(rb_text));
38
51
  return rb_text;
39
52
  }
40
53
 
54
+ /* call-seq: unicode_string -> String
55
+ *
56
+ * Returns the current system clipboard contents, decoding UTF-32 to UTF-8.
57
+ *
58
+ * @return [String] the current system clipboard contents, decoded from
59
+ * UTF-32 to a UTF-8 Ruby String
60
+ */
41
61
  static VALUE Clipboard_get_unicode_string(VALUE module) {
42
- const sfChar32 *chars = sfClipboard_getUnicodeString();
62
+ const sfChar32* chars = sfClipboard_getUnicodeString();
43
63
  VALUE string = rb_utf8_str_new("", 0);
44
64
 
45
65
  if (chars == NULL) {
@@ -57,10 +77,17 @@ static VALUE Clipboard_get_unicode_string(VALUE module) {
57
77
  return string;
58
78
  }
59
79
 
80
+ /* call-seq:
81
+ * unicode_string=(value) -> String
82
+ *
83
+ * Sets the system clipboard to +value+, encoding it to UTF-32 first.
84
+ *
85
+ * @return [String] +value+
86
+ */
60
87
  static VALUE Clipboard_set_unicode_string(VALUE module, VALUE rb_text) {
61
88
  VALUE codepoints;
62
89
  long count;
63
- sfChar32 *chars;
90
+ sfChar32* chars;
64
91
 
65
92
  StringValue(rb_text);
66
93
  codepoints = rb_funcall(rb_text, rb_intern("codepoints"), 0);
@@ -68,7 +95,7 @@ static VALUE Clipboard_set_unicode_string(VALUE module, VALUE rb_text) {
68
95
  chars = ALLOC_N(sfChar32, count + 1);
69
96
 
70
97
  for (long i = 0; i < count; i++) {
71
- chars[i] = (sfChar32) NUM2UINT(rb_ary_entry(codepoints, i));
98
+ chars[i] = (sfChar32)NUM2UINT(rb_ary_entry(codepoints, i));
72
99
  }
73
100
 
74
101
  chars[count] = 0;
@@ -79,8 +106,11 @@ static VALUE Clipboard_set_unicode_string(VALUE module, VALUE rb_text) {
79
106
  return rb_text;
80
107
  }
81
108
 
82
- void Init_Clipboard(VALUE rb_module) {
83
- VALUE rb_mClipboard = rb_define_module_under(rb_module, "Clipboard");
109
+ /* Document-module: SFML::Clipboard
110
+ * Access to the system clipboard, as plain text or Unicode text.
111
+ */
112
+ void Init_Clipboard(VALUE rb_mSFML) {
113
+ VALUE rb_mClipboard = rb_define_module_under(rb_mSFML, "Clipboard");
84
114
 
85
115
  rb_define_module_function(rb_mClipboard, "string", Clipboard_get_string, 0);
86
116
  rb_define_module_function(rb_mClipboard, "string=", Clipboard_set_string, 1);
data/ext/window/context.c CHANGED
@@ -8,59 +8,112 @@
8
8
 
9
9
  static VALUE rb_cContext;
10
10
 
11
- static void Context_free(void *ptr) {
11
+ static void Context_free(void* ptr) {
12
12
  sfContext_destroy(ptr);
13
13
  }
14
14
 
15
15
  static const rb_data_type_t Context_data_type = {
16
16
  .wrap_struct_name = "SFML::Context",
17
17
  .function = {.dmark = NULL, .dfree = Context_free, .dsize = NULL},
18
- .flags = RUBY_TYPED_FREE_IMMEDIATELY
19
- };
18
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY};
20
19
 
21
- static sfContext *Get_Context_Struct(VALUE self) {
22
- sfContext *ptr;
20
+ static sfContext* Get_Context_Struct(VALUE self) {
21
+ sfContext* ptr;
23
22
  TypedData_Get_Struct(self, sfContext, &Context_data_type, ptr);
24
23
  return ptr;
25
24
  }
26
25
 
27
- static VALUE Context_new(VALUE klass) {
28
- sfContext *context = sfContext_create();
29
- VALUE self;
26
+ static VALUE Context_alloc(VALUE klass) {
27
+ sfContext* context = sfContext_create();
30
28
 
31
29
  if (context == NULL) {
32
30
  rb_raise(rb_eRuntimeError, "failed to create context");
33
31
  }
34
32
 
35
- self = TypedData_Wrap_Struct(klass, &Context_data_type, context);
33
+ return TypedData_Wrap_Struct(klass, &Context_data_type, context);
34
+ }
36
35
 
36
+ /* call-seq:
37
+ * Context.new -> Context
38
+ *
39
+ * Creates and activates a new OpenGL context on the current thread.
40
+ *
41
+ * @return [Context]
42
+ * @raise [RuntimeError] if context creation fails
43
+ */
44
+ static VALUE Context_initialize(VALUE self) {
37
45
  return self;
38
46
  }
39
47
 
48
+ /* call-seq:
49
+ * extension_available?(name) -> true or false
50
+ *
51
+ * Returns +true+ if the OpenGL extension +name+ is available in the currently
52
+ * active context.
53
+ *
54
+ * @return [Boolean] whether the OpenGL extension +name+ is available in the
55
+ * currently active context
56
+ */
40
57
  static VALUE Context_extension_available(VALUE klass, VALUE rb_name) {
41
58
  return BOOL2RB(sfContext_isExtensionAvailable(StringValueCStr(rb_name)));
42
59
  }
43
60
 
61
+ /* call-seq:
62
+ * active=(value) -> true or false
63
+ *
64
+ * Activates or deactivates this context as the current one on the calling
65
+ * thread.
66
+ *
67
+ * @return [Boolean] whether activation succeeded
68
+ */
44
69
  static VALUE Context_set_active(VALUE self, VALUE rb_active) {
45
70
  return BOOL2RB(sfContext_setActive(Get_Context_Struct(self), RTEST(rb_active)));
46
71
  }
47
72
 
73
+ /* call-seq:
74
+ * function(name) -> Integer
75
+ *
76
+ * Returns the address of the OpenGL function +name+ as an Integer, or 0 when
77
+ * it is not available.
78
+ *
79
+ * @return [Integer] the address of the OpenGL function +name+, as a raw
80
+ * pointer value, or 0 if it is not available
81
+ */
48
82
  static VALUE Context_get_function(VALUE klass, VALUE rb_name) {
49
- return ULL2NUM((unsigned long long) (uintptr_t) sfContext_getFunction(StringValueCStr(rb_name)));
83
+ return ULL2NUM((unsigned long long)(uintptr_t)sfContext_getFunction(StringValueCStr(rb_name)));
50
84
  }
51
85
 
86
+ /* call-seq: settings -> ContextSettings
87
+ *
88
+ * Returns the settings this context was created with.
89
+ *
90
+ * @return [ContextSettings] the settings this context was created with
91
+ */
52
92
  static VALUE Context_get_settings(VALUE self) {
53
93
  return context_settings_to_rb(sfContext_getSettings(Get_Context_Struct(self)));
54
94
  }
55
95
 
96
+ /* call-seq: active_context_id -> Integer
97
+ *
98
+ * Returns the unique identifier of the currently active context on the
99
+ * calling thread, or 0 if none is active.
100
+ *
101
+ * @return [Integer] the unique identifier of the currently active context on
102
+ * the calling thread, or 0 if none is active
103
+ */
56
104
  static VALUE Context_active_context_id(VALUE klass) {
57
105
  return ULL2NUM(sfContext_getActiveContextId());
58
106
  }
59
107
 
60
- void Init_Context(VALUE rb_module) {
61
- rb_cContext = rb_define_class_under(rb_module, "Context", rb_cObject);
108
+ /* Document-class: SFML::Context
109
+ * A raw OpenGL context, usable off-screen or on a thread without a Window.
110
+ */
111
+ void Init_Context(VALUE rb_mSFML) {
112
+ rb_cContext = rb_define_class_under(rb_mSFML, "Context", rb_cObject);
113
+
114
+ rb_define_alloc_func(rb_cContext, Context_alloc);
115
+ rb_define_method(rb_cContext, "initialize", Context_initialize, 0);
62
116
 
63
- rb_define_singleton_method(rb_cContext, "new", Context_new, 0);
64
117
  rb_define_singleton_method(rb_cContext, "extension_available?", Context_extension_available, 1);
65
118
  rb_define_singleton_method(rb_cContext, "function", Context_get_function, 1);
66
119
  rb_define_singleton_method(rb_cContext, "active_context_id", Context_active_context_id, 0);
@@ -13,25 +13,28 @@ typedef struct {
13
13
 
14
14
  static VALUE rb_cContextSettings;
15
15
 
16
- static void ContextSettings_free(void *ptr) {
16
+ static void ContextSettings_free(void* ptr) {
17
17
  free(ptr);
18
18
  }
19
19
 
20
20
  static const rb_data_type_t ContextSettings_data_type = {
21
21
  .wrap_struct_name = "SFML::ContextSettings",
22
22
  .function = {.dmark = NULL, .dfree = ContextSettings_free, .dsize = NULL},
23
- .flags = RUBY_TYPED_FREE_IMMEDIATELY
24
- };
23
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY};
25
24
 
26
25
  static VALUE ContextSettings_wrap(sfContextSettings settings) {
27
- ContextSettings *ptr = malloc(sizeof(ContextSettings));
26
+ ContextSettings* ptr = malloc(sizeof(ContextSettings));
27
+
28
+ if (ptr == NULL) {
29
+ rb_raise(rb_eNoMemError, "failed to allocate context settings");
30
+ }
28
31
 
29
32
  ptr->settings = settings;
30
33
 
31
34
  return TypedData_Wrap_Struct(rb_cContextSettings, &ContextSettings_data_type, ptr);
32
35
  }
33
36
 
34
- static const char *context_flag_name(VALUE value) {
37
+ static const char* context_flag_name(VALUE value) {
35
38
  if (SYMBOL_P(value)) {
36
39
  return rb_id2name(SYM2ID(value));
37
40
  }
@@ -39,38 +42,57 @@ static const char *context_flag_name(VALUE value) {
39
42
  return StringValueCStr(value);
40
43
  }
41
44
 
42
- static VALUE ContextSettings_new(int argc, VALUE *argv, VALUE klass) { sfContextSettings settings = {0, 0, 0, 1, 1, sfContextDefault, false};
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
+
57
+ /* call-seq:
58
+ * ContextSettings.new(depth_bits = 0, stencil_bits = 0, antialiasing_level = 0, major_version =
59
+ * 1, minor_version = 1, flags = :default, srgb_capable = false) -> ContextSettings
60
+ *
61
+ * @return [ContextSettings]
62
+ * @raise [ArgumentError] if +flags+ contains an unknown attribute name
63
+ */
64
+ static VALUE ContextSettings_initialize(int argc, VALUE* argv, VALUE self) {
65
+ sfContextSettings settings = {0, 0, 0, 1, 1, sfContextDefault, false};
43
66
  VALUE rb_depth, rb_stencil, rb_antialiasing, rb_major, rb_minor, rb_flags, rb_srgb;
44
- VALUE self;
45
67
 
46
- rb_scan_args(argc, argv, "07", &rb_depth, &rb_stencil, &rb_antialiasing, &rb_major, &rb_minor, &rb_flags,
47
- &rb_srgb);
68
+ rb_scan_args(argc, argv, "07", &rb_depth, &rb_stencil, &rb_antialiasing, &rb_major, &rb_minor,
69
+ &rb_flags, &rb_srgb);
48
70
 
49
71
  if (!NIL_P(rb_depth)) {
50
- settings.depthBits = (unsigned int) NUM2UINT(rb_depth);
72
+ settings.depthBits = (unsigned int)NUM2UINT(rb_depth);
51
73
  }
52
74
 
53
75
  if (!NIL_P(rb_stencil)) {
54
- settings.stencilBits = (unsigned int) NUM2UINT(rb_stencil);
76
+ settings.stencilBits = (unsigned int)NUM2UINT(rb_stencil);
55
77
  }
56
78
 
57
79
  if (!NIL_P(rb_antialiasing)) {
58
- settings.antiAliasingLevel = (unsigned int) NUM2UINT(rb_antialiasing);
80
+ settings.antiAliasingLevel = (unsigned int)NUM2UINT(rb_antialiasing);
59
81
  }
60
82
 
61
83
  if (!NIL_P(rb_major)) {
62
- settings.majorVersion = (unsigned int) NUM2UINT(rb_major);
84
+ settings.majorVersion = (unsigned int)NUM2UINT(rb_major);
63
85
  }
64
86
 
65
87
  if (!NIL_P(rb_minor)) {
66
- settings.minorVersion = (unsigned int) NUM2UINT(rb_minor);
88
+ settings.minorVersion = (unsigned int)NUM2UINT(rb_minor);
67
89
  }
68
90
 
69
91
  if (!NIL_P(rb_flags)) {
70
92
  uint32_t flags = 0;
71
93
 
72
94
  if (RB_INTEGER_TYPE_P(rb_flags)) {
73
- flags = (uint32_t) NUM2UINT(rb_flags);
95
+ flags = (uint32_t)NUM2UINT(rb_flags);
74
96
  } else if (SYMBOL_P(rb_flags) || RB_TYPE_P(rb_flags, T_STRING)) {
75
97
  rb_flags = rb_ary_new_from_args(1, rb_flags);
76
98
  }
@@ -78,7 +100,7 @@ static VALUE ContextSettings_new(int argc, VALUE *argv, VALUE klass) { sfCont
78
100
  if (RB_TYPE_P(rb_flags, T_ARRAY)) {
79
101
  for (long i = 0; i < RARRAY_LEN(rb_flags); i++) {
80
102
  VALUE entry = rb_ary_entry(rb_flags, i);
81
- const char *name = context_flag_name(entry);
103
+ const char* name = context_flag_name(entry);
82
104
 
83
105
  if (strcmp(name, "core") == 0) {
84
106
  flags |= sfContextCore;
@@ -97,28 +119,42 @@ static VALUE ContextSettings_new(int argc, VALUE *argv, VALUE klass) { sfCont
97
119
  settings.sRgbCapable = RTEST(rb_srgb);
98
120
  }
99
121
 
100
- self = ContextSettings_wrap(settings);
122
+ ((ContextSettings*)Get_ContextSettings_Struct(self))->settings = settings;
101
123
 
102
124
  return self;
103
125
  }
104
126
 
105
- #define CONTEXT_SETTINGS_UINT_ACCESSOR(name, field) \
106
- static VALUE ContextSettings_get_##name(VALUE self) { \
107
- return UINT2NUM(((ContextSettings *) Get_ContextSettings_Struct(self))->settings.field); \
108
- } \
109
- static VALUE ContextSettings_set_##name(VALUE self, VALUE rb_value) { \
110
- ((ContextSettings *) Get_ContextSettings_Struct(self))->settings.field = (unsigned int) NUM2UINT(rb_value); \
111
- return rb_value; \
127
+ #define CONTEXT_SETTINGS_UINT_ACCESSOR(name, field) \
128
+ static VALUE ContextSettings_get_##name(VALUE self) { \
129
+ return UINT2NUM(((ContextSettings*)Get_ContextSettings_Struct(self))->settings.field); \
130
+ } \
131
+ static VALUE ContextSettings_set_##name(VALUE self, VALUE rb_value) { \
132
+ rb_check_frozen(self); \
133
+ ((ContextSettings*)Get_ContextSettings_Struct(self))->settings.field = \
134
+ (unsigned int)NUM2UINT(rb_value); \
135
+ return rb_value; \
112
136
  }
113
137
 
138
+ /* Macro-generated accessor pairs (depth_bits, stencil_bits,
139
+ * antialiasing_level, major_version, minor_version); documented together via
140
+ * Document-class's @!attribute tags below since the comment can't attach to
141
+ * an individual macro expansion. */
114
142
  CONTEXT_SETTINGS_UINT_ACCESSOR(depth_bits, depthBits)
115
143
  CONTEXT_SETTINGS_UINT_ACCESSOR(stencil_bits, stencilBits)
116
144
  CONTEXT_SETTINGS_UINT_ACCESSOR(antialiasing_level, antiAliasingLevel)
117
145
  CONTEXT_SETTINGS_UINT_ACCESSOR(major_version, majorVersion)
118
146
  CONTEXT_SETTINGS_UINT_ACCESSOR(minor_version, minorVersion)
119
147
 
148
+ /* Document-method: SFML::ContextSettings#attribute_flags
149
+ * call-seq: attribute_flags -> Array<Symbol>
150
+ *
151
+ * Returns the context attribute flags as a list of symbols.
152
+ *
153
+ * @return [Array<Symbol>] a subset of +[:core, :debug]+, or +[:default]+ if
154
+ * neither is set
155
+ */
120
156
  static VALUE ContextSettings_get_attribute_flags(VALUE self) {
121
- uint32_t flags = ((ContextSettings *) Get_ContextSettings_Struct(self))->settings.attributeFlags;
157
+ uint32_t flags = ((ContextSettings*)Get_ContextSettings_Struct(self))->settings.attributeFlags;
122
158
  VALUE array = rb_ary_new();
123
159
 
124
160
  if ((flags & sfContextCore) != 0) {
@@ -136,18 +172,28 @@ static VALUE ContextSettings_get_attribute_flags(VALUE self) {
136
172
  return array;
137
173
  }
138
174
 
175
+ /* Document-method: SFML::ContextSettings#attribute_flags=
176
+ * call-seq: attribute_flags = value -> value
177
+ *
178
+ * Sets the context attribute flags from +value+, an Integer bitmask or a
179
+ * Symbol/Array of +:core+ and +:debug+.
180
+ *
181
+ * @return [Integer, Array<Symbol>] +value+
182
+ */
139
183
  static VALUE ContextSettings_set_attribute_flags(VALUE self, VALUE rb_value) {
140
- ContextSettings *settings = Get_ContextSettings_Struct(self);
184
+ ContextSettings* settings = Get_ContextSettings_Struct(self);
141
185
  uint32_t flags = 0;
142
186
 
187
+ rb_check_frozen(self);
188
+
143
189
  if (RB_INTEGER_TYPE_P(rb_value)) {
144
- flags = (uint32_t) NUM2UINT(rb_value);
190
+ flags = (uint32_t)NUM2UINT(rb_value);
145
191
  } else {
146
192
  VALUE array = RB_TYPE_P(rb_value, T_ARRAY) ? rb_value : rb_ary_new_from_args(1, rb_value);
147
193
 
148
194
  for (long i = 0; i < RARRAY_LEN(array); i++) {
149
195
  VALUE entry = rb_ary_entry(array, i);
150
- const char *name = context_flag_name(entry);
196
+ const char* name = context_flag_name(entry);
151
197
 
152
198
  if (strcmp(name, "core") == 0) {
153
199
  flags |= sfContextCore;
@@ -162,34 +208,78 @@ static VALUE ContextSettings_set_attribute_flags(VALUE self, VALUE rb_value) {
162
208
  return rb_value;
163
209
  }
164
210
 
211
+ /* call-seq: srgb_capable? -> true or false
212
+ *
213
+ * Returns +true+ if the context was created with sRGB-capable framebuffers.
214
+ *
215
+ * @return [Boolean]
216
+ */
165
217
  static VALUE ContextSettings_get_srgb(VALUE self) {
166
- return BOOL2RB(((ContextSettings *) Get_ContextSettings_Struct(self))->settings.sRgbCapable);
218
+ return BOOL2RB(((ContextSettings*)Get_ContextSettings_Struct(self))->settings.sRgbCapable);
167
219
  }
168
220
 
221
+ /* call-seq:
222
+ * srgb_capable=(value) -> true or false
223
+ *
224
+ * Requests sRGB-capable framebuffers when +value+ is +true+.
225
+ *
226
+ * @return [Boolean] +value+
227
+ */
169
228
  static VALUE ContextSettings_set_srgb(VALUE self, VALUE rb_value) {
170
- ((ContextSettings *) Get_ContextSettings_Struct(self))->settings.sRgbCapable = RTEST(rb_value);
229
+ rb_check_frozen(self);
230
+ ((ContextSettings*)Get_ContextSettings_Struct(self))->settings.sRgbCapable = RTEST(rb_value);
171
231
  return rb_value;
172
232
  }
173
233
 
174
- void Init_ContextSettings(VALUE rb_module) {
175
- rb_cContextSettings = rb_define_class_under(rb_module, "ContextSettings", rb_cObject);
176
-
177
- rb_define_singleton_method(rb_cContextSettings, "new", ContextSettings_new, -1);
234
+ /* Document-class: SFML::ContextSettings
235
+ * Settings requested when creating a Window or Context: depth/stencil buffer
236
+ * sizes, antialiasing level, requested OpenGL version, and attribute flags.
237
+ *
238
+ * The +depth_bits+/+stencil_bits+/+antialiasing_level+/+major_version+/
239
+ * +minor_version+ accessors are generated through a shared C macro, so
240
+ * (unlike this class's other accessors) they are documented here rather than
241
+ * above their own definitions.
242
+ *
243
+ * @!attribute depth_bits
244
+ * Number of bits per pixel requested for the depth buffer.
245
+ * @return [Integer] requested depth buffer bits per pixel
246
+ * @!attribute stencil_bits
247
+ * Number of bits per pixel requested for the stencil buffer.
248
+ * @return [Integer] requested stencil buffer bits per pixel
249
+ * @!attribute antialiasing_level
250
+ * Requested antialiasing level.
251
+ * @return [Integer] requested antialiasing level
252
+ * @!attribute major_version
253
+ * Requested OpenGL major version.
254
+ * @return [Integer] requested OpenGL major version
255
+ * @!attribute minor_version
256
+ * Requested OpenGL minor version.
257
+ * @return [Integer] requested OpenGL minor version
258
+ */
259
+ void Init_ContextSettings(VALUE rb_mSFML) {
260
+ rb_cContextSettings = rb_define_class_under(rb_mSFML, "ContextSettings", rb_cObject);
261
+
262
+ rb_define_alloc_func(rb_cContextSettings, ContextSettings_alloc);
263
+ rb_define_method(rb_cContextSettings, "initialize", ContextSettings_initialize, -1);
178
264
 
179
265
  rb_define_method(rb_cContextSettings, "depth_bits", ContextSettings_get_depth_bits, 0);
180
266
  rb_define_method(rb_cContextSettings, "stencil_bits", ContextSettings_get_stencil_bits, 0);
181
- rb_define_method(rb_cContextSettings, "antialiasing_level", ContextSettings_get_antialiasing_level, 0);
267
+ rb_define_method(rb_cContextSettings, "antialiasing_level",
268
+ ContextSettings_get_antialiasing_level, 0);
182
269
  rb_define_method(rb_cContextSettings, "major_version", ContextSettings_get_major_version, 0);
183
270
  rb_define_method(rb_cContextSettings, "minor_version", ContextSettings_get_minor_version, 0);
184
- rb_define_method(rb_cContextSettings, "attribute_flags", ContextSettings_get_attribute_flags, 0);
271
+ rb_define_method(rb_cContextSettings, "attribute_flags", ContextSettings_get_attribute_flags,
272
+ 0);
185
273
  rb_define_method(rb_cContextSettings, "srgb_capable?", ContextSettings_get_srgb, 0);
186
274
 
187
275
  rb_define_method(rb_cContextSettings, "depth_bits=", ContextSettings_set_depth_bits, 1);
188
276
  rb_define_method(rb_cContextSettings, "stencil_bits=", ContextSettings_set_stencil_bits, 1);
189
- rb_define_method(rb_cContextSettings, "antialiasing_level=", ContextSettings_set_antialiasing_level, 1);
277
+ rb_define_method(rb_cContextSettings,
278
+ "antialiasing_level=", ContextSettings_set_antialiasing_level, 1);
190
279
  rb_define_method(rb_cContextSettings, "major_version=", ContextSettings_set_major_version, 1);
191
280
  rb_define_method(rb_cContextSettings, "minor_version=", ContextSettings_set_minor_version, 1);
192
- rb_define_method(rb_cContextSettings, "attribute_flags=", ContextSettings_set_attribute_flags, 1);
281
+ rb_define_method(rb_cContextSettings, "attribute_flags=", ContextSettings_set_attribute_flags,
282
+ 1);
193
283
  rb_define_method(rb_cContextSettings, "srgb_capable=", ContextSettings_set_srgb, 1);
194
284
  }
195
285
 
@@ -197,22 +287,22 @@ VALUE Get_Klass_ContextSettings(void) {
197
287
  return rb_cContextSettings;
198
288
  }
199
289
 
200
- void *Get_ContextSettings_Struct(VALUE self) {
201
- ContextSettings *ptr;
290
+ void* Get_ContextSettings_Struct(VALUE self) {
291
+ ContextSettings* ptr;
202
292
  TypedData_Get_Struct(self, ContextSettings, &ContextSettings_data_type, ptr);
203
293
  return ptr;
204
294
  }
205
295
 
206
296
  sfContextSettings context_settings_from_rb(VALUE rb_settings) {
207
297
  if (NIL_P(rb_settings)) {
208
- return (sfContextSettings) {0, 0, 0, 1, 1, sfContextDefault, false};
298
+ return (sfContextSettings){0, 0, 0, 1, 1, sfContextDefault, false};
209
299
  }
210
300
 
211
301
  if (!rb_obj_is_kind_of(rb_settings, rb_cContextSettings)) {
212
302
  raise_invalid_argument_class(rb_cContextSettings);
213
303
  }
214
304
 
215
- return ((ContextSettings *) Get_ContextSettings_Struct(rb_settings))->settings;
305
+ return ((ContextSettings*)Get_ContextSettings_Struct(rb_settings))->settings;
216
306
  }
217
307
 
218
308
  VALUE context_settings_to_rb(sfContextSettings settings) {
data/ext/window/cursor.c CHANGED
@@ -9,17 +9,16 @@
9
9
 
10
10
  static VALUE rb_cCursor;
11
11
 
12
- static void Cursor_free(void *ptr) {
12
+ static void Cursor_free(void* ptr) {
13
13
  sfCursor_destroy(ptr);
14
14
  }
15
15
 
16
16
  static const rb_data_type_t Cursor_data_type = {
17
17
  .wrap_struct_name = "SFML::Cursor",
18
18
  .function = {.dmark = NULL, .dfree = Cursor_free, .dsize = NULL},
19
- .flags = RUBY_TYPED_FREE_IMMEDIATELY
20
- };
19
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY};
21
20
 
22
- static VALUE Cursor_wrap(sfCursor *cursor) {
21
+ static VALUE Cursor_wrap(sfCursor* cursor) {
23
22
  if (cursor == NULL) {
24
23
  rb_raise(rb_eRuntimeError, "failed to create cursor");
25
24
  }
@@ -27,19 +26,56 @@ static VALUE Cursor_wrap(sfCursor *cursor) {
27
26
  return TypedData_Wrap_Struct(rb_cCursor, &Cursor_data_type, cursor);
28
27
  }
29
28
 
29
+ /* call-seq:
30
+ * Cursor.from_pixels(pixels, size, hotspot) -> Cursor
31
+ *
32
+ * Creates a cursor from RGBA32 pixel data (+width * height * 4+ bytes,
33
+ * row-major, top-to-bottom).
34
+ *
35
+ * @return [Cursor]
36
+ * @raise [ArgumentError] if +pixels+ is shorter than required
37
+ * @raise [RuntimeError] if cursor creation fails
38
+ */
30
39
  static VALUE Cursor_from_pixels(VALUE klass, VALUE rb_pixels, VALUE rb_size, VALUE rb_hotspot) {
40
+ sfVector2u size = vec2u_from_rb(rb_size);
41
+ size_t expected = (size_t)size.x * size.y * 4;
42
+
31
43
  StringValue(rb_pixels);
32
44
 
33
- return Cursor_wrap(sfCursor_createFromPixels((const uint8_t *) RSTRING_PTR(rb_pixels), vec2u_from_rb(rb_size),
45
+ if ((size_t)RSTRING_LEN(rb_pixels) < expected) {
46
+ rb_raise(rb_eArgError, "pixel data too short: expected %zu bytes", expected);
47
+ }
48
+
49
+ return Cursor_wrap(sfCursor_createFromPixels((const uint8_t*)RSTRING_PTR(rb_pixels), size,
34
50
  vec2u_from_rb(rb_hotspot)));
35
51
  }
36
52
 
53
+ /* call-seq:
54
+ * Cursor.from_system(type) -> Cursor
55
+ *
56
+ * Creates a cursor from a native system cursor type (a Symbol, e.g.
57
+ * +:arrow+, +:hand+, +:crosshair+; see the cursor type symbols this binding
58
+ * accepts).
59
+ *
60
+ * @return [Cursor]
61
+ * @raise [RuntimeError] if cursor creation fails
62
+ */
37
63
  static VALUE Cursor_from_system(VALUE klass, VALUE rb_type) {
38
64
  return Cursor_wrap(sfCursor_createFromSystem(cursor_type_from_rb(rb_type)));
39
65
  }
40
66
 
41
- void Init_Cursor(VALUE rb_module) {
42
- rb_cCursor = rb_define_class_under(rb_module, "Cursor", rb_cObject);
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
+
72
+ /* Document-class: SFML::Cursor
73
+ * A native mouse cursor, either built from raw pixel data or one of the
74
+ * platform's built-in system cursors. Assign to Window#cursor= to apply.
75
+ */
76
+ void Init_Cursor(VALUE rb_mSFML) {
77
+ rb_cCursor = rb_define_class_under(rb_mSFML, "Cursor", rb_cObject);
78
+ rb_define_alloc_func(rb_cCursor, Cursor_alloc);
43
79
 
44
80
  rb_define_singleton_method(rb_cCursor, "from_pixels", Cursor_from_pixels, 3);
45
81
  rb_define_singleton_method(rb_cCursor, "from_system", Cursor_from_system, 1);
@@ -49,8 +85,8 @@ VALUE Get_Klass_Cursor(void) {
49
85
  return rb_cCursor;
50
86
  }
51
87
 
52
- const sfCursor *Get_Cursor_Struct(VALUE self) {
53
- sfCursor *ptr;
88
+ const sfCursor* Get_Cursor_Struct(VALUE self) {
89
+ sfCursor* ptr;
54
90
  TypedData_Get_Struct(self, sfCursor, &Cursor_data_type, ptr);
55
91
  return ptr;
56
92
  }