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

Commit b055ad0

Browse files
Added Lib.libraries(). Fixed some issues. Added sample test program
1 parent d391f3f commit b055ad0

File tree

3 files changed

+115
-17
lines changed

3 files changed

+115
-17
lines changed

glfw_base.lua

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,14 @@ Lib.joystickPresent = wrap(glfw, "glfwJoystickPresent")
6464

6565
-- Functions with special Lua code
6666

67+
-- Shortcut for localizing libraries
68+
function Lib.libraries()
69+
return gl, glc, glu, glfw, glext
70+
end
71+
6772
-- Throws an error on failure
6873
function Lib.init()
69-
if glfw.glfwInit() ~= 0 then
74+
if glfw.glfwInit() == 0 then
7075
error("glfwInit failed",0)
7176
end
7277
end
@@ -131,6 +136,14 @@ function Lib.getPrimaryMonitor()
131136
return monitor
132137
end
133138

139+
-- These functions can't be jit compiled, because they may call callbacks.
140+
-- Don't use wrap because jit.off affects the prototype, which will affect other functions too.
141+
Lib.pollEvents = function() return glfw.glfwPollEvents() end
142+
jit.off(Lib.pollEvents)
143+
144+
Lib.waitEvents = function() return glfw.glfwWaitEvents() end
145+
jit.off(Lib.waitEvents)
146+
134147
---------------------------------------------------------------------------------------------------------------------
135148
-- Window
136149

@@ -146,7 +159,6 @@ function Window:__new(w, h, title, monitor, share)
146159
end
147160

148161
-- C functions that don't need special handling
149-
Window.destroy = wrap(glfw, "glfwDestroyWindow")
150162
Window.getAttrib = wrap(glfw, "glfwGetWindowAttrib")
151163
Window.getMonitor = wrap(glfw, "glfwGetWindowMonitor")
152164
Window.hide = wrap(glfw, "glfwHideWindow")
@@ -158,7 +170,6 @@ Window.setSize = wrap(glfw, "glfwSetWindowSize")
158170
Window.setTitle = wrap(glfw, "glfwSetWindowTitle")
159171
Window.show = wrap(glfw, "glfwShowWindow")
160172
Window.hint = wrap(glfw, "glfwWindowHint")
161-
Window.shouldClose = wrap(glfw, "glfwWindowShouldClose")
162173
Window.makeContextCurrent = wrap(glfw, "glfwMakeContextCurrent")
163174
Window.swapBuffers = wrap(glfw, "glfwSwapBuffers")
164175
Window.setClipboardString = wrap(glfw, "glfwSetClipboardString")
@@ -181,14 +192,6 @@ Window.setCursorPosCallback = wrap(glfw, "glfwSetCursorPosCallback")
181192
Window.setCursorEnterCallback = wrap(glfw, "glfwSetCursorEnterCallback")
182193
Window.setScrollCallback = wrap(glfw, "glfwSetScrollCallback")
183194

184-
-- These functions can't be jit compiled, because they may call callbacks.
185-
-- Don't use wrap because jit.off affects the prototype, which will affect other functions too.
186-
Window.pollEvents = function(self) return glfw.glfwPollEvents(self) end
187-
jit.off(Window.pollEvents)
188-
189-
Window.waitEvents = function(self) return glfw.glfwWaitEvents(self) end
190-
jit.off(Window.waitEvents)
191-
192195
-- Functions with special Lua code
193196

194197
-- Returns width, height instead of needing out parameters
@@ -220,6 +223,18 @@ function Window:getCursorPos()
220223
return double_buffer[0], double_buffer[1]
221224
end
222225

226+
function Window:shouldClose()
227+
return glfw.glfwWindowShouldClose(self) ~= 0
228+
end
229+
230+
function Window:destroy()
231+
glfw.glfwDestroyWindow(ffi.gc(self, nil))
232+
end
233+
234+
function Window:__gc()
235+
glfw.glfwDestroyWindow(self)
236+
end
237+
223238
ffi.metatype(Window_t, Window)
224239

225240
---------------------------------------------------------------------------------------------------------------------

readme.md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,21 @@ Usage
2222
To load the library, use the `require` function:
2323

2424
```lua
25-
local glfw = require "glfw"
25+
local luajit_glfw = require "glfw"
2626
```
2727

2828
LuaJIT-GLFW loads the following libraries:
2929

