sfml3-rb 0.0.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 (58) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.md +14 -0
  3. data/README.md +110 -0
  4. data/ext/circle.c +198 -0
  5. data/ext/clock.c +65 -0
  6. data/ext/color.c +43 -0
  7. data/ext/drawable.c +39 -0
  8. data/ext/event.c +148 -0
  9. data/ext/event_name.c +45 -0
  10. data/ext/exceptions.c +44 -0
  11. data/ext/ext/exceptions.h +16 -0
  12. data/ext/ext/klass/clock.h +12 -0
  13. data/ext/ext/klass/color.h +26 -0
  14. data/ext/ext/klass/drawable/circle.h +12 -0
  15. data/ext/ext/klass/drawable/image.h +4 -0
  16. data/ext/ext/klass/drawable/polygon.h +4 -0
  17. data/ext/ext/klass/drawable/rectangle.h +4 -0
  18. data/ext/ext/klass/drawable/sprite.h +4 -0
  19. data/ext/ext/klass/drawable/texture.h +4 -0
  20. data/ext/ext/klass/event/event_name.h +8 -0
  21. data/ext/ext/klass/event.h +12 -0
  22. data/ext/ext/klass/keyboard.h +8 -0
  23. data/ext/ext/klass/render_state.h +16 -0
  24. data/ext/ext/klass/target.h +18 -0
  25. data/ext/ext/klass/transform.h +17 -0
  26. data/ext/ext/klass/transformable.h +13 -0
  27. data/ext/ext/klass/video_mode.h +12 -0
  28. data/ext/ext/klass/view.h +14 -0
  29. data/ext/ext/klass/window.h +12 -0
  30. data/ext/ext/module/drawable/drawable.h +10 -0
  31. data/ext/ext/module/transform.h +18 -0
  32. data/ext/ext/module.h +10 -0
  33. data/ext/ext/rect.h +16 -0
  34. data/ext/ext/sfml.h +16 -0
  35. data/ext/ext/style_name.h +11 -0
  36. data/ext/ext/vec2.h +38 -0
  37. data/ext/ext.c +47 -0
  38. data/ext/extconf.rb +84 -0
  39. data/ext/image.c +5 -0
  40. data/ext/keyboard.c +135 -0
  41. data/ext/polygon.c +5 -0
  42. data/ext/ports.rb +368 -0
  43. data/ext/rect.c +26 -0
  44. data/ext/rectangle.c +5 -0
  45. data/ext/render_state.c +109 -0
  46. data/ext/sprite.c +5 -0
  47. data/ext/style_name.c +15 -0
  48. data/ext/target.c +106 -0
  49. data/ext/texture.c +5 -0
  50. data/ext/transform.c +107 -0
  51. data/ext/transformable.c +145 -0
  52. data/ext/vec2.c +42 -0
  53. data/ext/video_mode.c +78 -0
  54. data/ext/view.c +170 -0
  55. data/ext/window.c +274 -0
  56. data/lib/sfml/version.rb +3 -0
  57. data/lib/sfml.rb +9 -0
  58. metadata +104 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 7a02547990eb0f6906513fabe688b9bcb43ba17aaa510f5732a3f441ca7ad3b0
