Giter Club home page Giter Club logo

luajit-glfw's Introduction

Features

  • Full support of 3.4.0 API (except native functions)
  • Supports both Luajit and Lua with luaffi
  • Additional OO style for monitors, windows, and cursors
 glfw.ShowWindow(window)
 -- or
 window:Show()
  • Constants can be used in two ways
 glfw.GetKey(window, GLFW.KEY_SPACE)
 -- or
 glfw.GetKey(window, 'KEY_SPACE')

Differences from the C API

Binding is so close to the original API as possible, but some things still differ.

  1. Names lost 'glfw' prefix.
  2. Arrays of chars replaced by lua string (except joystick buttons).
  3. Arrays of structs and strings replaced by lua tables.
  4. Values returned by reference replaced by returning table or multiple results.
  5. Video mode returned as table. This may change in future if necessary.
  6. Some OO methods have shortened names.

Start using

Before calling glfw functions you need to initialize binding with library name or path. Luajit uses dynamic library loading API directly, so behaviour may be different on each OS. Filename and location of glfw library may also vary. Several examples:

-- Windows
local glfw = require 'glfw' ('glfw3')
local glfw = require 'glfw' ('../some/path/glfw3.dll')
-- Linux
local glfw = require 'glfw' ('./libglfw3.so')
local glfw = require 'glfw' ('/usr/local/lib/libglfw3.so')
-- Mac OS X
local glfw = require 'glfw' ('/opt/local/lib/libglfw.3.1.dylib')

For statically linked glfw, just skip argument.

-- Any OS
local glfw = require 'glfw' ()

Constants stored in const table. It is recommended to save it in variable with GLFW name.

local GLFW = glfw.const

If you need vulkan-related functions, then specify 'bind_vulkan' option. Vulkan types MUST be declared (manually or with other binding) before this.

local glfw = require 'glfw' { 'glfw3',
  bind_vulkan = true
}

If you use or want to support Lua+luaffi, then compare pointers with NULL only in this way:

pointer == GLFW.NULL

Quick example

local glfw = require 'glfw' ('glfw3')
local GLFW = glfw.const

-- Initialize the library
if glfw.Init() == 0 then
  return
end

-- Create a windowed mode window and its OpenGL context
local window = glfw.CreateWindow(640, 480, "Hello World")
if window == GLFW.NULL then
  glfw.Terminate()
  return
end

-- Make the window's context current
glfw.MakeContextCurrent(window)

-- Loop until the user closes the window
while glfw.WindowShouldClose(window) == 0 do
  -- Render here

  -- Swap front and back buffers
  glfw.SwapBuffers(window)

  -- Poll for and process events
  glfw.PollEvents()
end

glfw.Terminate()

Quick example with OO style

local glfw = require 'glfw' ('glfw3')
local GLFW = glfw.const

-- Initialize the library
if glfw.Init() == 0 then
  return
end

-- Create a windowed mode window and its OpenGL context
local window = glfw.CreateWindow(640, 480, "Hello World")
if window == GLFW.NULL then
  glfw.Terminate()
  return
end

-- Make the window's context current
window:MakeContextCurrent()

-- Loop until the user closes the window
while window:ShouldClose() == 0 do
  -- Render here

  -- Swap front and back buffers
  window:SwapBuffers()

  -- Poll for and process events
  glfw.PollEvents()
end

glfw.Terminate()

Other examples

  local version = glfw.GetVersion()
  print(version.major, version.minor, version.rev)

  local monitors = glfw.GetMonitors()
  for i = 1, #monitors do ... end

  local x,y = glfw.GetMonitorPos(monitor)
  local x,y = monitor:GetPos()

  local w,h = glfw.GetMonitorPhysicalSize(monitor)
  local w,h = monitor:GetPhysicalSize()

  local modes = glfw.GetVideoModes(monitor)
  local modes = monitor:GetVideoModes()
  for i = 1, #modes do ... end

  local mode = glfw.GetVideoMode(monitor)
  local mode = monitor:GetVideoMode()
  for k,v in pairs(mode) do
    print(k,v)
  end

  local x,y = glfw.GetWindowPos(window)
  local x,y = window:GetPos()

  local fsize = glfw.GetWindowFrameSize(window)
  local fsize = window:GetFrameSize()
  print(fsize.left, fsize.top, fsize.right, fsize.bottom)

  local axes = glfw.GetJoystickAxes(joy)
  for i = 1, #axes do ... end

Acknowledgements

luajit-glfw's People

Contributors

playermet avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

luajit-glfw's Issues

