Skip to content
This repository was archived by the owner on Jul 1, 2020. It is now read-only.

Commit 3677f74

Browse files
Fixed hints being a window method but not supposed to be. Throw errors when certain things return null.
1 parent b055ad0 commit 3677f74

File tree

3 files changed

+67
-39
lines changed

3 files changed

+67
-39
lines changed

build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
1+
#!/bin/bash
22
set -e
33
gcc -E -dD -I ./gen/ ./gen/headers.h | python3 ./gen/cdef.py | cat - glfw_base.lua > glfw.lua

glfw_base.lua

Lines changed: 64 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -37,30 +37,43 @@ Lib.glext = setmetatable({}, {
3737
end,
3838
})
3939

40-
-- TODO: The docs say that `lib.foo` is faster than `local foo = lib.foo; foo()`, but is the overhead of a closure
40+
-- TODO: The docs say that `lib.foo()` is faster than `local foo = lib.foo; foo()`, but is the overhead of a closure
4141
-- worth it?
4242
local function wrap(lib, fname)
4343
return function(...)
4444
return lib[fname](...)
4545
end
4646
end
4747

48+
-- Similar to wrap, but errors if the returned object is null
49+
local function wrapErrorOnNull(lib, fname)
50+
return function(...)
51+
local obj = lib[fname](...)
52+
if obj == nil then
53+
error(fname.." failed",2)
54+
end
55+
return obj
56+
end
57+
end
58+
59+
-- Some buffers for out parameters
4860
local int_buffer = ffi.new("int[2]")
4961
local double_buffer = ffi.new("double[2]")
5062

5163
---------------------------------------------------------------------------------------------------------------------
5264
-- GLFW Global Functions
5365

5466
-- C functions that don't need special handling
55-
Lib.terminate = wrap(glfw, "glfwTerminate")
56-
57-
Lib.getTime = wrap(glfw, "glfwGetTime")
58-
Lib.setTime = wrap(glfw, "glfwSetTime")
5967
Lib.defaultWindowHints = wrap(glfw, "glfwDefaultWindowHints")
60-
Lib.swapInterval = wrap(glfw, "glfwSwapInterval")
6168
Lib.getCurrentContext = wrap(glfw, "glfwGetCurrentContext")
69+
Lib.getPrimaryMonitor = wrapErrorOnNull(glfw, "glfwGetPrimaryMonitor")
6270
Lib.getProcAddress = wrap(glfw, "glfwGetProcAddress")
71+
Lib.getTime = wrap(glfw, "glfwGetTime")
72+
Lib.hint = wrap(glfw, "glfwWindowHint")
6373
Lib.joystickPresent = wrap(glfw, "glfwJoystickPresent")
74+
Lib.setTime = wrap(glfw, "glfwSetTime")
75+
Lib.swapInterval = wrap(glfw, "glfwSwapInterval")
76+
Lib.terminate = wrap(glfw, "glfwTerminate")
6477

6578
-- Functions with special Lua code
6679

@@ -113,12 +126,14 @@ function Lib.getJoystickButtons(joy, arr)
113126
return arr
114127
end
115128

129+
-- Returns Lua string
116130
function Lib.getJoystickName(joy)
117131
local name = glfw.glfwGetJoystickName(joy)
118132
if name == nil then error("Invalid joystick: "..joy, 2) end
119133
return ffi.string(name)
120134
end
121135

136+
-- Returns Lua table
122137
function Lib.getMonitors()
123138
local cmonitors = glfw.glfwGetMonitors(int_buffer)
124139
if cmonitors == nil then error("glfwGetMonitors failed",2) end
@@ -130,12 +145,6 @@ function Lib.getMonitors()
130145
return monitors
131146
end
132147

133-
function Lib.getPrimaryMonitor()
134-
local monitor = glfw.glfwGetPrimaryMonitor()
135-
if monitor == nil then error("glfwGetPrimaryMonitor failed",2) end
136-
return monitor
137-
end
138-
139148
-- These functions can't be jit compiled, because they may call callbacks.
140149
-- Don't use wrap because jit.off affects the prototype, which will affect other functions too.
141150
Lib.pollEvents = function() return glfw.glfwPollEvents() end
@@ -159,41 +168,47 @@ function Window:__new(w, h, title, monitor, share)
159168
end
160169

