Module: GLFWNative

Defined in:
ext/glfw_native/glfw_native.c

Class Method Summary collapse

Class Method Details

.create_window(width, height, title, monitor, share) ⇒ Object

CreateWindow(width, height, title, monitor, share)



170
171
172
173
174
175
176
177
178
# File 'ext/glfw_native/glfw_native.c', line 170

static VALUE rb_glfw_create_window(VALUE self, VALUE width, VALUE height, VALUE title, VALUE monitor, VALUE share) {
    if (!pfn_glfwCreateWindow) rb_raise(rb_eRuntimeError, "GLFW not loaded");
    const char* title_str = StringValueCStr(title);
    GLFWmonitor* mon = NIL_P(monitor) ? NULL : (GLFWmonitor*)(uintptr_t)NUM2ULL(monitor);
    GLFWwindow* shr = NIL_P(share) ? NULL : (GLFWwindow*)(uintptr_t)NUM2ULL(share);
    GLFWwindow* window = pfn_glfwCreateWindow(NUM2INT(width), NUM2INT(height), title_str, mon, shr);
    if (!window) return Qnil;
    return ULL2NUM((uintptr_t)window);
}

.destroy_window(window) ⇒ Object

DestroyWindow(window)



181
182
183
184
185
186
# File 'ext/glfw_native/glfw_native.c', line 181

static VALUE rb_glfw_destroy_window(VALUE self, VALUE window) {
    if (!pfn_glfwDestroyWindow) rb_raise(rb_eRuntimeError, "GLFW not loaded");
    GLFWwindow* win = (GLFWwindow*)(uintptr_t)NUM2ULL(window);
    pfn_glfwDestroyWindow(win);
    return Qnil;
}

.focus_window(window) ⇒ Object

FocusWindow(window)



246
247
248
249
250
251
# File 'ext/glfw_native/glfw_native.c', line 246

static VALUE rb_glfw_focus_window(VALUE self, VALUE window) {
    if (!pfn_glfwFocusWindow) rb_raise(rb_eRuntimeError, "GLFW not loaded");
    GLFWwindow* win = (GLFWwindow*)(uintptr_t)NUM2ULL(window);
    pfn_glfwFocusWindow(win);
    return Qnil;
}

.get_errorObject

GetError()



222
223
224
225
226
227
# File 'ext/glfw_native/glfw_native.c', line 222

static VALUE rb_glfw_get_error(VALUE self) {
    if (!pfn_glfwGetError) return Qnil;
    const char* description = NULL;
    int code = pfn_glfwGetError(&description);
    return rb_ary_new3(2, INT2NUM(code), description ? rb_str_new2(description) : Qnil);
}

.get_framebuffer_size(window, width_buf, height_buf) ⇒ Object

GetFramebufferSize(window, width_buf, height_buf) - writes to buffers



262
263
264
265
266
267
268
269
270
271
# File 'ext/glfw_native/glfw_native.c', line 262

static VALUE rb_glfw_get_framebuffer_size(VALUE self, VALUE window, VALUE width_buf, VALUE height_buf) {
    if (!pfn_glfwGetFramebufferSize) rb_raise(rb_eRuntimeError, "GLFW not loaded");
    GLFWwindow* win = (GLFWwindow*)(uintptr_t)NUM2ULL(window);
    Check_Type(width_buf, T_STRING);
    Check_Type(height_buf, T_STRING);
    int* w_ptr = (int*)RSTRING_PTR(width_buf);
    int* h_ptr = (int*)RSTRING_PTR(height_buf);
    pfn_glfwGetFramebufferSize(win, w_ptr, h_ptr);
    return Qnil;
}

.get_input_mode(window, mode) ⇒ Object

GetInputMode(window, mode)



332
333
334
335
336
337
# File 'ext/glfw_native/glfw_native.c', line 332

