Class: Glfw::Monitor

Inherits:
Data
  • Object
show all
Defined in:
lib/glfw3/monitor.rb,
ext/glfw3/glfw3.c

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.monitorsObject

Gets an array of all currently connected monitors.

call-seq:

monitors -> [Glfw::Monitor, ...]

Wraps glfwGetMonitors.



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'ext/glfw3/glfw3.c', line 155

static VALUE rb_glfw_get_monitors(VALUE self)
{
  long monitor_index = 0;
  int num_monitors = 0;
  GLFWmonitor **monitors = glfwGetMonitors(&num_monitors);
  VALUE monitors_out = Qnil;

  if (monitors) {
    monitors_out = rb_ary_new();
    for (; monitor_index < num_monitors; ++monitor_index) {
      VALUE monitor = Data_Wrap_Struct(s_glfw_monitor_klass, 0, 0, monitors[monitor_index]);
      rb_obj_call_init(monitor, 0, 0);
      rb_ary_push(monitors_out, monitor);
    }
  }
  return monitors_out;
}

.primary_monitorObject

Gets the primary monitor.

call-seq:

primary_monitor -> Glfw::Monitor

Wraps glfwGetPrimaryMonitor.



183
184
185
186
187
188
189
190
191
192
# File 'ext/glfw3/glfw3.c', line 183

static VALUE rb_glfw_get_primary_monitor(VALUE self)
{
  GLFWmonitor *monitor = glfwGetPrimaryMonitor();
  VALUE rb_monitor = Qnil;
  if (monitor != NULL) {
    rb_monitor = Data_Wrap_Struct(s_glfw_monitor_klass, 0, 0, monitor);
    rb_obj_call_init(rb_monitor, 0, 0);
  }
  return rb_monitor;
}

Instance Method Details

#get_gamma_rampObject Also known as: gamma_ramp

