sfml3-rb 0.0.1 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (191) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +321 -0
  3. data/README.md +44 -14
  4. data/TODO.md +199 -0
  5. data/ext/audio/audio_enums.c +131 -0
  6. data/ext/audio/audio_enums.h +18 -0
  7. data/ext/audio/effect_processor.c +233 -0
  8. data/ext/audio/effect_processor.h +25 -0
  9. data/ext/audio/listener.c +83 -0
  10. data/ext/audio/listener.h +8 -0
  11. data/ext/audio/music.c +174 -0
  12. data/ext/audio/music.h +14 -0
  13. data/ext/audio/sound.c +139 -0
  14. data/ext/audio/sound.h +14 -0
  15. data/ext/audio/sound_buffer.c +274 -0
  16. data/ext/audio/sound_buffer.h +18 -0
  17. data/ext/audio/sound_buffer_recorder.c +109 -0
  18. data/ext/audio/sound_buffer_recorder.h +14 -0
  19. data/ext/audio/sound_recorder.c +268 -0
  20. data/ext/audio/sound_recorder.h +14 -0
  21. data/ext/audio/sound_source.h +13 -0
  22. data/ext/audio/sound_source.inc +271 -0
  23. data/ext/audio/sound_source_cone.c +138 -0
  24. data/ext/audio/sound_source_cone.h +20 -0
  25. data/ext/audio/sound_stream.c +294 -0
  26. data/ext/audio/sound_stream.h +14 -0
  27. data/ext/{exceptions.c → core/exceptions.c} +10 -8
  28. data/ext/{ext → core}/exceptions.h +3 -3
  29. data/ext/core/foreign_thread.c +149 -0
  30. data/ext/core/foreign_thread.h +31 -0
  31. data/ext/core/macros.h +8 -0
  32. data/ext/{ext → core}/sfml.h +5 -3
  33. data/ext/core/unicode.c +46 -0
  34. data/ext/core/unicode.h +25 -0
  35. data/ext/ext.c +125 -14
  36. data/ext/extconf.rb +51 -6
  37. data/ext/graphics/blend_mode.c +261 -0
  38. data/ext/graphics/blend_mode.h +18 -0
  39. data/ext/graphics/circle.c +319 -0
  40. data/ext/{ext/klass/drawable → graphics}/circle.h +3 -3
  41. data/ext/graphics/color.c +318 -0
  42. data/ext/graphics/color.h +37 -0
  43. data/ext/{drawable.c → graphics/drawable.c} +11 -11
  44. data/ext/graphics/drawable.h +10 -0
  45. data/ext/graphics/enums.c +123 -0
  46. data/ext/graphics/enums.h +20 -0
  47. data/ext/graphics/font.c +148 -0
  48. data/ext/graphics/font.h +14 -0
  49. data/ext/graphics/glyph.c +60 -0
  50. data/ext/graphics/glyph.h +14 -0
  51. data/ext/graphics/image.c +205 -0
  52. data/ext/graphics/image.h +16 -0
  53. data/ext/graphics/polygon.c +300 -0
  54. data/ext/graphics/polygon.h +10 -0
  55. data/ext/graphics/rect.c +287 -0
  56. data/ext/graphics/rect.h +30 -0
  57. data/ext/graphics/rectangle.c +302 -0
  58. data/ext/graphics/rectangle.h +10 -0
  59. data/ext/graphics/render_state.c +204 -0
  60. data/ext/graphics/render_state.h +18 -0
  61. data/ext/graphics/render_target.inc +181 -0
  62. data/ext/graphics/render_texture.c +182 -0
  63. data/ext/graphics/render_texture.h +14 -0
  64. data/ext/graphics/shader.c +540 -0
  65. data/ext/graphics/shader.h +14 -0
  66. data/ext/graphics/shape.c +311 -0
  67. data/ext/graphics/shape.h +10 -0
  68. data/ext/graphics/sprite.c +249 -0
  69. data/ext/graphics/sprite.h +10 -0
  70. data/ext/graphics/stencil_mode.c +211 -0
  71. data/ext/graphics/stencil_mode.h +18 -0
  72. data/ext/graphics/target.c +133 -0
  73. data/ext/graphics/target.h +40 -0
  74. data/ext/graphics/text.c +344 -0
  75. data/ext/graphics/text.h +10 -0
  76. data/ext/graphics/texture.c +379 -0
  77. data/ext/graphics/texture.h +16 -0
  78. data/ext/graphics/transform.c +372 -0
  79. data/ext/graphics/transform.h +35 -0
  80. data/ext/{transformable.c → graphics/transformable.c} +168 -144
  81. data/ext/{ext/klass → graphics}/transformable.h +3 -3
  82. data/ext/graphics/vertex.c +179 -0
  83. data/ext/graphics/vertex.h +23 -0
  84. data/ext/graphics/vertex_array.c +127 -0
  85. data/ext/graphics/vertex_array.h +10 -0
  86. data/ext/graphics/vertex_buffer.c +211 -0
  87. data/ext/graphics/vertex_buffer.h +14 -0
  88. data/ext/{view.c → graphics/view.c} +168 -169
  89. data/ext/{ext/klass → graphics}/view.h +3 -3
  90. data/ext/network/ftp.c +405 -0
  91. data/ext/network/ftp.h +20 -0
  92. data/ext/network/http.c +243 -0
  93. data/ext/network/http.h +18 -0
  94. data/ext/network/ip_address.c +164 -0
  95. data/ext/network/ip_address.h +20 -0
  96. data/ext/network/network_enums.c +298 -0
  97. data/ext/network/network_enums.h +26 -0
  98. data/ext/network/packet.c +189 -0
  99. data/ext/network/packet.h +14 -0
  100. data/ext/network/socket_selector.c +142 -0
  101. data/ext/network/socket_selector.h +14 -0
  102. data/ext/network/tcp_listener.c +115 -0
  103. data/ext/network/tcp_listener.h +14 -0
  104. data/ext/network/tcp_socket.c +199 -0
  105. data/ext/network/tcp_socket.h +17 -0
  106. data/ext/network/udp_socket.c +190 -0
  107. data/ext/network/udp_socket.h +14 -0
  108. data/ext/ports.rb +88 -21
  109. data/ext/system/buffer.c +77 -0
  110. data/ext/system/buffer.h +14 -0
  111. data/ext/system/clock.c +100 -0
  112. data/ext/{ext/klass → system}/clock.h +3 -3
  113. data/ext/system/input_stream.c +229 -0
  114. data/ext/system/input_stream.h +23 -0
  115. data/ext/system/sleep.c +16 -0
  116. data/ext/system/sleep.h +8 -0
  117. data/ext/system/time.c +218 -0
  118. data/ext/system/time.h +18 -0
  119. data/ext/system/vec2.c +251 -0
  120. data/ext/system/vec2.h +55 -0
  121. data/ext/system/vec3.c +219 -0
  122. data/ext/system/vec3.h +18 -0
  123. data/ext/window/clipboard.c +89 -0
  124. data/ext/window/clipboard.h +8 -0
  125. data/ext/window/context.c +70 -0
  126. data/ext/window/context.h +8 -0
  127. data/ext/window/context_settings.c +220 -0
  128. data/ext/window/context_settings.h +18 -0
  129. data/ext/window/cursor.c +56 -0
  130. data/ext/window/cursor.h +14 -0
  131. data/ext/window/event.c +216 -0
  132. data/ext/window/event.h +14 -0
  133. data/ext/{event_name.c → window/event_name.c} +2 -2
  134. data/ext/window/event_name.h +8 -0
  135. data/ext/window/input_enums.c +261 -0
  136. data/ext/window/input_enums.h +36 -0
  137. data/ext/window/joystick.c +60 -0
  138. data/ext/window/joystick.h +8 -0
  139. data/ext/{keyboard.c → window/keyboard.c} +74 -2
  140. data/ext/window/keyboard.h +12 -0
  141. data/ext/window/mouse.c +70 -0
  142. data/ext/window/mouse.h +8 -0
  143. data/ext/window/sensor.c +30 -0
  144. data/ext/window/sensor.h +8 -0
  145. data/ext/window/touch.c +49 -0
  146. data/ext/window/touch.h +8 -0
  147. data/ext/window/video_mode.c +133 -0
  148. data/ext/window/video_mode.h +14 -0
  149. data/ext/window/vulkan.c +44 -0
  150. data/ext/window/vulkan.h +8 -0
  151. data/ext/window/window.c +414 -0
  152. data/ext/{ext/klass → window}/window.h +3 -3
  153. data/lib/sfml/version.rb +4 -2
  154. data/lib/sfml.rb +2 -0
  155. metadata +163 -56
  156. data/ext/circle.c +0 -198
  157. data/ext/clock.c +0 -65
  158. data/ext/color.c +0 -43
  159. data/ext/event.c +0 -148
  160. data/ext/ext/klass/color.h +0 -26
  161. data/ext/ext/klass/drawable/image.h +0 -4
  162. data/ext/ext/klass/drawable/polygon.h +0 -4
  163. data/ext/ext/klass/drawable/rectangle.h +0 -4
  164. data/ext/ext/klass/drawable/sprite.h +0 -4
  165. data/ext/ext/klass/drawable/texture.h +0 -4
  166. data/ext/ext/klass/event/event_name.h +0 -8
  167. data/ext/ext/klass/event.h +0 -12
  168. data/ext/ext/klass/keyboard.h +0 -8
  169. data/ext/ext/klass/render_state.h +0 -16
  170. data/ext/ext/klass/target.h +0 -18
  171. data/ext/ext/klass/transform.h +0 -17
  172. data/ext/ext/klass/video_mode.h +0 -12
  173. data/ext/ext/module/drawable/drawable.h +0 -10
  174. data/ext/ext/module/transform.h +0 -18
  175. data/ext/ext/module.h +0 -10
  176. data/ext/ext/rect.h +0 -16
  177. data/ext/ext/style_name.h +0 -11
  178. data/ext/ext/vec2.h +0 -38
  179. data/ext/image.c +0 -5
  180. data/ext/polygon.c +0 -5
  181. data/ext/rect.c +0 -26
  182. data/ext/rectangle.c +0 -5
  183. data/ext/render_state.c +0 -109
  184. data/ext/sprite.c +0 -5
  185. data/ext/style_name.c +0 -15
  186. data/ext/target.c +0 -106
  187. data/ext/texture.c +0 -5
  188. data/ext/transform.c +0 -107
  189. data/ext/vec2.c +0 -42
  190. data/ext/video_mode.c +0 -78
  191. data/ext/window.c +0 -274
