glfw3 0.4.5 → 0.4.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: df407facc1820d92c316b35de01040569b79e376
4
- data.tar.gz: 12b236e4b4c291898eca5166f116133f27091ceb
3
+ metadata.gz: 3587273712306283209d889f6d43ba43161cad94
4
+ data.tar.gz: f9eea41ba36fa06658d0e2f568f789b46a594863
5
5
  SHA512:
6
- metadata.gz: dcdc263c8718b4aa259020d25d023b46f15482e31e3d4a5b3065dff299aa9e46d36c5ca6e5b2ea15d93861af6437e77f7fb90eb3db5532c8356e770375cd4c09
7
- data.tar.gz: 8f0fe94522d88a65979f6039733b341280cde3fca756c6796531bc8fe62452d1065088a368abc48599c7dd17b18acd416deead6cccd5bec877c6a9bc0329f3a1
6
+ metadata.gz: 697392601c4b0cea552420068b5c950dc94f784e500e931a45e9a658359d3fd9e4100e6abd8f3b2ffb3c259ab75a8da01506b8ef66affd15108319743d246793
7
+ data.tar.gz: 9e07041cf488e8455c9f11ff0ad9272ba449f9a2ca82ba1ba3c2c150b89d065073f037e7695cbf62b49fd3abcdb2cf4e5b40143ff690872373cfd75d6750b448
@@ -1,6 +1,7 @@
1
1
  #include "ruby.h"
2
2
  #include <GLFW/glfw3.h>
3
3
 
4
+ void Init_glfw3(void);
4
5
 
5
6
  static const char *kRB_IVAR_WINDOW_INTERNAL_NAME = "@__internal_window";
6
7
  static const char *kRB_IVAR_WINDOW_KEY_CALLBACK_NAME = "@__key_callback";
@@ -59,6 +60,9 @@ static void rb_glfw_error_callback(int error_code, const char *description);
59
60
  static void rb_glfw_monitor_callback(GLFWmonitor *monitor, int message);
60
61
 
61
62
 
63
+ #define Q_IS_A(OBJ, KLASS) RTEST(rb_obj_is_kind_of((OBJ), (KLASS)))
64
+
65
+
62
66
  #define RB_ENABLE_CALLBACK_DEF(NAME, CALLBACK, GLFW_FUNC) \
63
67
  static VALUE NAME (VALUE self, VALUE enabled) \
