Module: Glfw

Defined in:
lib/glfw3.rb,
lib/glfw3/window.rb,
lib/glfw3/monitor.rb,
lib/glfw3/callbacks.rb,
ext/glfw3/glfw3.c

Overview

The core Glfw module, contains functions for setting error and monitor callbacks, joystick input, constants, timing, event handling, and so on.

See also Glfw::Window, Glfw::Monitor, and Glfw::VideoMode.

Defined Under Namespace

Classes: Monitor, VideoMode, Window

Constant Summary collapse

@@__error_callback =
nil
@@__monitor_callback =
nil

Class Method Summary collapse

Class Method Details

.error_callbackObject

Gets the current error callback object.

call-seq:

error_callback -> obj or nil

13
14
15
# File 'lib/glfw3/callbacks.rb', line 13

def self.error_callback
  @@__error_callback
end

.error_callback=(lambda) ⇒ Object

Sets the current error callback object to the given value. Presumably a Proc or some other object, but one that implements a call(…) function.

If set to nil, any GLFW error will raise a RuntimeError exception.

The error callback is expected to take two arguments: the error code and a description of the error. For example:

Glfw.error_callback = lambda { |error_code, description|
  raise RuntimeError, "GLFW Error #{error_code}: #{description}"
}

30
31
32
# File 'lib/glfw3/callbacks.rb', line 30

def self.error_callback=(lambda)
  @@__error_callback = lambda
end

.extension_supported?(extension) ⇒ Boolean