4
+ data.tar.gz: 964b816e089af1e0ba5cfb23c8cbafd71cc3f1260aef6247174d7f7247accba8
5
+ SHA512:
6
+ metadata.gz: 215e64381b46f7b7ab21ad2d8ec7bec060b983d544253063f160d0b293ca17455a54f32b9f01a9167affe0b31426fa7e43c7bc00688be6c0b64630b702c97e54
7
+ data.tar.gz: 0db0e2855768aeb46f2bccaded19fdc5534aac48e181aa70e34d5dcb84bf3592278f9fa1c07859ff6d42edd312cc5b5ed1173dbe3e4e213f9fb053ca902115f9
data/LICENSE.md ADDED
@@ -0,0 +1,14 @@
1
+ BSD Zero Clause License
2
+
3
+ Copyright (c) 2026 Algaves
4
+
5
+ Permission to use, copy, modify, and/or distribute this software for any
6
+ purpose with or without fee is hereby granted.
7
+
8
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
9
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
10
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
11
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
12
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
13
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
14
+ PERFORMANCE OF THIS SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,110 @@
1
+ # sfml3.rb
2
+
3
+ Ruby bindings for [SFML 3](https://www.sfml-dev.org/), via its C API, [CSFML](https://github.com/SFML/CSFML).
4
+
5
+ ## Status
6
+
7
+ Bound against **CSFML 3**. See [TODO.md](TODO.md) for which parts of the SFML 3 API are ported so
8
+ far, and [CHANGELOG.md](CHANGELOG.md) for what changed recently.
9
+
10
+ ## Install
11
+
12
+ ```sh
13
+ gem install sfml3-rb
14
+ ```
15
+
16
+ On a platform with a precompiled gem this installs a binary with FreeType, SFML 3 and CSFML 3
17
+ already linked in — no toolchain, no build, nothing to install system-wide:
18
+
19
+ | Platform | Status |
20
+ | --- | --- |
21
+ | `x86_64-linux-gnu` | built, installed and tested |
22
+ | `x64-mingw-ucrt` (Windows, RubyInstaller 3.1+) | builds and links cleanly; not yet run on Windows |
23
+ | `aarch64-linux-gnu`, `x86_64-linux-musl`, `x86_64-darwin`, `arm64-darwin` | experimental, not yet built |
24
+
25
+ Each gem carries one extension per Ruby ABI, covering Ruby 3.1 through 4.0.
26
+
27
+ Anywhere else, RubyGems falls back to the source gem, which downloads and builds FreeType, SFML 3
28
+ and CSFML 3 from pinned, checksum-verified tarballs at install time. That takes a few minutes and
29
+ needs:
30
+
31
+ * A C/C++ toolchain and CMake >= 3.22
32
+ * On Linux, the X11/udev/OpenGL development headers SFML links against — these can't be bundled.
33
+ On Fedora:
34
+
35
+ ```sh
36
+ sudo dnf install cmake gcc-c++ libX11-devel \
37
+ libXrandr-devel libXcursor-devel libXi-devel systemd-devel libglvnd-devel
38
+ ```
39
+
40
+ or on Debian/Ubuntu:
41
+
42
+ ```sh
43
+ sudo apt-get install cmake build-essential libx11-dev \
44
+ libxrandr-dev libxcursor-dev libxi-dev libudev-dev libgl1-mesa-dev
45
+ ```
46
+
47
+ Each installed gem version builds its own copy; there's no build cache shared across versions.
48
+
49
+ To link against a system CSFML 3 instead (no download, no build):
50
+
51
+ ```sh
52
+ gem install sfml3-rb -- --enable-system-libraries
53
+ ```
54
+
55
+ ## Usage
56
+
57
+ ```ruby
58
+ require 'sfml'
59
+ include SFML
60
+
61
+ window = Window.new VideoMode.new(640, 480, 32), 'SFML'
62
+ event = Event.new
63
+
64
+ while window.is_open?
65
+ while window.poll_event! event
66
+ window.close! if event.type == 'closed'
67
+ end
68
+
69
+ window.clear [51, 76, 102, 255] # [r, g, b, a], 0-255
70
+ window.display
71
+ end
72
+ ```
73
+
74
+ See [`test/hello-world.rb`](test/hello-world.rb) for a fuller example with shapes and transforms.
75
+
76
+ ## Development
77
+
78
+ ```sh
79
+ rake ports # build the vendored FreeType + SFML 3 + CSFML 3 (rake compile does this too)
80
+ rake compile # build the C extension into lib/sfml/
81
+ rake test # compile, then run the test suite
82
+ rake gem # build the source gem into pkg/
83
+ ```
84
+
85
+ `CMakeLists.txt` is a CLion/IDE convenience build against the same vendored `ports/` prefix — `rake`
86
+ is the build of record.
87
+
88
+ ### Building the binary gems
89
+
90
+ Cross-compilation runs in [rake-compiler-dock](https://github.com/rake-compiler/rake-compiler-dock),
91
+ which supplies the cross toolchains and the cross-compiled rubies. It needs Docker or Podman, and
92
+ pulls a large image per platform on first use.
93
+
94
+ ```sh
95
+ rake platforms # list the targets
96
+ rake gem:x86_64-linux-gnu # build one platform into pkg/
97
+ rake gem:native # build all of them
98
+ ```
99
+
100
+ Each run provisions the container with [`script/provision.sh`](script/provision.sh) — a current
101
+ CMake everywhere, plus the target-side X11/udev/GL development files on Linux targets, which the
102
+ images don't ship. Windows and macOS need nothing extra: SFML uses OS libraries and frameworks the
103
+ mingw toolchain and the osxcross SDK already provide.
104
+
105
+ Binary gems carry one extension per Ruby ABI under `lib/sfml/<major.minor>/`; `lib/sfml.rb` prefers
106
+ that and falls back to the single `lib/sfml/sfml_ext.so` a source build installs.
107
+
108
+ ## License
109
+
110
+ [0BSD](LICENSE.md)
data/ext/circle.c ADDED
@@ -0,0 +1,198 @@
1
+ #include "ext/klass/drawable/circle.h"
2
+
3
+ #include <stdio.h>
4
+
5
+ #include "ext/module/drawable/drawable.h"
6
+ #include "ext/klass/target.h"
7
+ #include "ext/klass/render_state.h"
8
+ #include "ext/exceptions.h"
9
+ #include "ext/module/transform.h"
10
+ #include "ext/klass/transformable.h"
11
+ #include "ext/klass/color.h"
12
+ #include "ext/vec2.h"
13
+ #include "ext/rect.h"
14
+ #include "ext/module.h"
15
+ #include "ext/sfml.h"
16
+
17
+
18
+ static VALUE rb_cCircle;
19
+
20
+ static sfCircleShape *Circle_create(float r) {
21
+ sfCircleShape *circle = sfCircleShape_create();
22
+
23
+ sfCircleShape_setRadius(circle, r);
24
+
25
+ return circle;
26
+ }
27
+
28
+ static void Circle_free(void *ptr) {
29
+ sfCircleShape_destroy(ptr);
30
+ }
31
+
32
+ static VALUE Circle_new(int argc, VALUE *argv, VALUE klass) {
33
+ sfCircleShape *circle;
34
+ float radius;
35
+ VALUE self;
36
+
37
+ if (argc > 1) {
38
+ raise_invalid_arguments_excepted(1, argc);
39
+ }
40
+
41
+ radius = 0;
42
+
43
+ if (argc == 1) {
44
+ radius = NUM2DBL(argv[0]);
45
+ }
46
+
47
+ circle = Circle_create(radius);
48
+
49
+ self = Data_Wrap_Struct(klass, 0, Circle_free, circle);
50
+
51
+ rb_obj_call_init(self, argc, argv);
52
+
53
+ return self;
54
+ }
55
+
56
+ static VALUE Circle_init(int argc, VALUE *argv, VALUE self) {
57
+ return self;
58
+ }
59
+
60
+ static VALUE Circle_set_fill_color(VALUE self, VALUE rb_color) {
61
+ color_check(rb_color);
62
+
63
+ sfCircleShape_setFillColor(Get_Circle_Struct(self), color_new_from_rb(rb_color));
64
+
65
+ return self;
66
+ }
67
+
68
+ static VALUE Circle_get_fill_color(VALUE self) {
69
+ sfColor color = sfCircleShape_getFillColor(Get_Circle_Struct(self));
70
+
71
+ return color_new_from_c(color);
72
+ }
73
+
74
+ static VALUE Circle_set_radius(VALUE self, VALUE rb_radius) {
75
+ sfCircleShape_setRadius(Get_Circle_Struct(self), NUM2DBL(rb_radius));
76
+
77
+ return self;
78
+ }
79
+
80
+ static VALUE Circle_set_position(VALUE self, VALUE rb_position) {
81
+ sfCircleShape_setPosition(Get_Circle_Struct(self), vec2f_new_from_ruby(rb_position));
82
+
83
+ return self;
84
+ }
85
+
86
+ static VALUE Circle_set_rotation(VALUE self, VALUE rb_angle) {
87
+ sfCircleShape_setRotation(Get_Circle_Struct(self), NUM2DBL(rb_angle));
88
+
89
+ return self;
90
+ }
91
+
92
+ static VALUE Circle_set_origin(VALUE self, VALUE rb_origin) {
93
+ sfCircleShape_setOrigin(Get_Circle_Struct(self), vec2f_new_from_ruby(rb_origin));
94
+
95
+ return self;
96
+ }
97
+
98
+ static VALUE Circle_set_scale(VALUE self, VALUE rb_scale) {
99
+ sfCircleShape_setScale(Get_Circle_Struct(self), vec2f_new_from_ruby(rb_scale));
100
+
101
+ return self;
102
+ }
103
+
104
+ static VALUE Circle_get_radius(VALUE self) {
105
+ return DBL2NUM(sfCircleShape_getRadius(Get_Circle_Struct(self)));
106
+ }
107
+
108
+ static VALUE Circle_get_position(VALUE self) {
109
+ return vec2f_new_from_c(sfCircleShape_getPosition(Get_Circle_Struct(self)));
110
+ }
111
+
112
+ static VALUE Circle_get_rotation(VALUE self) {
113
+ return DBL2NUM(sfCircleShape_getRotation(Get_Circle_Struct(self)));
114
+ }
115
+
116
+ static VALUE Circle_get_scale(VALUE self) {
117
+ return vec2f_new_from_c(sfCircleShape_getPosition(Get_Circle_Struct(self)));
118
+ }
119
+
120
+ static VALUE Circle_get_origin(VALUE self) {
121
+ return vec2f_new_from_c(sfCircleShape_getOrigin(Get_Circle_Struct(self)));
122
+ }
123
+
124
+ static VALUE Circle_move(VALUE self, VALUE rb_move) {
125
+ sfCircleShape_move(Get_Circle_Struct(self), vec2f_new_from_ruby(rb_move));
126
+ return self;
127
+ }
128
+
129
+ static VALUE Circle_rotate(VALUE self, VALUE rb_angle) {
130
+ sfCircleShape_rotate(Get_Circle_Struct(self), NUM2DBL(rb_angle));
131
+ return self;
132
+ }
133
+
134
+ static VALUE Circle_scale(VALUE self, VALUE rb_scale) {
135
+ sfCircleShape_scale(Get_Circle_Struct(self), vec2f_new_from_ruby(rb_scale));
136
+ return self;
137
+ }
138
+
139
+ static VALUE Circle_get_matrix(VALUE self) {
140
+ return Transform_MatrixToArray(sfCircleShape_getTransform(Get_Circle_Struct(self)).matrix);
141
+ }
142
+
143
+ static VALUE Circle_draw(VALUE self, VALUE rb_target, VALUE rb_state) {
144
+ if (!rb_obj_is_kind_of(rb_target, Get_Klass_Target())) {
145
+ raise_invalid_argument_class(Get_Klass_Target());
146
+ }
147
+
148
+ if (!rb_obj_is_kind_of(rb_target, Get_Klass_RenderState())) {
149
+ raise_invalid_argument_class(Get_Klass_RenderState());
150
+ }
151
+
152
+ sfRenderWindow_drawCircleShape(((Target *) Get_Target_Struct(rb_target))->window, Get_Circle_Struct(self),
153
+ Get_RenderState_Struct(rb_state));
154
+ return Qnil;
155
+ }
156
+
157
+ void Init_Circle(VALUE rb_module) {
158
+ rb_cCircle = rb_define_class_under(rb_module, "Circle", rb_cObject);
159
+
160
+ rb_include_module(rb_cCircle, Get_Module_Drawable());
161
+
162
+ rb_define_singleton_method(rb_cCircle, "new", Circle_new, -1);
163
+
164
+ // methods
165
+ rb_define_method(rb_cCircle, "initialize", Circle_init, -1);
166
+ rb_define_method(rb_cCircle, "move", Circle_move, 1);
167
+ rb_define_method(rb_cCircle, "rotate", Circle_rotate, 1);
168
+ rb_define_method(rb_cCircle, "escalate", Circle_scale, 1);
169
+ rb_define_method(rb_cCircle, "draw", Circle_draw, 2);
170
+
171
+ // setters
172
+ rb_define_method(rb_cCircle, "radius=", Circle_set_radius, 1);
173
+ rb_define_method(rb_cCircle, "position=", Circle_set_position, 1);
174
+ rb_define_method(rb_cCircle, "angle=", Circle_set_rotation, 1);
175
+ rb_define_method(rb_cCircle, "scale=", Circle_set_scale, 1);
176
+ rb_define_method(rb_cCircle, "origin=", Circle_set_origin, 1);
177
+ rb_define_method(rb_cCircle, "fill_color=", Circle_set_fill_color, 1);
178
+
179
+ // getters
180
+ rb_define_method(rb_cCircle, "radius", Circle_get_radius, 0);
181
+ rb_define_method(rb_cCircle, "position", Circle_get_position, 0);
182
+ rb_define_method(rb_cCircle, "angle", Circle_get_rotation, 0);
183
+ rb_define_method(rb_cCircle, "scale", Circle_get_scale, 0);
184
+ rb_define_method(rb_cCircle, "origin", Circle_get_origin, 0);
185
+ rb_define_method(rb_cCircle, "fill_color", Circle_get_fill_color, 0);
186
+ rb_define_method(rb_cCircle, "matrix", Circle_get_matrix, 0);
187
+
188
+ }
189
+
190
+ void *Get_Circle_Struct(VALUE self) {
191
+ sfCircleShape *ptr;
192
+ Data_Get_Struct(self, sfCircleShape, ptr);
193
+ return ptr;
194
+ }
195
+
196
+ VALUE Get_Klass_Circle() {
197
+ return rb_cCircle;
198
+ }
data/ext/clock.c ADDED
@@ -0,0 +1,65 @@
1
+ #include "ext/klass/clock.h"
2
+
3
+ #include <stdio.h>
4
+
5
+ #include "ext/vec2.h"
6
+ #include "ext/module.h"
7
+ #include "ext/sfml.h"
8
+
9
+ static VALUE rb_cClock;
10
+
11
+ static sfClock *Clock_create() {
12
+ return sfClock_create();
13
+ }
14
+
15
+ static void Clock_free(void *ptr) {
16
+ sfClock_destroy(ptr);
17
+ }
18
+
19
+ static VALUE Clock_new(VALUE klass) {
20
+ sfClock *clock;
21
+ VALUE self;
22
+
23
+ clock = Clock_create();
24
+
25
+ self = Data_Wrap_Struct(klass, 0, Clock_free, clock);
26
+
27
+ rb_obj_call_init(self, 0, NULL);
28
+
29
+ return self;
30
+ }
31
+
32
+ static VALUE Clock_init(VALUE self) {
33
+ return self;
34
+ }
35
+
36
+ static VALUE Clock_get_elapsed_time(VALUE self) {
37
+ return DBL2NUM(sfTime_asSeconds(sfClock_getElapsedTime(Get_Clock_Struct(self))));
38
+ }
39
+
40
+ static VALUE Clock_restart(VALUE self) {
41
+ return DBL2NUM(sfTime_asSeconds(sfClock_restart(Get_Clock_Struct(self))));
42
+ }
43
+
44
+ void Init_Clock(VALUE rb_module) {
45
+ rb_cClock = rb_define_class_under(rb_module, "Clock", rb_cObject);
46
+
47
+ rb_define_singleton_method(rb_cClock, "new", Clock_new, 0);
48
+
49
+ // methods
50
+ rb_define_method(rb_cClock, "initialize", Clock_init, 0);
51
+ rb_define_method(rb_cClock, "restart!", Clock_restart, 0);
52
+
53
+ // getters
54
+ rb_define_method(rb_cClock, "elapsed_time", Clock_get_elapsed_time, 0);
55
+ }
56
+
57
+ void *Get_Clock_Struct(VALUE self) {
58
+ sfClock *clock;
59
+ Data_Get_Struct(self, sfClock, clock);
60
+ return clock;
61
+ }
62
+
63
+ VALUE Get_Klass_Clock() {
64
+ return rb_cClock;
65
+ }
data/ext/color.c ADDED
@@ -0,0 +1,43 @@
1
+ #include "ext/klass/color.h"
2
+
3
+ #include <ruby.h>
4
+ #include <stdio.h>
5
+
6
+ #include "ext/exceptions.h"
7
+
8
+ VALUE color_new(int r, int g, int b, int a) {
9
+ VALUE rb_arr;
10
+
11
+ rb_arr = rb_ary_new();
12
+
13
+ rb_ary_push(rb_arr, INT2NUM(r));
14
+ rb_ary_push(rb_arr, INT2NUM(g));
15
+ rb_ary_push(rb_arr, INT2NUM(b));
16
+ rb_ary_push(rb_arr, INT2NUM(a));
17
+
18
+ return rb_arr;
19
+ }
20
+
21
+ void color_check(VALUE rb_arr) {
22
+ if (rb_array_len(rb_arr) != VALID_LENGTH_COLOR) {
23
+ raise_invalid_array_length(VALID_LENGTH_COLOR);
24
+ }
25
+ }
26
+
27
+ void color_swap(sfColor *color_a, sfColor *color_b) {
28
+ sfColor c;
29
+
30
+ c = *color_b;
31
+ *color_b = *color_a;
32
+ *color_a = c;
33
+ }
34
+
35
+ sfColor color_new_from_rb(VALUE rb_arr) {
36
+ sfColor color = COLOR_RB2C(rb_arr);
37
+
38
+ return color;
39
+ }
40
+
41
+ VALUE color_new_from_c(sfColor color) {
42
+ return COLOR_C2RB(color);
43
+ }
data/ext/drawable.c ADDED
@@ -0,0 +1,39 @@
1
+ #include "ext/module/drawable/drawable.h"
2
+
3
+ #include <ruby.h>
4
+ #include <stdio.h>
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"
16
+
17
+ static VALUE rb_mDrawable;
18
+
19
+ static VALUE Drawable_draw(VALUE self, VALUE rb_target, VALUE rb_state) {
20
+ if (!rb_obj_is_kind_of(rb_target, Get_Klass_Target())) {
21
+
22
+ }
23
+
24
+ if (!rb_obj_is_kind_of(rb_state, Get_Klass_RenderState())) {
25
+
26
+ }
27
+
28
+ return Qnil;
29
+ }
30
+
31
+ void Init_Drawable(VALUE rb_module) {
32
+ rb_mDrawable = rb_define_module_under(rb_module, "Drawable");
33
+
34
+ rb_define_method(rb_mDrawable, "draw", Drawable_draw, 2);
35
+ }
36
+
37
+ VALUE Get_Module_Drawable() {
38
+ return rb_mDrawable;
39
+ }
data/ext/event.c ADDED
@@ -0,0 +1,148 @@
1
+ #include "ext/klass/event.h"
2
+
3
+ #include <ruby.h>
4
+ #include <stdio.h>
5
+
6
+ #include "ext/klass/event/event_name.h"
7
+ #include "ext/klass/keyboard.h"
8
+ #include "ext/klass/video_mode.h"
9
+ #include "ext/vec2.h"
10
+ #include "ext/module.h"
11
+ #include "ext/sfml.h"
12
+
13
+ static VALUE rb_cEvent;
14
+
15
+ static sfEvent *Event_create() {
16
+ return malloc(sizeof(sfEvent));
17
+ }
18
+
19
+ static void Event_free(void *ptr) {
20
+ free(ptr);
21
+ }
22
+
23
+ static VALUE Event_new(VALUE klass) {
24
+ VALUE self;
25
+ sfEvent *event;
26
+
27
+ VALUE argv[] = {};
28
+
29
+ event = Event_create();
30
+ self = Data_Wrap_Struct(klass, 0, Event_free, event);
31
+
32
+ rb_obj_call_init(self, 0, argv);
33
+
34
+ return self;
35
+ }
36
+
37
+ static VALUE Event_init(VALUE self) {
38
+ return self;
39
+ }
40
+
41
+ static VALUE Event_type(VALUE self) {
42
+ sfEvent *event = Get_Event_Struct(self);
43
+ return rb_str_new2(get_event_name(event->type));
44
+ }
45
+
46
+ static VALUE Event_key(VALUE self) {
47
+ VALUE arr;
48
+ VALUE hash;
49
+ sfEvent *event = Get_Event_Struct(self);
50
+
51
+ const char *key_name = get_key_event(event->key.code);
52
+
53
+ /*
54
+ event->key.system;
55
+ event->key.alt;
56
+ event->key.control;
57
+ event->key.shift;
58
+ */
59
+
60
+ arr = rb_ary_new();
61
+ rb_ary_push(arr, rb_str_new2(key_name));
62
+ rb_ary_push(arr, INT2NUM((int) event->key.system));
63
+ rb_ary_push(arr, INT2NUM((int) event->key.alt));
64
+ rb_ary_push(arr, INT2NUM((int) event->key.control));
65
+ rb_ary_push(arr, INT2NUM((int) event->key.shift));
66
+
67
+ return arr;
68
+ }
69
+
70
+ static VALUE Event_size(VALUE self) {
71
+ sfEvent *event = Get_Event_Struct(self);
72
+
73
+ return vec2_new(event->size.size.x, event->size.size.y);
74
+ }
75
+
76
+ static VALUE Event_text(VALUE self) {
77
+ return Qnil;
78
+ }
79
+
80
+ static VALUE Event_mouse_move(VALUE self) {
81
+ return Qnil;
82
+ }
83
+
84
+ static VALUE Event_mouse_button(VALUE self) {
85
+ return Qnil;
86
+ }
87
+
88
+ static VALUE Event_mouse_wheel(VALUE self) {
89
+ return Qnil;
90
+ }
91
+
92
+ static VALUE Event_mouse_wheel_scroll(VALUE self) {
93
+ return Qnil;
94
+ }
95
+
96
+ static VALUE Event_joystick_move(VALUE self) {
97
+ return Qnil;
98
+ }
99
+
100
+ static VALUE Event_joystick_button(VALUE self) {
101
+ return Qnil;
102
+ }
103
+
104
+ static VALUE Event_joystick_connect(VALUE self) {
105
+ return Qnil;
106
+ }
107
+
108
+ static VALUE Event_touch(VALUE self) {
109
+ return Qnil;
110
+ }
111
+
112
+ static VALUE Event_sensor(VALUE self) {
113
+ return Qnil;
114
+ }
115
+
116
+ void Init_Event(VALUE rb_module) {
117
+ rb_cEvent = rb_define_class_under(rb_module, "Event", rb_cObject);
118
+
119
+ rb_define_singleton_method(rb_cEvent, "new", Event_new, 0);
120
+
121
+ // methods
122
+ rb_define_method(rb_cEvent, "initialize", Event_init, 0);
123
+
124
+ // getters
125
+ rb_define_method(rb_cEvent, "type", Event_type, 0);
126
+ rb_define_method(rb_cEvent, "key", Event_key, 0);
127
+ rb_define_method(rb_cEvent, "size", Event_size, 0);
128
+ rb_define_method(rb_cEvent, "text", Event_text, 0);
129
+ rb_define_method(rb_cEvent, "mouse_move", Event_mouse_move, 0);
130
+ rb_define_method(rb_cEvent, "mouse_button", Event_mouse_button, 0);
131
+ rb_define_method(rb_cEvent, "mouse_wheel", Event_mouse_wheel, 0);
132
+ rb_define_method(rb_cEvent, "mouse_wheel_scroll", Event_mouse_wheel_scroll, 0);
133
+ rb_define_method(rb_cEvent, "joystick_move", Event_joystick_move, 0);
134
+ rb_define_method(rb_cEvent, "joystick_button", Event_joystick_button, 0);
135
+ rb_define_method(rb_cEvent, "joystick_connect", Event_joystick_connect, 0);
136
+ rb_define_method(rb_cEvent, "touch", Event_touch, 0);
137
+ rb_define_method(rb_cEvent, "sensor", Event_sensor, 0);
138
+ }
139
+
140
+ void *Get_Event_Struct(VALUE self) {
141
+ sfEvent *ptr;
142
+ Data_Get_Struct(self, sfEvent, ptr);
143
+ return ptr;
144
+ }
145
+
146
+ VALUE Get_Klass_Event() {
147
+ return rb_cEvent;
148
+ }
data/ext/event_name.c ADDED
@@ -0,0 +1,45 @@
1
+ #include "ext/klass/event/event_name.h"
2
+
3
+ #include <stddef.h>
4
+
5
+ #include "ext/sfml.h"
6
+
7
+ /* Indexed by sfEventType rather than by position. The CSFML 2.x table was
8
+ positional, which broke silently under CSFML 3: the enum still has 24
9
+ entries, but MouseWheelMoved was removed and MouseMovedRaw added, shifting
10
+ every name from index 7 onward without any bounds error. */
11
+ static const char *events[] = {
12
+ [sfEvtClosed] = "closed",
13
+ [sfEvtResized] = "resized",
14
+ [sfEvtFocusLost] = "lost-focus",
15
+ [sfEvtFocusGained] = "gained-focus",
16
+ [sfEvtTextEntered] = "text-entered",
17
+ [sfEvtKeyPressed] = "key-pressed",
18
+ [sfEvtKeyReleased] = "key-released",
19
+ [sfEvtMouseWheelScrolled] = "mouse-wheel-scrolled",
20
+ [sfEvtMouseButtonPressed] = "mouse-button-pressed",
21
+ [sfEvtMouseButtonReleased] = "mouse-button-released",
22
+ [sfEvtMouseMoved] = "mouse-moved",
23
+ [sfEvtMouseMovedRaw] = "mouse-moved-raw",
24
+ [sfEvtMouseEntered] = "mouse-entered",
25
+ [sfEvtMouseLeft] = "mouse-left",
26
+ [sfEvtJoystickButtonPressed] = "joystick-button-pressed",
27
+ [sfEvtJoystickButtonReleased] = "joystick-button-released",
28
+ [sfEvtJoystickMoved] = "joystick-moved",
29
+ [sfEvtJoystickConnected] = "joystick-connected",
30
+ [sfEvtJoystickDisconnected] = "joystick-disconnected",
31
+ [sfEvtTouchBegan] = "touch-began",
32
+ [sfEvtTouchMoved] = "touch-moved",
33
+ [sfEvtTouchEnded] = "touch-ended",
34
+ [sfEvtSensorChanged] = "sensor-changed"
35
+ };
36
+
37
+ #define LENGTH_EVENTS (sizeof(events) / sizeof(events[0]))
38
+
39
+ const char *get_event_name(size_t name) {
40
+ if (name < LENGTH_EVENTS && events[name] != NULL) {
41
+ return events[name];
42
+ }
43
+
44
+ return "";
45
+ }