static VALUE rb_glfw_get_input_mode(VALUE self, VALUE window, VALUE mode) {
    if (!pfn_glfwGetInputMode) rb_raise(rb_eRuntimeError, "GLFW not loaded");
    GLFWwindow* win = (GLFWwindow*)(uintptr_t)NUM2ULL(window);
    int result = pfn_glfwGetInputMode(win, NUM2INT(mode));
    return INT2NUM(result);
}

.get_primary_monitorObject

GetPrimaryMonitor()



296
297
298
299
300
301
# File 'ext/glfw_native/glfw_native.c', line 296

static VALUE rb_glfw_get_primary_monitor(VALUE self) {
    if (!pfn_glfwGetPrimaryMonitor) rb_raise(rb_eRuntimeError, "GLFW not loaded");
    GLFWmonitor* monitor = pfn_glfwGetPrimaryMonitor();
    if (!monitor) return Qnil;
    return ULL2NUM((uintptr_t)monitor);
}

.get_video_mode(monitor) ⇒ Object

GetVideoMode(monitor) - returns pointer as integer



304
305
306
307
308
309
310
# File 'ext/glfw_native/glfw_native.c', line 304

static VALUE rb_glfw_get_video_mode(VALUE self, VALUE monitor) {
    if (!pfn_glfwGetVideoMode) rb_raise(rb_eRuntimeError, "GLFW not loaded");
    GLFWmonitor* mon = (GLFWmonitor*)(uintptr_t)NUM2ULL(monitor);
    const GLFWvidmode* mode = pfn_glfwGetVideoMode(mon);
    if (!mode) return Qnil;
    return ULL2NUM((uintptr_t)mode);
}

.get_video_modes(monitor, count_buf) ⇒ Object

GetVideoModes(monitor, count_buf) - returns pointer as integer, writes count to buffer



313
314
315
316
317
318
319
320
321
# File 'ext/glfw_native/glfw_native.c', line 313

static VALUE rb_glfw_get_video_modes(VALUE self, VALUE monitor, VALUE count_buf) {
    if (!pfn_glfwGetVideoModes) rb_raise(rb_eRuntimeError, "GLFW not loaded");
    GLFWmonitor* mon = (GLFWmonitor*)(uintptr_t)NUM2ULL(monitor);
    Check_Type(count_buf, T_STRING);
    int* count_ptr = (int*)RSTRING_PTR(count_buf);
    const GLFWvidmode* modes = pfn_glfwGetVideoModes(mon, count_ptr);
    if (!modes) return Qnil;
    return ULL2NUM((uintptr_t)modes);
}

.initObject

Init()



156
157
158
159
160
# File 'ext/glfw_native/glfw_native.c', line 156

static VALUE rb_glfw_init(VALUE self) {
    if (!pfn_glfwInit) rb_raise(rb_eRuntimeError, "GLFW not loaded");
    int result = pfn_glfwInit();
    return INT2NUM(result);
}

.load_lib(path) ⇒ Object

load_lib(path) - Load the GLFW shared library



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'ext/glfw_native/glfw_native.c', line 108