Retursn whether a given OpenGL or context creation API extension is supported by the current context. (See Glfw::Window#make_context_current)

Bear in mind that this function does not cache its results, so calls may be expensive. If you find yourself using it, consider caching the results yourself.

call-seq:

extension_supported?(extension) -> true or false

Wraps glfwExtensionSupported.

Returns:

  • (Boolean)

1534
1535
1536
1537
# File 'ext/glfw3/glfw3.c', line 1534

static VALUE rb_glfw_extension_supported(VALUE self, VALUE extension)
{
  return glfwExtensionSupported(StringValueCStr(extension)) ? Qtrue : Qfalse;
}

.initObject

Initializes GLFW. Returns true on success, false on failure.

call-seq:

init -> true or false

Wraps glfwInit.


86
87
88
89
90
91
92
93
# File 'ext/glfw3/glfw3.c', line 86

static VALUE rb_glfw_init(VALUE self)
{
  VALUE result = glfwInit() ? Qtrue : Qfalse;
  if (result == Qtrue) {
    glfwSetMonitorCallback(rb_glfw_monitor_callback);
  }
  return result;
}

.joystick_axes(joystick) ⇒ Object

Gets the values of all axes of the given joystick. Returns nil if the joystick isn't present. See #joystick_present?.

call-seq:

joystick_axes(joystick) -> [Float, ...] or nil

Wraps glfwGetJoystickAxes.


1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
# File 'ext/glfw3/glfw3.c', line 1295

static VALUE rb_glfw_get_joystick_axes(VALUE self, VALUE joystick)
{
  VALUE rb_axes = Qnil;
  int num_axes = 0;
  int axis_index = 0;
  const float *axes = glfwGetJoystickAxes(NUM2INT(joystick), &num_axes);
  if (num_axes > 0) {
    rb_axes = rb_ary_new();
    for (; axis_index < num_axes; ++axis_index) {
      rb_ary_push(rb_axes, rb_float_new(axes[axis_index]));
    }
  }
  return rb_axes;
}

.joystick_buttons(joystick) ⇒ Object

Gets the button values of the given joystick. Returns nil if the joystick isn't present. See #joystick_present?.

call-seq:

joystick_buttons(joystick) -> [Fixed, ...] or nil

Wraps glfwGetJoystickButtons.


1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
# File 'ext/glfw3/glfw3.c', line 1321

static VALUE rb_glfw_get_joystick_buttons(VALUE self, VALUE joystick)
{
  VALUE rb_buttons = Qnil;
  int num_buttons = 0;
  int button_index = 0;
  const unsigned char *buttons = glfwGetJoystickButtons(NUM2INT(joystick), &num_buttons);
  if (num_buttons > 0) {
    rb_buttons = rb_ary_new();
    for (; button_index < num_buttons; ++button_index) {
      rb_ary_push(rb_buttons, rb_float_new(INT2FIX((int)buttons[button_index])));
    }
  }
  return rb_buttons;
}

.joystick_name(joystick) ⇒ Object

Returns the name of the given joystick.

call-seq:

joystick_name(joystick) -> String

Wraps glfwGetJoystickName.


1346
1347
1348
1349
1350
1351
1352
1353
1354
# File 'ext/glfw3/glfw3.c', line 1346

static VALUE rb_glfw_get_joystick_name(VALUE self, VALUE joystick)
{
  const char *joy_name = glfwGetJoystickName(NUM2INT(joystick));
  if (joy_name) {
    return rb_str_new2(joy_name);
  } else {
    return Qnil;
  }
}

.joystick_present?(joystick) ⇒ Boolean

Returns whether the given joystick is present.

call-seq:

joystick_present?(joystick) -> true or false

Wraps glfwJoystickPresent.

Returns:

  • (Boolean)

1279
1280
1281
1282
# File 'ext/glfw3/glfw3.c', line 1279

static VALUE rb_glfw_joystick_present(VALUE self, VALUE joystick)
{
  return glfwJoystickPresent(NUM2INT(joystick)) ? Qtrue : Qfalse;
}

.monitor_callbackObject

Gets the current monitor callback object.

call-seq:

monitor_callback -> obj or nil

47
48
49
# File 'lib/glfw3/callbacks.rb', line 47

def self.monitor_callback
  @@__monitor_callback
end

.monitor_callback=(lambda) ⇒ Object

Sets the current monitor callback object to the given object. Presumably a Proc or some other object, but one that implements a call(…) function.

If set to nil, the callback is disabled.

The monitor callback is expected to take two arguments: the monitor and an event (one of either Glfw::CONNECTED or Glfw::DISCONNECTED). For example:

Glfw.monitor_callback = lambda { |monitor, event|
  # ...
}

64
65
66
# File 'lib/glfw3/callbacks.rb', line 64

def self.monitor_callback=(lambda)
  @@__monitor_callback = lambda
end

.poll_eventsObject

Polls for events without blocking until an event occurs.

Wraps glfwPollEvents.

This would likely be called at the beginning of your main loop, like so:

loop {
  Glfw.poll_events()

  # ...
}

1053
1054
1055
1056
1057
# File 'ext/glfw3/glfw3.c', line 1053

static VALUE rb_glfw_poll_events(VALUE self)
{
  glfwPollEvents();
  return self;
}

.set_error_callback(&block) ⇒ Object

Sets the current error callback to a Proc generated from the provided block.


37
38
39
# File 'lib/glfw3/callbacks.rb', line 37

def self.set_error_callback(&block)
  self.error_callback = block
end

.set_monitor_callback(&block) ⇒ Object

Sets the current monitor callback to a Proc generated from the provided block.


72
73
74
# File 'lib/glfw3/callbacks.rb', line 72

def self.set_monitor_callback(&block)
  self.monitor_callback = block
end

.swap_interval=(interval) ⇒ Object

Sets the swap interval for the current context. (See Glfw::Window#make_context_current)

call-seq:

swap_interval=(interval) -> self
swap_interval = interval -> self

Wraps glfwSwapInterval.


1512
1513
1514
1515
1516
# File 'ext/glfw3/glfw3.c', line 1512

static VALUE rb_glfw_swap_interval(VALUE self, VALUE interval)
{
  glfwSwapInterval(NUM2INT(interval));
  return self;
}

.terminateObject

Terminates GLFW.

Wraps glfwTerminate.


103
104
105
106
107
# File 'ext/glfw3/glfw3.c', line 103

static VALUE rb_glfw_terminate(VALUE self)
{
  glfwTerminate();
  return self;
}

.timeObject

Gets the current time in seconds. The returned time is relative to the time since GLFW was initialized unless the time has been set using #timer=.

call-seq:

time -> Float

Wraps glfwGetTime.


1401
1402
1403
1404
# File 'ext/glfw3/glfw3.c', line 1401

static VALUE rb_glfw_get_time(VALUE self)
{
  return rb_float_new(glfwGetTime());
}

.time=(time_) ⇒ Object

Sets the current time in seconds. If set, GLFW will continue measuring time elapsed from that time forward.

In most cases, you will not need to use this.

call-seq:

time = Float

Wraps glfwSetTime.


1419
1420
1421
1422
1423
# File 'ext/glfw3/glfw3.c', line 1419

static VALUE rb_glfw_set_time(VALUE self, VALUE time_)
{
  glfwSetTime(NUM2DBL(time_));
  return time_;
}

.versionObject

Returns GLFW's version in an array.

call-seq:

version -> [major, minor, revision]

Wraps glfwGetVersion.


119
120
121
122
123
124
125
126
# File 'ext/glfw3/glfw3.c', line 119

static VALUE rb_glfw_version(VALUE self)
{
  int major = 0;
  int minor = 0;
  int revision = 0;
  glfwGetVersion(&major, &minor, &revision);
  return rb_ary_new3(3, INT2NUM(major), INT2NUM(minor), INT2NUM(revision));
}

.wait_eventsObject

Polls for events. Blocks until an event occurs.

Wraps glfwWaitEvents.

This would likely be called at the beginning of your main loop, like so:

loop {
  Glfw.wait_events()

  # ...
}

1074
1075
1076
1077
1078
# File 'ext/glfw3/glfw3.c', line 1074

static VALUE rb_glfw_wait_events(VALUE self)
{
  glfwWaitEvents();
  return self;
}