30-
* `glfw.gl`: OpenGL
31-
* `glfw.glu`: GLU
32-
* `glfw.glfw`: GLFW
33-
* `glfw.glext`: A table that, when indexed, loads and returns the specified extension function. Ex. `glext.glMyExtFuncARB(p1, p2)`
34-
* `glfw.glc`: `#define`d values for OpenGL and GLFW (this must be a Lua table instead of `static const` values, because OpenGL uses `longs` in a couple of places)
30+
* `luajit_glfw.gl`: OpenGL
31+
* `luajit_glfw.glc`: `#define`d values for OpenGL and GLFW (this must be a Lua table instead of `static const` values, because OpenGL uses `longs` in a couple of places)
32+
* `luajit_glfw.glu`: GLU
33+
* `luajit_glfw.glfw`: GLFW
34+
* `luajit_glfw.glext`: A table that, when indexed, loads and returns the specified extension function. Ex. `glext.glMyExtFuncARB(p1, p2)`
35+
36+
You can also use the following snippet to concisely localize the libraries.
37+
38+
```lua
39+
local gl, glc, glu, glfw, glext = luajit_glfw.libraries()
40+
```
3541

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

test.lua

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
2+
-- Load libraries
3+
local lj_glfw = require "glfw"
4+
local ffi = require "ffi"
5+
local bit = require "bit"
6+
-- Localize the FFI libraries
7+
local gl, glc, glu, glfw, glext = lj_glfw.libraries()
8+
9+
-- Define vertex arrays for the model we will draw
10+
local CubeVerticies = {}
11+
CubeVerticies.v = ffi.new("const float[8][3]", {
12+
{0,0,1}, {0,0,0}, {0,1,0}, {0,1,1},
13+
{1,0,1}, {1,0,0}, {1,1,0}, {1,1,1}
14+
})
15+
16+
CubeVerticies.n = ffi.new("const float[6][3]", {
17+
{-1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {1.0, 0.0, 0.0},
18+
{0.0, -1.0, 0.0}, {0.0, 0.0, -1.0}, {0.0, 0.0, 1.0}
19+
})
20+
21+
CubeVerticies.f = ffi.new("const float[6][4]", {
22+
{0, 1, 2, 3}, {3, 2, 6, 7}, {7, 6, 5, 4},
23+
{4, 5, 1, 0}, {5, 6, 2, 1}, {7, 4, 0, 3}
24+
})
25+
26+
-- Initialize GLFW. Unline glfwInit, this throws an error on failure
27+
lj_glfw.init()
28+
29+
-- Creates a new window. lj_glfw.Window is a ctype object with a __new metamethod that
30+
-- runs glfwCreateWindow.
31+
-- The window ctype has most of the windows functions as methods
32+
local window = lj_glfw.Window(1024, 768, "LuaJIT-GLFW Test")
33+
34+
-- Initialize the context. This needs to be called before any OpenGL calls.
35+
window:makeContextCurrent()
36+
37+
local w, h = window:getFramebufferSize()
38+
39+
-- Set up OpenGL
40+
gl.glEnable(glc.GL_DEPTH_TEST);
41+
42+
gl.glMatrixMode(glc.GL_PROJECTION)
43+
glu.gluPerspective(60, w/h, 0.01, 1000)
44+
gl.glMatrixMode(glc.GL_MODELVIEW)
45+
glu.gluLookAt(0,0,5,
46+
0,0,0,
47+
0,1,0)
48+
49+
-- Set up some values
50+
local rotx, roty, rotz = 1/math.sqrt(2), 1/math.sqrt(2), 0
51+
local boxx, boxy, boxz = -0.5,-0.5,2
52+
53+
-- Main loop
54+
while not window:shouldClose() do
55+
gl.glClear(bit.bor(glc.GL_COLOR_BUFFER_BIT, glc.GL_DEPTH_BUFFER_BIT))
56+
57+
gl.glPushMatrix()
58+
gl.glColor3d(1,1,1)
59+
gl.glTranslated(boxx, boxy, boxz)
60+
gl.glRotated(lj_glfw.getTime()*10, rotx, roty, rotz)
61+
for i=0,5 do
62+
gl.glBegin(glc.GL_QUADS)
63+
gl.glNormal3fv(CubeVerticies.n[i])
64+
for j=0,3 do
65+
gl.glVertex3fv(CubeVerticies.v[CubeVerticies.f[i][j]])
66+
end
67+
gl.glEnd()
68+
end
69+
gl.glPopMatrix()
70+
71+
window:swapBuffers()
72+
lj_glfw.pollEvents()
73+
end
74+
75+
-- Destroy the window and deinitialize GLFW.
76+
window:destroy()
77+
lj_glfw.terminate()

0 commit comments

Comments
 (0)