glfw 1.0.3 → 3.3.2.0

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 (44) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +161 -0
  3. data/.yardopts +6 -0
  4. data/Gemfile +2 -4
  5. data/README.md +29 -33
  6. data/Rakefile +6 -4
  7. data/ext/glfw/common.c +159 -0
  8. data/ext/glfw/cursor.c +34 -30
  9. data/ext/glfw/extconf.rb +7 -17
  10. data/ext/glfw/glfw.c +122 -377
  11. data/ext/glfw/glfw.h +46 -41
  12. data/ext/glfw/image.c +96 -64
  13. data/ext/glfw/joystick.c +169 -0
  14. data/ext/glfw/monitor.c +231 -155
  15. data/ext/glfw/stb_image.h +7656 -0
  16. data/ext/glfw/window.c +708 -576
  17. data/glfw.gemspec +10 -9
  18. data/lib/glfw.rb +4 -14
  19. data/lib/glfw/constants.rb +365 -0
  20. data/lib/glfw/stubs.rb +234 -0
  21. data/lib/glfw/stubs/cursor.rb +21 -0
  22. data/lib/glfw/stubs/gamepad_state.rb +30 -0
  23. data/lib/glfw/stubs/image.rb +39 -0
  24. data/lib/glfw/stubs/joystick.rb +125 -0
  25. data/lib/glfw/stubs/monitor.rb +115 -0
  26. data/lib/glfw/stubs/video_mode.rb +32 -0
  27. data/lib/glfw/stubs/window.rb +626 -0
  28. data/lib/glfw/version.rb +8 -1
  29. metadata +31 -35
  30. data/.travis.yml +0 -5
  31. data/Makefile +0 -267
  32. data/ext/glfw/common.h +0 -46
  33. data/ext/glfw/cursor.h +0 -14
  34. data/ext/glfw/glfw3.h +0 -4248
  35. data/ext/glfw/glfw3native.h +0 -456
  36. data/ext/glfw/image.h +0 -14
  37. data/ext/glfw/ming32/WINDOWS USERS PLACE libglf3.a HERE.txt b/data/ext/glfw/ming32/WINDOWS USERS PLACE libglf3.a → HERE.txt +0 -0
  38. data/ext/glfw/ming64/WINDOWS USERS PLACE libglf3.a HERE.txt b/data/ext/glfw/ming64/WINDOWS USERS PLACE libglf3.a → HERE.txt +0 -0
  39. data/ext/glfw/monitor.h +0 -29
  40. data/ext/glfw/video_mode.c +0 -60
  41. data/ext/glfw/video_mode.h +0 -21
  42. data/ext/glfw/vulkan.c +0 -48
  43. data/ext/glfw/vulkan.h +0 -16
  44. data/ext/glfw/window.h +0 -87
@@ -1,41 +1,46 @@
1
- #ifndef GLFW_RB_H
2
- #define GLFW_RB_H 1
3
-
4
- #include "common.h"
5
- #include "window.h"
6
- #include "monitor.h"
7
- #include "image.h"
8
- #include "cursor.h"
9
- #include "video_mode.h"
10
- #include "vulkan.h"
11
-
12
- void rb_glfw_error_callback(int error, const char *message);
13
- void rb_glfw_monitor_callback(GLFWmonitor *monitor, int connected);
14
- void rb_glfw_joystick_callback(int joystick, int connected);
15
-
16
- VALUE rb_glfw_init(VALUE klass);
17
- VALUE rb_glfw_terminate(VALUE klass);
18
- VALUE rb_glfw_poll_events(VALUE klass);
19
- VALUE rb_glfw_wait_events(VALUE klass);
20
- VALUE rb_glfw_get_time(VALUE klass);
21
- VALUE rb_glfw_set_time(VALUE klass, VALUE t);
22
- VALUE rb_glfw_swap_interval(VALUE klass, VALUE interval);
23
- VALUE rb_glfe_event_timeout(VALUE klass, VALUE timeout);
24
- VALUE rb_glfw_current_context(VALUE klass);
25
- VALUE rb_glfw_supported_p(VALUE klass, volatile VALUE extension);
26
- VALUE rb_glfw_monitors(VALUE klass);
27
- VALUE rb_glfw_load_default_hints(VALUE klass);
28
- VALUE rb_glfw_window_hint(VALUE klass, VALUE hint, VALUE value);
29
- VALUE rb_glfw_key_name(VALUE klass, VALUE key, VALUE scancode);
30
- VALUE rb_glfw_post_empty(VALUE klass);
31
- VALUE rb_glfw_timer_frequency(VALUE klass);
32
- VALUE rb_glfw_timer_value(VALUE klass);
33
- VALUE rb_glfw_joystick_p(VALUE klass, VALUE joystick);
34
- VALUE rb_glfw_joystick_name(VALUE klass, VALUE joystick);
35
- VALUE rb_glfw_joystick_axes(VALUE klass, VALUE joystick);
36
- VALUE rb_glfw_joystick_buttons(VALUE klass, VALUE joystick);
37
- VALUE rb_glfw_monitor_changed(VALUE klass, VALUE monitor, VALUE connected);
38
- VALUE rb_glfw_joystick_changed(VALUE klass, VALUE joystick, VALUE connected);
39
- VALUE rb_glfw_proc_address(VALUE klass, VALUE name);
40
-
41
- #endif /* GLFW_RB_H */
1
+ #ifndef RB_GLFW_H
2
+ #define RB_GLFW_H 1
3
+
4
+ #include "ruby.h"
5
+ #include "GLFW/glfw3.h"
6
+
7
+ #define GLFW_IMAGE_LOADER 1
8
+
9
+ extern VALUE mGLFW;
10
+ extern VALUE eGLFWError;
11
+ extern VALUE cWindow;
12
+ extern VALUE cMonitor;
13
+ extern VALUE cImage;
14
+ extern VALUE cCursor;
15
+ extern VALUE cVideoMode;
16
+ extern VALUE mJoystick;
17
+ extern VALUE cGamepadState;
18
+ extern VALUE cPoint;
19
+ extern VALUE cSize;
20
+ extern VALUE cVec2;
21
+
22
+ #define RB_BOOL(exp) ((exp) ? Qtrue : Qfalse)
23
+ #define NUM2FLT(v) ((float) NUM2DBL(v))
24
+ #define STR2SYM(str) (ID2SYM(rb_intern(str)))
25
+
26
+ void rb_glfw_window_init(void);
27
+ void rb_glfw_monitor_init(void);
28
+ void rb_glfw_image_init(void);
29
+ void rb_glfw_cursor_init(void);
30
+ void rb_glfw_common_init(void);
31
+ void rb_glfw_joystick_init(void);
32
+
33
+ typedef struct {
34
+ int x;
35
+ int y;
36
+ } RBivec2;
37
+
38
+ typedef struct {
39
+ double x;
40
+ double y;
41
+ } RBvec2;
42
+
43
+ VALUE rb_glfw_ivec2_create(VALUE klass, int x, int y);
44
+ VALUE rb_glfw_vec2_create(VALUE klass, double x, double y);
45
+
46
+ #endif /* RB_GLFW_H */
@@ -1,77 +1,109 @@
1
1
 