161170
-- C functions that don't need special handling
162-
Window.getAttrib = wrap(glfw, "glfwGetWindowAttrib")
171+
Window.__gc = wrap(glfw, "glfwDestroyWindow")
172+
Window.getInputMode = wrap(glfw, "glfwGetInputMode")
173+
Window.getKey = wrap(glfw, "glfwGetKey")
163174
Window.getMonitor = wrap(glfw, "glfwGetWindowMonitor")
175+
Window.getMouseButton = wrap(glfw, "glfwGetMouseButton")
164176
Window.hide = wrap(glfw, "glfwHideWindow")
165177
Window.iconify = wrap(glfw, "glfwIconifyWindow")
166-
Window.restore = wrap(glfw, "glfwRestoreWindow")
167-
Window.setPos = wrap(glfw, "glfwSetWindowPos")
168-
Window.setShouldClose = wrap(glfw, "glfwSetWindowShouldClose")
169-
Window.setSize = wrap(glfw, "glfwSetWindowSize")
170-
Window.setTitle = wrap(glfw, "glfwSetWindowTitle")
171-
Window.show = wrap(glfw, "glfwShowWindow")
172-
Window.hint = wrap(glfw, "glfwWindowHint")
173178
Window.makeContextCurrent = wrap(glfw, "glfwMakeContextCurrent")
174-
Window.swapBuffers = wrap(glfw, "glfwSwapBuffers")
179+
Window.restore = wrap(glfw, "glfwRestoreWindow")
180+
Window.setCharCallback = wrap(glfw, "glfwSetCharCallback")
175181
Window.setClipboardString = wrap(glfw, "glfwSetClipboardString")
176-
Window.getInputMode = wrap(glfw, "glfwGetInputMode")
177-
Window.setInputMode = wrap(glfw, "glfwSetInputMode")
178-
Window.getKey = wrap(glfw, "glfwGetKey")
179-
Window.getMouseButton = wrap(glfw, "glfwGetMouseButton")
180-
Window.setCursorPos = wrap(glfw, "glfwSetCursorPos")
181-
Window.setFramebufferSizeCallback = wrap(glfw, "glfwSetFramebufferSizeCallback")
182182
Window.setCloseCallback = wrap(glfw, "glfwSetWindowCloseCallback")
183+
Window.setCursorEnterCallback = wrap(glfw, "glfwSetCursorEnterCallback")
184+
Window.setCursorPos = wrap(glfw, "glfwSetCursorPos")
185+
Window.setCursorPosCallback = wrap(glfw, "glfwSetCursorPosCallback")
183186
Window.setFocusCallback = wrap(glfw, "glfwSetWindowFocusCallback")
187+
Window.setFramebufferSizeCallback = wrap(glfw, "glfwSetFramebufferSizeCallback")
184188
Window.setIconifyCallback = wrap(glfw, "glfwSetWindowIconifyCallback")
185-
Window.setPosCallback = wrap(glfw, "glfwSetWindowPosCallback")
186-
Window.setRefreshCallback = wrap(glfw, "glfwSetWindowRefreshCallback")
187-
Window.setSizeCallback = wrap(glfw, "glfwSetWindowSizeCallback")
189+
Window.setInputMode = wrap(glfw, "glfwSetInputMode")
188190
Window.setKeyCallback = wrap(glfw, "glfwSetKeyCallback")
189-
Window.setCharCallback = wrap(glfw, "glfwSetCharCallback")
190191
Window.setMouseButtonCallback = wrap(glfw, "glfwSetMouseButtonCallback")
191-
Window.setCursorPosCallback = wrap(glfw, "glfwSetCursorPosCallback")
192-
Window.setCursorEnterCallback = wrap(glfw, "glfwSetCursorEnterCallback")
192+
Window.setPos = wrap(glfw, "glfwSetWindowPos")
193+
Window.setPosCallback = wrap(glfw, "glfwSetWindowPosCallback")
194+
Window.setRefreshCallback = wrap(glfw, "glfwSetWindowRefreshCallback")
193195
Window.setScrollCallback = wrap(glfw, "glfwSetScrollCallback")
196+
Window.setShouldClose = wrap(glfw, "glfwSetWindowShouldClose")
197+
Window.setSize = wrap(glfw, "glfwSetWindowSize")
198+
Window.setSizeCallback = wrap(glfw, "glfwSetWindowSizeCallback")
199+
Window.setTitle = wrap(glfw, "glfwSetWindowTitle")
200+
Window.show = wrap(glfw, "glfwShowWindow")
201+
Window.swapBuffers = wrap(glfw, "glfwSwapBuffers")
194202