static VALUE rb_glfw_load_lib(VALUE self, VALUE path) {
    const char* lib_path = StringValueCStr(path);

#ifdef _WIN32
    glfw_lib = LoadLibrary(lib_path);
#else
    glfw_lib = dlopen(lib_path, RTLD_LAZY | RTLD_GLOBAL);
#endif

    if (!glfw_lib) {
#ifdef _WIN32
        rb_raise(rb_eRuntimeError, "Failed to load GLFW library: %s", lib_path);
#else
        rb_raise(rb_eRuntimeError, "Failed to load GLFW library: %s - %s", lib_path, dlerror());
#endif
    }

    /* Load all function pointers */
    pfn_glfwInit = (PFN_glfwInit)load_func("glfwInit");
    pfn_glfwTerminate = (PFN_glfwTerminate)load_func("glfwTerminate");
    pfn_glfwCreateWindow = (PFN_glfwCreateWindow)load_func("glfwCreateWindow");
    pfn_glfwDestroyWindow = (PFN_glfwDestroyWindow)load_func("glfwDestroyWindow");
    pfn_glfwWindowHint = (PFN_glfwWindowHint)load_func("glfwWindowHint");
    pfn_glfwSetWindowAttrib = (PFN_glfwSetWindowAttrib)load_func("glfwSetWindowAttrib");
    pfn_glfwSetWindowTitle = (PFN_glfwSetWindowTitle)load_func("glfwSetWindowTitle");
    pfn_glfwSetWindowMonitor = (PFN_glfwSetWindowMonitor)load_func("glfwSetWindowMonitor");
    pfn_glfwWindowShouldClose = (PFN_glfwWindowShouldClose)load_func("glfwWindowShouldClose");
    pfn_glfwSetWindowShouldClose = (PFN_glfwSetWindowShouldClose)load_func("glfwSetWindowShouldClose");
    pfn_glfwFocusWindow = (PFN_glfwFocusWindow)load_func("glfwFocusWindow");
    pfn_glfwMakeContextCurrent = (PFN_glfwMakeContextCurrent)load_func("glfwMakeContextCurrent");
    pfn_glfwGetFramebufferSize = (PFN_glfwGetFramebufferSize)load_func("glfwGetFramebufferSize");
    pfn_glfwPollEvents = (PFN_glfwPollEvents)load_func("glfwPollEvents");
    pfn_glfwSwapBuffers = (PFN_glfwSwapBuffers)load_func("glfwSwapBuffers");
    pfn_glfwSwapInterval = (PFN_glfwSwapInterval)load_func("glfwSwapInterval");
    pfn_glfwSetKeyCallback = (PFN_glfwSetKeyCallback)load_func("glfwSetKeyCallback");
    pfn_glfwSetCursorPosCallback = (PFN_glfwSetCursorPosCallback)load_func("glfwSetCursorPosCallback");
    pfn_glfwSetMouseButtonCallback = (PFN_glfwSetMouseButtonCallback)load_func("glfwSetMouseButtonCallback");
    pfn_glfwGetPrimaryMonitor = (PFN_glfwGetPrimaryMonitor)load_func("glfwGetPrimaryMonitor");
    pfn_glfwGetVideoMode = (PFN_glfwGetVideoMode)load_func("glfwGetVideoMode");
    pfn_glfwGetVideoModes = (PFN_glfwGetVideoModes)load_func("glfwGetVideoModes");
    pfn_glfwSetInputMode = (PFN_glfwSetInputMode)load_func("glfwSetInputMode");
    pfn_glfwGetInputMode = (PFN_glfwGetInputMode)load_func("glfwGetInputMode");
    pfn_glfwGetError = (PFN_glfwGetError)load_func("glfwGetError");

    return Qtrue;
}

.make_context_current(window) ⇒ Object

MakeContextCurrent(window)



254
255
256
257
258
259
# File 'ext/glfw_native/glfw_native.c', line 254

static VALUE rb_glfw_make_context_current(VALUE self, VALUE window) {
    if (!pfn_glfwMakeContextCurrent) rb_raise(rb_eRuntimeError, "GLFW not loaded");
    GLFWwindow* win = (GLFWwindow*)(uintptr_t)NUM2ULL(window);
    pfn_glfwMakeContextCurrent(win);
    return Qnil;
}

.poll_eventsObject

PollEvents()



274
275
276
277
278
# File 'ext/glfw_native/glfw_native.c', line 274

static VALUE rb_glfw_poll_events(VALUE self) {
    if (!pfn_glfwPollEvents) rb_raise(rb_eRuntimeError, "GLFW not loaded");
    pfn_glfwPollEvents();
    return Qnil;
}

.set_cursor_pos_callback(window, callback) ⇒ Object

SetCursorPosCallback(window, callback)



388
389
390
391
392
393
394
395
396
397
398
399
# File 'ext/glfw_native/glfw_native.c', line 388