2
- #include "image.h"
3
-
4
- VALUE rb_cGLFWimage;
5
-
6
- void Init_glfw_image(VALUE module) {
7
- rb_cGLFWimage = rb_define_class_under(module, "Image", rb_cObject);
8
- rb_define_alloc_func(rb_cGLFWimage, rb_glfw_image_alloc);
9
- rb_define_method(rb_cGLFWimage, "initialize", rb_glfw_image_initialize, -1);
10
-
11
- rb_define_method(rb_cGLFWimage, "width", rb_glfw_image_width, 0);
12
- rb_define_method(rb_cGLFWimage, "height", rb_glfw_image_height, 0);
13
- rb_define_method(rb_cGLFWimage, "pixels", rb_glfw_image_pixels, 0);
14
-
15
- rb_define_alias(rb_cGLFWimage, "columns", "width");
16
- rb_define_alias(rb_cGLFWimage, "rows", "height");
17
- rb_define_alias(rb_cGLFWimage, "to_blob", "pixels");
2
+ #include "glfw.h"
3
+
4
+ #ifdef GLFW_IMAGE_LOADER
5
+ #define STBI_MALLOC xmalloc
6
+ #define STBI_REALLOC xrealloc
7
+ #define STBI_FREE xfree
8
+ #define STB_IMAGE_IMPLEMENTATION 1
9
+ #include "stb_image.h"
10
+ #define ARG_PATTERN "12"
11
+ #else
12
+ #define ARG_PATTERN "21"
13
+ #endif
14
+
15
+ #define BYTES_PER_PIXEL (4)
16
+
17
+ VALUE cImage;
18
+
19
+ static void rb_glfw_image_free(void *image)
20
+ {
21
+ GLFWimage *img = image;
22
+ if (img->pixels)
23
+ xfree(img->pixels);
24
+ xfree(img);
18
25
  }
19
26
 
20
- static VALUE rb_glfw_image_alloc(VALUE klass) {
21
- GLFWimage *image = ALLOC(GLFWimage);
22
- memset(image, 0, sizeof(GLFWimage));
23
- return Data_Wrap_Struct(klass, NULL, RUBY_DEFAULT_FREE, image);
27
+ static VALUE rb_glfw_image_alloc(VALUE klass)
28
+ {
29
+ GLFWimage *img = ALLOC(GLFWimage);
30
+ memset(img, 0, sizeof(GLFWimage));
31
+ return Data_Wrap_Struct(klass, NULL, rb_glfw_image_free, img);
24
32
  }
25
33
 
26
- VALUE rb_glfw_image_initialize(int argc, VALUE *argv, VALUE self) {
27
- if (argc == 2 || argc == 3)
28
- {
29
- GLFWimage *image = ALLOC(GLFWimage);
30
- image->width = NUM2INT(argv[0]);
31
- image->height = NUM2INT(argv[1]);
32
-
33
- if (argc == 3)
34
- {
35
- if (RB_TYPE_P(argv[2], T_STRING))
36
- {
37
- image->pixels = StringValuePtr(argv[2]);
38
- }
39
- else
40
- {
41
- uint32_t packed = NUM2UINT(argv[2]);
42
- int size = sizeof(uint32_t) * image->width * image->height;
43
- image->pixels = malloc(size);
44
- memset(image->pixels, packed, size);
45
- }
46
- }
47
- else
48
- {
49
- int size = 4 * image->width * image->height;
50
- image->pixels = malloc(size);
51
- memset(image->pixels, 0, size);
52
- }
53
- RDATA(self)->data = image;
54
- return Qnil;
55
- }
34
+ static VALUE rb_glfw_image_width(VALUE self)
35
+ {
36
+ return INT2NUM(((GLFWimage *)DATA_PTR(self))->width);
37
+ }
56
38
 
57
- rb_raise(rb_eArgError, "wrong number of arguments (given %d, expected 2, 3)", argc);
39
+ static VALUE rb_glfw_image_height(VALUE self)
40
+ {
41
+ return INT2NUM(((GLFWimage *)DATA_PTR(self))->height);
58
42
  }
59
43
 
60
- VALUE rb_glfw_image_width(VALUE self) {
61
- GLFWimage *image;
62
- Data_Get_Struct(self, GLFWimage, image);
63
- return INT2NUM(image->width);
44
+ static VALUE rb_glfw_image_pixels(VALUE self)
45
+ {
46
+ GLFWimage *img = DATA_PTR(self);
47
+ long len = img->width * img->height * BYTES_PER_PIXEL;
48
+ return rb_str_new(img->pixels, len);
64
49
  }
65
50
 
66
- VALUE rb_glfw_image_height(VALUE self) {
67
- GLFWimage *image;
68
- Data_Get_Struct(self, GLFWimage, image);
69
- return INT2NUM(image->height);
51
+ static VALUE rb_glfw_image_initialize(int argc, VALUE *argv, VALUE self)
52
+ {
53
+ VALUE w, h, blob;
54
+ rb_scan_args(argc, argv, ARG_PATTERN, &w, &h, &blob);
55
+ GLFWimage *image = DATA_PTR(self);
56
+
57
+ switch (argc)
58
+ {
59
+ #ifdef GLFW_IMAGE_LOADER
60
+ case 1:
61
+ {
62
+ const char *filename = StringValueCStr(w);
63
+ image->pixels = stbi_load(filename, &image->width, &image->height, NULL, 4);
64
+ break;
65
+ }
66
+ #endif
67
+ case 2:
68
+ {
69
+ image->width = NUM2INT(w);
70
+ image->height = NUM2INT(h);
71
+ size_t size = image->width * image->height * BYTES_PER_PIXEL;
72
+ if (size <= 0)
73
+ rb_raise(rb_eArgError, "invalid image size specified");
74
+
75
+ image->pixels = xcalloc(size, 1);
76
+ break;
77
+ }
78
+ case 3:
79
+ {
80
+ image->width = NUM2INT(w);
81
+ image->height = NUM2INT(h);
82
+ size_t size = image->width * image->height * BYTES_PER_PIXEL;
83
+
84
+ if (size <= 0)
85
+ rb_raise(rb_eArgError, "invalid image size specified");
86
+
87
+ image->pixels = xmalloc(size);
88
+ memcpy(image->pixels, StringValuePtr(blob), size);
89
+ break;
90
+ }
91
+ }
92
+
93
+ return Qnil;
70
94
  }
71
95
 
72
- VALUE rb_glfw_image_pixels(VALUE self) {
73
- GLFWimage *image;
74
- Data_Get_Struct(self, GLFWimage, image);
75
- int size = image->width * image->height * 4;
76
- return rb_str_new(image->pixels, size);
96
+ void rb_glfw_image_init(void)
97
+ {
98
+ cImage = rb_define_class_under(mGLFW, "Image", rb_cObject);
99
+ rb_define_alloc_func(cImage, rb_glfw_image_alloc);
100
+ rb_define_method(cImage, "initialize", rb_glfw_image_initialize, -1);
101
+
102
+ rb_define_method(cImage, "width", rb_glfw_image_width, 0);
103
+ rb_define_method(cImage, "height", rb_glfw_image_height, 0);
104
+ rb_define_method(cImage, "pixels", rb_glfw_image_pixels, 0);
105
+
106
+ rb_define_alias(cImage, "columns", "width");
107
+ rb_define_alias(cImage, "rows", "height");
108
+ rb_define_alias(cImage, "to_blob", "pixels");
77
109
  }
@@ -0,0 +1,169 @@
1
+
2
+ #include "glfw.h"
3
+
4
+ VALUE mJoystick;
5
+ VALUE cGamepadState;
6
+ VALUE cb_joystick;
7
+
8
+ static VALUE rb_glfw_gamepad_state_alloc(VALUE klass)
9
+ {
10
+ GLFWgamepadstate *state = ALLOC(GLFWgamepadstate);
11
+ memset(state, 0, sizeof(GLFWgamepadstate));
12
+ return Data_Wrap_Struct(klass, NULL, RUBY_DEFAULT_FREE, state);
13
+ }
14
+
15
+ static VALUE rb_glfw_gamepad_axis(VALUE self, VALUE axis)
16
+ {
17
+ int index = NUM2INT(axis);
18
+ if (index >= 0 && index <= GLFW_GAMEPAD_AXIS_LAST)
19
+ return DBL2NUM(((GLFWgamepadstate*) DATA_PTR(self))->axes[index]);
20
+ return DBL2NUM(0.0);
21
+ }
22
+
23
+ static VALUE rb_glfw_gamepad_button(VALUE self, VALUE button)
24
+ {
25
+ int index = NUM2INT(button);
26
+ if (index >= 0 && index <= GLFW_GAMEPAD_BUTTON_LAST)
27
+ return RB_BOOL(((GLFWgamepadstate*) DATA_PTR(self))->buttons[index]);
28
+ return Qfalse;
29
+ }
30
+
31
+ static VALUE rb_glfw_joystick_is_present(VALUE module, VALUE jid)
32
+ {
33
+ return RB_BOOL(glfwJoystickPresent(NUM2INT(jid)));
34
+ }
35
+
36
+ static VALUE rb_glfw_joystick_is_gamepad(VALUE module, VALUE jid)
37
+ {
38
+ return RB_BOOL(glfwJoystickIsGamepad(NUM2INT(jid)));
39
+ }
40
+
41
+ static VALUE rb_glfw_joystick_name(VALUE module, VALUE jid)
42
+ {
43
+ const char *name = glfwGetJoystickName(NUM2INT(jid));
44
+ return name ? rb_str_new_cstr(name) : Qnil;
45
+ }
46
+
47
+ static VALUE rb_glfw_joystick_gamepad_name(VALUE module, VALUE jid)
48
+ {
49
+ const char *name = glfwGetGamepadName(NUM2INT(jid));
50
+ return name ? rb_str_new_cstr(name) : Qnil;
51
+ }
52
+
53
+ static VALUE rb_glfw_joystick_guid(VALUE module, VALUE jid)
54
+ {
55
+ const char *guid = glfwGetJoystickGUID(NUM2INT(jid));
56
+ return guid ? rb_str_new_cstr(guid) : Qnil;
57
+ }
58
+
59
+ static void rb_glfw_joystick_cb_connection(int jid, int event)
60
+ {
61
+ if (RTEST(cb_joystick))
62
+ {
63
+ VALUE args = rb_ary_new_from_args(2, INT2NUM(jid), RB_BOOL(event == GLFW_CONNECTED));
64
+ rb_proc_call(cb_joystick, args);
65
+ }
66
+ }
67
+
68
+ static VALUE rb_glfw_joystick_on_connection(VALUE module)
69
+ {
70
+ VALUE current = cb_joystick;
71
+ if (rb_block_given_p())
72
+ {
73
+ cb_joystick = rb_block_proc();
74
+ rb_iv_set(mJoystick, "@connection_proc", cb_joystick);
75
+ glfwSetJoystickCallback(rb_glfw_joystick_cb_connection);
76
+ }
77
+ else
78
+ {
79
+ cb_joystick = Qnil;
80
+ rb_iv_set(mJoystick, "@connection_proc", Qnil);
81
+ glfwSetJoystickCallback(NULL);
82
+ }
83
+ return current;
84
+ }
85
+
86
+ static VALUE rb_glfw_joystick_update_mappings(int argc, VALUE *argv, VALUE module)
87
+ {
88
+ int count = 0;
89
+ for (int i = 0; i < argc; i++)
90
+ {
91
+ const char *str = StringValueCStr(argv[i]);
92
+ count += glfwUpdateGamepadMappings(str);
93
+ }
94
+ return INT2NUM(count);
95
+ }
96
+
97
+ static VALUE rb_glfw_joystick_gamepad_state(VALUE module, VALUE jid)
98
+ {
99
+ GLFWgamepadstate *state = ALLOC(GLFWgamepadstate);
100
+ if (glfwGetGamepadState(NUM2INT(jid), state))
101
+ return Data_Wrap_Struct(cGamepadState, NULL, RUBY_DEFAULT_FREE, state);
102
+ xfree(state);
103
+ return Qnil;
104
+ }
105
+
106
+ static VALUE rb_glfw_joystick_buttons(VALUE module, VALUE jid)
107
+ {
108
+ int count;
109
+ const unsigned char* buttons = glfwGetJoystickButtons(NUM2INT(jid), &count);
110
+ if (count > 0)
111
+ {
112
+ VALUE ary = rb_ary_new_capa(count);
113
+ for (int i = 0; i < count; i++)
114
+ rb_ary_store(ary, i, RB_BOOL(buttons[i]));
115
+ return ary;
116
+ }
117
+ return Qnil;
118
+ }
119
+
120
+ static VALUE rb_glfw_joystick_axes(VALUE module, VALUE jid)
121
+ {
122
+ int count;
123
+ const float* axes = glfwGetJoystickAxes(NUM2INT(jid), &count);
124
+ if (count > 0)
125
+ {
126
+ VALUE ary = rb_ary_new_capa(count);
127
+ for (int i = 0; i < count; i++)
128
+ rb_ary_store(ary, i, DBL2NUM(axes[i]));
129
+ return ary;
130
+ }
131
+ return Qnil;
132
+ }
133
+
134
+ static VALUE rb_glfw_joystick_hats(VALUE module, VALUE jid)
135
+ {
136
+ int count;
137
+ const unsigned char* hats = glfwGetJoystickHats(NUM2INT(jid), &count);
138
+ if (count > 0)
139
+ {
140
+ VALUE ary = rb_ary_new_capa(count);
141
+ for (int i = 0; i < count; i++)
142
+ rb_ary_store(ary, i, RB_BOOL(hats[i]));
143
+ return ary;
144
+ }
145
+ return Qnil;
146
+ }
147
+
148
+ void rb_glfw_joystick_init(void)
149
+ {
150
+ cGamepadState = rb_define_class_under(mGLFW, "GamepadState", rb_cObject);
151
+ rb_define_alloc_func(cGamepadState, rb_glfw_gamepad_state_alloc);
152
+ rb_define_method(cGamepadState, "axis", rb_glfw_gamepad_axis, 1);
153
+ rb_define_method(cGamepadState, "button", rb_glfw_gamepad_button, 1);
154
+
155
+ mJoystick = rb_define_module_under(mGLFW, "Joystick");
156
+ rb_define_singleton_method(mJoystick, "gamepad_state", rb_glfw_joystick_gamepad_state, 1);
157
+ rb_define_singleton_method(mJoystick, "name", rb_glfw_joystick_name, 1);
158
+ rb_define_singleton_method(mJoystick, "gamepad_name", rb_glfw_joystick_gamepad_name, 1);
159
+ rb_define_singleton_method(mJoystick, "present?", rb_glfw_joystick_is_present, 1);
160
+ rb_define_singleton_method(mJoystick, "gamepad?", rb_glfw_joystick_is_gamepad, 1);
161
+ rb_define_singleton_method(mJoystick, "guid", rb_glfw_joystick_guid, 1);
162
+ rb_define_singleton_method(mJoystick, "buttons", rb_glfw_joystick_buttons, 1);
163
+ rb_define_singleton_method(mJoystick, "axes", rb_glfw_joystick_axes, 1);
164
+ rb_define_singleton_method(mJoystick, "hats", rb_glfw_joystick_hats, 1);
165
+ rb_define_singleton_method(mJoystick, "update_mappings", rb_glfw_joystick_update_mappings, -1);
166
+ rb_define_singleton_method(mJoystick, "on_connection", rb_glfw_joystick_on_connection, 0);
167
+
168
+ cb_joystick = Qnil;
169
+ }
@@ -1,209 +1,285 @@
1
+ #include "glfw.h"
1
2
 
2
- #include "monitor.h"
3
+ VALUE cb_monitor;
4
+ VALUE cMonitor;
5
+ VALUE cVideoMode;
3
6
 
4
- VALUE rb_cGLFWmonitor;
5
-
6
- void Init_glfw_monitor(VALUE module) {
7
- rb_cGLFWmonitor = rb_define_class_under(module, "Monitor", rb_cObject);
8
-
9
- rb_define_method(rb_cGLFWmonitor, "name", rb_glfw_monitor_name, 0);
10
- rb_define_method(rb_cGLFWmonitor, "position", rb_glfw_monitor_pos, 0);
11
- rb_define_method(rb_cGLFWmonitor, "size", rb_glfw_monitor_size, 0);
12
- rb_define_method(rb_cGLFWmonitor, "x", rb_glfw_monitor_x, 0);
13
- rb_define_method(rb_cGLFWmonitor, "y", rb_glfw_monitor_y, 0);
14
- rb_define_method(rb_cGLFWmonitor, "width", rb_glfw_monitor_width, 0);
15
- rb_define_method(rb_cGLFWmonitor, "height", rb_glfw_monitor_height, 0);
16
- rb_define_method(rb_cGLFWmonitor, "dimensions", rb_glfw_monitor_dimensions, 0);
17
-
18
- rb_define_method(rb_cGLFWmonitor, "gamma", rb_glfw_monitor_gamma, 1);
19
- rb_define_method(rb_cGLFWmonitor, "gamma_ramp", rb_glfw_monitor_get_gamma_ramp, 0);
20
- rb_define_method(rb_cGLFWmonitor, "set_gamma_ramp", rb_glfw_monitor_set_gamma_ramp, -1);
21
- rb_define_method(rb_cGLFWmonitor, "video_mode", rb_glfw_monitor_video_mode, 0);
22
- rb_define_method(rb_cGLFWmonitor, "video_modes", rb_glfw_monitor_video_modes, 0);
23
-
24
- rb_define_singleton_method(rb_cGLFWmonitor, "primary", rb_glfw_monitor_primary, 0);
25
- rb_define_const(rb_cGLFWmonitor, "NONE", rb_glfw_monitor_alloc(rb_cGLFWmonitor));
26
-
27
- rb_funcall(rb_cGLFWmonitor, rb_intern("private_class_method"), 1, STR2SYM("new"));
7
+ static inline VALUE rb_glfw_video_create(const GLFWvidmode *value)
8
+ {
9
+ GLFWvidmode *video = ALLOC(GLFWvidmode);
10
+ if (value)
11
+ memcpy(video, value, sizeof(GLFWvidmode));
12
+ else
13
+ memset(video, 0, sizeof(GLFWvidmode));
14
+ return Data_Wrap_Struct(cVideoMode, NULL, RUBY_DEFAULT_FREE, video);
28
15
  }
29
16
 
30
- static VALUE rb_glfw_monitor_alloc(VALUE klass) {
31
- GLFWmonitor *monitor = ruby_xmalloc(SIZEOF_INTPTR_T);
32
- memset(monitor, 0, SIZEOF_INTPTR_T);
33
- return Data_Wrap_Struct(rb_cGLFWmonitor, NULL, RUBY_DEFAULT_FREE, monitor);
17
+ static VALUE rb_glfw_video_alloc(VALUE klass)
18
+ {
19
+ GLFWvidmode *video = ALLOC(GLFWvidmode);
20
+ memset(video, 0, sizeof(GLFWvidmode));
21
+ return Data_Wrap_Struct(klass, NULL, RUBY_DEFAULT_FREE, video);
34
22
  }
35
23
 
36
- VALUE rb_glfw_monitor_name(VALUE self) {
37
- MONITOR();
38
- const char *name = glfwGetMonitorName(m);
39
- return rb_str_new_cstr(name);
24
+ static VALUE rb_glfw_video_width(VALUE self)
25
+ {
26
+ return INT2NUM(((GLFWvidmode*) DATA_PTR(self))->width);
40
27
  }
41
28
 
42
- VALUE rb_glfw_monitor_pos(VALUE self) {
43
- MONITOR();
44
- int x, y;
45
- glfwGetMonitorPos(m, &x, &y);
46
- VALUE ary = rb_ary_new_capa(2);
47
- rb_ary_store(ary, 0, INT2NUM(x));
48
- rb_ary_store(ary, 1, INT2NUM(y));
49
- return ary;
29
+ static VALUE rb_glfw_video_height(VALUE self)
30
+ {
31
+ return INT2NUM(((GLFWvidmode*) DATA_PTR(self))->height);
50
32
  }
51
33
 
52
- VALUE rb_glfw_monitor_size(VALUE self) {
53
- MONITOR();
54
- int width, height;
55
- glfwGetMonitorPhysicalSize(m, &width, &height);
56
- VALUE ary = rb_ary_new_capa(2);
57
- rb_ary_store(ary, 0, INT2NUM(width));
58
- rb_ary_store(ary, 1, INT2NUM(height));
59
- return ary;
34
+ static VALUE rb_glfw_video_r_bits(VALUE self)
35
+ {
36
+ return INT2NUM(((GLFWvidmode*) DATA_PTR(self))->redBits);
60
37
  }
61
38
 
62
- VALUE rb_glfw_monitor_x(VALUE self) {
63
- MONITOR();
64
- int x;
65
- glfwGetMonitorPos(m, &x, NULL);
66
- return INT2NUM(x);
39
+ static VALUE rb_glfw_video_g_bits(VALUE self)
40
+ {
41
+ return INT2NUM(((GLFWvidmode*) DATA_PTR(self))->greenBits);
67
42
  }
68
43
 
69
- VALUE rb_glfw_monitor_y(VALUE self) {
70
- MONITOR();
71
- int y;
72
- glfwGetMonitorPos(m, NULL, &y);
73
- return INT2NUM(y);
44
+ static VALUE rb_glfw_video_b_bits(VALUE self)
45
+ {
46
+ return INT2NUM(((GLFWvidmode*) DATA_PTR(self))->blueBits);
74
47
  }
75
48
 
76
- VALUE rb_glfw_monitor_width(VALUE self) {
77
- MONITOR();
78
- int width;
79
- glfwGetMonitorPhysicalSize(m, &width, NULL);
80
- return INT2NUM(width);
49
+ static VALUE rb_glfw_video_refresh_rate(VALUE self)
50
+ {
51
+ return INT2NUM(((GLFWvidmode*) DATA_PTR(self))->refreshRate);
81
52
  }
82
53
 
83
- VALUE rb_glfw_monitor_height(VALUE self) {
84
- MONITOR();
85
- int height;
86
- glfwGetMonitorPhysicalSize(m, NULL, &height);
87
- return INT2NUM(height);
54
+ static VALUE rb_glfw_video_to_s(VALUE self)
55
+ {
56
+ GLFWvidmode *v = DATA_PTR(self);
57
+ return rb_sprintf("<%s: width:%d, height:%d, r:%d, g:%d, b:%d, rate:%dhz>", rb_class2name(CLASS_OF(self)), v->width, v->height, v->redBits, v->greenBits, v->blueBits, v->refreshRate);
88
58
  }
89
59
 
90
- VALUE rb_glfw_monitor_dimensions(VALUE self) {
91
- MONITOR();
92
- int x, y, width, height;
93
- glfwGetMonitorPos(m, &x, &y);
94
- glfwGetMonitorPhysicalSize(m, &width, &height);
95
- VALUE ary = rb_ary_new_capa(4);
96
- rb_ary_store(ary, 0, INT2NUM(x));
97
- rb_ary_store(ary, 1, INT2NUM(y));
98
- rb_ary_store(ary, 2, INT2NUM(width));
99
- rb_ary_store(ary, 3, INT2NUM(height));
100
- return ary;
101
- }
60
+ static VALUE rb_glfw_monitor_get_gamma_ramp(VALUE self)
61
+ {
62
+ const GLFWgammaramp *ramp = glfwGetGammaRamp(DATA_PTR(self));
102
63
 
103
- VALUE rb_glfw_monitor_get_gamma_ramp(VALUE self) {
104
- MONITOR();
105
- const GLFWgammaramp *ramp = glfwGetGammaRamp(m);
64
+ long capa = (long) ramp->size;
65
+ VALUE r = rb_ary_new_capa(capa);
66
+ VALUE g = rb_ary_new_capa(capa);
67
+ VALUE b = rb_ary_new_capa(capa);
106
68
 
107
- if (ramp == NULL)
108
- return Qnil;
69
+ for (long i = 0; i < capa; i++)
70
+ {
71
+ rb_ary_store(r, i, USHORT2NUM(ramp->red[i]));
72
+ rb_ary_store(g, i, USHORT2NUM(ramp->green[i]));
73
+ rb_ary_store(b, i, USHORT2NUM(ramp->blue[i]));
74
+ }
75
+ return rb_ary_new_from_args(3, r, g, b);
76
+ }
109
77
 
110
- int count = ramp->size;
111
- VALUE hash = rb_hash_new();
112
- VALUE red = rb_ary_new_capa(count), green = rb_ary_new_capa(count), blue = rb_ary_new_capa(count);
78
+ static VALUE rb_glfw_monitor_set_gamma_ramp(VALUE self, VALUE gamma)
79
+ {
80
+ if (!RB_TYPE_P(gamma, T_ARRAY))
81
+ rb_raise(rb_eTypeError, "%s us not an Array", rb_obj_classname(CLASS_OF(gamma)));
82
+ if (rb_array_len(gamma) != 3)
83
+ rb_raise(rb_eArgError, "gamma array must have a length of 3");
113
84
 
114
- for (int i = 0; i < count; i++) {
115
- rb_ary_store(red, i, INT2NUM(ramp->red[i]));
116
- rb_ary_store(green, i, INT2NUM(ramp->green[i]));
117
- rb_ary_store(blue, i, INT2NUM(ramp->blue[i]));
118
- }
119
85
 
120
- rb_hash_aset(hash, ID2SYM(rb_intern("red")), red);
121
- rb_hash_aset(hash, ID2SYM(rb_intern("green")), green);
122
- rb_hash_aset(hash, ID2SYM(rb_intern("blue")), blue);
86
+ VALUE r = rb_ary_entry(gamma, 0);
87
+ VALUE g = rb_ary_entry(gamma, 1);
88
+ VALUE b = rb_ary_entry(gamma, 2);
123
89
 
124
- return hash;
125
- }
90
+ long capa = rb_array_len(g);
91
+ if (rb_array_len(r) != capa || rb_array_len(b) != capa)
92
+ rb_raise(rb_eArgError, "all arrays must be the same length");
126
93
 
127
- VALUE rb_glfw_monitor_set_gamma_ramp(int argc, VALUE *argv, VALUE self) {
128
- if (argc != 1 && argc != 3)
94
+ GLFWgammaramp ramp = {0};
95
+ ramp.size = (unsigned int) capa;
96
+ ramp.red = xmalloc(sizeof(short) * capa);
97
+ ramp.green = xmalloc(sizeof(short) * capa);
98
+ ramp.blue = xmalloc(sizeof(short) * capa);
99
+
100
+ for (long i = 0; i < capa; i++)
129
101
  {
130
- rb_raise(rb_eArgError, "wrong number of arguments (given %d, expected 1, 3)", argc);
131
- return Qnil;
102
+ ramp.red[i] = NUM2USHORT(rb_ary_entry(r, i));
103
+ ramp.green[i] = NUM2USHORT(rb_ary_entry(g, i));
104
+ ramp.blue[i] = NUM2USHORT(rb_ary_entry(b, i));
132
105
  }
133
106
 
134
- MONITOR();
135
- VALUE red, green, blue;
107
+ glfwSetGammaRamp(DATA_PTR(self), &ramp);
136
108
 
137
- if (argc == 1)
138
- {
139
- Check_Type(argv[0], T_HASH);
140
- red = rb_hash_aref(argv[0], ID2SYM(rb_intern("red")));
141
- green = rb_hash_aref(argv[0], ID2SYM(rb_intern("green")));
142
- blue = rb_hash_aref(argv[0], ID2SYM(rb_intern("blue")));
143
- }
144
- else
145
- {
146
- red = argv[0];
147
- green = argv[1];
148
- blue = argv[2];
149
- }
109
+ xfree(ramp.red);
110
+ xfree(ramp.green);
111
+ xfree(ramp.blue);
150
112
 
151
- Check_Type(red, T_ARRAY);
152
- Check_Type(green, T_ARRAY);
153
- Check_Type(blue, T_ARRAY);
113
+ return gamma;
114
+ }
154
115
 
155
- int size = rb_array_len(red);
156
- if (rb_array_len(green) != size || rb_array_len(blue) != size)
157
- {
158
- rb_raise(rb_eRuntimeError, "red, green, and blue gamma ramps must all be of equal length");
159
- return Qnil;
160
- }
116
+ static VALUE rb_glfw_monitor_gamma(VALUE self, VALUE gamma)
117
+ {
118
+ glfwSetGamma(DATA_PTR(self), NUM2FLT(gamma));
119
+ return Qnil;
120
+ }
121
+
122
+ static VALUE rb_glfw_monitor_content_scale(VALUE self)
123
+ {
124
+ float x, y;
125
+ glfwGetMonitorContentScale(DATA_PTR(self), &x, &y);
126
+ return rb_glfw_vec2_create(cVec2, x, y);
127
+ }
161
128
 
162
- GLFWgammaramp *ramp = malloc(sizeof(GLFWgammaramp));
163
- ramp->size = size;
164
- ramp->red = malloc(sizeof(u_short) * size);
165
- ramp->green = malloc(sizeof(u_short) * size);
166
- ramp->blue = malloc(sizeof(u_short) * size);
129
+ static VALUE rb_glfw_monitor_video_mode(VALUE self)
130
+ {
131
+ const GLFWvidmode *mode = glfwGetVideoMode(DATA_PTR(self));
132
+ return rb_glfw_video_create(mode);
133
+ }
134
+
135
+ static VALUE rb_glfw_monitor_video_modes(VALUE self)
136
+ {
137
+ int count;
138
+ const GLFWvidmode *modes = glfwGetVideoModes(DATA_PTR(self), &count);
167
139
 
168
- for (int i = 0; i < size; i++)
140
+ VALUE ary = rb_ary_new_capa(count);
141
+ for (int i = 0; i < count; i++)
169
142
  {
170
- ramp->red[i] = NUM2USHORT(rb_ary_entry(red, i));
171
- ramp->green[i] = NUM2USHORT(rb_ary_entry(green, i));
172
- ramp->blue[i] = NUM2USHORT(rb_ary_entry(blue, i));
143
+ VALUE video = rb_glfw_video_create(&modes[i]);
144
+ rb_ary_store(ary, i, video);
173
145
  }
146
+ return ary;
147
+ }
174
148
 
175
- glfwSetGammaRamp(m, ramp);
149
+ static VALUE rb_glfw_monitor_work_area_location(VALUE self)
150
+ {
151
+ int x, y;
152
+ glfwGetMonitorWorkarea(DATA_PTR(self), &x, &y, NULL, NULL);
153
+ return rb_glfw_ivec2_create(cPoint, x, y);
154
+ }
176
155
 
177
- free(ramp);
178
- return self;
156
+ static VALUE rb_glfw_monitor_work_area_size(VALUE self)
157
+ {
158
+ int w, h;
159
+ glfwGetMonitorWorkarea(DATA_PTR(self), NULL, NULL, &w, &h);
160
+ return rb_glfw_ivec2_create(cSize, w, h);
179
161
  }
180
162
 
181
- VALUE rb_glfw_monitor_gamma(VALUE self, VALUE exponent) {
182
- MONITOR();
183
- float e = (float) NUM2DBL(exponent);
184
- glfwSetGamma(m, e);
185
- return exponent;
163
+ static VALUE rb_glfw_monitor_size(VALUE self)
164
+ {
165
+ const GLFWvidmode *vid = glfwGetVideoMode(DATA_PTR(self));
166
+ return rb_glfw_ivec2_create(cSize, vid->width, vid->height);
186
167
  }
187
168
 
188
- VALUE rb_glfw_monitor_primary(VALUE klass) {
189
- GLFWmonitor *m = glfwGetPrimaryMonitor();
190
- return Data_Wrap_Struct(klass, NULL, RUBY_DEFAULT_FREE, m);
169
+ static VALUE rb_glfw_monitor_physical_size(VALUE self)
170
+ {
171
+ int w, h;
172
+ glfwGetMonitorPhysicalSize(DATA_PTR(self), &w, &h);
173
+ return rb_glfw_ivec2_create(cSize, w, h);
191
174
  }
192
175
 
193
- VALUE rb_glfw_monitor_video_mode(VALUE self) {
194
- MONITOR();
195
- const GLFWvidmode *v = glfwGetVideoMode(m);
196
- return Data_Wrap_Struct(rb_cGLFWvidmode, NULL, RUBY_DEFAULT_FREE, (void*) v);
176
+ static VALUE rb_glfw_monitor_position(VALUE self)
177
+ {
178
+ int x, y;
179
+ glfwGetMonitorPos(DATA_PTR(self), &x, &y);
180
+ return rb_glfw_ivec2_create(cPoint, x, y);
197
181
  }
198
182
 
199
- VALUE rb_glfw_monitor_video_modes(VALUE self) {
200
- MONITOR();
183
+ static VALUE rb_glfw_monitor_available(VALUE klass)
184
+ {
201
185
  int count;
202
- const GLFWvidmode *modes = glfwGetVideoModes(m, &count);
186
+ GLFWmonitor **monitors = glfwGetMonitors(&count);
203
187
  VALUE ary = rb_ary_new_capa(count);
188
+
204
189
  for (int i = 0; i < count; i++)
205
190
  {
206
- rb_ary_store(ary, i, Data_Wrap_Struct(rb_cGLFWvidmode, NULL, RUBY_DEFAULT_FREE, (void*) &modes[i]));
191
+ VALUE monitor = Data_Wrap_Struct(klass, NULL, NULL, monitors[i]);
192
+ rb_ary_store(ary, i, monitor);
207
193
  }
194
+
208
195
  return ary;
209
196
  }
197
+
198
+ static VALUE rb_glfw_monitor_name(VALUE self)
199
+ {
200
+ return rb_str_new_cstr(glfwGetMonitorName(DATA_PTR(self)));
201
+ }
202
+
203
+ static VALUE rb_glfw_monitor_eql(VALUE self, VALUE other)
204
+ {
205
+
206
+ if (CLASS_OF(self) != CLASS_OF(other))
207
+ return Qfalse;
208
+
209
+ return RB_BOOL(DATA_PTR(self) == DATA_PTR(other));
210
+ }
211
+
212
+ static VALUE rb_glfw_monitor_primary(VALUE klass)
213
+ {
214
+ GLFWmonitor *monitor = glfwGetPrimaryMonitor();
215
+ return monitor ? Data_Wrap_Struct(klass, NULL, NULL, monitor) : Qnil;
216
+ }
217
+
218
+ static void rb_glfw_monitor_cb_connected(GLFWmonitor *monitor, int connected)
219
+ {
220
+ if (RTEST(cb_monitor))
221
+ {
222
+ VALUE mon = Data_Wrap_Struct(cMonitor, NULL, NULL, monitor);
223
+ VALUE state = RB_BOOL(connected == GLFW_CONNECTED);
224
+ rb_proc_call(cb_monitor, rb_ary_new_from_args(2, mon, state));
225
+ }
226
+ }
227
+
228
+ static VALUE rb_glfw_monitor_on_connection(VALUE klass)
229
+ {
230
+ VALUE current = cb_monitor;
231
+ if (rb_block_given_p())
232
+ {
233
+ cb_monitor = rb_block_proc();
234
+ glfwSetMonitorCallback(rb_glfw_monitor_cb_connected);
235
+ rb_iv_set(cMonitor, "@connection_proc", cb_monitor);
236
+ }
237
+ else
238
+ {
239
+ cb_monitor = Qnil;
240
+ glfwSetMonitorCallback(NULL);
241
+ rb_iv_set(cMonitor, "@connection_proc", Qnil);
242
+ }
243
+ return current;
244
+ }
245
+
246
+ static VALUE rb_glfw_monitor_to_s(VALUE self) {
247
+ return rb_str_new_cstr(glfwGetMonitorName(DATA_PTR(self)));
248
+ }
249
+
250
+ void rb_glfw_monitor_init(void)
251
+ {
252
+
253
+ cVideoMode = rb_define_class_under(mGLFW, "VideoMode", rb_cObject);
254
+ rb_define_alloc_func(cVideoMode, rb_glfw_video_alloc);
255
+ rb_define_method(cVideoMode, "width", rb_glfw_video_width, 0);
256
+ rb_define_method(cVideoMode, "height", rb_glfw_video_height, 0);
257
+ rb_define_method(cVideoMode, "red_bits", rb_glfw_video_r_bits, 0);
258
+ rb_define_method(cVideoMode, "green_bits", rb_glfw_video_g_bits, 0);
259
+ rb_define_method(cVideoMode, "blue_bits", rb_glfw_video_b_bits, 0);
260
+ rb_define_method(cVideoMode, "refresh_rate", rb_glfw_video_refresh_rate, 0);
261
+ rb_define_method(cVideoMode, "to_s", rb_glfw_video_to_s, 0);
262
+
263
+ cMonitor = rb_define_class_under(mGLFW, "Monitor", rb_cObject);
264
+ rb_define_singleton_method(cMonitor, "available", rb_glfw_monitor_available, 0);
265
+ rb_define_singleton_method(cMonitor, "primary", rb_glfw_monitor_primary, 0);
266
+ rb_define_singleton_method(cMonitor, "on_connection", rb_glfw_monitor_on_connection, 0);
267
+
268
+ rb_define_method(cMonitor, "name", rb_glfw_monitor_name, 0);
269
+ rb_define_method(cMonitor, "position", rb_glfw_monitor_position, 0);
270
+ rb_define_method(cMonitor, "physical_size", rb_glfw_monitor_physical_size, 0);
271
+ rb_define_method(cMonitor, "size", rb_glfw_monitor_size, 0);
272
+ rb_define_method(cMonitor, "client_position", rb_glfw_monitor_work_area_location, 0);
273
+ rb_define_method(cMonitor, "client_size", rb_glfw_monitor_work_area_size, 0);
274
+ rb_define_method(cMonitor, "video_mode", rb_glfw_monitor_video_mode, 0);
275
+ rb_define_method(cMonitor, "video_modes", rb_glfw_monitor_video_modes, 0);
276
+ rb_define_method(cMonitor, "content_scale", rb_glfw_monitor_content_scale, 0);
277
+ rb_define_method(cMonitor, "gamma", rb_glfw_monitor_gamma, 1);
278
+ rb_define_method(cMonitor, "gamma_ramp", rb_glfw_monitor_get_gamma_ramp, 0);
279
+ rb_define_method(cMonitor, "gamma_ramp=", rb_glfw_monitor_set_gamma_ramp, 1);
280
+ rb_define_method(cMonitor, "==", rb_glfw_monitor_eql, 1);
281
+ rb_define_method(cMonitor, "eql?", rb_glfw_monitor_eql, 1);
282
+ rb_define_method(cMonitor, "to_s", rb_glfw_monitor_to_s, 0);
283
+
284
+ cb_monitor = Qnil;
285
+ }