195203
-- Functions with special Lua code
196204

205+
-- Errors on zero
206+
function Window:getAttrib(attrib)
207+
local v = glfw.glfwGetWindowAttrib(self, attrib)
208+
if v == 0 then error("glfwGetWindowAttrib failed", 2) end
209+
return v
210+
end
211+
197212
-- Returns width, height instead of needing out parameters
198213
function Window:getFramebufferSize()
199214
glfw.glfwGetFramebufferSize(self, int_buffer, int_buffer+1)
@@ -223,18 +238,16 @@ function Window:getCursorPos()
223238
return double_buffer[0], double_buffer[1]
224239
end
225240

241+
-- Returns a boolean
226242
function Window:shouldClose()
227243
return glfw.glfwWindowShouldClose(self) ~= 0
228244
end
229245

246+
-- Prevents the GC from double free'ing
230247
function Window:destroy()
231248
glfw.glfwDestroyWindow(ffi.gc(self, nil))
232249
end
233250

234-
function Window:__gc()
235-
glfw.glfwDestroyWindow(self)
236-
end
237-
238251
ffi.metatype(Window_t, Window)
239252

240253
---------------------------------------------------------------------------------------------------------------------
@@ -245,18 +258,31 @@ Monitor.__index = Monitor
245258
local Monitor_t = ffi.typeof("GLFWmonitor")
246259
Lib.Monitor = Monitor_t
247260

248-
Monitor.setCallback = wrap(glfw, "glfwSetMonitorCallback")
261+
Monitor.setCallback = wrap(glfw, "glfwSetMonitorCallback")
262+
Monitor.getVideoMode = wrapErrorOnNull(glfw, "glfwGetVideoMode")
249263

264+
-- Returns x,y instead of needing out params
250265
function Monitor:getPos()
251266
glfw.glfwGetMonitorPos(self, int_buffer, int_buffer+1)
252267
return int_buffer[0], int_buffer[1]
253268
end
254269

270+
-- Retruns w,h instead of needing out params
255271
function Monitor:getPhysicalSize()
256272
glfw.glfwGetMonitorPhysicalSize(self, int_buffer, int_buffer+1)
257273
return int_buffer[0], int_buffer[1]
258274
end
259275

276+
-- Returns Lua table
277+
function Monitor:getVideoModes()
278+
local carr = glfw.glfwGetVideoModes(self, int_buffer)
279+
local arr = {}
280+
for i=0,int_buffer[0]-1 do
281+
arr[i+1] = carr[i]
282+
end
283+
return arr
284+
end
285+
260286
ffi.metatype(Monitor_t, Monitor)
261287

262288
---------------------------------------------------------------------------------------------------------------------

readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ You can also use the following snippet to concisely localize the libraries.
3737

3838
```lua
3939
local gl, glc, glu, glfw, glext = luajit_glfw.libraries()
40+
-- Or if you just need the libraries:
41+
local gl, glc, glu, glfw, glext = require('glfw').libraries()
4042
```
4143

4244
Additionally, LuaJIT-GLFW wraps GLFW functions and sets metatypes for GLFW structs for convenience. See `glfw_base.lua`

0 commit comments

Comments
 (0)