static VALUE rb_glfw_set_cursor_pos_callback(VALUE self, VALUE window, VALUE callback) {
    if (!pfn_glfwSetCursorPosCallback) rb_raise(rb_eRuntimeError, "GLFW not loaded");
    GLFWwindow* win = (GLFWwindow*)(uintptr_t)NUM2ULL(window);

    rb_cursor_pos_callback = callback;
    if (NIL_P(callback)) {
        pfn_glfwSetCursorPosCallback(win, NULL);
    } else {
        pfn_glfwSetCursorPosCallback(win, cursor_pos_callback_trampoline);
    }
    return Qnil;
}

.set_input_mode(window, mode, value) ⇒ Object

SetInputMode(window, mode, value)



324
325
326
327
328
329
# File 'ext/glfw_native/glfw_native.c', line 324

static VALUE rb_glfw_set_input_mode(VALUE self, VALUE window, VALUE mode, VALUE value) {
    if (!pfn_glfwSetInputMode) rb_raise(rb_eRuntimeError, "GLFW not loaded");
    GLFWwindow* win = (GLFWwindow*)(uintptr_t)NUM2ULL(window);
    pfn_glfwSetInputMode(win, NUM2INT(mode), NUM2INT(value));
    return Qnil;
}

.set_key_callback(window, callback) ⇒ Object

SetKeyCallback(window, callback) - callback is a Ruby proc/block



374
375
376
377
378
379
380
381
382
383
384
385
# File 'ext/glfw_native/glfw_native.c', line 374

static VALUE rb_glfw_set_key_callback(VALUE self, VALUE window, VALUE callback) {
    if (!pfn_glfwSetKeyCallback) rb_raise(rb_eRuntimeError, "GLFW not loaded");
    GLFWwindow* win = (GLFWwindow*)(uintptr_t)NUM2ULL(window);

    rb_key_callback = callback;
    if (NIL_P(callback)) {
        pfn_glfwSetKeyCallback(win, NULL);
    } else {
        pfn_glfwSetKeyCallback(win, key_callback_trampoline);
    }
    return Qnil;
}

.set_mouse_button_callback(window, callback) ⇒ Object

SetMouseButtonCallback(window, callback)



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

static VALUE rb_glfw_set_mouse_button_callback(VALUE self, VALUE window, VALUE callback) {
    if (!pfn_glfwSetMouseButtonCallback) rb_raise(rb_eRuntimeError, "GLFW not loaded");
    GLFWwindow* win = (GLFWwindow*)(uintptr_t)NUM2ULL(window);

    rb_mouse_button_callback = callback;
    if (NIL_P(callback)) {
        pfn_glfwSetMouseButtonCallback(win, NULL);
    } else {
        pfn_glfwSetMouseButtonCallback(win, mouse_button_callback_trampoline);
    }
    return Qnil;
}

.set_window_attrib(window, attrib, value) ⇒ Object

SetWindowAttrib(window, attrib, value)



196
197
198
199
200
201
# File 'ext/glfw_native/glfw_native.c', line 196

static VALUE rb_glfw_set_window_attrib(VALUE self, VALUE window, VALUE attrib, VALUE value) {
    if (!pfn_glfwSetWindowAttrib) rb_raise(rb_eRuntimeError, "GLFW not loaded");
    GLFWwindow* win = (GLFWwindow*)(uintptr_t)NUM2ULL(window);
    pfn_glfwSetWindowAttrib(win, NUM2INT(attrib), NUM2INT(value));
    return Qnil;
}

.set_window_monitor(window, monitor, xpos, ypos, width, height, refresh_rate) ⇒ Object

SetWindowMonitor(window, monitor, xpos, ypos, width, height, refreshRate)



213
214
215
216
217
218
219
# File 'ext/glfw_native/glfw_native.c', line 213

