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
@@ -31,7 +31,30 @@ static VALUE Vertex_wrap(sfVertex vertex) {
31
31
  return TypedData_Wrap_Struct(rb_cVertex, &Vertex_data_type, ptr);
32
32
  }
33
33
 
34
- static VALUE Vertex_new(int argc, VALUE* argv, VALUE klass) {
34
+ static VALUE Vertex_alloc(VALUE klass) {
35
+ Vertex* ptr = malloc(sizeof(Vertex));
36
+
37
+ if (ptr == NULL) {
38
+ rb_raise(rb_eNoMemError, "failed to allocate vertex");
39
+ }
40
+
41
+ ptr->vertex = (sfVertex){{0, 0}, {0, 0, 0, 0}, {0, 0}};
42
+
43
+ return TypedData_Wrap_Struct(klass, &Vertex_data_type, ptr);
44
+ }
45
+
46
+ /* call-seq:
47
+ * Vertex.new -> Vertex
48
+ * Vertex.new(position) -> Vertex
49
+ * Vertex.new(position, color) -> Vertex
50
+ * Vertex.new(position, color, tex_coords) -> Vertex
51
+ *
52
+ * Any omitted argument defaults to position +(0, 0)+, color white, and
53
+ * tex_coords +(0, 0)+.
54
+ *
55
+ * @return [Vertex]
56
+ */
57
+ static VALUE Vertex_initialize(int argc, VALUE* argv, VALUE self) {
35
58
  VALUE rb_position, rb_color, rb_tex_coords;
36
59
  sfVertex vertex = {{0, 0}, {255, 255, 255, 255}, {0, 0}};
37
60
 
@@ -49,42 +72,96 @@ static VALUE Vertex_new(int argc, VALUE* argv, VALUE klass) {
49
72
  vertex.texCoords = vec2f_from_rb(rb_tex_coords);
50
73
  }
51
74
 
52
- return Vertex_wrap(vertex);
75
+ ((Vertex*)Get_Vertex_Struct(self))->vertex = vertex;
76
+
77
+ return self;
53
78
  }
54
79
 
80
+ /* call-seq: position -> Vector2
81
+ *
82
+ * Returns the object's position.
83
+ *
84
+ * @return [Vector2]
85
+ */
55
86
  static VALUE Vertex_get_position(VALUE self) {
56
87
  return vec2f_to_rb(((Vertex*)Get_Vertex_Struct(self))->vertex.position);
57
88
  }
58
89
 
90
+ /* call-seq:
91
+ * position=(value) -> Vector2
92
+ *
93
+ * Sets the object's position.
94
+ *
95
+ * @return [Vector2] +value+
96
+ */
59
97
  static VALUE Vertex_set_position(VALUE self, VALUE rb_position) {
60
98
  ((Vertex*)Get_Vertex_Struct(self))->vertex.position = vec2f_from_rb(rb_position);
61
99
  return rb_position;
62
100
  }
63
101
 
102
+ /* call-seq: color -> Color
103
+ *
104
+ * Returns the object's color.
105
+ *
106
+ * @return [Color]
107
+ */
64
108
  static VALUE Vertex_get_color(VALUE self) {
65
109
  return color_to_rb(((Vertex*)Get_Vertex_Struct(self))->vertex.color);
66
110
  }
67
111
 
112
+ /* call-seq:
113
+ * color=(value) -> Color
114
+ *
115
+ * Sets the object's color.
116
+ *
117
+ * @return [Color] +value+
118
+ */
68
119
  static VALUE Vertex_set_color(VALUE self, VALUE rb_color) {
69
120
  ((Vertex*)Get_Vertex_Struct(self))->vertex.color = color_from_rb(rb_color);
70
121
  return rb_color;
71
122
  }
72
123
 
124
+ /* call-seq: tex_coords -> Vector2
125
+ *
126
+ * Returns the vertex's texture coordinates.
127
+ *
128
+ * @return [Vector2]
129
+ */
73
130
  static VALUE Vertex_get_tex_coords(VALUE self) {
74
131
  return vec2f_to_rb(((Vertex*)Get_Vertex_Struct(self))->vertex.texCoords);
75
132
  }
76
133
 
134
+ /* call-seq:
135
+ * tex_coords=(value) -> Vector2
136
+ *
137
+ * Sets the vertex's texture coordinates.
138
+ *
139
+ * @return [Vector2] +value+
140
+ */
77
141
  static VALUE Vertex_set_tex_coords(VALUE self, VALUE rb_tex_coords) {
78
142
  ((Vertex*)Get_Vertex_Struct(self))->vertex.texCoords = vec2f_from_rb(rb_tex_coords);
79
143
  return rb_tex_coords;
80
144
  }
81
145
 
146
+ /* call-seq: to_a -> [Vector2, Color]
147
+ *
148
+ * Returns the object as an Array.
149
+ *
150
+ * @return [Array] +[position, color]+
151
+ */
82
152
  static VALUE Vertex_to_a(VALUE self) {
83
153
  Vertex* v = Get_Vertex_Struct(self);
84
154
 
85
155
  return rb_ary_new_from_args(2, vec2f_to_rb(v->vertex.position), color_to_rb(v->vertex.color));
86
156
  }
87
157
 
158
+ /* call-seq:
159
+ * self == other -> true or false
160
+ *
161
+ * Returns +true+ if the two objects are equal.
162
+ *
163
+ * @return [Boolean]
164
+ */
88
165
  static VALUE Vertex_eql(VALUE self, VALUE rb_other) {
89
166
  sfVertex a = ((Vertex*)Get_Vertex_Struct(self))->vertex;
90
167
  sfVertex b;
@@ -101,10 +178,25 @@ static VALUE Vertex_eql(VALUE self, VALUE rb_other) {
101
178
  a.texCoords.y == b.texCoords.y);
102
179
  }
103
180
 
104
- void Init_Vertex(VALUE rb_module) {
105
- rb_cVertex = rb_define_class_under(rb_module, "Vertex", rb_cObject);
106
-
107
- rb_define_singleton_method(rb_cVertex, "new", Vertex_new, -1);
181
+ /* Document-class: SFML::Vertex
182
+ * A single point used to build up VertexArray and VertexBuffer primitives:
183
+ * a position, a color, and texture coordinates.
184
+ *
185
+ * @!attribute position
186
+ * The object's position.
187
+ * @return [Vector2]
188
+ * @!attribute color
189
+ * The object's color.
190
+ * @return [Color]
191
+ * @!attribute tex_coords
192
+ * The vertex's texture coordinates.
193
+ * @return [Vector2]
194
+ */
195
+ void Init_Vertex(VALUE rb_mSFML) {
196
+ rb_cVertex = rb_define_class_under(rb_mSFML, "Vertex", rb_cObject);
197
+
198
+ rb_define_alloc_func(rb_cVertex, Vertex_alloc);
199
+ rb_define_method(rb_cVertex, "initialize", Vertex_initialize, -1);
108
200
 
109
201
  rb_define_method(rb_cVertex, "position", Vertex_get_position, 0);
110
202
  rb_define_method(rb_cVertex, "color", Vertex_get_color, 0);
@@ -14,17 +14,16 @@
14
14
 
15
15
  static VALUE rb_cVertexArray;
16
16
 
17
- static void VertexArray_free(void *ptr) {
17
+ static void VertexArray_free(void* ptr) {
18
18
  sfVertexArray_destroy(ptr);
19
19
  }
20
20
 
21
21
  static const rb_data_type_t VertexArray_data_type = {
22
22
  .wrap_struct_name = "SFML::VertexArray",
23
23
  .function = {.dmark = NULL, .dfree = VertexArray_free, .dsize = NULL},
24
- .flags = RUBY_TYPED_FREE_IMMEDIATELY
25
- };
24
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY};
26
25
 
27
- static VALUE VertexArray_wrap(VALUE klass, sfVertexArray *array) {
26
+ static VALUE VertexArray_wrap(VALUE klass, sfVertexArray* array) {
28
27
  if (array == NULL) {
29
28
  rb_raise(rb_eRuntimeError, "failed to create vertex array");
30
29
  }
@@ -32,82 +31,204 @@ static VALUE VertexArray_wrap(VALUE klass, sfVertexArray *array) {
32
31
  return TypedData_Wrap_Struct(klass, &VertexArray_data_type, array);
33
32
  }
34
33
 
35
- static sfVertexArray *Get_VertexArray_Struct(VALUE self) {
36
- sfVertexArray *ptr;
34
+ static sfVertexArray* Get_VertexArray_Struct(VALUE self) {
35
+ sfVertexArray* ptr;
37
36
  TypedData_Get_Struct(self, sfVertexArray, &VertexArray_data_type, ptr);
38
37
  return ptr;
39
38
  }
40
39
 
41
- static VALUE VertexArray_new(VALUE klass) {
42
- return VertexArray_wrap(klass, sfVertexArray_create());
40
+ static VALUE VertexArray_alloc(VALUE klass) {
41
+ sfVertexArray* array = sfVertexArray_create();
42
+
43
+ if (array == NULL) {
44
+ rb_raise(rb_eRuntimeError, "failed to create vertex array");
45
+ }
46
+
47
+ return TypedData_Wrap_Struct(klass, &VertexArray_data_type, array);
48
+ }
49
+
50
+ /* call-seq:
51
+ * VertexArray.new -> VertexArray
52
+ *
53
+ * Creates a vertex array using the given +primitive+ type.
54
+ *
55
+ * @return [VertexArray] an empty array of :points primitives
56
+ */
57
+ static VALUE VertexArray_initialize(VALUE self) {
58
+ return self;
43
59
  }
44
60
 
61
+ /* call-seq: copy -> VertexArray
62
+ *
63
+ * Returns a deep copy of the object.
64
+ *
65
+ * @return [VertexArray] an independent copy
66
+ */
45
67
  static VALUE VertexArray_copy(VALUE self) {
46
- return VertexArray_wrap(Get_Klass_VertexArray(), sfVertexArray_copy(Get_VertexArray_Struct(self)));
68
+ return VertexArray_wrap(Get_Klass_VertexArray(),
69
+ sfVertexArray_copy(Get_VertexArray_Struct(self)));
47
70
  }
48
71
 
72
+ /* call-seq: vertex_count -> Integer
73
+ *
74
+ * Returns the number of vertices stored.
75
+ *
76
+ * @return [Integer]
77
+ */
49
78
  static VALUE VertexArray_get_vertex_count(VALUE self) {
50
79
  return SIZET2NUM(sfVertexArray_getVertexCount(Get_VertexArray_Struct(self)));
51
80
  }
52
81
 
53
- static VALUE VertexArray_get_vertex(VALUE self, VALUE rb_index) {
54
- return vertex_to_rb(*sfVertexArray_getVertex(Get_VertexArray_Struct(self), (size_t) NUM2SIZET(rb_index)));
82
+ static size_t VertexArray_check_index(VALUE self, VALUE rb_index) {
83
+ void* array = Get_VertexArray_Struct(self);
84
+ size_t index = (size_t)NUM2SIZET(rb_index);
85
+ size_t count = sfVertexArray_getVertexCount(array);
86
+
87
+ if (index >= count) {
88
+ rb_raise(rb_eIndexError, "index %zu outside of vertex count %zu", index, count);
89
+ }
90
+
91
+ return index;
55
92
  }
56
93
 
94
+ /* call-seq:
95
+ * vertex(index) -> Vertex
96
+ *
97
+ * Returns the vertex at +index+.
98
+ *
99
+ * @return [Vertex] a copy of the vertex at +index+
100
+ * @raise [IndexError] if +index+ is out of range
101
+ */
102
+ static VALUE VertexArray_get_vertex(VALUE self, VALUE rb_index) {
103
+ return vertex_to_rb(*sfVertexArray_getVertex(Get_VertexArray_Struct(self),
104
+ VertexArray_check_index(self, rb_index)));
105
+ }
106
+
107
+ /* call-seq:
108
+ * set_vertex(index, vertex) -> Vertex
109
+ *
110
+ * Replaces the vertex at +index+ with +vertex+.
111
+ *
112
+ * @return [Vertex] +vertex+
113
+ * @raise [IndexError] if +index+ is out of range
114
+ */
57
115
  static VALUE VertexArray_set_vertex(VALUE self, VALUE rb_index, VALUE rb_vertex) {
58
- *sfVertexArray_getVertex(Get_VertexArray_Struct(self), (size_t) NUM2SIZET(rb_index)) = vertex_from_rb(rb_vertex);
116
+ *sfVertexArray_getVertex(Get_VertexArray_Struct(self),
117
+ VertexArray_check_index(self, rb_index)) = vertex_from_rb(rb_vertex);
59
118
  return rb_vertex;
60
119
  }
61
120
 
121
+ /* call-seq:
122
+ * append(vertex) -> self
123
+ *
124
+ * Appends +vertex+ to the array.
125
+ *
126
+ * @return [self]
127
+ */
62
128
  static VALUE VertexArray_append(VALUE self, VALUE rb_vertex) {
63
129
  sfVertexArray_append(Get_VertexArray_Struct(self), vertex_from_rb(rb_vertex));
64
130
  return self;
65
131
  }
66
132
 
133
+ /* call-seq: clear! -> self
134
+ *
135
+ * Removes all vertices.
136
+ *
137
+ * @return [self]
138
+ */
67
139
  static VALUE VertexArray_clear(VALUE self) {
68
140
  sfVertexArray_clear(Get_VertexArray_Struct(self));
69
141
  return self;
70
142
  }
71
143
 
144
+ /* call-seq:
145
+ * resize(count) -> self
146
+ *
147
+ * Grows or shrinks the array to +count+ vertices; new vertices are default
148
+ * ones.
149
+ *
150
+ * @return [self]
151
+ */
72
152
  static VALUE VertexArray_resize(VALUE self, VALUE rb_count) {
73
- sfVertexArray_resize(Get_VertexArray_Struct(self), (size_t) NUM2SIZET(rb_count));
153
+ sfVertexArray_resize(Get_VertexArray_Struct(self), (size_t)NUM2SIZET(rb_count));
74
154
  return self;
75
155
  }
76
156
 
157
+ /* call-seq: primitive -> Symbol
158
+ *
159
+ * Returns the primitive type used to draw the vertices.
160
+ *
161
+ * @return [Symbol] the primitive type vertices are interpreted as (e.g.
162
+ * :points, :lines, :triangles)
163
+ */
77
164
  static VALUE VertexArray_get_primitive_type(VALUE self) {
78
- return ID2SYM(rb_intern(primitive_type_name(sfVertexArray_getPrimitiveType(Get_VertexArray_Struct(self)))));
165
+ return ID2SYM(rb_intern(
166
+ primitive_type_name(sfVertexArray_getPrimitiveType(Get_VertexArray_Struct(self)))));
79
167
  }
80
168
 
169
+ /* call-seq:
170
+ * primitive=(value) -> Symbol
171
+ *
172
+ * Sets the primitive type used to draw the vertices.
173
+ *
174
+ * @return [Symbol] +value+
175
+ */
81
176
  static VALUE VertexArray_set_primitive_type(VALUE self, VALUE rb_type) {
82
177
  sfVertexArray_setPrimitiveType(Get_VertexArray_Struct(self), primitive_type_from_rb(rb_type));
83
178
  return rb_type;
84
179
  }
85
180
 
181
+ /* call-seq: bounds -> Rect
182
+ *
183
+ * Returns the axis-aligned bounding box of all vertices.
184
+ *
185
+ * @return [Rect] the axis-aligned bounding box of all vertices
186
+ */
86
187
  static VALUE VertexArray_get_bounds(VALUE self) {
87
188
  return rect_to_rb(sfVertexArray_getBounds(Get_VertexArray_Struct(self)));
88
189
  }
89
190
 
191
+ /* call-seq:
192
+ * draw(target, state) -> nil
193
+ *
194
+ * Part of the Drawable interface; call Target#draw instead of this
195
+ * directly.
196
+ *
197
+ * @return [nil]
198
+ * @raise [TypeError] if +target+ is not a Target or +state+ is not a
199
+ * RenderState
200
+ */
90
201
  static VALUE VertexArray_draw(VALUE self, VALUE rb_target, VALUE rb_state) {
91
- if (!rb_obj_is_kind_of(rb_target, Get_Klass_Target())) {
92
- raise_invalid_argument_class(Get_Klass_Target());
93
- }
202
+ TargetView view = Get_RenderTarget_View(rb_target);
94
203
 
95
204
  if (!rb_obj_is_kind_of(rb_state, Get_Klass_RenderState())) {
96
205
  raise_invalid_argument_class(Get_Klass_RenderState());
97
206
  }
98
207
 
99
- TARGET_DRAW(Get_Target_Struct(rb_target), sfRenderWindow_drawVertexArray, sfRenderTexture_drawVertexArray,
208
+ TARGET_DRAW(view, sfRenderWindow_drawVertexArray, sfRenderTexture_drawVertexArray,
100
209
  Get_VertexArray_Struct(self), Get_RenderState_Struct(rb_state));
101
210
 
102
211
  return Qnil;
103
212
  }
104
213
 
105
- void Init_VertexArray(VALUE rb_module) {
106
- rb_cVertexArray = rb_define_class_under(rb_module, "VertexArray", rb_cObject);
214
+ /* Document-class: SFML::VertexArray
215
+ * A resizable, drawable set of Vertex objects interpreted as a given
216
+ * primitive type (points, lines, triangles, ...).
217
+ *
218
+ * Includes +Drawable+.
219
+ *
220
+ * @!attribute primitive
221
+ * The primitive type used to draw the vertices.
222
+ * @return [Symbol]
223
+ */
224
+ void Init_VertexArray(VALUE rb_mSFML) {
225
+ rb_cVertexArray = rb_define_class_under(rb_mSFML, "VertexArray", rb_cObject);
226
+
227
+ rb_define_alloc_func(rb_cVertexArray, VertexArray_alloc);
107
228
 
108
229
  rb_include_module(rb_cVertexArray, Get_Module_Drawable());
109
230
 
110
- rb_define_singleton_method(rb_cVertexArray, "new", VertexArray_new, 0);
231
+ rb_define_method(rb_cVertexArray, "initialize", VertexArray_initialize, 0);
111
232
 
112
233
  rb_define_method(rb_cVertexArray, "copy", VertexArray_copy, 0);
113
234
  rb_define_method(rb_cVertexArray, "vertex_count", VertexArray_get_vertex_count, 0);
@@ -17,7 +17,9 @@ static VALUE rb_cVertexBuffer;
17
17
  static const char* usage_names[] = {"stream", "dynamic", "static"};
18
18
 
19
19
  static void VertexBuffer_free(void* ptr) {
20
- sfVertexBuffer_destroy(ptr);
20
+ if (ptr != NULL) {
21
+ sfVertexBuffer_destroy(ptr);
22
+ }
21
23
  }
22
24
 
23
25
  static const rb_data_type_t VertexBuffer_data_type = {
@@ -60,11 +62,35 @@ static sfVertexBufferUsage usage_from_rb(VALUE rb_usage) {
60
62
  return sfVertexBufferStatic;
61
63
  }
62
64
 
63
- static VALUE VertexBuffer_new(int argc, VALUE* argv, VALUE klass) {
65
+ static VALUE VertexBuffer_initialize_copy(VALUE self, VALUE other) {
66
+ (void)other;
67
+ rb_raise(rb_eTypeError, "can't copy a %s", rb_obj_classname(self));
68
+ }
69
+
70
+ static VALUE VertexBuffer_alloc(VALUE klass) {
71
+ return TypedData_Wrap_Struct(klass, &VertexBuffer_data_type, NULL);
72
+ }
73
+
74
+ /* call-seq:
75
+ * VertexBuffer.new -> VertexBuffer
76
+ * VertexBuffer.new(count) -> VertexBuffer
77
+ * VertexBuffer.new(count, primitive) -> VertexBuffer
78
+ * VertexBuffer.new(count, primitive, usage) -> VertexBuffer
79
+ *
80
+ * Creates a GPU-side vertex buffer holding +count+ vertices (default 0),
81
+ * interpreted as +primitive+ (default :points, see VertexArray#primitive),
82
+ * with the given +usage+ hint (one of :stream, :dynamic, :static; default
83
+ * :static).
84
+ *
85
+ * @return [VertexBuffer]
86
+ * @raise [RuntimeError] if creation fails
87
+ */
88
+ static VALUE VertexBuffer_initialize(int argc, VALUE* argv, VALUE self) {
64
89
  VALUE rb_count, rb_primitive, rb_usage;
65
90
  size_t count = 0;
66
91
  sfPrimitiveType primitive = sfPoints;
67
92
  sfVertexBufferUsage usage = sfVertexBufferStatic;
93
+ sfVertexBuffer* buffer;
68
94
 
69
95
  rb_scan_args(argc, argv, "03", &rb_count, &rb_primitive, &rb_usage);
70
96
 
@@ -80,18 +106,48 @@ static VALUE VertexBuffer_new(int argc, VALUE* argv, VALUE klass) {
80
106
  usage = usage_from_rb(rb_usage);
81
107
  }
82
108
 
83
- return VertexBuffer_wrap(klass, sfVertexBuffer_create(count, primitive, usage));
109
+ buffer = sfVertexBuffer_create(count, primitive, usage);
110
+
111
+ if (buffer == NULL) {
112
+ rb_raise(rb_eRuntimeError, "failed to create vertex buffer");
113
+ }
114
+
115
+ DATA_PTR(self) = buffer;
116
+
117
+ return self;
84
118
  }
85
119
 
120
+ /* call-seq: copy -> VertexBuffer
121
+ *
122
+ * Returns a deep copy of the object.
123
+ *
124
+ * @return [VertexBuffer] an independent copy
125
+ */
86
126
  static VALUE VertexBuffer_copy(VALUE self) {
87
127
  return VertexBuffer_wrap(Get_Klass_VertexBuffer(),
88
128
  sfVertexBuffer_copy(Get_VertexBuffer_Struct(self)));
89
129
  }
90
130
 
131
+ /* call-seq: vertex_count -> Integer
132
+ *
133
+ * Returns the number of vertices stored.
134
+ *
135
+ * @return [Integer]
136
+ */
91
137
  static VALUE VertexBuffer_get_vertex_count(VALUE self) {
92
138
  return SIZET2NUM(sfVertexBuffer_getVertexCount(Get_VertexBuffer_Struct(self)));
93
139
  }
94
140
 
141
+ /* call-seq:
142
+ * update(vertices) -> true or false
143
+ * update(vertices, offset) -> true or false
144
+ *
145
+ * Uploads +vertices+ (an Array of Vertex, or of anything Vertex.new
146
+ * accepts) into the buffer starting at +offset+ (default 0). The buffer
147
+ * must have enough room; use #resize (via .new) if not.
148
+ *
149
+ * @return [Boolean] whether the update succeeded
150
+ */
95
151
  static VALUE VertexBuffer_update(int argc, VALUE* argv, VALUE self) {
96
152
  VALUE rb_vertices, rb_offset;
97
153
  unsigned int offset = 0;
@@ -115,6 +171,15 @@ static VALUE VertexBuffer_update(int argc, VALUE* argv, VALUE self) {
115
171
  return BOOL2RB(result);
116
172
  }
117
173
 
174
+ /* call-seq:
175
+ * update_from(other) -> true or false
176
+ *
177
+ * Copies the contents of +other+ (a VertexBuffer of the same vertex count)
178
+ * into this buffer.
179
+ *
180
+ * @return [Boolean] whether the update succeeded
181
+ * @raise [TypeError] if +other+ is not a VertexBuffer
182
+ */
118
183
  static VALUE VertexBuffer_update_from(VALUE self, VALUE rb_other) {
119
184
  if (!rb_obj_is_kind_of(rb_other, rb_cVertexBuffer)) {
120
185
  raise_invalid_argument_class(rb_cVertexBuffer);
@@ -126,6 +191,14 @@ static VALUE VertexBuffer_update_from(VALUE self, VALUE rb_other) {
126
191
 
127
192
  /* Exchanges the two buffers' contents in place, so anything already holding
128
193
  either object sees the swap. */
194
+ /* call-seq:
195
+ * swap(other) -> self
196
+ *
197
+ * Swaps the contents with +other+.
198
+ *
199
+ * @return [self]
200
+ * @raise [TypeError] if +other+ is not a VertexBuffer
201
+ */
129
202
  static VALUE VertexBuffer_swap(VALUE self, VALUE rb_other) {
130
203
  if (!rb_obj_is_kind_of(rb_other, rb_cVertexBuffer)) {
131
204
  raise_invalid_argument_class(rb_cVertexBuffer);
@@ -136,60 +209,129 @@ static VALUE VertexBuffer_swap(VALUE self, VALUE rb_other) {
136
209
  return self;
137
210
  }
138
211
 
212
+ /* call-seq: primitive -> Symbol
213
+ *
214
+ * Returns the primitive type used to draw the vertices.
215
+ *
216
+ * @return [Symbol] see VertexArray#primitive
217
+ */
139
218
  static VALUE VertexBuffer_get_primitive_type(VALUE self) {
140
219
  return ID2SYM(rb_intern(
141
220
  primitive_type_name(sfVertexBuffer_getPrimitiveType(Get_VertexBuffer_Struct(self)))));
142
221
  }
143
222
 
223
+ /* call-seq:
224
+ * primitive=(value) -> Symbol
225
+ *
226
+ * Sets the primitive type used to draw the vertices.
227
+ *
228
+ * @return [Symbol] +value+
229
+ */
144
230
  static VALUE VertexBuffer_set_primitive_type(VALUE self, VALUE rb_type) {
145
231
  sfVertexBuffer_setPrimitiveType(Get_VertexBuffer_Struct(self), primitive_type_from_rb(rb_type));
146
232
  return rb_type;
147
233
  }
148
234
 
235
+ /* call-seq: usage -> Symbol
236
+ *
237
+ * Returns the buffer's usage hint.
238
+ *
239
+ * @return [Symbol] one of :stream, :dynamic, :static
240
+ */
149
241
  static VALUE VertexBuffer_get_usage(VALUE self) {
150
242
  return ID2SYM(rb_intern(usage_names[sfVertexBuffer_getUsage(Get_VertexBuffer_Struct(self))]));
151
243
  }
152
244
 
245
+ /* call-seq:
246
+ * usage=(value) -> Symbol
247
+ *
248
+ * Sets the buffer's usage hint.
249
+ *
250
+ * @return [Symbol] +value+
251
+ */
153
252
  static VALUE VertexBuffer_set_usage(VALUE self, VALUE rb_usage) {
154
253
  sfVertexBuffer_setUsage(Get_VertexBuffer_Struct(self), usage_from_rb(rb_usage));
155
254
  return rb_usage;
156
255
  }
157
256
 
257
+ /* call-seq: native_handle -> Integer
258
+ *
259
+ * Returns the underlying OpenGL handle.
260
+ *
261
+ * @return [Integer] the underlying OpenGL buffer handle
262
+ */
158
263
  static VALUE VertexBuffer_get_native_handle(VALUE self) {
159
264
  return UINT2NUM(sfVertexBuffer_getNativeHandle(Get_VertexBuffer_Struct(self)));
160
265
  }
161
266
 
267
+ /* call-seq: bind -> self
268
+ *
269
+ * Binds the buffer for drawing.
270
+ *
271
+ * @return [self]
272
+ */
162
273
  static VALUE VertexBuffer_bind(VALUE self) {
163
274
  sfVertexBuffer_bind(Get_VertexBuffer_Struct(self));
164
275
  return self;
165
276
  }
166
277
 
278
+ /* call-seq:
279
+ * VertexBuffer.available? -> true or false
280
+ *
281
+ * Returns +true+ if vertex buffers are supported.
282
+ *
283
+ * @return [Boolean] whether the system supports vertex buffers
284
+ */
167
285
  static VALUE VertexBuffer_is_available(VALUE klass) {
168
286
  return BOOL2RB(sfVertexBuffer_isAvailable());
169
287
  }
170
288
 
289
+ /* call-seq:
290
+ * draw(target, state) -> nil
291
+ *
292
+ * Part of the Drawable interface; call Target#draw instead of this
293
+ * directly.
294
+ *
295
+ * @return [nil]
296
+ * @raise [TypeError] if +target+ is not a Target or +state+ is not a
297
+ * RenderState
298
+ */
171
299
  static VALUE VertexBuffer_draw(VALUE self, VALUE rb_target, VALUE rb_state) {
172
- if (!rb_obj_is_kind_of(rb_target, Get_Klass_Target())) {
173
- raise_invalid_argument_class(Get_Klass_Target());
174
- }
300
+ TargetView view = Get_RenderTarget_View(rb_target);
175
301
 
176
302
  if (!rb_obj_is_kind_of(rb_state, Get_Klass_RenderState())) {
177
303
  raise_invalid_argument_class(Get_Klass_RenderState());
178
304
  }
179
305
 
180
- TARGET_DRAW(Get_Target_Struct(rb_target), sfRenderWindow_drawVertexBuffer,
181
- sfRenderTexture_drawVertexBuffer, Get_VertexBuffer_Struct(self),
182
- Get_RenderState_Struct(rb_state));
306
+ TARGET_DRAW(view, sfRenderWindow_drawVertexBuffer, sfRenderTexture_drawVertexBuffer,
307
+ Get_VertexBuffer_Struct(self), Get_RenderState_Struct(rb_state));
183
308
 
184
309
  return Qnil;
185
310
  }
186
311
 
187
- void Init_VertexBuffer(VALUE rb_module) {
188
- rb_cVertexBuffer = rb_define_class_under(rb_module, "VertexBuffer", rb_cObject);
312
+ /* Document-class: SFML::VertexBuffer
313
+ * Like VertexArray, but the vertex data lives in GPU memory rather than
314
+ * being re-uploaded on every draw -- more efficient for large, static
315
+ * or semi-static vertex sets.
316
+ *
317
+ * Includes +Drawable+.
318
+ *
319
+ * @!attribute primitive
320
+ * The primitive type used to draw the vertices.
321
+ * @return [Symbol]
322
+ * @!attribute usage
323
+ * The buffer's usage hint.
324
+ * @return [Symbol] one of :stream, :dynamic, :static
325
+ */
326
+ void Init_VertexBuffer(VALUE rb_mSFML) {
327
+ rb_cVertexBuffer = rb_define_class_under(rb_mSFML, "VertexBuffer", rb_cObject);
328
+
329
+ rb_define_alloc_func(rb_cVertexBuffer, VertexBuffer_alloc);
189
330
 
190
331
  rb_include_module(rb_cVertexBuffer, Get_Module_Drawable());
191
332
 
192
- rb_define_singleton_method(rb_cVertexBuffer, "new", VertexBuffer_new, -1);
333
+ rb_define_method(rb_cVertexBuffer, "initialize", VertexBuffer_initialize, -1);
334
+ rb_define_private_method(rb_cVertexBuffer, "initialize_copy", VertexBuffer_initialize_copy, 1);
193
335
  rb_define_singleton_method(rb_cVertexBuffer, "available?", VertexBuffer_is_available, 0);
194
336
 
195
337
  rb_define_method(rb_cVertexBuffer, "copy", VertexBuffer_copy, 0);