@@ -1,18 +1,18 @@
1
- #include "ext/module/drawable/drawable.h"
1
+ #include "graphics/drawable.h"
2
2
 
3
3
  #include <ruby.h>
4
4
  #include <stdio.h>
5
5
 
6
- #include "ext/module/transform.h"
7
- #include "ext/klass/transformable.h"
8
- #include "ext/klass/render_state.h"
9
- #include "ext/klass/target.h"
10
- #include "ext/klass/window.h"
11
- #include <ext/exceptions.h>
12
- #include "ext/vec2.h"
13
- #include "ext/rect.h"
14
- #include "ext/module.h"
15
- #include "ext/sfml.h"
6
+ #include "graphics/transform.h"
7
+ #include "graphics/transformable.h"
8
+ #include "graphics/render_state.h"
9
+ #include "graphics/target.h"
10
+ #include "window/window.h"
11
+ #include "core/exceptions.h"
12
+ #include "system/vec2.h"
13
+ #include "graphics/rect.h"
14
+ #include "core/macros.h"
15
+ #include "core/sfml.h"
16
16
 
17
17
  static VALUE rb_mDrawable;
18
18
 
@@ -0,0 +1,10 @@
1
+ #ifndef SFML_RB_GRAPHICS_DRAWABLE_H
2
+ #define SFML_RB_GRAPHICS_DRAWABLE_H
3
+
4
+ #include <ruby.h>
5
+
6
+ void Init_Drawable(VALUE rb_module);
7
+
8
+ VALUE Get_Module_Drawable();
9
+
10
+ #endif //SFML_RB_GRAPHICS_DRAWABLE_H
@@ -0,0 +1,123 @@
1
+ #include "graphics/enums.h"
2
+
3
+ #include <ruby.h>
4
+ #include <string.h>
5
+
6
+ #include "core/exceptions.h"
7
+
8
+ static const char *primitive_names[] = {
9
+ "points", "lines", "line_strip", "triangles", "triangle_strip", "triangle_fan"
10
+ };
11
+
12
+ const char *coordinate_type_name(sfCoordinateType type) {
13
+ return (type == sfCoordinateTypePixels) ? "pixels" : "normalized";
14
+ }
15
+
16
+ sfCoordinateType coordinate_type_from_rb(VALUE rb_type) {
17
+ if (SYMBOL_P(rb_type)) {
18
+ const char *name = rb_id2name(SYM2ID(rb_type));
19
+
20
+ if (strcmp(name, "pixels") == 0) {
21
+ return sfCoordinateTypePixels;
22
+ }
23
+
24
+ if (strcmp(name, "normalized") == 0) {
25
+ return sfCoordinateTypeNormalized;
26
+ }
27
+ }
28
+
29
+ if (RB_INTEGER_TYPE_P(rb_type)) {
30
+ return (sfCoordinateType) NUM2INT(rb_type);
31
+ }
32
+
33
+ rb_raise(rb_eArgError, "expected :normalized or :pixels");
34
+ return sfCoordinateTypeNormalized;
35
+ }
36
+
37
+ const char *primitive_type_name(sfPrimitiveType type) {
38
+ if (type >= sfPoints && type <= sfTriangleFan) {
39
+ return primitive_names[type];
40
+ }
41
+
42
+ return "points";
43
+ }
44
+
45
+ sfPrimitiveType primitive_type_from_rb(VALUE rb_type) {
46
+ if (SYMBOL_P(rb_type)) {
47
+ const char *name = rb_id2name(SYM2ID(rb_type));
48
+ size_t i;
49
+
50
+ for (i = 0; i < sizeof(primitive_names) / sizeof(primitive_names[0]); i++) {
51
+ if (strcmp(name, primitive_names[i]) == 0) {
52
+ return (sfPrimitiveType) i;
53
+ }
54
+ }
55
+ }
56
+
57
+ if (RB_INTEGER_TYPE_P(rb_type)) {
58
+ return (sfPrimitiveType) NUM2INT(rb_type);
59
+ }
60
+
61
+ rb_raise(rb_eArgError, "expected a primitive type symbol");
62
+ return sfPoints;
63
+ }
64
+
65
+ VALUE text_style_to_rb(uint32_t style) {
66
+ VALUE array = rb_ary_new();
67
+
68
+ if ((style & sfTextBold) != 0) {
69
+ rb_ary_push(array, ID2SYM(rb_intern("bold")));
70
+ }
71
+
72
+ if ((style & sfTextItalic) != 0) {
73
+ rb_ary_push(array, ID2SYM(rb_intern("italic")));
74
+ }
75
+
76
+ if ((style & sfTextUnderlined) != 0) {
77
+ rb_ary_push(array, ID2SYM(rb_intern("underlined")));
78
+ }
79
+
80
+ if ((style & sfTextStrikeThrough) != 0) {
81
+ rb_ary_push(array, ID2SYM(rb_intern("strike_through")));
82
+ }
83
+
84
+ return array;
85
+ }
86
+
87
+ uint32_t text_style_from_rb(VALUE rb_style) {
88
+ uint32_t style = 0;
89
+
90
+ if (SYMBOL_P(rb_style) || RB_TYPE_P(rb_style, T_STRING)) {
91
+ rb_style = rb_ary_new_from_args(1, rb_style);
92
+ }
93
+
94
+ if (RB_INTEGER_TYPE_P(rb_style)) {
95
+ return (uint32_t) NUM2UINT(rb_style);
96
+ }
97
+
98
+ if (!RB_TYPE_P(rb_style, T_ARRAY)) {
99
+ rb_raise(rb_eArgError, "expected a text style symbol or array of symbols");
100
+ }
101
+
102
+ for (long i = 0; i < RARRAY_LEN(rb_style); i++) {
103
+ const char *name = rb_id2name(SYM2ID(rb_ary_entry(rb_style, i)));
104
+
105
+ if (strcmp(name, "regular") == 0) {
106
+ continue;
107
+ }
108
+
109
+ if (strcmp(name, "bold") == 0) {
110
+ style |= sfTextBold;
111
+ } else if (strcmp(name, "italic") == 0) {
112
+ style |= sfTextItalic;
113
+ } else if (strcmp(name, "underlined") == 0 || strcmp(name, "underline") == 0) {
114
+ style |= sfTextUnderlined;
115
+ } else if (strcmp(name, "strike_through") == 0 || strcmp(name, "strikethrough") == 0) {
116
+ style |= sfTextStrikeThrough;
117
+ } else {
118
+ rb_raise(rb_eArgError, "unknown text style: %s", name);
119
+ }
120
+ }
121
+
122
+ return style;
123
+ }
@@ -0,0 +1,20 @@
1
+ #ifndef SFML_RB_GRAPHICS_ENUMS_H
2
+ #define SFML_RB_GRAPHICS_ENUMS_H
3
+
4
+ #include <ruby.h>
5
+
6
+ #include "core/sfml.h"
7
+
8
+ const char *coordinate_type_name(sfCoordinateType type);
9
+
10
+ sfCoordinateType coordinate_type_from_rb(VALUE rb_type);
11
+
12
+ const char *primitive_type_name(sfPrimitiveType type);
13
+
14
+ sfPrimitiveType primitive_type_from_rb(VALUE rb_type);
15
+
16
+ VALUE text_style_to_rb(uint32_t style);
17
+
18
+ uint32_t text_style_from_rb(VALUE rb_style);
19
+
20
+ #endif //SFML_RB_GRAPHICS_ENUMS_H
@@ -0,0 +1,148 @@
1
+ #include "graphics/font.h"
2
+
3
+ #include <ruby.h>
4
+
5
+ #include "graphics/glyph.h"
6
+ #include "graphics/texture.h"
7
+ #include "system/input_stream.h"
8
+ #include "core/exceptions.h"
9
+ #include "core/macros.h"
10
+
11
+ static VALUE rb_cFont;
12
+
13
+ static void Font_free(void *ptr) {
14
+ sfFont_destroy(ptr);
15
+ }
16
+
17
+ static const rb_data_type_t Font_data_type = {
18
+ .wrap_struct_name = "SFML::Font",
19
+ .function = {.dmark = NULL, .dfree = Font_free, .dsize = NULL},
20
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY
21
+ };
22
+
23
+ static VALUE Font_wrap(VALUE klass, sfFont *font) {
24
+ if (font == NULL) {
25
+ rb_raise(rb_eRuntimeError, "failed to load font");
26
+ }
27
+
28
+ return TypedData_Wrap_Struct(klass, &Font_data_type, font);
29
+ }
30
+
31
+ static VALUE Font_from_file(VALUE klass, VALUE rb_path) {
32
+ return Font_wrap(klass, sfFont_createFromFile(StringValueCStr(rb_path)));
33
+ }
34
+
35
+ static VALUE Font_from_memory(VALUE klass, VALUE rb_data) {
36
+ StringValue(rb_data);
37
+
38
+ return Font_wrap(klass, sfFont_createFromMemory(RSTRING_PTR(rb_data), (size_t) RSTRING_LEN(rb_data)));
39
+ }
40
+
41
+ static VALUE Font_from_stream(VALUE klass, VALUE rb_stream) {
42
+ VALUE holder = Qnil;
43
+ sfInputStream *stream = input_stream_from_rb(rb_stream, &holder);
44
+
45
+ (void) holder;
46
+
47
+ return Font_wrap(klass, sfFont_createFromStream(stream));
48
+ }
49
+
50
+ static VALUE Font_copy(VALUE self) {
51
+ return Font_wrap(Get_Klass_Font(), sfFont_copy(Get_Font_Struct(self)));
52
+ }
53
+
54
+ static VALUE Font_get_glyph(int argc, VALUE *argv, VALUE self) {
55
+ VALUE rb_codepoint, rb_size, rb_bold, rb_outline;
56
+ bool bold = false;
57
+ float outline = 0;
58
+
59
+ rb_scan_args(argc, argv, "22", &rb_codepoint, &rb_size, &rb_bold, &rb_outline);
60
+
61
+ if (!NIL_P(rb_bold)) {
62
+ bold = RTEST(rb_bold);
63
+ }
64
+
65
+ if (!NIL_P(rb_outline)) {
66
+ outline = NUM2DBL(rb_outline);
67
+ }
68
+
69
+ return glyph_from_c(sfFont_getGlyph(Get_Font_Struct(self), (uint32_t) NUM2UINT(rb_codepoint),
70
+ (unsigned int) NUM2UINT(rb_size), bold, outline));
71
+ }
72
+
73
+ static VALUE Font_has_glyph(VALUE self, VALUE rb_codepoint) {
74
+ return BOOL2RB(sfFont_hasGlyph(Get_Font_Struct(self), (uint32_t) NUM2UINT(rb_codepoint)));
75
+ }
76
+
77
+ static VALUE Font_get_kerning(VALUE self, VALUE rb_first, VALUE rb_second, VALUE rb_size) {
78
+ return DBL2NUM(sfFont_getKerning(Get_Font_Struct(self), (uint32_t) NUM2UINT(rb_first),
79
+ (uint32_t) NUM2UINT(rb_second), (unsigned int) NUM2UINT(rb_size)));
80
+ }
81
+
82
+ static VALUE Font_get_bold_kerning(VALUE self, VALUE rb_first, VALUE rb_second, VALUE rb_size) {
83
+ return DBL2NUM(sfFont_getBoldKerning(Get_Font_Struct(self), (uint32_t) NUM2UINT(rb_first),
84
+ (uint32_t) NUM2UINT(rb_second), (unsigned int) NUM2UINT(rb_size)));
85
+ }
86
+
87
+ static VALUE Font_get_line_spacing(VALUE self, VALUE rb_size) {
88
+ return DBL2NUM(sfFont_getLineSpacing(Get_Font_Struct(self), (unsigned int) NUM2UINT(rb_size)));
89
+ }
90
+
91
+ static VALUE Font_get_underline_position(VALUE self, VALUE rb_size) {
92
+ return DBL2NUM(sfFont_getUnderlinePosition(Get_Font_Struct(self), (unsigned int) NUM2UINT(rb_size)));
93
+ }
94
+
95
+ static VALUE Font_get_underline_thickness(VALUE self, VALUE rb_size) {
96
+ return DBL2NUM(sfFont_getUnderlineThickness(Get_Font_Struct(self), (unsigned int) NUM2UINT(rb_size)));
97
+ }
98
+
99
+ static VALUE Font_get_texture(VALUE self, VALUE rb_size) {
100
+ return texture_from_borrowed(sfFont_getTexture((sfFont *) Get_Font_Struct(self),
101
+ (unsigned int) NUM2UINT(rb_size)));
102
+ }
103
+
104
+ static VALUE Font_set_smooth(VALUE self, VALUE rb_smooth) {
105
+ sfFont_setSmooth((sfFont *) Get_Font_Struct(self), RTEST(rb_smooth));
106
+ return rb_smooth;
107
+ }
108
+
109
+ static VALUE Font_is_smooth(VALUE self) {
110
+ return BOOL2RB(sfFont_isSmooth(Get_Font_Struct(self)));
111
+ }
112
+
113
+ static VALUE Font_get_info(VALUE self) {
114
+ sfFontInfo info = sfFont_getInfo(Get_Font_Struct(self));
115
+
116
+ return rb_str_new_cstr(info.family != NULL ? info.family : "");
117
+ }
118
+
119
+ void Init_Font(VALUE rb_module) {
120
+ rb_cFont = rb_define_class_under(rb_module, "Font", rb_cObject);
121
+
122
+ rb_define_singleton_method(rb_cFont, "from_file", Font_from_file, 1);
123
+ rb_define_singleton_method(rb_cFont, "from_memory", Font_from_memory, 1);
124
+ rb_define_singleton_method(rb_cFont, "from_stream", Font_from_stream, 1);
125
+
126
+ rb_define_method(rb_cFont, "copy", Font_copy, 0);
127
+ rb_define_method(rb_cFont, "glyph", Font_get_glyph, -1);
128
+ rb_define_method(rb_cFont, "has_glyph?", Font_has_glyph, 1);
129
+ rb_define_method(rb_cFont, "kerning", Font_get_kerning, 3);
130
+ rb_define_method(rb_cFont, "bold_kerning", Font_get_bold_kerning, 3);
131
+ rb_define_method(rb_cFont, "line_spacing", Font_get_line_spacing, 1);
132
+ rb_define_method(rb_cFont, "underline_position", Font_get_underline_position, 1);
133
+ rb_define_method(rb_cFont, "underline_thickness", Font_get_underline_thickness, 1);
134
+ rb_define_method(rb_cFont, "texture", Font_get_texture, 1);
135
+ rb_define_method(rb_cFont, "smooth=", Font_set_smooth, 1);
136
+ rb_define_method(rb_cFont, "smooth?", Font_is_smooth, 0);
137
+ rb_define_method(rb_cFont, "info", Font_get_info, 0);
138
+ }
139
+
140
+ VALUE Get_Klass_Font(void) {
141
+ return rb_cFont;
142
+ }
143
+
144
+ const sfFont *Get_Font_Struct(VALUE self) {
145
+ sfFont *ptr;
146
+ TypedData_Get_Struct(self, sfFont, &Font_data_type, ptr);
147
+ return ptr;
148
+ }
@@ -0,0 +1,14 @@
1
+ #ifndef SFML_RB_GRAPHICS_FONT_H
2
+ #define SFML_RB_GRAPHICS_FONT_H
3
+
4
+ #include <ruby.h>
5
+
6
+ #include "core/sfml.h"
7
+
8
+ void Init_Font(VALUE rb_module);
9
+
10
+ VALUE Get_Klass_Font(void);
11
+
12
+ const sfFont *Get_Font_Struct(VALUE self);
13
+
14
+ #endif //SFML_RB_GRAPHICS_FONT_H
@@ -0,0 +1,60 @@
1
+ #include "graphics/glyph.h"
2
+
3
+ #include <ruby.h>
4
+ #include <stdlib.h>
5
+
6
+ #include "graphics/rect.h"
7
+
8
+ typedef struct {
9
+ sfGlyph glyph;
10
+ } Glyph;
11
+
12
+ static VALUE rb_cGlyph;
13
+
14
+ static void Glyph_free(void *ptr) {
15
+ free(ptr);
16
+ }
17
+
18
+ static const rb_data_type_t Glyph_data_type = {
19
+ .wrap_struct_name = "SFML::Glyph",
20
+ .function = {.dmark = NULL, .dfree = Glyph_free, .dsize = NULL},
21
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY
22
+ };
23
+
24
+ VALUE glyph_from_c(sfGlyph glyph) {
25
+ Glyph *ptr = malloc(sizeof(Glyph));
26
+
27
+ ptr->glyph = glyph;
28
+
29
+ return TypedData_Wrap_Struct(rb_cGlyph, &Glyph_data_type, ptr);
30
+ }
31
+
32
+ static Glyph *Get_Glyph(VALUE self) {
33
+ Glyph *ptr;
34
+ TypedData_Get_Struct(self, Glyph, &Glyph_data_type, ptr);
35
+ return ptr;
36
+ }
37
+
38
+ static VALUE Glyph_get_advance(VALUE self) {
39
+ return DBL2NUM(Get_Glyph(self)->glyph.advance);
40
+ }
41
+
42
+ static VALUE Glyph_get_bounds(VALUE self) {
43
+ return rect_to_rb(Get_Glyph(self)->glyph.bounds);
44
+ }
45
+
46
+ static VALUE Glyph_get_texture_rect(VALUE self) {
47
+ return int_rect_to_rb(Get_Glyph(self)->glyph.textureRect);
48
+ }
49
+
50
+ void Init_Glyph(VALUE rb_module) {
51
+ rb_cGlyph = rb_define_class_under(rb_module, "Glyph", rb_cObject);
52
+
53
+ rb_define_method(rb_cGlyph, "advance", Glyph_get_advance, 0);
54
+ rb_define_method(rb_cGlyph, "bounds", Glyph_get_bounds, 0);
55
+ rb_define_method(rb_cGlyph, "texture_rect", Glyph_get_texture_rect, 0);
56
+ }
57
+
58
+ VALUE Get_Klass_Glyph(void) {
59
+ return rb_cGlyph;
60
+ }
@@ -0,0 +1,14 @@
1
+ #ifndef SFML_RB_GRAPHICS_GLYPH_H
2
+ #define SFML_RB_GRAPHICS_GLYPH_H
3
+
4
+ #include <ruby.h>
5
+
6
+ #include "core/sfml.h"
7
+
8
+ void Init_Glyph(VALUE rb_module);
9
+
10
+ VALUE Get_Klass_Glyph(void);
11
+
12
+ VALUE glyph_from_c(sfGlyph glyph);
13
+
14
+ #endif //SFML_RB_GRAPHICS_GLYPH_H
@@ -0,0 +1,205 @@
1
+ #include "graphics/image.h"
2
+
3
+ #include <ruby.h>
4
+ #include <stdlib.h>
5
+
6
+ #include "graphics/color.h"
7
+ #include "graphics/rect.h"
8
+ #include "system/buffer.h"
9
+ #include "system/input_stream.h"
10
+ #include "system/vec2.h"
11
+ #include "core/exceptions.h"
12
+ #include "core/macros.h"
13
+
14
+ static VALUE rb_cImage;
15
+
16
+ static void Image_free(void *ptr) {
17
+ sfImage_destroy(ptr);
18
+ }
19
+
20
+ static const rb_data_type_t Image_data_type = {
21
+ .wrap_struct_name = "SFML::Image",
22
+ .function = {.dmark = NULL, .dfree = Image_free, .dsize = NULL},
23
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY
24
+ };
25
+
26
+ static VALUE Image_wrap(VALUE klass, sfImage *image) {
27
+ if (image == NULL) {
28
+ rb_raise(rb_eRuntimeError, "failed to create image");
29
+ }
30
+
31
+ return TypedData_Wrap_Struct(klass, &Image_data_type, image);
32
+ }
33
+
34
+ static VALUE Image_new(VALUE klass, VALUE rb_size) {
35
+ return Image_wrap(klass, sfImage_create(vec2u_from_rb(rb_size)));
36
+ }
37
+
38
+ static VALUE Image_from_color(VALUE klass, VALUE rb_size, VALUE rb_color) {
39
+ return Image_wrap(klass, sfImage_createFromColor(vec2u_from_rb(rb_size), color_from_rb(rb_color)));
40
+ }
41
+
42
+ static VALUE Image_from_pixels(VALUE klass, VALUE rb_size, VALUE rb_pixels) {
43
+ sfVector2u size = vec2u_from_rb(rb_size);
44
+ size_t expected = (size_t) size.x * size.y * 4;
45
+ sfImage *image;
46
+
47
+ StringValue(rb_pixels);
48
+
49
+ if ((size_t) RSTRING_LEN(rb_pixels) < expected) {
50
+ rb_raise(rb_eArgError, "pixel data too short: expected %lu bytes", (unsigned long) expected);
51
+ }
52
+
53
+ image = sfImage_createFromPixels(size, (const uint8_t *) RSTRING_PTR(rb_pixels));
54
+
55
+ return Image_wrap(klass, image);
56
+ }
57
+
58
+ static VALUE Image_from_file(VALUE klass, VALUE rb_path) {
59
+ return Image_wrap(klass, sfImage_createFromFile(StringValueCStr(rb_path)));
60
+ }
61
+
62
+ static VALUE Image_from_memory(VALUE klass, VALUE rb_data) {
63
+ StringValue(rb_data);
64
+
65
+ return Image_wrap(klass, sfImage_createFromMemory(RSTRING_PTR(rb_data), (size_t) RSTRING_LEN(rb_data)));
66
+ }
67
+
68
+ static VALUE Image_from_stream(VALUE klass, VALUE rb_stream) {
69
+ VALUE holder = Qnil;
70
+ sfInputStream *stream = input_stream_from_rb(rb_stream, &holder);
71
+
72
+ (void) holder;
73
+
74
+ return Image_wrap(klass, sfImage_createFromStream(stream));
75
+ }
76
+
77
+ static VALUE Image_copy(VALUE self) {
78
+ return Image_wrap(Get_Klass_Image(), sfImage_copy(Get_Image_Struct(self)));
79
+ }
80
+
81
+ static VALUE Image_get_size(VALUE self) {
82
+ return vec2f_to_rb((sfVector2f) {
83
+ (float) sfImage_getSize(Get_Image_Struct(self)).x,
84
+ (float) sfImage_getSize(Get_Image_Struct(self)).y
85
+ });
86
+ }
87
+
88
+ static VALUE Image_save_to_file(VALUE self, VALUE rb_path) {
89
+ return BOOL2RB(sfImage_saveToFile(Get_Image_Struct(self), StringValueCStr(rb_path)));
90
+ }
91
+
92
+ static VALUE Image_save_to_memory(int argc, VALUE *argv, VALUE self) {
93
+ VALUE rb_buffer;
94
+ const char *format = "png";
95
+ if (argc > 1) {
96
+ raise_invalid_arguments_excepted(1, argc);
97
+ }
98
+
99
+ if (argc == 1 && !NIL_P(argv[0])) {
100
+ format = StringValueCStr(argv[0]);
101
+ }
102
+
103
+ rb_buffer = rb_funcall(Get_Klass_Buffer(), rb_intern("new"), 0);
104
+
105
+ if (sfImage_saveToMemory(Get_Image_Struct(self), Get_Buffer_Struct(rb_buffer), format)) {
106
+ return rb_buffer;
107
+ }
108
+
109
+ return Qnil;
110
+ }
111
+
112
+ static VALUE Image_create_mask_from_color(VALUE self, VALUE rb_color, VALUE rb_alpha) {
113
+ sfImage_createMaskFromColor((sfImage *) Get_Image_Struct(self), color_from_rb(rb_color), (uint8_t) NUM2INT(rb_alpha));
114
+ return self;
115
+ }
116
+
117
+ static VALUE Image_copy_image(int argc, VALUE *argv, VALUE self) {
118
+ VALUE rb_source, rb_dest, rb_source_rect, rb_apply_alpha;
119
+ bool apply_alpha = true;
120
+
121
+ rb_scan_args(argc, argv, "31", &rb_source, &rb_dest, &rb_source_rect, &rb_apply_alpha);
122
+
123
+ if (!rb_obj_is_kind_of(rb_source, rb_cImage)) {
124
+ raise_invalid_argument_class(rb_cImage);
125
+ }
126
+
127
+ if (!NIL_P(rb_apply_alpha)) {
128
+ apply_alpha = RTEST(rb_apply_alpha);
129
+ }
130
+
131
+ return BOOL2RB(sfImage_copyImage((sfImage *) Get_Image_Struct(self), Get_Image_Struct(rb_source),
132
+ vec2u_from_rb(rb_dest), int_rect_from_rb(rb_source_rect), apply_alpha));
133
+ }
134
+
135
+ static VALUE Image_get_pixel(VALUE self, VALUE rb_x, VALUE rb_y) {
136
+ sfColor color = sfImage_getPixel(Get_Image_Struct(self),
137
+ (sfVector2u) {(unsigned) NUM2UINT(rb_x), (unsigned) NUM2UINT(rb_y)});
138
+
139
+ return color_to_rb(color);
140
+ }
141
+
142
+ static VALUE Image_set_pixel(VALUE self, VALUE rb_x, VALUE rb_y, VALUE rb_color) {
143
+ sfImage_setPixel((sfImage *) Get_Image_Struct(self),
144
+ (sfVector2u) {(unsigned) NUM2UINT(rb_x), (unsigned) NUM2UINT(rb_y)}, color_from_rb(rb_color));
145
+ return self;
146
+ }
147
+
148
+ static VALUE Image_get_pixels(VALUE self) {
149
+ const sfImage *image = Get_Image_Struct(self);
150
+ sfVector2u size = sfImage_getSize(image);
151
+ const uint8_t *pixels = sfImage_getPixelsPtr(image);
152
+
153
+ if (pixels == NULL) {
154
+ return rb_str_new("", 0);
155
+ }
156
+
157
+ return rb_str_new((const char *) pixels, (long) (size.x * size.y * 4));
158
+ }
159
+
160
+ static VALUE Image_flip_horizontally(VALUE self) {
161
+ sfImage_flipHorizontally((sfImage *) Get_Image_Struct(self));
162
+ return self;
163
+ }
164
+
165
+ static VALUE Image_flip_vertically(VALUE self) {
166
+ sfImage_flipVertically((sfImage *) Get_Image_Struct(self));
167
+ return self;
168
+ }
169
+
170
+ void Init_Image(VALUE rb_module) {
171
+ rb_cImage = rb_define_class_under(rb_module, "Image", rb_cObject);
172
+
173
+ rb_define_singleton_method(rb_cImage, "new", Image_new, 1);
174
+ rb_define_singleton_method(rb_cImage, "from_color", Image_from_color, 2);
175
+ rb_define_singleton_method(rb_cImage, "from_pixels", Image_from_pixels, 2);
176
+ rb_define_singleton_method(rb_cImage, "from_file", Image_from_file, 1);
177
+ rb_define_singleton_method(rb_cImage, "from_memory", Image_from_memory, 1);
178
+ rb_define_singleton_method(rb_cImage, "from_stream", Image_from_stream, 1);
179
+
180
+ rb_define_method(rb_cImage, "copy", Image_copy, 0);
181
+ rb_define_method(rb_cImage, "size", Image_get_size, 0);
182
+ rb_define_method(rb_cImage, "save_to_file", Image_save_to_file, 1);
183
+ rb_define_method(rb_cImage, "save_to_memory", Image_save_to_memory, -1);
184
+ rb_define_method(rb_cImage, "create_mask_from_color", Image_create_mask_from_color, 2);
185
+ rb_define_method(rb_cImage, "copy_image", Image_copy_image, -1);
186
+ rb_define_method(rb_cImage, "pixel", Image_get_pixel, 2);
187
+ rb_define_method(rb_cImage, "set_pixel", Image_set_pixel, 3);
188
+ rb_define_method(rb_cImage, "pixels", Image_get_pixels, 0);
189
+ rb_define_method(rb_cImage, "flip_horizontally!", Image_flip_horizontally, 0);
190
+ rb_define_method(rb_cImage, "flip_vertically!", Image_flip_vertically, 0);
191
+ }
192
+
193
+ VALUE Get_Klass_Image(void) {
194
+ return rb_cImage;
195
+ }
196
+
197
+ const sfImage *Get_Image_Struct(VALUE self) {
198
+ sfImage *ptr;
199
+ TypedData_Get_Struct(self, sfImage, &Image_data_type, ptr);
200
+ return ptr;
201
+ }
202
+
203
+ VALUE image_from_c(sfImage *image) {
204
+ return Image_wrap(Get_Klass_Image(), image);
205
+ }
@@ -0,0 +1,16 @@
1
+ #ifndef SFML_RB_GRAPHICS_IMAGE_H
2
+ #define SFML_RB_GRAPHICS_IMAGE_H
3
+
4
+ #include <ruby.h>
5
+
6
+ #include "core/sfml.h"
7
+
8
+ void Init_Image(VALUE rb_module);
9
+
10
+ VALUE Get_Klass_Image(void);
11
+
12
+ const sfImage *Get_Image_Struct(VALUE self);
13
+
14
+ VALUE image_from_c(sfImage *image);
15
+
16
+ #endif //SFML_RB_GRAPHICS_IMAGE_H