Gets the monitor’s gamma ramp and returns it as a hash of its red, green, and blue gamma ramps. By default, this might be equal to ‘{ :red => [], :green => [], :blue => []}`. Ramp values are in the range of 0 through 65535, or the 0 through USHRT_MAX (in C).

call-seq:

  get_gamma_ramp -> { :red => [...], :green => [...], :blue => [...] } or nil
  gamma_ramp = -> hash or nil

Wraps glfwGetGammaRamp.


447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
# File 'ext/glfw3/glfw3.c', line 447

static VALUE rb_monitor_get_gamma_ramp(VALUE self)
{
  GLFWmonitor *monitor = NULL;
  const GLFWgammaramp *ramp = NULL;
  VALUE rb_gamma_hash, rb_red, rb_green, rb_blue;
  unsigned int ramp_index;
  unsigned int ramp_len;

  Data_Get_Struct(self, GLFWmonitor, monitor);
  ramp = glfwGetGammaRamp(monitor);

  if (ramp == NULL) {
    return Qnil;
  }
  ramp_len = ramp->size;

  rb_gamma_hash  = rb_hash_new();
  rb_red   = rb_ary_new();
  rb_green = rb_ary_new();
  rb_blue  = rb_ary_new();

  for (ramp_index = 0; ramp_index < ramp_len; ++ramp_index) {
    rb_ary_push(rb_red, INT2NUM(ramp->red[ramp_index]));
    rb_ary_push(rb_green, INT2NUM(ramp->green[ramp_index]));
    rb_ary_push(rb_blue, INT2NUM(ramp->blue[ramp_index]));
  }

  rb_hash_aset(rb_gamma_hash, ID2SYM(kRB_RED), rb_red);
  rb_hash_aset(rb_gamma_hash, ID2SYM(kRB_GREEN), rb_green);
  rb_hash_aset(rb_gamma_hash, ID2SYM(kRB_BLUE), rb_blue);

  return rb_gamma_hash;
}

#nameObject

Gets the name of the monitor.

call-seq:

name -> String

Wraps glfwGetMonitorName.



244
245
246
247
248
249
250
251
# File 'ext/glfw3/glfw3.c', line 244

static VALUE rb_monitor_name(VALUE self)
{
  GLFWmonitor *monitor = NULL;
  const char *monitor_name = NULL;
  Data_Get_Struct(self, GLFWmonitor, monitor);
  monitor_name = glfwGetMonitorName(monitor);
  return monitor_name ? rb_str_new2(monitor_name) : Qnil;
}

#physical_sizeObject

Gets the physical size of the monitor.

call-seq:

physical_size -> [width, height]

Wraps glfwGetMonitorPhysicalSize.



224
225
226
227
228
229
230
231
232
# File 'ext/glfw3/glfw3.c', line 224

static VALUE rb_monitor_physical_size(VALUE self)
{
  GLFWmonitor *monitor = NULL;
  int width = 0;
  int height = 0;
  Data_Get_Struct(self, GLFWmonitor, monitor);
  glfwGetMonitorPhysicalSize(monitor, &width, &height);
  return rb_ary_new3(2, INT2FIX(width), INT2FIX(height));
}

#positionObject

Gets the monitor’s position in screen-space.

call-seq:

position -> [x, y]

Wraps glfwGetMonitorPos.



204
205
206
207
208
209
210
211
212
# File 'ext/glfw3/glfw3.c', line 204

static VALUE rb_monitor_position(VALUE self)
{
  GLFWmonitor *monitor = NULL;
  int xpos = 0;
  int ypos = 0;
  Data_Get_Struct(self, GLFWmonitor, monitor);
  glfwGetMonitorPos(monitor, &xpos, &ypos);
  return rb_ary_new3(2, INT2FIX(xpos), INT2FIX(ypos));
}

#set_gamma(gamma) ⇒ Object

Sets the monitor’s gamma ramp to a 256-element ramp generated by the given exponent.

call-seq:

set_gamma(gamma) -> self

Wraps glfwSetGamma.



425
426
427
428
429
430
431
# File 'ext/glfw3/glfw3.c', line 425

static VALUE rb_monitor_set_gamma(VALUE self, VALUE gamma)
{
  GLFWmonitor *monitor = NULL;
  Data_Get_Struct(self, GLFWmonitor, monitor);
  glfwSetGamma(monitor, (float)NUM2DBL(gamma));
  return self;
}

#set_gamma_ramp(ramp_hash) ⇒ Object Also known as: gamma_ramp=

Sets the monitor’s gamma ramp with the ramps provided in the ramp hash. The ramp hash must have arrays for :red, :green, and :blue keys. The arrays should be the same length, otherwise the shortest array length will be used.

Gamma ramps should be in the range of an usngiend short, so 0 through 65535 or USHRT_MAX.

For example, to set a linear gamma ramp that is half as intense as what is likely your default:

ramp = (0 ... 32768).step(128).to_a
monitor.set_gamma_ramp({ :red => ramp, :green => ramp, :blue => ramp })

call-seq:

   set_gamma_ramp(ramp) -> self
   gamma_ramp = ramp -> self

Wraps glfwSetGammaRamp.


504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
# File 'ext/glfw3/glfw3.c', line 504

static VALUE rb_monitor_set_gamma_ramp(VALUE self, VALUE ramp_hash)
{
  GLFWmonitor *monitor = NULL;
  GLFWgammaramp ramp;
  VALUE rb_red, rb_green, rb_blue;
  unsigned int ramp_index = 0;
  unsigned int ramp_len = 0;
  unsigned short *ramp_buffer = NULL;

  if (!Q_IS_A(ramp_hash, rb_cHash)) {
    rb_raise(rb_eArgError, "ramp_hash must be a Hash");
  }

  Data_Get_Struct(self, GLFWmonitor, monitor);

  rb_red = rb_hash_aref(ramp_hash, ID2SYM(kRB_RED));
  rb_green = rb_hash_aref(ramp_hash, ID2SYM(kRB_GREEN));
  rb_blue = rb_hash_aref(ramp_hash, ID2SYM(kRB_BLUE));

  if (!(Q_IS_A(rb_red, rb_cArray) && Q_IS_A(rb_green, rb_cArray) && Q_IS_A(rb_blue, rb_cArray))) {
    rb_raise(rb_eArgError, "Ramp Hash must contain :red, :green, and :blue arrays");
  }

  /* NOTE: loses precision */
  ramp_len = (unsigned int)RARRAY_LEN(rb_red);
  if (RARRAY_LEN(rb_green) != ramp_len || RARRAY_LEN(rb_blue) != ramp_len) {
    rb_raise(rb_eArgError, "All ramps must have the same length");
  }

  ramp_buffer = ALLOC_N(unsigned short, 3 * ramp_len);
  ramp.red = ramp_buffer;
  ramp.green = &ramp_buffer[ramp_len];
  ramp.blue = &ramp_buffer[ramp_len * 2];

  for (; ramp_index < ramp_len; ++ramp_index) {
    ramp.red[ramp_index] = (unsigned short)NUM2UINT(rb_ary_entry(rb_red, ramp_index));
    ramp.green[ramp_index] = (unsigned short)NUM2UINT(rb_ary_entry(rb_green, ramp_index));
    ramp.blue[ramp_index] = (unsigned short)NUM2UINT(rb_ary_entry(rb_blue, ramp_index));
  }

  ramp.size = ramp_len;

  glfwSetGammaRamp(monitor, &ramp);

  free(ramp_buffer);

  return self;
}

#video_modeObject

Gets the monitor’s current video mode.

call-seq:

video_mode -> Glfw::VideoMode

Wraps glfwGetVideoMode.



401
402
403
404
405
406
407
408
409
410
411
412
# File 'ext/glfw3/glfw3.c', line 401

static VALUE rb_monitor_video_mode(VALUE self)
{
  GLFWmonitor *monitor = NULL;
  GLFWvidmode *mode = NULL;
  VALUE rb_mode = Qnil;
  Data_Get_Struct(self, GLFWmonitor, monitor);
  mode = ALLOC(GLFWvidmode);
  *mode = *glfwGetVideoMode(monitor);
  rb_mode = Data_Wrap_Struct(s_glfw_videomode_klass, 0, free, mode);
  rb_obj_call_init(rb_mode, 0, 0);
  return rb_mode;
}

#video_modesObject

Gets an array of all video modes associated with the monitor, sorted ascending first by color depth and then the video mode’s area (width x height).

call-seq:

video_mode -> [Glfw::VideoMode, ...]

Wraps glfwGetVideoModes.



367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
# File 'ext/glfw3/glfw3.c', line 367

static VALUE rb_monitor_video_modes(VALUE self)
{
  GLFWmonitor *monitor = NULL;
  const GLFWvidmode *modes = NULL;
  VALUE rb_modes = Qnil;
  int num_modes = 0;
  int mode_index = 0;
  VALUE rb_mode = Qnil;
  GLFWvidmode *mode = NULL;

  Data_Get_Struct(self, GLFWmonitor, monitor);
  rb_modes = rb_ary_new();
  modes = glfwGetVideoModes(monitor, &num_modes);

  for (; mode_index < num_modes; ++mode_index) {
    mode = ALLOC(GLFWvidmode);
    *mode = modes[mode_index];
    rb_mode = Data_Wrap_Struct(s_glfw_videomode_klass, 0, free, mode);
    rb_obj_call_init(rb_mode, 0, 0);
    rb_ary_push(rb_modes, rb_mode);
  }
  return rb_modes;
}