static VALUE rb_glfw_set_window_monitor(VALUE self, VALUE window, VALUE monitor, VALUE xpos, VALUE ypos, VALUE width, VALUE height, VALUE refresh_rate) {
    if (!pfn_glfwSetWindowMonitor) rb_raise(rb_eRuntimeError, "GLFW not loaded");
    GLFWwindow* win = (GLFWwindow*)(uintptr_t)NUM2ULL(window);
    GLFWmonitor* mon = NIL_P(monitor) ? NULL : (GLFWmonitor*)(uintptr_t)NUM2ULL(monitor);
    pfn_glfwSetWindowMonitor(win, mon, NUM2INT(xpos), NUM2INT(ypos), NUM2INT(width), NUM2INT(height), NUM2INT(refresh_rate));
    return Qnil;
}

.set_window_should_close(window, value) ⇒ Object

SetWindowShouldClose(window, value)



238
239
240
241
242
243
# File 'ext/glfw_native/glfw_native.c', line 238

static VALUE rb_glfw_set_window_should_close(VALUE self, VALUE window, VALUE value) {
    if (!pfn_glfwSetWindowShouldClose) rb_raise(rb_eRuntimeError, "GLFW not loaded");
    GLFWwindow* win = (GLFWwindow*)(uintptr_t)NUM2ULL(window);
    pfn_glfwSetWindowShouldClose(win, NUM2INT(value));
    return Qnil;
}

.set_window_title(window, title) ⇒ Object

SetWindowTitle(window, title)



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

static VALUE rb_glfw_set_window_title(VALUE self, VALUE window, VALUE title) {
    if (!pfn_glfwSetWindowTitle) rb_raise(rb_eRuntimeError, "GLFW not loaded");
    GLFWwindow* win = (GLFWwindow*)(uintptr_t)NUM2ULL(window);
    const char* title_str = StringValueCStr(title);
    pfn_glfwSetWindowTitle(win, title_str);
    return Qnil;
}

.swap_buffers(window) ⇒ Object

SwapBuffers(window)



281
282
283
284
285
286
# File 'ext/glfw_native/glfw_native.c', line 281

static VALUE rb_glfw_swap_buffers(VALUE self, VALUE window) {
    if (!pfn_glfwSwapBuffers) rb_raise(rb_eRuntimeError, "GLFW not loaded");
    GLFWwindow* win = (GLFWwindow*)(uintptr_t)NUM2ULL(window);
    pfn_glfwSwapBuffers(win);
    return Qnil;
}

.swap_interval(interval) ⇒ Object

SwapInterval(interval)



289
290
291
292
293
# File 'ext/glfw_native/glfw_native.c', line 289

static VALUE rb_glfw_swap_interval(VALUE self, VALUE interval) {
    if (!pfn_glfwSwapInterval) rb_raise(rb_eRuntimeError, "GLFW not loaded");
    pfn_glfwSwapInterval(NUM2INT(interval));
    return Qnil;
}

.terminateObject

Terminate()



163
164
165
166
167
# File 'ext/glfw_native/glfw_native.c', line 163

static VALUE rb_glfw_terminate(VALUE self) {
    if (!pfn_glfwTerminate) rb_raise(rb_eRuntimeError, "GLFW not loaded");
    pfn_glfwTerminate();
    return Qnil;
}

.window_hint(hint, value) ⇒ Object

WindowHint(hint, value)



189
190
191
192
193
# File 'ext/glfw_native/glfw_native.c', line 189

static VALUE rb_glfw_window_hint(VALUE self, VALUE hint, VALUE value) {
    if (!pfn_glfwWindowHint) rb_raise(rb_eRuntimeError, "GLFW not loaded");
    pfn_glfwWindowHint(NUM2INT(hint), NUM2INT(value));
    return Qnil;
}

.window_should_close(window) ⇒ Object

WindowShouldClose(window)



230
231
232
233
234
235
# File 'ext/glfw_native/glfw_native.c', line 230

static VALUE rb_glfw_window_should_close(VALUE self, VALUE window) {
    if (!pfn_glfwWindowShouldClose) rb_raise(rb_eRuntimeError, "GLFW not loaded");
    GLFWwindow* win = (GLFWwindow*)(uintptr_t)NUM2ULL(window);
    int result = pfn_glfwWindowShouldClose(win);
    return INT2NUM(result);
}