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)



167
168
169
170
171
172
173
174
175
# File 'ext/glfw_native/glfw_native.c', line 167

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)



178
179
180
181
182
183
# File 'ext/glfw_native/glfw_native.c', line 178

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)



235
236
237
238
239
240
# File 'ext/glfw_native/glfw_native.c', line 235

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_framebuffer_size(window, width_buf, height_buf) ⇒ Object

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



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

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)



321
322
323
324
325
326
# File 'ext/glfw_native/glfw_native.c', line 321

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()



285
286
287
288
289
290
# File 'ext/glfw_native/glfw_native.c', line 285

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



293
294
295
296
297
298
299
# File 'ext/glfw_native/glfw_native.c', line 293

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



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

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()



153
154
155
156
157
# File 'ext/glfw_native/glfw_native.c', line 153

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



106
107
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
# File 'ext/glfw_native/glfw_native.c', line 106

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");

    return Qtrue;
}

.make_context_current(window) ⇒ Object

MakeContextCurrent(window)



243
244
245
246
247
248
# File 'ext/glfw_native/glfw_native.c', line 243

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()



263
264
265
266
267
# File 'ext/glfw_native/glfw_native.c', line 263

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)



377
378
379
380
381
382
383
384
385
386
387
388
# File 'ext/glfw_native/glfw_native.c', line 377

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)



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

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



363
364
365
366
367
368
369
370
371
372
373
374
# File 'ext/glfw_native/glfw_native.c', line 363

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)



391
392
393
394
395
396
397
398
399
400
401
402
# File 'ext/glfw_native/glfw_native.c', line 391

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)



193
194
195
196
197
198
# File 'ext/glfw_native/glfw_native.c', line 193

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)



210
211
212
213
214
215
216
# File 'ext/glfw_native/glfw_native.c', line 210

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)



227
228
229
230
231
232
# File 'ext/glfw_native/glfw_native.c', line 227

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)



201
202
203
204
205
206
207
# File 'ext/glfw_native/glfw_native.c', line 201

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)



270
271
272
273
274
275
# File 'ext/glfw_native/glfw_native.c', line 270

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)



278
279
280
281
282
# File 'ext/glfw_native/glfw_native.c', line 278

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()



160
161
162
163
164
# File 'ext/glfw_native/glfw_native.c', line 160

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)



186
187
188
189
190
# File 'ext/glfw_native/glfw_native.c', line 186

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)



219
220
221
222
223
224
# File 'ext/glfw_native/glfw_native.c', line 219

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);
}