64
68
  { \
@@ -81,7 +85,6 @@ static VALUE NAME (VALUE self, VALUE enabled) \
81
85
  */
82
86
  static VALUE rb_glfw_init(VALUE self)
83
87
  {
84
- (void)self;
85
88
  VALUE result = glfwInit() ? Qtrue : Qfalse;
86
89
  if (result == Qtrue) {
87
90
  glfwSetMonitorCallback(rb_glfw_monitor_callback);
@@ -128,7 +131,7 @@ static void rb_glfw_error_callback(int error_code, const char *description)
128
131
  {
129
132
  VALUE lambda = rb_cvar_get(s_glfw_module, kRB_CVAR_GLFW_ERROR_CALLBACK);
130
133
 
131
- if (RTEST(lambda)) {
134
+ if (rb_obj_respond_to(lambda, kRB_CALL, 0)) {
132
135
  VALUE rb_description = rb_str_new2(description);
133
136
  VALUE rb_error_code = INT2FIX(error_code);
134
137
  OBJ_FREEZE(rb_description);
@@ -149,7 +152,7 @@ static void rb_glfw_error_callback(int error_code, const char *description)
149
152
  *
150
153
  * Wraps glfwGetMonitors.
151
154
  */
152
- VALUE rb_glfw_get_monitors(VALUE self)
155
+ static VALUE rb_glfw_get_monitors(VALUE self)
153
156
  {
154
157
  long monitor_index = 0;
155
158
  int num_monitors = 0;
@@ -158,7 +161,7 @@ VALUE rb_glfw_get_monitors(VALUE self)
158
161
 
159
162
  if (monitors) {
160
163
  monitors_out = rb_ary_new();
161
- for (; num_monitors; --num_monitors, ++monitor_index) {
164
+ for (; monitor_index < num_monitors; ++monitor_index) {
162
165
  VALUE monitor = Data_Wrap_Struct(s_glfw_monitor_klass, 0, 0, monitors[monitor_index]);
163
166
  rb_obj_call_init(monitor, 0, 0);
164
167
  rb_ary_push(monitors_out, monitor);
@@ -177,7 +180,7 @@ VALUE rb_glfw_get_monitors(VALUE self)
177
180
  *
178
181
  * Wraps glfwGetPrimaryMonitor.
179
182
  */
180
- VALUE rb_glfw_get_primary_monitor(VALUE self)
183
+ static VALUE rb_glfw_get_primary_monitor(VALUE self)
181
184
  {
182
185
  GLFWmonitor *monitor = glfwGetPrimaryMonitor();
183
186
  VALUE rb_monitor = Qnil;
@@ -198,12 +201,12 @@ VALUE rb_glfw_get_primary_monitor(VALUE self)
198
201
  *
199
202
  * Wraps glfwGetMonitorPos.
200
203
  */
201
- VALUE rb_monitor_position(VALUE self)
204
+ static VALUE rb_monitor_position(VALUE self)
202
205
  {
203
- GLFWmonitor *monitor;
204
- Data_Get_Struct(self, GLFWmonitor, monitor);
206
+ GLFWmonitor *monitor = NULL;
205
207
  int xpos = 0;
206
208
  int ypos = 0;
209
+ Data_Get_Struct(self, GLFWmonitor, monitor);
207
210
  glfwGetMonitorPos(monitor, &xpos, &ypos);
208
211
  return rb_ary_new3(2, INT2FIX(xpos), INT2FIX(ypos));
209
212
  }
@@ -218,12 +221,12 @@ VALUE rb_monitor_position(VALUE self)
218
221
  *
219
222
  * Wraps glfwGetMonitorPhysicalSize.
220
223
  */
221
- VALUE rb_monitor_physical_size(VALUE self)
224
+ static VALUE rb_monitor_physical_size(VALUE self)
222
225
  {
223
- GLFWmonitor *monitor;
224
- Data_Get_Struct(self, GLFWmonitor, monitor);
226
+ GLFWmonitor *monitor = NULL;
225
227
  int width = 0;
226
228
  int height = 0;
229
+ Data_Get_Struct(self, GLFWmonitor, monitor);
227
230
  glfwGetMonitorPhysicalSize(monitor, &width, &height);
228
231
  return rb_ary_new3(2, INT2FIX(width), INT2FIX(height));
229
232
  }
@@ -238,9 +241,9 @@ VALUE rb_monitor_physical_size(VALUE self)
238
241
  *
239
242
  * Wraps glfwGetMonitorName.
240
243
  */
241
- VALUE rb_monitor_name(VALUE self)
244
+ static VALUE rb_monitor_name(VALUE self)
242
245
  {
243
- GLFWmonitor *monitor;
246
+ GLFWmonitor *monitor = NULL;
244
247
  const char *monitor_name = NULL;
245
248
  Data_Get_Struct(self, GLFWmonitor, monitor);
246
249
  monitor_name = glfwGetMonitorName(monitor);
@@ -252,7 +255,7 @@ VALUE rb_monitor_name(VALUE self)
252
255
  static void rb_glfw_monitor_callback(GLFWmonitor *monitor, int message)
253
256
  {
254
257
  VALUE lambda = rb_cvar_get(s_glfw_module, kRB_CVAR_GLFW_MONITOR_CALLBACK);
255
- if (RTEST(lambda)) {
258
+ if (rb_obj_respond_to(lambda, kRB_CALL, 0)) {
256
259
  VALUE rb_monitor = Data_Wrap_Struct(s_glfw_monitor_klass, 0, 0, monitor);
257
260
  rb_obj_call_init(rb_monitor, 0, 0);
258
261
  rb_funcall(lambda, kRB_CALL, 2, rb_monitor, INT2FIX(message));
@@ -271,7 +274,7 @@ static void rb_glfw_monitor_callback(GLFWmonitor *monitor, int message)
271
274
  */
272
275
  static VALUE rb_videomode_width(VALUE self)
273
276
  {
274
- GLFWvidmode *mode;
277
+ GLFWvidmode *mode = NULL;
275
278
  Data_Get_Struct(self, GLFWvidmode, mode);
276
279
  return INT2FIX(mode->width);
277
280
  }
@@ -286,7 +289,7 @@ static VALUE rb_videomode_width(VALUE self)
286
289
  */
287
290
  static VALUE rb_videomode_height(VALUE self)
288
291
  {
289
- GLFWvidmode *mode;
292
+ GLFWvidmode *mode = NULL;
290
293
  Data_Get_Struct(self, GLFWvidmode, mode);
291
294
  return INT2FIX(mode->height);
292
295
  }
@@ -301,7 +304,7 @@ static VALUE rb_videomode_height(VALUE self)
301
304
  */
302
305
  static VALUE rb_videomode_red_bits(VALUE self)
303
306
  {
304
- GLFWvidmode *mode;
307
+ GLFWvidmode *mode = NULL;
305
308
  Data_Get_Struct(self, GLFWvidmode, mode);
306
309
  return INT2FIX(mode->redBits);
307
310
  }
@@ -316,7 +319,7 @@ static VALUE rb_videomode_red_bits(VALUE self)
316
319
  */
317
320
  static VALUE rb_videomode_green_bits(VALUE self)
318
321
  {
319
- GLFWvidmode *mode;
322
+ GLFWvidmode *mode = NULL;
320
323
  Data_Get_Struct(self, GLFWvidmode, mode);
321
324
  return INT2FIX(mode->greenBits);
322
325
  }
@@ -331,7 +334,7 @@ static VALUE rb_videomode_green_bits(VALUE self)
331
334
  */
332
335
  static VALUE rb_videomode_blue_bits(VALUE self)
333
336
  {
334
- GLFWvidmode *mode;
337
+ GLFWvidmode *mode = NULL;
335
338
  Data_Get_Struct(self, GLFWvidmode, mode);
336
339
  return INT2FIX(mode->blueBits);
337
340
  }
@@ -346,7 +349,7 @@ static VALUE rb_videomode_blue_bits(VALUE self)
346
349
  */
347
350
  static VALUE rb_videomode_refresh_rate(VALUE self)
348
351
  {
349
- GLFWvidmode *mode;
352
+ GLFWvidmode *mode = NULL;
350
353
  Data_Get_Struct(self, GLFWvidmode, mode);
351
354
  return INT2FIX(mode->refreshRate);
352
355
  }
@@ -363,16 +366,22 @@ static VALUE rb_videomode_refresh_rate(VALUE self)
363
366
  */
364
367
  static VALUE rb_monitor_video_modes(VALUE self)
365
368
  {
366
- GLFWmonitor *monitor;
367
- Data_Get_Struct(self, GLFWmonitor, monitor);
368
- VALUE rb_modes = rb_ary_new();
369
+ GLFWmonitor *monitor = NULL;
370
+ const GLFWvidmode *modes = NULL;
371
+ VALUE rb_modes = Qnil;
369
372
  int num_modes = 0;
370
373
  int mode_index = 0;
371
- const GLFWvidmode *modes = glfwGetVideoModes(monitor, &num_modes);
372
- for (; num_modes; --num_modes, ++mode_index) {
373
- GLFWvidmode *mode = ALLOC(GLFWvidmode);
374
+ VALUE rb_mode = Qnil;
375
+ GLFWvidmode *mode = NULL;
376
+
377
+ Data_Get_Struct(self, GLFWmonitor, monitor);
378
+ rb_modes = rb_ary_new();
379
+ modes = glfwGetVideoModes(monitor, &num_modes);
380
+
381
+ for (; mode_index < num_modes; ++mode_index) {
382
+ ALLOC(GLFWvidmode);
374
383
  *mode = modes[mode_index];
375
- VALUE rb_mode = Data_Wrap_Struct(s_glfw_videomode_klass, 0, free, mode);
384
+ rb_mode = Data_Wrap_Struct(s_glfw_videomode_klass, 0, free, mode);
376
385
  rb_obj_call_init(rb_mode, 0, 0);
377
386
  rb_ary_push(rb_modes, rb_mode);
378
387
  }
@@ -391,11 +400,13 @@ static VALUE rb_monitor_video_modes(VALUE self)
391
400
  */
392
401
  static VALUE rb_monitor_video_mode(VALUE self)
393
402
  {
394
- GLFWmonitor *monitor;
403
+ GLFWmonitor *monitor = NULL;
404
+ GLFWvidmode *mode = NULL;
405
+ VALUE rb_mode = Qnil;
395
406
  Data_Get_Struct(self, GLFWmonitor, monitor);
396
- GLFWvidmode *mode = ALLOC(GLFWvidmode);
407
+ mode = ALLOC(GLFWvidmode);
397
408
  *mode = *glfwGetVideoMode(monitor);
398
- VALUE rb_mode = Data_Wrap_Struct(s_glfw_videomode_klass, 0, free, mode);
409
+ rb_mode = Data_Wrap_Struct(s_glfw_videomode_klass, 0, free, mode);
399
410
  rb_obj_call_init(rb_mode, 0, 0);
400
411
  return rb_mode;
401
412
  }
@@ -413,7 +424,7 @@ static VALUE rb_monitor_video_mode(VALUE self)
413
424
  */
414
425
  static VALUE rb_monitor_set_gamma(VALUE self, VALUE gamma)
415
426
  {
416
- GLFWmonitor *monitor;
427
+ GLFWmonitor *monitor = NULL;
417
428
  Data_Get_Struct(self, GLFWmonitor, monitor);
418
429
  glfwSetGamma(monitor, (float)NUM2DBL(gamma));
419
430
  return self;
@@ -435,8 +446,8 @@ static VALUE rb_monitor_set_gamma(VALUE self, VALUE gamma)
435
446
  */
436
447
  static VALUE rb_monitor_get_gamma_ramp(VALUE self)
437
448
  {
438
- GLFWmonitor *monitor;
439
- const GLFWgammaramp *ramp;
449
+ GLFWmonitor *monitor = NULL;
450
+ const GLFWgammaramp *ramp = NULL;
440
451
  VALUE rb_gamma_hash, rb_red, rb_green, rb_blue;
441
452
  unsigned int ramp_index;
442
453
  unsigned int ramp_len = ramp->size;
@@ -487,11 +498,16 @@ static VALUE rb_monitor_get_gamma_ramp(VALUE self)
487
498
  */
488
499
  static VALUE rb_monitor_set_gamma_ramp(VALUE self, VALUE ramp_hash)
489
500
  {
490
- GLFWmonitor *monitor;
501
+ GLFWmonitor *monitor = NULL;
491
502
  GLFWgammaramp ramp;
492
503
  VALUE rb_red, rb_green, rb_blue;
493
- unsigned int ramp_index;
504
+ unsigned int ramp_index = 0;
494
505
  unsigned int ramp_len = 0;
506
+ unsigned short *ramp_buffer = NULL;
507
+
508
+ if (!Q_IS_A(ramp_hash, rb_cHash)) {
509
+ rb_raise(rb_eArgError, "ramp_hash must be a Hash");
510
+ }
495
511
 
496
512
  Data_Get_Struct(self, GLFWmonitor, monitor);
497
513
 
@@ -499,26 +515,25 @@ static VALUE rb_monitor_set_gamma_ramp(VALUE self, VALUE ramp_hash)
499
515
  rb_green = rb_hash_aref(ramp_hash, ID2SYM(kRB_GREEN));
500
516
  rb_blue = rb_hash_aref(ramp_hash, ID2SYM(kRB_BLUE));
501
517
 
502
- if (!(RTEST(rb_red) && RTEST(rb_green) && RTEST(rb_blue))) {
518
+ if (!(Q_IS_A(rb_red, rb_cArray) && Q_IS_A(rb_green, rb_cArray) && Q_IS_A(rb_blue, rb_cArray))) {
503
519
  rb_raise(rb_eArgError, "Ramp Hash must contain :red, :green, and :blue arrays");
504
520
  }
505
521
 
506
- ramp_len = RARRAY_LEN(rb_red);
507
- if ((ramp_index = (unsigned int)RARRAY_LEN(rb_green)) < ramp_len) {
508
- ramp_len = ramp_index;
509
- } else if ((ramp_index = (unsigned int)RARRAY_LEN(rb_blue)) < ramp_len) {
510
- ramp_len = ramp_index;
522
+ /* NOTE: loses precision */
523
+ ramp_len = (unsigned int)RARRAY_LEN(rb_red);
524
+ if (RARRAY_LEN(rb_green) != ramp_len || RARRAY_LEN(rb_blue) != ramp_len) {
525
+ rb_raise(rb_eArgError, "All ramps must have the same length");
511
526
  }
512
527
 
513
- unsigned short *ramp_buffer = ALLOC_N(unsigned short, 3 * ramp_len);
528
+ ramp_buffer = ALLOC_N(unsigned short, 3 * ramp_len);
514
529
  ramp.red = ramp_buffer;
515
530
  ramp.green = &ramp_buffer[ramp_len];
516
531
  ramp.blue = &ramp_buffer[ramp_len * 2];
517
532
 
518
- for (ramp_index = 0; ramp_index < ramp_len; ++ramp_index) {
519
- ramp.red[ramp_index] = rb_num2uint(rb_ary_entry(rb_red, ramp_index));
520
- ramp.green[ramp_index] = rb_num2uint(rb_ary_entry(rb_green, ramp_index));
521
- ramp.blue[ramp_index] = rb_num2uint(rb_ary_entry(rb_blue, ramp_index));
533
+ for (; ramp_index < ramp_len; ++ramp_index) {
534
+ ramp.red[ramp_index] = (unsigned short)rb_num2uint(rb_ary_entry(rb_red, ramp_index));
535
+ ramp.green[ramp_index] = (unsigned short)rb_num2uint(rb_ary_entry(rb_green, ramp_index));
536
+ ramp.blue[ramp_index] = (unsigned short)rb_num2uint(rb_ary_entry(rb_blue, ramp_index));
522
537
  }
523
538
 
524
539
  ramp.size = ramp_len;
@@ -567,7 +582,13 @@ static VALUE rb_window_window_hint(VALUE self, VALUE target, VALUE hint)
567
582
  /* Auxiliary function for extracting a Glfw::Window object from a GLFWwindow. */
568
583
  static VALUE rb_lookup_window(GLFWwindow *window)
569
584
  {
570
- return (VALUE)glfwGetWindowUserPointer(window);
585
+ if (window) {
586
+ void *uptr = glfwGetWindowUserPointer(window);
587
+ if (uptr) {
588
+ return (VALUE)uptr;
589
+ }
590
+ }
591
+ return Qnil;
571
592
  }
572
593
 
573
594
  /* And the opposite of rb_lookup_window */
@@ -622,11 +643,11 @@ static VALUE rb_window_new(int argc, VALUE *argv, VALUE self)
622
643
  title = StringValueCStr(rb_title);
623
644
  }
624
645
 
625
- if (RTEST(rb_monitor) && RTEST(rb_obj_is_kind_of(rb_monitor, s_glfw_monitor_klass))) {
646
+ if (Q_IS_A(rb_monitor, s_glfw_monitor_klass)) {
626
647
  Data_Get_Struct(rb_monitor, GLFWmonitor, monitor);
627
648
  }
628
649
 
629
- if (RTEST(rb_share) && RTEST(rb_obj_is_kind_of(rb_share, s_glfw_window_klass))) {
650
+ if (Q_IS_A(rb_share, s_glfw_window_klass)) {
630
651
  VALUE rb_shared_window = rb_ivar_get(rb_share, ivar_window);
631
652
  Data_Get_Struct(rb_shared_window, GLFWwindow, share);
632
653
  }
@@ -678,11 +699,12 @@ static VALUE rb_window_new(int argc, VALUE *argv, VALUE self)
678
699
  */
679
700
  static VALUE rb_window_destroy(VALUE self)
680
701
  {
702
+ VALUE rb_windows = Qnil;
681
703
  GLFWwindow *window = rb_get_window(self);
682
704
  if (window) {
683
705
  glfwDestroyWindow(window);
684
706
  rb_ivar_set(self, kRB_IVAR_WINDOW_INTERNAL, Qnil);
685
- VALUE rb_windows = rb_cvar_get(s_glfw_window_klass, kRB_CVAR_WINDOW_WINDOWS);
707
+ rb_windows = rb_cvar_get(s_glfw_window_klass, kRB_CVAR_WINDOW_WINDOWS);
686
708
  rb_hash_delete(rb_windows, INT2FIX((int)window));
687
709
  }
688
710
  return self;
@@ -908,8 +930,12 @@ static VALUE rb_window_get_monitor(VALUE self)
908
930
  static void rb_window_window_position_callback(GLFWwindow *window, int x, int y)
909
931
  {
910
932
  VALUE rb_window = rb_lookup_window(window);
911
- VALUE rb_func = rb_ivar_get(rb_window, kRB_IVAR_WINDOW_POSITION_CALLBACK);
912
- rb_funcall(rb_func, kRB_CALL, 3, rb_window, INT2FIX(x), INT2FIX(y));
933
+ if (RTEST(rb_window)) {
934
+ VALUE rb_func = rb_ivar_get(rb_window, kRB_IVAR_WINDOW_POSITION_CALLBACK);
935
+ if (rb_obj_respond_to(rb_func, kRB_CALL, 0)) {
936
+ rb_funcall(rb_func, kRB_CALL, 3, rb_window, INT2FIX(x), INT2FIX(y));
937
+ }
938
+ }
913
939
  }
914
940
 
915
941
  RB_ENABLE_CALLBACK_DEF(rb_window_set_window_position_callback, rb_window_window_position_callback, glfwSetWindowPosCallback);
@@ -919,8 +945,12 @@ RB_ENABLE_CALLBACK_DEF(rb_window_set_window_position_callback, rb_window_window_
919
945
  static void rb_window_window_size_callback(GLFWwindow *window, int width, int height)
920
946
  {
921
947
  VALUE rb_window = rb_lookup_window(window);
922
- VALUE rb_func = rb_ivar_get(rb_window, kRB_IVAR_WINDOW_SIZE_CALLBACK);
923
- rb_funcall(rb_func, kRB_CALL, 3, rb_window, INT2FIX(width), INT2FIX(height));
948
+ if (RTEST(rb_window)) {
949
+ VALUE rb_func = rb_ivar_get(rb_window, kRB_IVAR_WINDOW_SIZE_CALLBACK);
950
+ if (rb_obj_respond_to(rb_func, kRB_CALL, 0)) {
951
+ rb_funcall(rb_func, kRB_CALL, 3, rb_window, INT2FIX(width), INT2FIX(height));
952
+ }
953
+ }
924
954
  }
925
955
 
926
956
  RB_ENABLE_CALLBACK_DEF(rb_window_set_window_size_callback, rb_window_window_size_callback, glfwSetWindowSizeCallback);
@@ -930,8 +960,12 @@ RB_ENABLE_CALLBACK_DEF(rb_window_set_window_size_callback, rb_window_window_size
930
960
  static void rb_window_close_callback(GLFWwindow *window)
931
961
  {
932
962
  VALUE rb_window = rb_lookup_window(window);
933
- VALUE rb_func = rb_ivar_get(rb_window, kRB_IVAR_WINDOW_CLOSE_CALLBACK);
934
- rb_funcall(rb_func, kRB_CALL, 1, rb_window);
963
+ if (RTEST(rb_window)) {
964
+ VALUE rb_func = rb_ivar_get(rb_window, kRB_IVAR_WINDOW_CLOSE_CALLBACK);
965
+ if (rb_obj_respond_to(rb_func, kRB_CALL, 0)) {
966
+ rb_funcall(rb_func, kRB_CALL, 1, rb_window);
967
+ }
968
+ }
935
969
  }
936
970
 
937
971
  RB_ENABLE_CALLBACK_DEF(rb_window_set_close_callback, rb_window_close_callback, glfwSetWindowCloseCallback);
@@ -941,8 +975,12 @@ RB_ENABLE_CALLBACK_DEF(rb_window_set_close_callback, rb_window_close_callback, g
941
975
  static void rb_window_refresh_callback(GLFWwindow *window)
942
976
  {
943
977
  VALUE rb_window = rb_lookup_window(window);
944
- VALUE rb_func = rb_ivar_get(rb_window, kRB_IVAR_WINDOW_REFRESH_CALLBACK);
945
- rb_funcall(rb_func, kRB_CALL, 1, rb_window);
978
+ if (RTEST(rb_window)) {
979
+ VALUE rb_func = rb_ivar_get(rb_window, kRB_IVAR_WINDOW_REFRESH_CALLBACK);
980
+ if (rb_obj_respond_to(rb_func, kRB_CALL, 0)) {
981
+ rb_funcall(rb_func, kRB_CALL, 1, rb_window);
982
+ }
983
+ }
946
984
  }
947
985
 
948
986
  RB_ENABLE_CALLBACK_DEF(rb_window_set_refresh_callback, rb_window_refresh_callback, glfwSetWindowRefreshCallback);
@@ -952,8 +990,12 @@ RB_ENABLE_CALLBACK_DEF(rb_window_set_refresh_callback, rb_window_refresh_callbac
952
990
  static void rb_window_focus_callback(GLFWwindow *window, int focused)
953
991
  {
954
992
  VALUE rb_window = rb_lookup_window(window);
955
- VALUE rb_func = rb_ivar_get(rb_window, kRB_IVAR_WINDOW_FOCUS_CALLBACK);
956
- rb_funcall(rb_func, kRB_CALL, 2, rb_window, focused ? Qtrue : Qfalse);
993
+ if (RTEST(rb_window)) {
994
+ VALUE rb_func = rb_ivar_get(rb_window, kRB_IVAR_WINDOW_FOCUS_CALLBACK);
995
+ if (rb_obj_respond_to(rb_func, kRB_CALL, 0)) {
996
+ rb_funcall(rb_func, kRB_CALL, 2, rb_window, focused ? Qtrue : Qfalse);
997
+ }
998
+ }
957
999
  }
958
1000
 
959
1001
  RB_ENABLE_CALLBACK_DEF(rb_window_set_focus_callback, rb_window_focus_callback, glfwSetWindowFocusCallback);
@@ -963,8 +1005,12 @@ RB_ENABLE_CALLBACK_DEF(rb_window_set_focus_callback, rb_window_focus_callback, g
963
1005
  static void rb_window_iconify_callback(GLFWwindow *window, int iconified)
964
1006
  {
965
1007
  VALUE rb_window = rb_lookup_window(window);
966
- VALUE rb_func = rb_ivar_get(rb_window, kRB_IVAR_WINDOW_ICONIFY_CALLBACK);
967
- rb_funcall(rb_func, kRB_CALL, 2, rb_window, iconified ? Qtrue : Qfalse);
1008
+ if (RTEST(rb_window)) {
1009
+ VALUE rb_func = rb_ivar_get(rb_window, kRB_IVAR_WINDOW_ICONIFY_CALLBACK);
1010
+ if (rb_obj_respond_to(rb_func, kRB_CALL, 0)) {
1011
+ rb_funcall(rb_func, kRB_CALL, 2, rb_window, iconified ? Qtrue : Qfalse);
1012
+ }
1013
+ }
968
1014
  }
969
1015
 
970
1016
  RB_ENABLE_CALLBACK_DEF(rb_window_set_iconify_callback, rb_window_iconify_callback, glfwSetWindowIconifyCallback);
@@ -974,8 +1020,12 @@ RB_ENABLE_CALLBACK_DEF(rb_window_set_iconify_callback, rb_window_iconify_callbac
974
1020
  static void rb_window_fbsize_callback(GLFWwindow *window, int width, int height)
975
1021
  {
976
1022
  VALUE rb_window = rb_lookup_window(window);
977
- VALUE rb_func = rb_ivar_get(rb_window, kRB_IVAR_WINDOW_FRAMEBUFFER_SIZE_CALLBACK);
978
- rb_funcall(rb_func, kRB_CALL, 3, rb_window, INT2FIX(width), INT2FIX(height));
1023
+ if (RTEST(rb_window)) {
1024
+ VALUE rb_func = rb_ivar_get(rb_window, kRB_IVAR_WINDOW_FRAMEBUFFER_SIZE_CALLBACK);
1025
+ if (rb_obj_respond_to(rb_func, kRB_CALL, 0)) {
1026
+ rb_funcall(rb_func, kRB_CALL, 3, rb_window, INT2FIX(width), INT2FIX(height));
1027
+ }
1028
+ }
979
1029
  }
980
1030
 
981
1031
  RB_ENABLE_CALLBACK_DEF(rb_window_set_fbsize_callback, rb_window_fbsize_callback, glfwSetFramebufferSizeCallback);
@@ -1124,8 +1174,12 @@ static VALUE rb_window_set_cursor_pos(VALUE self, VALUE x, VALUE y)
1124
1174
  static void rb_window_key_callback(GLFWwindow *window, int key, int scancode, int action, int mods)
1125
1175
  {
1126
1176
  VALUE rb_window = rb_lookup_window(window);
1127
- VALUE rb_func = rb_ivar_get(rb_window, kRB_IVAR_WINDOW_KEY_CALLBACK);
1128
- rb_funcall(rb_func, kRB_CALL, 5, rb_window, INT2FIX(key), INT2FIX(scancode), INT2FIX(action), INT2FIX(mods));
1177
+ if (RTEST(rb_window)) {
1178
+ VALUE rb_func = rb_ivar_get(rb_window, kRB_IVAR_WINDOW_KEY_CALLBACK);
1179
+ if (rb_obj_respond_to(rb_func, kRB_CALL, 0)) {
1180
+ rb_funcall(rb_func, kRB_CALL, 5, rb_window, INT2FIX(key), INT2FIX(scancode), INT2FIX(action), INT2FIX(mods));
1181
+ }
1182
+ }
1129
1183
  }
1130
1184
 
1131
1185
  RB_ENABLE_CALLBACK_DEF(rb_window_set_key_callback, rb_window_key_callback, glfwSetKeyCallback);
@@ -1135,8 +1189,12 @@ RB_ENABLE_CALLBACK_DEF(rb_window_set_key_callback, rb_window_key_callback, glfwS
1135
1189
  static void rb_window_char_callback(GLFWwindow *window, unsigned int code)
1136
1190
  {
1137
1191
  VALUE rb_window = rb_lookup_window(window);
1138
- VALUE rb_func = rb_ivar_get(rb_window, kRB_IVAR_WINDOW_CHAR_CALLBACK);
1139
- rb_funcall(rb_func, kRB_CALL, 2, rb_window, UINT2NUM(code));
1192
+ if (RTEST(rb_window)) {
1193
+ VALUE rb_func = rb_ivar_get(rb_window, kRB_IVAR_WINDOW_CHAR_CALLBACK);
1194
+ if (rb_obj_respond_to(rb_func, kRB_CALL, 0)) {
1195
+ rb_funcall(rb_func, kRB_CALL, 2, rb_window, UINT2NUM(code));
1196
+ }
1197
+ }
1140
1198
  }
1141
1199
 
1142
1200
  RB_ENABLE_CALLBACK_DEF(rb_window_set_char_callback, rb_window_char_callback, glfwSetCharCallback);
@@ -1147,8 +1205,12 @@ RB_ENABLE_CALLBACK_DEF(rb_window_set_char_callback, rb_window_char_callback, glf
1147
1205
  static void rb_window_mouse_button_callback(GLFWwindow *window, int button, int action, int mods)
1148
1206
  {
1149
1207
  VALUE rb_window = rb_lookup_window(window);
1150
- VALUE rb_func = rb_ivar_get(rb_window, kRB_IVAR_WINDOW_MOUSE_BUTTON_CALLBACK);
1151
- rb_funcall(rb_func, kRB_CALL, 4, rb_window, INT2FIX(button), INT2FIX(action), INT2FIX(mods));
1208
+ if (RTEST(rb_window)) {
1209
+ VALUE rb_func = rb_ivar_get(rb_window, kRB_IVAR_WINDOW_MOUSE_BUTTON_CALLBACK);
1210
+ if (rb_obj_respond_to(rb_func, kRB_CALL, 0)) {
1211
+ rb_funcall(rb_func, kRB_CALL, 4, rb_window, INT2FIX(button), INT2FIX(action), INT2FIX(mods));
1212
+ }
1213
+ }
1152
1214
  }
1153
1215
 
1154
1216
  RB_ENABLE_CALLBACK_DEF(rb_window_set_mouse_button_callback, rb_window_mouse_button_callback, glfwSetMouseButtonCallback);
@@ -1158,8 +1220,12 @@ RB_ENABLE_CALLBACK_DEF(rb_window_set_mouse_button_callback, rb_window_mouse_butt
1158
1220
  static void rb_window_cursor_position_callback(GLFWwindow *window, double x, double y)
1159
1221
  {
1160
1222
  VALUE rb_window = rb_lookup_window(window);
1161
- VALUE rb_func = rb_ivar_get(rb_window, kRB_IVAR_WINDOW_CURSOR_POSITION_CALLBACK);
1162
- rb_funcall(rb_func, kRB_CALL, 3, rb_window, rb_float_new(x), rb_float_new(y));
1223
+ if (RTEST(rb_window)) {
1224
+ VALUE rb_func = rb_ivar_get(rb_window, kRB_IVAR_WINDOW_CURSOR_POSITION_CALLBACK);
1225
+ if (rb_obj_respond_to(rb_func, kRB_CALL, 0)) {
1226
+ rb_funcall(rb_func, kRB_CALL, 3, rb_window, rb_float_new(x), rb_float_new(y));
1227
+ }
1228
+ }
1163
1229
  }
1164
1230
 
1165
1231
  RB_ENABLE_CALLBACK_DEF(rb_window_set_cursor_position_callback, rb_window_cursor_position_callback, glfwSetCursorPosCallback);
@@ -1169,8 +1235,12 @@ RB_ENABLE_CALLBACK_DEF(rb_window_set_cursor_position_callback, rb_window_cursor_
1169
1235
  static void rb_window_cursor_enter_callback(GLFWwindow *window, int entered)
1170
1236
  {
1171
1237
  VALUE rb_window = rb_lookup_window(window);
1172
- VALUE rb_func = rb_ivar_get(rb_window, kRB_IVAR_WINDOW_CURSOR_ENTER_CALLBACK);
1173
- rb_funcall(rb_func, kRB_CALL, 2, rb_window, entered ? Qtrue : Qfalse);
1238
+ if (RTEST(rb_window)) {
1239
+ VALUE rb_func = rb_ivar_get(rb_window, kRB_IVAR_WINDOW_CURSOR_ENTER_CALLBACK);
1240
+ if (rb_obj_respond_to(rb_func, kRB_CALL, 0)) {
1241
+ rb_funcall(rb_func, kRB_CALL, 2, rb_window, entered ? Qtrue : Qfalse);
1242
+ }
1243
+ }
1174
1244
  }
1175
1245
 
1176
1246
  RB_ENABLE_CALLBACK_DEF(rb_window_set_cursor_enter_callback, rb_window_cursor_enter_callback, glfwSetCursorEnterCallback);
@@ -1180,8 +1250,12 @@ RB_ENABLE_CALLBACK_DEF(rb_window_set_cursor_enter_callback, rb_window_cursor_ent
1180
1250
  static void rb_window_scroll_callback(GLFWwindow *window, double x, double y)
1181
1251
  {
1182
1252
  VALUE rb_window = rb_lookup_window(window);
1183
- VALUE rb_func = rb_ivar_get(rb_window, kRB_IVAR_WINDOW_SCROLL_CALLBACK);
1184
- rb_funcall(rb_func, kRB_CALL, 3, rb_window, rb_float_new(x), rb_float_new(y));
1253
+ if (RTEST(rb_window)) {
1254
+ VALUE rb_func = rb_ivar_get(rb_window, kRB_IVAR_WINDOW_SCROLL_CALLBACK);
1255
+ if (rb_obj_respond_to(rb_func, kRB_CALL, 0)) {
1256
+ rb_funcall(rb_func, kRB_CALL, 3, rb_window, rb_float_new(x), rb_float_new(y));
1257
+ }
1258
+ }
1185
1259
  }
1186
1260
 
1187
1261
  RB_ENABLE_CALLBACK_DEF(rb_window_set_scroll_callback, rb_window_scroll_callback, glfwSetScrollCallback);
@@ -1217,11 +1291,12 @@ static VALUE rb_glfw_get_joystick_axes(VALUE self, VALUE joystick)
1217
1291
  {
1218
1292
  VALUE rb_axes = Qnil;
1219
1293
  int num_axes = 0;
1294
+ int axis_index = 0;
1220
1295
  const float *axes = glfwGetJoystickAxes(NUM2INT(joystick), &num_axes);
1221
1296
  if (num_axes > 0) {
1222
1297
  rb_axes = rb_ary_new();
1223
- for (; num_axes; --num_axes, ++axes) {
1224
- rb_ary_push(rb_axes, rb_float_new(*axes));
1298
+ for (; axis_index < num_axes; ++axis_index) {
1299
+ rb_ary_push(rb_axes, rb_float_new(axes[axis_index]));
1225
1300
  }
1226
1301
  }
1227
1302
  return rb_axes;
@@ -1242,11 +1317,12 @@ static VALUE rb_glfw_get_joystick_buttons(VALUE self, VALUE joystick)
1242
1317
  {
1243
1318
  VALUE rb_buttons = Qnil;
1244
1319
  int num_buttons = 0;
1320
+ int button_index = 0;
1245
1321
  const unsigned char *buttons = glfwGetJoystickButtons(NUM2INT(joystick), &num_buttons);
1246
1322
  if (num_buttons > 0) {
1247
1323
  rb_buttons = rb_ary_new();
1248
- for (; num_buttons; --num_buttons, ++buttons) {
1249
- rb_ary_push(rb_buttons, rb_float_new(INT2FIX((int)*buttons)));
1324
+ for (; button_index < num_buttons; ++button_index) {
1325
+ rb_ary_push(rb_buttons, rb_float_new(INT2FIX((int)buttons[button_index])));
1250
1326
  }
1251
1327
  }
1252
1328
  return rb_buttons;
@@ -1391,12 +1467,7 @@ static VALUE rb_window_unset_context(VALUE self)
1391
1467
  */
1392
1468
  static VALUE rb_window_get_current_context(VALUE self)
1393
1469
  {
1394
- GLFWwindow *window = glfwGetCurrentContext();
1395
- if (window) {
1396
- return (VALUE)glfwGetWindowUserPointer(window);
1397
- } else {
1398
- return Qnil;
1399
- }
1470
+ return rb_lookup_window(glfwGetCurrentContext());
1400
1471
  }
1401
1472
 
1402
1473
 
@@ -1487,10 +1558,10 @@ void Init_glfw3(void)
1487
1558
  kRB_BLUE = rb_intern(kRB_BLUE_NAME);
1488
1559
 
1489
1560
  s_glfw_module = rb_define_module("Glfw");
1490
- s_glfw_monitor_klass = rb_define_class_under(s_glfw_module, "Monitor", rb_cObject);
1561
+ s_glfw_monitor_klass = rb_define_class_under(s_glfw_module, "Monitor", rb_cData);
1491
1562
  s_glfw_window_klass = rb_define_class_under(s_glfw_module, "Window", rb_cObject);
1492
- s_glfw_window_internal_klass = rb_define_class_under(s_glfw_window_klass, "InternalWindow", rb_cObject);
1493
- s_glfw_videomode_klass = rb_define_class_under(s_glfw_module, "VideoMode", rb_cObject);
1563
+ s_glfw_window_internal_klass = rb_define_class_under(s_glfw_window_klass, "InternalWindow", rb_cData);
1564
+ s_glfw_videomode_klass = rb_define_class_under(s_glfw_module, "VideoMode", rb_cData);
1494
1565
 
1495
1566
  /* Glfw::Monitor */
1496
1567
  rb_define_singleton_method(s_glfw_monitor_klass, "monitors", rb_glfw_get_monitors, 0);
@@ -35,7 +35,7 @@ module Glfw
35
35
  # Sets the current error callback to a Proc generated from the provided block.
36
36
  #
37
37
  def self.set_error_callback(&block)
38
- self.error_callback = lambda(&block)
38
+ self.error_callback = block
39
39
  end
40
40
 
41
41
  #
@@ -70,6 +70,6 @@ module Glfw
70
70
  # block.
71
71
  #
72
72
  def self.set_monitor_callback(&block)
73
- self.monitor_callback = lambda(&block)
73
+ self.monitor_callback = block
74
74
  end
75
75
  end
@@ -98,119 +98,119 @@ class Glfw::Window
98
98
 
99
99
  def key_callback=(func)
100
100
  @__key_callback = func
101
- set_key_callback__(!func.nil?)
101
+ set_key_callback__(func.respond_to?(:call))
102
102
  end
103
103
 
104
104
  def set_key_callback(&block)
105
- self.key_callback = lambda(&block)
105
+ self.key_callback = block
106
106
  end
107
107
 
108
108
  def char_callback=(func)
109
109
  @__char_callback = func
110
- set_char_callback__(!func.nil?)
110
+ set_char_callback__(func.respond_to?(:call))
111
111
  end
112
112
 
113
113
  def set_char_callback(&block)
114
- self.char_callback = lambda(&block)
114
+ self.char_callback = block
115
115
  end
116
116
 
117
117
  def mouse_button_callback=(func)
118
118
  @__mouse_button_callback = func
119
- set_mouse_button_callback__(!func.nil?)
119
+ set_mouse_button_callback__(func.respond_to?(:call))
120
120
  end
121
121
 
122
122
  def set_mouse_button_callback(&block)
123
- self.mouse_button_callback = lambda(&block)
123
+ self.mouse_button_callback = block
124
124
  end
125
125
 
126
126
  def cursor_position_callback=(func)
127
127
  @__cursor_position_callback = func
128
- set_cursor_position_callback__(!func.nil?)
128
+ set_cursor_position_callback__(func.respond_to?(:call))
129
129
  end
130
130
 
131
131
  def set_cursor_position_callback(&block)
132
- self.cursor_position_callback = lambda(&block)
132
+ self.cursor_position_callback = block
133
133
  end
134
134
 
135
135
  def cursor_enter_callback=(func)
136
136
  @__cursor_enter_callback = func
137
- set_cursor_enter_callback__(!func.nil?)
137
+ set_cursor_enter_callback__(func.respond_to?(:call))
138
138
  end
139
139
 
140
140
  def set_cursor_enter_callback(&block)
141
- self.cursor_enter_callback = lambda(&block)
141
+ self.cursor_enter_callback = block
142
142
  end
143
143
 
144
144
  def scroll_callback=(func)
145
145
  @__scroll_callback = func
146
- set_scroll_callback__(!func.nil?)
146
+ set_scroll_callback__(func.respond_to?(:call))
147
147
  end
148
148
 
149
149
  def set_scroll_callback(&block)
150
- self.scroll_callback = lambda(&block)
150
+ self.scroll_callback = block
151
151
  end
152
152
 
153
153
  def position_callback=(func)
154
154
  @__position_callback = func
155
- set_window_position_callback__(!func.nil?)
155
+ set_window_position_callback__(func.respond_to?(:call))
156
156
  end
157
157
 
158
158
  def set_position_callback(&block)
159
- self.position_callback = lambda(&block)
159
+ self.position_callback = block
160
160
  end
161
161
 
162
162
  def size_callback=(func)
163
163
  @__size_callback = func
164
- set_window_size_callback__(!func.nil?)
164
+ set_window_size_callback__(func.respond_to?(:call))
165
165
  end
166
166
 
167
167
  def set_size_callback(&block)
168
- self.size_callback = lambda(&block)
168
+ self.size_callback = block
169
169
  end
170
170
 
171
171
  def close_callback=(func)
172
172
  @__close_callback = func
173
- set_close_callback__(!func.nil?)
173
+ set_close_callback__(func.respond_to?(:call))
174
174
  end
175
175
 
176
176
  def set_close_callback(&block)
177
- self.close_callback = lambda(&block)
177
+ self.close_callback = block
178
178
  end
179
179
 
180
180
  def refresh_callback=(func)
181
181
  @__refresh_callback = func
182
- set_refresh_callback__(!func.nil?)
182
+ set_refresh_callback__(func.respond_to?(:call))
183
183
  end
184
184
 
185
185
  def set_refresh_callback(&block)
186
- self.refresh_callback = lambda(&block)
186
+ self.refresh_callback = block
187
187
  end
188
188
 
189
189
  def focus_callback=(func)
190
190
  @__focus_callback = func
191
- set_focus_callback__(!func.nil?)
191
+ set_focus_callback__(func.respond_to?(:call))
192
192
  end
193
193
 
194
194
  def set_focus_callback(&block)
195
- self.focus_callback = lambda(&block)
195
+ self.focus_callback = block
196
196
  end
197
197
 
198
198
  def iconify_callback=(func)
199
199
  @__iconify_callback = func
200
- set_iconify_callback__(!func.nil?)
200
+ set_iconify_callback__(func.respond_to?(:call))
201
201
  end
202
202
 
203
203
  def set_iconify_callback(&block)
204
- self.iconify_callback = lambda(&block)
204
+ self.iconify_callback = block
205
205
  end
206
206
 
207
207
  def framebuffer_size_callback=(func)
208
208
  @__framebuffer_size_callback = func
209
- set_fbsize_callback__(!func.nil?)
209
+ set_fbsize_callback__(func.respond_to?(:call))
210
210
  end
211
211
 
212
212
  def set_framebuffer_size_callback(&block)
213
- self.framebuffer_size_callback = lambda(&block)
213
+ self.framebuffer_size_callback = block
214
214
  end
215
215
 
216
216
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: glfw3
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.5
4
+ version: 0.4.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Noel Raymond Cower
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-02-14 00:00:00.000000000 Z
11
+ date: 2014-04-04 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: GLFW 3 bindings for Ruby 2.x
14
14
  email: ncower@gmail.com
@@ -53,9 +53,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
53
53
  version: '0'
54
54
  requirements: []
55
55
  rubyforge_project:
56
- rubygems_version: 2.2.1
56
+ rubygems_version: 2.2.0
57
57
  signing_key:
58
58
  specification_version: 4
59
59
  summary: GLFW3
60
60
  test_files: []
61
- has_rdoc: true