Plain Lua + FFI support

It seems to me that a slight modification make this compatible with lua (not luaji) + ffifb (https://github.com/facebook/luaffifb):

index b831689..ece7b70 100644
--- a/glfw.lua
+++ b/glfw.lua
@@ -29,7 +29,6 @@
 ]]

 local ffi = require 'ffi'
-local jit = require 'jit'

 local mod = {} -- Lua module namespace
 local aux = {} -- Auxiliary utils
@@ -1042,9 +1041,12 @@ function bind_clib()
   -- Luajit does not allow to call Lua-callbacks
   -- from JIT-compiled C-functions, so we
   -- manually turn off JIT for them
-  jit.off(funcs.PollEvents)
-  jit.off(funcs.WaitEvents)
-  jit.off(funcs.WaitEventsTimeout)
+  pcall(function()
+    local jit = require 'jit'
+    jit.off(funcs.PollEvents)
+    jit.off(funcs.WaitEvents)
+    jit.off(funcs.WaitEventsTimeout)
+  end)

   -----------------------------------------------------------
   --  Extra functions

Can't get examples working, Luajit 2.0.4, Os X 10.9.5

Hello, I was testing out your Luajit GLFW bindings.
Running on Mac Os X 10.9.5, with Luajit 2.0.4 installed from MacPorts to /opt/local

When I try to run the examples, I just get this error message:

sakari@iViper /t/luajit-glfw (master)> luajit examples/base.lua
luajit: ./glfw.lua:972: dlopen(libglfw3.dylib, 5): image not found
stack traceback:
    [C]: in function 'load'
    ./glfw.lua:972: in function <./glfw.lua:969>
    examples/base.lua:1: in main chunk
    [C]: at 0x010ec83dca

So apparently Luajit can't find my glfw libraries. I have also installed libglfw 3.1.2 from MacPorts under /opt/local, as shown here:

sakari@iViper /t/luajit-glfw (master)> ls -l /opt/local/lib/libglfw.*
-rwxr-xr-x  1 root  admin  89396 Oct 16 19:54 /opt/local/lib/libglfw.3.1.dylib*
lrwxr-xr-x  1 root  admin     17 Oct 16 19:54 /opt/local/lib/libglfw.3.dylib@ -> libglfw.3.1.dylib
lrwxr-xr-x  1 root  admin     15 Oct 16 19:54 /opt/local/lib/libglfw.dylib@ -> libglfw.3.dylib

I tried also changing the code to directly point to the library by changing the 'name' variable, and also tried copying the libraries to the current working directory, no luck.

Can you tell me what I'm missing here? Thanks!

Can't run any opengl command

On the main example, I tried to call "glClear(GL_COLOR_BUFFER_BIT)" but nothing works.

I have to include opengl.dll on my program?

code don't work

so i was using your lua library for glfw and opengl and i tried to change the color of the window, but it isn't working

here is the code

local glfw = require 'glfw' ('glfw3')
local glfw = require 'glfw' ('C:\\lua\\glfw luajit\\glfw3.dll')
local GLFW = glfw.const
local gl = require 'gl' ('opengl32')
local gl = require 'gl' ('C:\\lua\\glfw luajit\\opengl32.dll')
local GL = gl.const

glfw.WindowHint(GLFW.VERSION_MAJOR, 3)
glfw.WindowHint(GLFW.VERSION_MINOR, 0)
glfw.WindowHint(GLFW.OPENGL_PROFILE, GLFW.OPENGL_CORE_PROFILE)

-- Initialize the library
if glfw.Init() == 0 then
  return
end

-- Create a windowed mode window and its OpenGL context
local window = glfw.CreateWindow(640, 480, "Hello World")
if window == GLFW.NULL then
  glfw.Terminate()
  return
end

-- Make the window's context current
glfw.MakeContextCurrent(window)
--[[gl.Viewport(0, 0, 640, 480)
gl.ClearColor(0.2, 0.3, 0.4, 1.0)
gl.Clear(GL.COLOR_BUFFER_BIT)]]

-- Loop until the user closes the window
while glfw.WindowShouldClose(window) == 0 do
  -- Render here
local W, H = glfw.GetFramebufferSize(window)
gl.Viewport(0, 0, W, H)
gl.Clear(GL.COLOR_BUFFER_BIT)
gl.ClearColor(0.02, 0.03, 0.04, 1.0)
--gl.End()

glfw.SwapBuffers(window)
  -- Poll for and process events
  glfw.PollEvents()
end

glfw.Terminate()

if you find any errors on the code say to me

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.