sfml3-rb 0.0.1 → 0.2.2

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 +342 -0
  3. data/README.md +51 -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 +97 -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
data/ext/ext.c CHANGED
@@ -1,20 +1,72 @@
1
1
  #include <ruby.h>
2
2
  #include <stdio.h>
3
3
 
4
- #include "ext/module.h"
5
- #include "ext/module/transform.h"
6
- #include "ext/module/drawable/drawable.h"
7
- #include "ext/klass/color.h"
8
- #include "ext/klass/transformable.h"
9
- #include <ext/klass/clock.h>
10
- #include <ext/klass/target.h>
11
- #include "ext/klass/render_state.h"
12
- #include "ext/klass/event.h"
13
- #include "ext/klass/window.h"
14
- #include "ext/klass/view.h"
15
- #include "ext/klass/video_mode.h"
16
- #include "ext/klass/drawable/circle.h"
17
-
4
+ #include "core/macros.h"
5
+ #include "core/foreign_thread.h"
6
+ #include "system/vec2.h"
7
+ #include "system/vec3.h"
8
+ #include "system/time.h"
9
+ #include "graphics/transform.h"
10
+ #include "graphics/drawable.h"
11
+ #include "graphics/color.h"
12
+ #include "graphics/rect.h"
13
+ #include "graphics/blend_mode.h"
14
+ #include "graphics/stencil_mode.h"
15
+ #include "graphics/image.h"
16
+ #include "graphics/texture.h"
17
+ #include "graphics/transformable.h"
18
+ #include "system/clock.h"
19
+ #include "system/sleep.h"
20
+ #include "system/buffer.h"
21
+ #include "system/input_stream.h"
22
+ #include "graphics/target.h"
23
+ #include "graphics/render_state.h"
24
+ #include "window/event.h"
25
+ #include "window/window.h"
26
+ #include "window/keyboard.h"
27
+ #include "window/mouse.h"
28
+ #include "window/joystick.h"
29
+ #include "window/touch.h"
30
+ #include "window/sensor.h"
31
+ #include "window/clipboard.h"
32
+ #include "window/cursor.h"
33
+ #include "window/context_settings.h"
34
+ #include "window/context.h"
35
+ #include "window/vulkan.h"
36
+ #include "graphics/view.h"
37
+ #include "window/video_mode.h"
38
+ #include "graphics/circle.h"
39
+ #include "graphics/rectangle.h"
40
+ #include "graphics/polygon.h"
41
+ #include "graphics/shape.h"
42
+ #include "graphics/sprite.h"
43
+ #include "graphics/vertex.h"
44
+ #include "graphics/vertex_array.h"
45
+ #include "graphics/vertex_buffer.h"
46
+ #include "graphics/glyph.h"
47
+ #include "graphics/font.h"
48
+ #include "graphics/text.h"
49
+ #include "graphics/render_texture.h"
50
+ #include "graphics/shader.h"
51
+ #include "audio/effect_processor.h"
52
+ #include "audio/audio_enums.h"
53
+ #include "audio/sound_source_cone.h"
54
+ #include "audio/listener.h"
55
+ #include "audio/sound_buffer.h"
56
+ #include "audio/sound.h"
57
+ #include "audio/music.h"
58
+ #include "audio/sound_stream.h"
59
+ #include "audio/sound_buffer_recorder.h"
60
+ #include "audio/sound_recorder.h"
61
+ #include "network/network_enums.h"
62
+ #include "network/ip_address.h"
63
+ #include "network/packet.h"
64
+ #include "network/tcp_socket.h"
65
+ #include "network/tcp_listener.h"
66
+ #include "network/udp_socket.h"
67
+ #include "network/socket_selector.h"
68
+ #include "network/http.h"
69
+ #include "network/ftp.h"
18
70
 
19
71
  // C Naming Convention:
20
72
  //
@@ -30,9 +82,25 @@
30
82
  // Local variables lower_case or lowerCase
31
83
  // Global variables g_lowerCase or g_lower_case (searchable by g_ prefix)
32
84
 
85
+ static VALUE rb_mExt;
86
+
33
87
  void Init_sfml_ext(void) {
34
88
  rb_mExt = rb_define_module("SFML");
35
89
 
90
+ Init_ForeignThread();
91
+
92
+ Init_Vector2(rb_mExt);
93
+ Init_Vector3(rb_mExt);
94
+ Init_Time(rb_mExt);
95
+ Init_Sleep(rb_mExt);
96
+ Init_Buffer(rb_mExt);
97
+ Init_InputStream(rb_mExt);
98
+ Init_Color(rb_mExt);
99
+ Init_Rect(rb_mExt);
100
+ Init_BlendMode(rb_mExt);
101
+ Init_StencilMode(rb_mExt);
102
+ Init_Image(rb_mExt);
103
+ Init_Texture(rb_mExt);
36
104
  Init_Drawable(rb_mExt);
37
105
  Init_Transform(rb_mExt);
38
106
  Init_Transformable(rb_mExt);
@@ -40,8 +108,51 @@ void Init_sfml_ext(void) {
40
108
  Init_Target(rb_mExt);
41
109
  Init_RenderState(rb_mExt);
42
110
  Init_Circle(rb_mExt);
111
+ Init_RectangleShape(rb_mExt);
112
+ Init_ConvexShape(rb_mExt);
113
+ Init_Shape(rb_mExt);
114
+ Init_Sprite(rb_mExt);
115
+ Init_Vertex(rb_mExt);
116
+ Init_VertexArray(rb_mExt);
117
+ Init_VertexBuffer(rb_mExt);
118
+ Init_Glyph(rb_mExt);
119
+ Init_Font(rb_mExt);
120
+ Init_Text(rb_mExt);
121
+ Init_RenderTexture(rb_mExt);
122
+ Init_Shader(rb_mExt);
43
123
  Init_Event(rb_mExt);
44
124
  Init_VideoMode(rb_mExt);
45
125
  Init_View(rb_mExt);
46
126
  Init_Window(rb_mExt);
127
+ Init_Keyboard(rb_mExt);
128
+ Init_Mouse(rb_mExt);
129
+ Init_Joystick(rb_mExt);
130
+ Init_Touch(rb_mExt);
131
+ Init_Sensor(rb_mExt);
132
+ Init_Clipboard(rb_mExt);
133
+ Init_Cursor(rb_mExt);
134
+ Init_ContextSettings(rb_mExt);
135
+ Init_Context(rb_mExt);
136
+ Init_Vulkan(rb_mExt);
137
+
138
+ Init_EffectProcessor();
139
+ Init_AudioEnums(rb_mExt);
140
+ Init_SoundSourceCone(rb_mExt);
141
+ Init_Listener(rb_mExt);
142
+ Init_SoundBuffer(rb_mExt);
143
+ Init_Sound(rb_mExt);
144
+ Init_Music(rb_mExt);
145
+ Init_SoundStream(rb_mExt);
146
+ Init_SoundBufferRecorder(rb_mExt);
147
+ Init_SoundRecorder(rb_mExt);
148
+
149
+ Init_NetworkEnums(rb_mExt);
150
+ Init_IpAddress(rb_mExt);
151
+ Init_Packet(rb_mExt);
152
+ Init_TcpSocket(rb_mExt);
153
+ Init_TcpListener(rb_mExt);
154
+ Init_UdpSocket(rb_mExt);
155
+ Init_SocketSelector(rb_mExt);
156
+ Init_Http(rb_mExt);
157
+ Init_Ftp(rb_mExt);
47
158
  }
data/ext/extconf.rb CHANGED
@@ -1,16 +1,42 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'mkmf'
2
4
  require_relative 'ports'
3
5
 
6
+ # Warnings are on everywhere: a user's build log is the only diagnostic anyone
7
+ # gets when an install fails on a platform we never tested.
8
+ $CFLAGS = "#{$CFLAGS} -Wall -Wextra -Wno-unused-parameter"
9
+
10
+ # Promoting them to errors is opt-in, and CI is the only caller. An unfamiliar
11
+ # compiler at `gem install` time will warn about things this code cannot
12
+ # predict, and a warning must never be what stops someone installing the gem.
13
+ # SFML_STRICT=1 is what keeps the TypedData migration from regressing: going
14
+ # back to Data_Wrap_Struct would reintroduce a deprecation warning and fail.
15
+ $CFLAGS = "#{$CFLAGS} -Werror=deprecated-declarations" if ENV['SFML_STRICT']
16
+
4
17
  # Link order matters for static archives: CSFML depends on SFML, SFML depends
5
- # on FreeType, and all of them depend on the target's OS libraries. This
6
- # mirrors the INTERFACE_LINK_LIBRARIES that SFML's own CMake config exports.
18
+ # on FreeType and the Ogg/Vorbis/FLAC codecs, and all of them depend on the
19
+ # target's OS libraries. This mirrors the INTERFACE_LINK_LIBRARIES that SFML's
20
+ # own CMake config exports. Within the codecs, vorbisfile/vorbisenc depend on
21
+ # vorbis, which depends on ogg, and FLAC stands alone; all of them must follow
22
+ # sfml-audio so the linker has already seen the references.
23
+ #
24
+ # These are named by full path rather than -l. mkmf puts the host's -L/usr/lib64
25
+ # ahead of the ports prefix, so a plain -lfreetype resolves to the host's shared
26
+ # library and the built extension silently gains a runtime dependency on it. A
27
+ # full path to the .a is used as-is by every linker (GNU ld, mingw, ld64) and
28
+ # cannot be shadowed.
7
29
  VENDORED_LIBS = %w[
8
- csfml-graphics-s csfml-window-s csfml-system-s
9
- sfml-graphics-s sfml-window-s sfml-system-s
30
+ csfml-graphics-s csfml-window-s csfml-system-s csfml-audio-s csfml-network-s
31
+ sfml-graphics-s sfml-window-s sfml-system-s sfml-audio-s sfml-network-s
10
32
  freetype
33
+ vorbisfile vorbisenc vorbis ogg
34
+ FLAC
11
35
  ].freeze
12
36
 
13
- SYSTEM_CSFML_LIBS = %w[csfml-graphics csfml-window csfml-system].freeze
37
+ SYSTEM_CSFML_LIBS = %w[
38
+ csfml-graphics csfml-window csfml-system csfml-audio csfml-network
39
+ ].freeze
14
40
 
15
41
  def use_vendored_ports
16
42
  $INCFLAGS = "-I#{Ports.prefix}/include #{$INCFLAGS}"
@@ -27,7 +53,8 @@ def use_vendored_ports
27
53
  # static SFML archives ahead of it.
28
54
  $libs = [
29
55
  $libs,
30
- *(VENDORED_LIBS + Ports.libs).map { |l| "-l#{l}" },
56
+ *VENDORED_LIBS.map { |l| File.join(Ports.prefix, 'lib', "lib#{l}.a") },
57
+ *Ports.libs.map { |l| "-l#{l}" },
31
58
  *Ports.cxx_runtime
32
59
  ].join(' ')
33
60
  end
@@ -81,4 +108,22 @@ C
81
108
  MSG
82
109
  end
83
110
 
111
+ # The sources live in per-subsystem directories (core/, system/, window/,
112
+ # graphics/) but mkmf only globs the top level of $srcdir, so the list has to be
113
+ # handed to it. Objects still land flat in the build directory: mkmf derives
114
+ # $objs from File.basename, and make finds each source through VPATH -- which is
115
+ # why no mkdir rules are needed for the object tree.
116
+ #
117
+ # That flattening also means every .c basename must be unique across the whole
118
+ # tree. mkmf enforces it, aborting with "source files duplication", so a
119
+ # collision fails the build loudly rather than dropping a file.
120
+ sources = Dir.glob("#{$srcdir}/**/*.c")
121
+ $srcs = sources
122
+ $VPATH.concat(
123
+ sources.map { |file| File.dirname(file) }
124
+ .uniq
125
+ .reject { |dir| dir == $srcdir }
126
+ .map { |dir| dir.sub(/\A#{Regexp.escape($srcdir)}/, '$(srcdir)') }
127
+ )
128
+
84
129
  create_makefile 'sfml/sfml_ext'
@@ -0,0 +1,261 @@
1
+ #include "graphics/blend_mode.h"
2
+
3
+ #include <ruby.h>
4
+ #include <stdlib.h>
5
+ #include <string.h>
6
+
7
+ #include "core/exceptions.h"
8
+ #include "core/macros.h"
9
+
10
+ typedef struct {
11
+ sfBlendMode mode;
12
+ } BlendMode;
13
+
14
+ static VALUE rb_cBlendMode;
15
+
16
+ static const char *factor_names[] = {
17
+ "zero", "one", "src_color", "one_minus_src_color", "dst_color",
18
+ "one_minus_dst_color", "src_alpha", "one_minus_src_alpha", "dst_alpha", "one_minus_dst_alpha"
19
+ };
20
+
21
+ static const char *equation_names[] = {
22
+ "add", "subtract", "reverse_subtract", "min", "max"
23
+ };
24
+
25
+ static void BlendMode_free(void *ptr) {
26
+ free(ptr);
27
+ }
28
+
29
+ static const rb_data_type_t BlendMode_data_type = {
30
+ .wrap_struct_name = "SFML::BlendMode",
31
+ .function = {.dmark = NULL, .dfree = BlendMode_free, .dsize = NULL},
32
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY
33
+ };
34
+
35
+ static VALUE BlendMode_wrap(sfBlendMode mode) {
36
+ BlendMode *ptr = malloc(sizeof(BlendMode));
37
+
38
+ ptr->mode = mode;
39
+
40
+ return TypedData_Wrap_Struct(rb_cBlendMode, &BlendMode_data_type, ptr);
41
+ }
42
+
43
+ static sfBlendFactor factor_from_rb(VALUE rb_factor) {
44
+ size_t i;
45
+
46
+ if (RB_INTEGER_TYPE_P(rb_factor)) {
47
+ return (sfBlendFactor) NUM2INT(rb_factor);
48
+ }
49
+
50
+ if (SYMBOL_P(rb_factor)) {
51
+ const char *name = rb_id2name(SYM2ID(rb_factor));
52
+
53
+ for (i = 0; i < sizeof(factor_names) / sizeof(factor_names[0]); i++) {
54
+ if (strcmp(name, factor_names[i]) == 0) {
55
+ return (sfBlendFactor) i;
56
+ }
57
+ }
58
+ }
59
+
60
+ rb_raise(rb_eArgError, "unknown blend factor");
61
+ return sfBlendFactorZero;
62
+ }
63
+
64
+ static sfBlendEquation equation_from_rb(VALUE rb_equation) {
65
+ size_t i;
66
+
67
+ if (RB_INTEGER_TYPE_P(rb_equation)) {
68
+ return (sfBlendEquation) NUM2INT(rb_equation);
69
+ }
70
+
71
+ if (SYMBOL_P(rb_equation)) {
72
+ const char *name = rb_id2name(SYM2ID(rb_equation));
73
+
74
+ for (i = 0; i < sizeof(equation_names) / sizeof(equation_names[0]); i++) {
75
+ if (strcmp(name, equation_names[i]) == 0) {
76
+ return (sfBlendEquation) i;
77
+ }
78
+ }
79
+ }
80
+
81
+ rb_raise(rb_eArgError, "unknown blend equation");
82
+ return sfBlendEquationAdd;
83
+ }
84
+
85
+ static VALUE BlendMode_new(int argc, VALUE *argv, VALUE klass) {
86
+ VALUE self;
87
+ BlendMode *ptr;
88
+ sfBlendMode mode = sfBlendAlpha;
89
+
90
+ if (argc == 6) {
91
+ mode = (sfBlendMode) {
92
+ factor_from_rb(argv[0]), factor_from_rb(argv[1]), equation_from_rb(argv[2]),
93
+ factor_from_rb(argv[3]), factor_from_rb(argv[4]), equation_from_rb(argv[5])
94
+ };
95
+ } else if (argc != 0) {
96
+ raise_invalid_arguments_excepted(6, argc);
97
+ }
98
+
99
+ ptr = malloc(sizeof(BlendMode));
100
+ ptr->mode = mode;
101
+
102
+ self = TypedData_Wrap_Struct(klass, &BlendMode_data_type, ptr);
103
+
104
+ return self;
105
+ }
106
+
107
+ static VALUE BlendMode_get_color_src_factor(VALUE self) {
108
+ return ID2SYM(rb_intern(factor_names[((BlendMode *) Get_BlendMode_Struct(self))->mode.colorSrcFactor]));
109
+ }
110
+
111
+ static VALUE BlendMode_get_color_dst_factor(VALUE self) {
112
+ return ID2SYM(rb_intern(factor_names[((BlendMode *) Get_BlendMode_Struct(self))->mode.colorDstFactor]));
113
+ }
114
+
115
+ static VALUE BlendMode_get_color_equation(VALUE self) {
116
+ return ID2SYM(rb_intern(equation_names[((BlendMode *) Get_BlendMode_Struct(self))->mode.colorEquation]));
117
+ }
118
+
119
+ static VALUE BlendMode_get_alpha_src_factor(VALUE self) {
120
+ return ID2SYM(rb_intern(factor_names[((BlendMode *) Get_BlendMode_Struct(self))->mode.alphaSrcFactor]));
121
+ }
122
+
123
+ static VALUE BlendMode_get_alpha_dst_factor(VALUE self) {
124
+ return ID2SYM(rb_intern(factor_names[((BlendMode *) Get_BlendMode_Struct(self))->mode.alphaDstFactor]));
125
+ }
126
+
127
+ static VALUE BlendMode_get_alpha_equation(VALUE self) {
128
+ return ID2SYM(rb_intern(equation_names[((BlendMode *) Get_BlendMode_Struct(self))->mode.alphaEquation]));
129
+ }
130
+
131
+ static VALUE BlendMode_set_color_src_factor(VALUE self, VALUE rb_value) {
132
+ ((BlendMode *) Get_BlendMode_Struct(self))->mode.colorSrcFactor = factor_from_rb(rb_value);
133
+ return rb_value;
134
+ }
135
+
136
+ static VALUE BlendMode_set_color_dst_factor(VALUE self, VALUE rb_value) {
137
+ ((BlendMode *) Get_BlendMode_Struct(self))->mode.colorDstFactor = factor_from_rb(rb_value);
138
+ return rb_value;
139
+ }
140
+
141
+ static VALUE BlendMode_set_color_equation(VALUE self, VALUE rb_value) {
142
+ ((BlendMode *) Get_BlendMode_Struct(self))->mode.colorEquation = equation_from_rb(rb_value);
143
+ return rb_value;
144
+ }
145
+
146
+ static VALUE BlendMode_set_alpha_src_factor(VALUE self, VALUE rb_value) {
147
+ ((BlendMode *) Get_BlendMode_Struct(self))->mode.alphaSrcFactor = factor_from_rb(rb_value);
148
+ return rb_value;
149
+ }
150
+
151
+ static VALUE BlendMode_set_alpha_dst_factor(VALUE self, VALUE rb_value) {
152
+ ((BlendMode *) Get_BlendMode_Struct(self))->mode.alphaDstFactor = factor_from_rb(rb_value);
153
+ return rb_value;
154
+ }
155
+
156
+ static VALUE BlendMode_set_alpha_equation(VALUE self, VALUE rb_value) {
157
+ ((BlendMode *) Get_BlendMode_Struct(self))->mode.alphaEquation = equation_from_rb(rb_value);
158
+ return rb_value;
159
+ }
160
+
161
+ static VALUE BlendMode_eql(VALUE self, VALUE rb_other) {
162
+ sfBlendMode a = ((BlendMode *) Get_BlendMode_Struct(self))->mode;
163
+ sfBlendMode b;
164
+
165
+ if (!rb_obj_is_kind_of(rb_other, rb_cBlendMode)) {
166
+ return Qfalse;
167
+ }
168
+
169
+ b = ((BlendMode *) Get_BlendMode_Struct(rb_other))->mode;
170
+
171
+ return BOOL2RB(a.colorSrcFactor == b.colorSrcFactor && a.colorDstFactor == b.colorDstFactor &&
172
+ a.colorEquation == b.colorEquation && a.alphaSrcFactor == b.alphaSrcFactor &&
173
+ a.alphaDstFactor == b.alphaDstFactor && a.alphaEquation == b.alphaEquation);
174
+ }
175
+
176
+ static void BlendMode_define_const(VALUE klass, const char *name, sfBlendMode mode) {
177
+ rb_define_const(klass, name, BlendMode_wrap(mode));
178
+ }
179
+
180
+ void Init_BlendMode(VALUE rb_module) {
181
+ rb_cBlendMode = rb_define_class_under(rb_module, "BlendMode", rb_cObject);
182
+
183
+ rb_define_singleton_method(rb_cBlendMode, "new", BlendMode_new, -1);
184
+
185
+ rb_define_method(rb_cBlendMode, "color_src_factor", BlendMode_get_color_src_factor, 0);
186
+ rb_define_method(rb_cBlendMode, "color_dst_factor", BlendMode_get_color_dst_factor, 0);
187
+ rb_define_method(rb_cBlendMode, "color_equation", BlendMode_get_color_equation, 0);
188
+ rb_define_method(rb_cBlendMode, "alpha_src_factor", BlendMode_get_alpha_src_factor, 0);
189
+ rb_define_method(rb_cBlendMode, "alpha_dst_factor", BlendMode_get_alpha_dst_factor, 0);
190
+ rb_define_method(rb_cBlendMode, "alpha_equation", BlendMode_get_alpha_equation, 0);
191
+
192
+ rb_define_method(rb_cBlendMode, "color_src_factor=", BlendMode_set_color_src_factor, 1);
193
+ rb_define_method(rb_cBlendMode, "color_dst_factor=", BlendMode_set_color_dst_factor, 1);
194
+ rb_define_method(rb_cBlendMode, "color_equation=", BlendMode_set_color_equation, 1);
195
+ rb_define_method(rb_cBlendMode, "alpha_src_factor=", BlendMode_set_alpha_src_factor, 1);
196
+ rb_define_method(rb_cBlendMode, "alpha_dst_factor=", BlendMode_set_alpha_dst_factor, 1);
197
+ rb_define_method(rb_cBlendMode, "alpha_equation=", BlendMode_set_alpha_equation, 1);
198
+
199
+ rb_define_method(rb_cBlendMode, "==", BlendMode_eql, 1);
200
+
201
+ BlendMode_define_const(rb_cBlendMode, "NONE", sfBlendNone);
202
+ BlendMode_define_const(rb_cBlendMode, "ALPHA", sfBlendAlpha);
203
+ BlendMode_define_const(rb_cBlendMode, "ADD", sfBlendAdd);
204
+ BlendMode_define_const(rb_cBlendMode, "MULTIPLY", sfBlendMultiply);
205
+ BlendMode_define_const(rb_cBlendMode, "MIN", sfBlendMin);
206
+ BlendMode_define_const(rb_cBlendMode, "MAX", sfBlendMax);
207
+ }
208
+
209
+ VALUE Get_Klass_BlendMode(void) {
210
+ return rb_cBlendMode;
211
+ }
212
+
213
+ void *Get_BlendMode_Struct(VALUE self) {
214
+ BlendMode *ptr;
215
+ TypedData_Get_Struct(self, BlendMode, &BlendMode_data_type, ptr);
216
+ return ptr;
217
+ }
218
+
219
+ sfBlendMode blend_mode_from_rb(VALUE rb_mode) {
220
+ if (rb_obj_is_kind_of(rb_mode, rb_cBlendMode)) {
221
+ return ((BlendMode *) Get_BlendMode_Struct(rb_mode))->mode;
222
+ }
223
+
224
+ if (RB_INTEGER_TYPE_P(rb_mode) == 0 && !SYMBOL_P(rb_mode)) {
225
+ raise_invalid_argument_class(rb_cBlendMode);
226
+ }
227
+
228
+ if (SYMBOL_P(rb_mode)) {
229
+ const char *name = rb_id2name(SYM2ID(rb_mode));
230
+
231
+ if (strcmp(name, "none") == 0) {
232
+ return sfBlendNone;
233
+ }
234
+
235
+ if (strcmp(name, "alpha") == 0) {
236
+ return sfBlendAlpha;
237
+ }
238
+
239
+ if (strcmp(name, "add") == 0) {
240
+ return sfBlendAdd;
241
+ }
242
+
243
+ if (strcmp(name, "multiply") == 0) {
244
+ return sfBlendMultiply;
245
+ }
246
+
247
+ if (strcmp(name, "min") == 0) {
248
+ return sfBlendMin;
249
+ }
250
+
251
+ if (strcmp(name, "max") == 0) {
252
+ return sfBlendMax;
253
+ }
254
+ }
255
+
256
+ return sfBlendAlpha;
257
+ }
258
+
259
+ VALUE blend_mode_to_rb(sfBlendMode mode) {
260
+ return BlendMode_wrap(mode);
261
+ }
@@ -0,0 +1,18 @@
1
+ #ifndef SFML_RB_GRAPHICS_BLEND_MODE_H
2
+ #define SFML_RB_GRAPHICS_BLEND_MODE_H
3
+
4
+ #include <ruby.h>
5
+
6
+ #include "core/sfml.h"
7
+
8
+ void Init_BlendMode(VALUE rb_module);
9
+
10
+ VALUE Get_Klass_BlendMode(void);
11
+
12
+ void *Get_BlendMode_Struct(VALUE self);
13
+
14
+ sfBlendMode blend_mode_from_rb(VALUE rb_mode);
15
+
16
+ VALUE blend_mode_to_rb(sfBlendMode mode);
17
+
18
+ #endif //SFML_RB_GRAPHICS_BLEND_MODE_H