├── .gitignore ├── examples ├── base_oo.lua └── base.lua ├── LICENSE.txt ├── README.md └── glfw.lua /.gitignore: -------------------------------------------------------------------------------- 1 | *.dll 2 | *.exe 3 | *.sublime-project 4 | *.sublime-workspace 5 | -------------------------------------------------------------------------------- /examples/base_oo.lua: -------------------------------------------------------------------------------- 1 | local glfw = require 'glfw' ('glfw3') 2 | local GLFW = glfw.const 3 | 4 | -- Initialize the library 5 | if glfw.Init() == 0 then 6 | return 7 | end 8 | 9 | -- Create a windowed mode window and its OpenGL context 10 | local window = glfw.CreateWindow(640, 480, "Hello World") 11 | if window == GLFW.NULL then 12 | glfw.Terminate() 13 | return 14 | end 15 | 16 | -- Make the window's context current 17 | window:MakeContextCurrent() 18 | 19 | -- Loop until the user closes the window 20 | while window:ShouldClose() == 0 do 21 | -- Render here 22 | 23 | -- Swap front and back buffers 24 | window:SwapBuffers() 25 | 26 | -- Poll for and process events 27 | glfw.PollEvents() 28 | end 29 | 30 | glfw.Terminate() 31 | -------------------------------------------------------------------------------- /examples/base.lua: -------------------------------------------------------------------------------- 1 | local glfw = require 'glfw' ('glfw3') 2 | local GLFW = glfw.const 3 | 4 | -- Initialize the library 5 | if glfw.Init() == 0 then 6 | return 7 | end 8 | 9 | -- Create a windowed mode window and its OpenGL context 10 | local window = glfw.CreateWindow(640, 480, "Hello World") 11 | if window == GLFW.NULL then 12 | glfw.Terminate() 13 | return 14 | end 15 | 16 | -- Make the window's context current 17 | glfw.MakeContextCurrent(window) 18 | 19 | -- Loop until the user closes the window 20 | while glfw.WindowShouldClose(window) == 0 do 21 | -- Render here 22 | 23 | -- Swap front and back buffers 24 | glfw.SwapBuffers(window) 25 | 26 | -- Poll for and process events 27 | glfw.PollEvents() 28 | end 29 | 30 | glfw.Terminate() 31 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | # ----------------------------------------------------------------------------- 2 | # GLFW - An OpenGL framework 3 | # API version: 3.2.1 4 | # WWW: http://www.glfw.org/ 5 | # ---------------------------------------------------------------------------- 6 | # Copyright (c) 2002-2006 Marcus Geelnard 7 | # Copyright (c) 2006-2010 Camilla Berglund 8 | # 9 | # LuaJIT bindings - Copyright (c) 2017 Oleg Dudka 10 | # 11 | # This software is provided 'as-is', without any express or implied 12 | # warranty. In no event will the authors be held liable for any damages 13 | # arising from the use of this software. 14 | # 15 | # Permission is granted to anyone to use this software for any purpose, 16 | # including commercial applications, and to alter it and redistribute it 17 | # freely, subject to the following restrictions: 18 | # 19 | # 1. The origin of this software must not be misrepresented; you must not 20 | # claim that you wrote the original software. If you use this software 21 | # in a product, an acknowledgment in the product documentation would 22 | # be appreciated but is not required. 23 | # 24 | # 2. Altered source versions must be plainly marked as such, and must not 25 | # be misrepresented as being the original software. 26 | # 27 | # 3. This notice may not be removed or altered from any source 28 | # distribution. 29 | # 30 | # ----------------------------------------------------------------------------- 31 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Features 2 | - Full support of 3.4.0 API (except native functions) 3 | - Supports both Luajit and Lua with luaffi 4 | - Additional OO style for monitors, windows, and cursors 5 | ```lua 6 | glfw.ShowWindow(window) 7 | -- or 8 | window:Show() 9 | ``` 10 | - Constants can be used in two ways 11 | ```lua 12 | glfw.GetKey(window, GLFW.KEY_SPACE) 13 | -- or 14 | glfw.GetKey(window, 'KEY_SPACE') 15 | ``` 16 | 17 | # Differences from the C API 18 | Binding is so close to the original API as possible, but some things still differ. 19 | 1. Names lost 'glfw' prefix. 20 | 2. Arrays of chars replaced by lua string (except joystick buttons). 21 | 3. Arrays of structs and strings replaced by lua tables. 22 | 4. Values returned by reference replaced by returning table or multiple results. 23 | 5. Video mode returned as table. This may change in future if necessary. 24 | 6. Some OO methods have shortened names. 25 | 26 | # Start using 27 | Before calling glfw functions you need to initialize binding with library name or path. 28 | Luajit uses dynamic library loading API directly, so behaviour may be different on each OS. 29 | Filename and location of glfw library may also vary. 30 | Several examples: 31 | ```lua 32 | -- Windows 33 | local glfw = require 'glfw' ('glfw3') 34 | local glfw = require 'glfw' ('../some/path/glfw3.dll') 35 | -- Linux 36 | local glfw = require 'glfw' ('./libglfw3.so') 37 | local glfw = require 'glfw' ('/usr/local/lib/libglfw3.so') 38 | -- Mac OS X 39 | local glfw = require 'glfw' ('/opt/local/lib/libglfw.3.1.dylib') 40 | ``` 41 | For statically linked glfw, just skip argument. 42 | ```lua 43 | -- Any OS 44 | local glfw = require 'glfw' () 45 | ``` 46 | Constants stored in const table. It is recommended to save it in variable with GLFW name. 47 | ```lua 48 | local GLFW = glfw.const 49 | ``` 50 | If you need vulkan-related functions, then specify 'bind_vulkan' option. 51 | Vulkan types MUST be declared (manually or with other binding) before this. 52 | ```lua 53 | local glfw = require 'glfw' { 'glfw3', 54 | bind_vulkan = true 55 | } 56 | ``` 57 | If you use or want to support Lua+luaffi, then compare pointers with NULL only in this way: 58 | ```lua 59 | pointer == GLFW.NULL 60 | ``` 61 | 62 | # Quick example 63 | ```lua 64 | local glfw = require 'glfw' ('glfw3') 65 | local GLFW = glfw.const 66 | 67 | -- Initialize the library 68 | if glfw.Init() == 0 then 69 | return 70 | end 71 | 72 | -- Create a windowed mode window and its OpenGL context 73 | local window = glfw.CreateWindow(640, 480, "Hello World") 74 | if window == GLFW.NULL then 75 | glfw.Terminate() 76 | return 77 | end 78 | 79 | -- Make the window's context current 80 | glfw.MakeContextCurrent(window) 81 | 82 | -- Loop until the user closes the window 83 | while glfw.WindowShouldClose(window) == 0 do 84 | -- Render here 85 | 86 | -- Swap front and back buffers 87 | glfw.SwapBuffers(window) 88 | 89 | -- Poll for and process events 90 | glfw.PollEvents() 91 | end 92 | 93 | glfw.Terminate() 94 | ``` 95 | 96 | # Quick example with OO style 97 | ```lua 98 | local glfw = require 'glfw' ('glfw3') 99 | local GLFW = glfw.const 100 | 101 | -- Initialize the library 102 | if glfw.Init() == 0 then 103 | return 104 | end 105 | 106 | -- Create a windowed mode window and its OpenGL context 107 | local window = glfw.CreateWindow(640, 480, "Hello World") 108 | if window == GLFW.NULL then 109 | glfw.Terminate() 110 | return 111 | end 112 | 113 | -- Make the window's context current 114 | window:MakeContextCurrent() 115 | 116 | -- Loop until the user closes the window 117 | while window:ShouldClose() == 0 do 118 | -- Render here 119 | 120 | -- Swap front and back buffers 121 | window:SwapBuffers() 122 | 123 | -- Poll for and process events 124 | glfw.PollEvents() 125 | end 126 | 127 | glfw.Terminate() 128 | ``` 129 | 130 | # Other examples 131 | ```lua 132 | local version = glfw.GetVersion() 133 | print(version.major, version.minor, version.rev) 134 | 135 | local monitors = glfw.GetMonitors() 136 | for i = 1, #monitors do ... end 137 | 138 | local x,y = glfw.GetMonitorPos(monitor) 139 | local x,y = monitor:GetPos() 140 | 141 | local w,h = glfw.GetMonitorPhysicalSize(monitor) 142 | local w,h = monitor:GetPhysicalSize() 143 | 144 | local modes = glfw.GetVideoModes(monitor) 145 | local modes = monitor:GetVideoModes() 146 | for i = 1, #modes do ... end 147 | 148 | local mode = glfw.GetVideoMode(monitor) 149 | local mode = monitor:GetVideoMode() 150 | for k,v in pairs(mode) do 151 | print(k,v) 152 | end 153 | 154 | local x,y = glfw.GetWindowPos(window) 155 | local x,y = window:GetPos() 156 | 157 | local fsize = glfw.GetWindowFrameSize(window) 158 | local fsize = window:GetFrameSize() 159 | print(fsize.left, fsize.top, fsize.right, fsize.bottom) 160 | 161 | local axes = glfw.GetJoystickAxes(joy) 162 | for i = 1, #axes do ... end 163 | 164 | ``` 165 | 166 | # Acknowledgements 167 | - Jeremy Lucas ([@jerluc](https://github.com/jerluc)) 168 | - pocomane ([@pocomane](https://github.com/pocomane)) 169 | -------------------------------------------------------------------------------- /glfw.lua: -------------------------------------------------------------------------------- 1 | ----------------------------------------------------------- 2 | -- Binding for GLFW v3.4.0 3 | ----------------------------------------------------------- 4 | 5 | --[[ LICENSE 6 | The MIT License (MIT) 7 | 8 | luajit-glfw - GLFW binding for LuaJIT 9 | 10 | Copyright (c) 2024 Playermet 11 | 12 | Permission is hereby granted, free of charge, to any person obtaining a copy 13 | of this software and associated documentation files (the "Software"), to deal 14 | in the Software without restriction, including without limitation the rights 15 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 16 | copies of the Software, and to permit persons to whom the Software is 17 | furnished to do so, subject to the following conditions: 18 | 19 | The above copyright notice and this permission notice shall be included in all 20 | copies or substantial portions of the Software. 21 | 22 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 23 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 24 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 25 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 26 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 27 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 28 | SOFTWARE. 29 | ]] 30 | 31 | local ffi = require 'ffi' 32 | local bit = require 'bit' 33 | 34 | local mod = {} -- Lua module namespace 35 | local aux = {} -- Auxiliary utils 36 | 37 | local args -- Arguments for binding 38 | local clib -- C library namespace 39 | 40 | local is_luajit, jit = pcall(require, 'jit') 41 | 42 | 43 | local load_clib, bind_clib -- Forward declaration 44 | 45 | local function init(mod, name_or_args) 46 | if clib ~= nil then 47 | return mod 48 | end 49 | 50 | if type(name_or_args) == 'table' then 51 | args = name_or_args 52 | args.name = args.name or args[1] 53 | elseif type(name_or_args) == 'string' then 54 | args = {} 55 | args.name = name_or_args 56 | end 57 | 58 | clib = load_clib() 59 | bind_clib() 60 | 61 | return mod 62 | end 63 | 64 | function load_clib() 65 | if args.clib ~= nil then 66 | return args.clib 67 | end 68 | 69 | if type(args.name) == 'string' then 70 | if type(args.path) == 'string' then 71 | return ffi.load(package.searchpath(args.name, args.path)) 72 | else 73 | return ffi.load(args.name) 74 | end 75 | end 76 | 77 | -- If no library or name is provided, we just 78 | -- assume that the appropriate GLFW libraries 79 | -- are statically linked to the calling program 80 | return ffi.C 81 | end 82 | 83 | function bind_clib() 84 | ----------------------------------------------------------- 85 | -- Namespaces 86 | ----------------------------------------------------------- 87 | local const = {} -- Table for contants 88 | local funcs = {} -- Table for functions 89 | local types = {} -- Table for types 90 | local cbs = {} -- Table for callbacks 91 | 92 | mod.const = const 93 | mod.funcs = funcs 94 | mod.types = types 95 | mod.cbs = cbs 96 | mod.clib = clib 97 | 98 | -- Access to funcs from module namespace by default 99 | aux.set_mt_method(mod, '__index', funcs) 100 | 101 | ----------------------------------------------------------- 102 | -- Constants 103 | ----------------------------------------------------------- 104 | const.VERSION_MAJOR = 3 105 | const.VERSION_MINOR = 4 106 | const.VERSION_REVISION = 0 107 | 108 | const.TRUE = 1 109 | const.FALSE = 0 110 | 111 | const.RELEASE = 0 112 | const.PRESS = 1 113 | const.REPEAT = 2 114 | 115 | const.HAT_CENTERED = 0 116 | const.HAT_UP = 1 117 | const.HAT_RIGHT = 2 118 | const.HAT_DOWN = 4 119 | const.HAT_LEFT = 8 120 | const.HAT_RIGHT_UP = bit.bor(const.HAT_RIGHT, const.HAT_UP) 121 | const.HAT_RIGHT_DOWN = bit.bor(const.HAT_RIGHT, const.HAT_DOWN) 122 | const.HAT_LEFT_UP = bit.bor(const.HAT_LEFT, const.HAT_UP) 123 | const.HAT_LEFT_DOWN = bit.bor(const.HAT_LEFT, const.HAT_DOWN) 124 | 125 | const.KEY_UNKNOWN = -1 126 | 127 | const.KEY_SPACE = 32 128 | const.KEY_APOSTROPHE = 39 129 | const.KEY_COMMA = 44 130 | const.KEY_MINUS = 45 131 | const.KEY_PERIOD = 46 132 | const.KEY_SLASH = 47 133 | const.KEY_0 = 48 134 | const.KEY_1 = 49 135 | const.KEY_2 = 50 136 | const.KEY_3 = 51 137 | const.KEY_4 = 52 138 | const.KEY_5 = 53 139 | const.KEY_6 = 54 140 | const.KEY_7 = 55 141 | const.KEY_8 = 56 142 | const.KEY_9 = 57 143 | const.KEY_SEMICOLON = 59 144 | const.KEY_EQUAL = 61 145 | const.KEY_A = 65 146 | const.KEY_B = 66 147 | const.KEY_C = 67 148 | const.KEY_D = 68 149 | const.KEY_E = 69 150 | const.KEY_F = 70 151 | const.KEY_G = 71 152 | const.KEY_H = 72 153 | const.KEY_I = 73 154 | const.KEY_J = 74 155 | const.KEY_K = 75 156 | const.KEY_L = 76 157 | const.KEY_M = 77 158 | const.KEY_N = 78 159 | const.KEY_O = 79 160 | const.KEY_P = 80 161 | const.KEY_Q = 81 162 | const.KEY_R = 82 163 | const.KEY_S = 83 164 | const.KEY_T = 84 165 | const.KEY_U = 85 166 | const.KEY_V = 86 167 | const.KEY_W = 87 168 | const.KEY_X = 88 169 | const.KEY_Y = 89 170 | const.KEY_Z = 90 171 | const.KEY_LEFT_BRACKET = 91 172 | const.KEY_BACKSLASH = 92 173 | const.KEY_RIGHT_BRACKET = 93 174 | const.KEY_GRAVE_ACCENT = 96 175 | const.KEY_WORLD_1 = 161 176 | const.KEY_WORLD_2 = 162 177 | 178 | const.KEY_ESCAPE = 256 179 | const.KEY_ENTER = 257 180 | const.KEY_TAB = 258 181 | const.KEY_BACKSPACE = 259 182 | const.KEY_INSERT = 260 183 | const.KEY_DELETE = 261 184 | const.KEY_RIGHT = 262 185 | const.KEY_LEFT = 263 186 | const.KEY_DOWN = 264 187 | const.KEY_UP = 265 188 | const.KEY_PAGE_UP = 266 189 | const.KEY_PAGE_DOWN = 267 190 | const.KEY_HOME = 268 191 | const.KEY_END = 269 192 | const.KEY_CAPS_LOCK = 280 193 | const.KEY_SCROLL_LOCK = 281 194 | const.KEY_NUM_LOCK = 282 195 | const.KEY_PRINT_SCREEN = 283 196 | const.KEY_PAUSE = 284 197 | const.KEY_F1 = 290 198 | const.KEY_F2 = 291 199 | const.KEY_F3 = 292 200 | const.KEY_F4 = 293 201 | const.KEY_F5 = 294 202 | const.KEY_F6 = 295 203 | const.KEY_F7 = 296 204 | const.KEY_F8 = 297 205 | const.KEY_F9 = 298 206 | const.KEY_F10 = 299 207 | const.KEY_F11 = 300 208 | const.KEY_F12 = 301 209 | const.KEY_F13 = 302 210 | const.KEY_F14 = 303 211 | const.KEY_F15 = 304 212 | const.KEY_F16 = 305 213 | const.KEY_F17 = 306 214 | const.KEY_F18 = 307 215 | const.KEY_F19 = 308 216 | const.KEY_F20 = 309 217 | const.KEY_F21 = 310 218 | const.KEY_F22 = 311 219 | const.KEY_F23 = 312 220 | const.KEY_F24 = 313 221 | const.KEY_F25 = 314 222 | const.KEY_KP_0 = 320 223 | const.KEY_KP_1 = 321 224 | const.KEY_KP_2 = 322 225 | const.KEY_KP_3 = 323 226 | const.KEY_KP_4 = 324 227 | const.KEY_KP_5 = 325 228 | const.KEY_KP_6 = 326 229 | const.KEY_KP_7 = 327 230 | const.KEY_KP_8 = 328 231 | const.KEY_KP_9 = 329 232 | const.KEY_KP_DECIMAL = 330 233 | const.KEY_KP_DIVIDE = 331 234 | const.KEY_KP_MULTIPLY = 332 235 | const.KEY_KP_SUBTRACT = 333 236 | const.KEY_KP_ADD = 334 237 | const.KEY_KP_ENTER = 335 238 | const.KEY_KP_EQUAL = 336 239 | const.KEY_LEFT_SHIFT = 340 240 | const.KEY_LEFT_CONTROL = 341 241 | const.KEY_LEFT_ALT = 342 242 | const.KEY_LEFT_SUPER = 343 243 | const.KEY_RIGHT_SHIFT = 344 244 | const.KEY_RIGHT_CONTROL = 345 245 | const.KEY_RIGHT_ALT = 346 246 | const.KEY_RIGHT_SUPER = 347 247 | const.KEY_MENU = 348 248 | 249 | const.KEY_LAST = const.KEY_MENU 250 | 251 | const.MOD_SHIFT = 0x0001 252 | const.MOD_CONTROL = 0x0002 253 | const.MOD_ALT = 0x0004 254 | const.MOD_SUPER = 0x0008 255 | const.MOD_CAPS_LOCK = 0x0010 256 | const.MOD_NUM_LOCK = 0x0020 257 | 258 | const.MOUSE_BUTTON_1 = 0 259 | const.MOUSE_BUTTON_2 = 1 260 | const.MOUSE_BUTTON_3 = 2 261 | const.MOUSE_BUTTON_4 = 3 262 | const.MOUSE_BUTTON_5 = 4 263 | const.MOUSE_BUTTON_6 = 5 264 | const.MOUSE_BUTTON_7 = 6 265 | const.MOUSE_BUTTON_8 = 7 266 | const.MOUSE_BUTTON_LAST = const.MOUSE_BUTTON_8 267 | const.MOUSE_BUTTON_LEFT = const.MOUSE_BUTTON_1 268 | const.MOUSE_BUTTON_RIGHT = const.MOUSE_BUTTON_2 269 | const.MOUSE_BUTTON_MIDDLE = const.MOUSE_BUTTON_3 270 | 271 | const.JOYSTICK_1 = 0 272 | const.JOYSTICK_2 = 1 273 | const.JOYSTICK_3 = 2 274 | const.JOYSTICK_4 = 3 275 | const.JOYSTICK_5 = 4 276 | const.JOYSTICK_6 = 5 277 | const.JOYSTICK_7 = 6 278 | const.JOYSTICK_8 = 7 279 | const.JOYSTICK_9 = 8 280 | const.JOYSTICK_10 = 9 281 | const.JOYSTICK_11 = 10 282 | const.JOYSTICK_12 = 11 283 | const.JOYSTICK_13 = 12 284 | const.JOYSTICK_14 = 13 285 | const.JOYSTICK_15 = 14 286 | const.JOYSTICK_16 = 15 287 | const.JOYSTICK_LAST = const.JOYSTICK_16 288 | 289 | const.GAMEPAD_BUTTON_A = 0 290 | const.GAMEPAD_BUTTON_B = 1 291 | const.GAMEPAD_BUTTON_X = 2 292 | const.GAMEPAD_BUTTON_Y = 3 293 | const.GAMEPAD_BUTTON_LEFT_BUMPER = 4 294 | const.GAMEPAD_BUTTON_RIGHT_BUMPER = 5 295 | const.GAMEPAD_BUTTON_BACK = 6 296 | const.GAMEPAD_BUTTON_START = 7 297 | const.GAMEPAD_BUTTON_GUIDE = 8 298 | const.GAMEPAD_BUTTON_LEFT_THUMB = 9 299 | const.GAMEPAD_BUTTON_RIGHT_THUMB = 10 300 | const.GAMEPAD_BUTTON_DPAD_UP = 11 301 | const.GAMEPAD_BUTTON_DPAD_RIGHT = 12 302 | const.GAMEPAD_BUTTON_DPAD_DOWN = 13 303 | const.GAMEPAD_BUTTON_DPAD_LEFT = 14 304 | const.GAMEPAD_BUTTON_LAST = const.GAMEPAD_BUTTON_DPAD_LEFT 305 | 306 | const.GAMEPAD_BUTTON_CROSS = const.GAMEPAD_BUTTON_A 307 | const.GAMEPAD_BUTTON_CIRCLE = const.GAMEPAD_BUTTON_B 308 | const.GAMEPAD_BUTTON_SQUARE = const.GAMEPAD_BUTTON_X 309 | const.GAMEPAD_BUTTON_TRIANGLE = const.GAMEPAD_BUTTON_Y 310 | 311 | const.GAMEPAD_AXIS_LEFT_X = 0 312 | const.GAMEPAD_AXIS_LEFT_Y = 1 313 | const.GAMEPAD_AXIS_RIGHT_X = 2 314 | const.GAMEPAD_AXIS_RIGHT_Y = 3 315 | const.GAMEPAD_AXIS_LEFT_TRIGGER = 4 316 | const.GAMEPAD_AXIS_RIGHT_TRIGGER = 5 317 | const.GAMEPAD_AXIS_LAST = const.GAMEPAD_AXIS_RIGHT_TRIGGER 318 | 319 | const.NO_ERROR = 0 320 | 321 | const.NOT_INITIALIZED = 0x00010001 322 | const.NO_CURRENT_CONTEXT = 0x00010002 323 | const.INVALID_ENUM = 0x00010003 324 | const.INVALID_VALUE = 0x00010004 325 | const.OUT_OF_MEMORY = 0x00010005 326 | const.API_UNAVAILABLE = 0x00010006 327 | const.VERSION_UNAVAILABLE = 0x00010007 328 | const.PLATFORM_ERROR = 0x00010008 329 | const.FORMAT_UNAVAILABLE = 0x00010009 330 | const.NO_WINDOW_CONTEXT = 0x0001000a 331 | const.CURSOR_UNAVAILABLE = 0x0001000b 332 | const.FEATURE_UNAVAILABLE = 0x0001000c 333 | const.FEATURE_UNIMPLEMENTED = 0x0001000d 334 | const.PLATFORM_UNAVAILABLE = 0x0001000e 335 | 336 | const.FOCUSED = 0x00020001 337 | const.ICONIFIED = 0x00020002 338 | const.RESIZABLE = 0x00020003 339 | const.VISIBLE = 0x00020004 340 | const.DECORATED = 0x00020005 341 | const.AUTO_ICONIFY = 0x00020006 342 | const.FLOATING = 0x00020007 343 | const.MAXIMIZED = 0x00020008 344 | const.CENTER_CURSOR = 0x00020009 345 | const.TRANSPARENT_FRAMEBUFFER = 0x0002000a 346 | const.HOVERED = 0x0002000b 347 | const.FOCUS_ON_SHOW = 0x0002000c 348 | const.MOUSE_PASSTHROUGH = 0x0002000d 349 | const.POSITION_X = 0x0002000e 350 | const.POSITION_Y = 0x0002000f 351 | 352 | const.RED_BITS = 0x00021001 353 | const.GREEN_BITS = 0x00021002 354 | const.BLUE_BITS = 0x00021003 355 | const.ALPHA_BITS = 0x00021004 356 | const.DEPTH_BITS = 0x00021005 357 | const.STENCIL_BITS = 0x00021006 358 | const.ACCUM_RED_BITS = 0x00021007 359 | const.ACCUM_GREEN_BITS = 0x00021008 360 | const.ACCUM_BLUE_BITS = 0x00021009 361 | const.ACCUM_ALPHA_BITS = 0x0002100a 362 | const.AUX_BUFFERS = 0x0002100b 363 | const.STEREO = 0x0002100c 364 | const.SAMPLES = 0x0002100d 365 | const.SRGB_CAPABLE = 0x0002100e 366 | const.REFRESH_RATE = 0x0002100f 367 | const.DOUBLEBUFFER = 0x00021010 368 | 369 | const.CLIENT_API = 0x00022001 370 | const.CONTEXT_VERSION_MAJOR = 0x00022002 371 | const.CONTEXT_VERSION_MINOR = 0x00022003 372 | const.CONTEXT_REVISION = 0x00022004 373 | const.CONTEXT_ROBUSTNESS = 0x00022005 374 | const.OPENGL_FORWARD_COMPAT = 0x00022006 375 | const.CONTEXT_DEBUG = 0x00022007 376 | const.OPENGL_DEBUG_CONTEXT = const.CONTEXT_DEBUG 377 | const.OPENGL_PROFILE = 0x00022008 378 | const.CONTEXT_RELEASE_BEHAVIOR = 0x00022009 379 | const.CONTEXT_NO_ERROR = 0x0002200a 380 | const.CONTEXT_CREATION_API = 0x0002200b 381 | const.SCALE_TO_MONITOR = 0x0002200c 382 | const.SCALE_FRAMEBUFFER = 0x0002200d 383 | const.COCOA_RETINA_FRAMEBUFFER = 0x00023001 384 | const.COCOA_FRAME_NAME = 0x00023002 385 | const.COCOA_GRAPHICS_SWITCHING = 0x00023003 386 | const.X11_CLASS_NAME = 0x00024001 387 | const.X11_INSTANCE_NAME = 0x00024002 388 | const.WIN32_KEYBOARD_MENU = 0x00025001 389 | const.WIN32_SHOWDEFAULT = 0x00025002 390 | const.WAYLAND_APP_ID = 0x00026001 391 | 392 | const.NO_API = 0 393 | const.OPENGL_API = 0x00030001 394 | const.OPENGL_ES_API = 0x00030002 395 | 396 | const.NO_ROBUSTNESS = 0 397 | const.NO_RESET_NOTIFICATION = 0x00031001 398 | const.LOSE_CONTEXT_ON_RESET = 0x00031002 399 | 400 | const.OPENGL_ANY_PROFILE = 0 401 | const.OPENGL_CORE_PROFILE = 0x00032001 402 | const.OPENGL_COMPAT_PROFILE = 0x00032002 403 | 404 | const.CURSOR = 0x00033001 405 | const.STICKY_KEYS = 0x00033002 406 | const.STICKY_MOUSE_BUTTONS = 0x00033003 407 | const.LOCK_KEY_MODS = 0x00033004 408 | const.RAW_MOUSE_MOTION = 0x00033005 409 | 410 | const.CURSOR_NORMAL = 0x00034001 411 | const.CURSOR_HIDDEN = 0x00034002 412 | const.CURSOR_DISABLED = 0x00034003 413 | const.CURSOR_CAPTURED = 0x00034004 414 | 415 | const.ANY_RELEASE_BEHAVIOR = 0 416 | const.RELEASE_BEHAVIOR_FLUSH = 0x00035001 417 | const.RELEASE_BEHAVIOR_NONE = 0x00035002 418 | 419 | const.NATIVE_CONTEXT_API = 0x00036001 420 | const.EGL_CONTEXT_API = 0x00036002 421 | const.OSMESA_CONTEXT_API = 0x00036003 422 | 423 | const.ANGLE_PLATFORM_TYPE_NONE = 0x00037001 424 | const.ANGLE_PLATFORM_TYPE_OPENGL = 0x00037002 425 | const.ANGLE_PLATFORM_TYPE_OPENGLES = 0x00037003 426 | const.ANGLE_PLATFORM_TYPE_D3D9 = 0x00037004 427 | const.ANGLE_PLATFORM_TYPE_D3D11 = 0x00037005 428 | const.ANGLE_PLATFORM_TYPE_VULKAN = 0x00037007 429 | const.ANGLE_PLATFORM_TYPE_METAL = 0x00037008 430 | 431 | const.WAYLAND_PREFER_LIBDECOR = 0x00038001 432 | const.WAYLAND_DISABLE_LIBDECOR = 0x00038002 433 | 434 | const.ANY_POSITION = 0x80000000 435 | 436 | const.ARROW_CURSOR = 0x00036001 437 | const.IBEAM_CURSOR = 0x00036002 438 | const.CROSSHAIR_CURSOR = 0x00036003 439 | const.POINTING_HAND_CURSOR = 0x00036004 440 | const.RESIZE_EW_CURSOR = 0x00036005 441 | const.RESIZE_NS_CURSOR = 0x00036006 442 | const.RESIZE_NWSE_CURSOR = 0x00036007 443 | const.RESIZE_NESW_CURSOR = 0x00036008 444 | const.RESIZE_ALL_CURSOR = 0x00036009 445 | const.NOT_ALLOWED_CURSOR = 0x0003600a 446 | const.HRESIZE_CURSOR = const.RESIZE_EW_CURSOR 447 | const.VRESIZE_CURSOR = const.RESIZE_NS_CURSOR 448 | const.HAND_CURSOR = const.POINTING_HAND_CURSOR 449 | 450 | const.CONNECTED = 0x00040001 451 | const.DISCONNECTED = 0x00040002 452 | 453 | const.JOYSTICK_HAT_BUTTONS = 0x00050001 454 | const.ANGLE_PLATFORM_TYPE = 0x00050002 455 | const.PLATFORM = 0x00050003 456 | const.COCOA_CHDIR_RESOURCES = 0x00051001 457 | const.COCOA_MENUBAR = 0x00051002 458 | const.X11_XCB_VULKAN_SURFACE = 0x00052001 459 | const.WAYLAND_LIBDECOR = 0x00053001 460 | 461 | const.ANY_PLATFORM = 0x00060000 462 | const.PLATFORM_WIN32 = 0x00060001 463 | const.PLATFORM_COCOA = 0x00060002 464 | const.PLATFORM_WAYLAND = 0x00060003 465 | const.PLATFORM_X11 = 0x00060004 466 | const.PLATFORM_NULL = 0x00060005 467 | 468 | const.DONT_CARE = -1 469 | 470 | if not is_luajit then 471 | const.NULL = ffi.C.NULL 472 | end 473 | 474 | ----------------------------------------------------------- 475 | -- Headers 476 | ----------------------------------------------------------- 477 | ffi.cdef [[ 478 | typedef void (*GLFWglproc)(void); 479 | typedef void (*GLFWvkproc)(void); 480 | 481 | typedef struct GLFWmonitor GLFWmonitor; 482 | typedef struct GLFWwindow GLFWwindow; 483 | typedef struct GLFWcursor GLFWcursor; 484 | 485 | typedef void* (* GLFWallocatefun)(size_t,void*); 486 | typedef void* (* GLFWreallocatefun)(void*,size_t,void*); 487 | typedef void (* GLFWdeallocatefun)(void* block,void*); 488 | typedef void (* GLFWerrorfun)(int,const char*); 489 | typedef void (* GLFWwindowposfun)(GLFWwindow*,int,int); 490 | typedef void (* GLFWwindowsizefun)(GLFWwindow*,int,int); 491 | typedef void (* GLFWwindowclosefun)(GLFWwindow*); 492 | typedef void (* GLFWwindowrefreshfun)(GLFWwindow*); 493 | typedef void (* GLFWwindowfocusfun)(GLFWwindow*,int); 494 | typedef void (* GLFWwindowiconifyfun)(GLFWwindow*,int); 495 | typedef void (* GLFWwindowmaximizefun)(GLFWwindow*,int); 496 | typedef void (* GLFWframebuffersizefun)(GLFWwindow*,int,int); 497 | typedef void (* GLFWwindowcontentscalefun)(GLFWwindow*,float,float); 498 | typedef void (* GLFWmousebuttonfun)(GLFWwindow*,int,int,int); 499 | typedef void (* GLFWcursorposfun)(GLFWwindow*,double,double); 500 | typedef void (* GLFWcursorenterfun)(GLFWwindow*,int); 501 | typedef void (* GLFWscrollfun)(GLFWwindow*,double,double); 502 | typedef void (* GLFWkeyfun)(GLFWwindow*,int,int,int,int); 503 | typedef void (* GLFWcharfun)(GLFWwindow*,unsigned int); 504 | typedef void (* GLFWcharmodsfun)(GLFWwindow*,unsigned int,int); 505 | typedef void (* GLFWdropfun)(GLFWwindow*,int,const char**); 506 | typedef void (* GLFWmonitorfun)(GLFWmonitor*,int); 507 | typedef void (* GLFWjoystickfun)(int,int); 508 | 509 | typedef struct GLFWvidmode 510 | { 511 | int width; 512 | int height; 513 | int redBits; 514 | int greenBits; 515 | int blueBits; 516 | int refreshRate; 517 | } GLFWvidmode; 518 | 519 | typedef struct GLFWgammaramp 520 | { 521 | unsigned short* red; 522 | unsigned short* green; 523 | unsigned short* blue; 524 | unsigned int size; 525 | } GLFWgammaramp; 526 | 527 | typedef struct GLFWimage 528 | { 529 | int width; 530 | int height; 531 | unsigned char* pixels; 532 | } GLFWimage; 533 | 534 | typedef struct GLFWgamepadstate 535 | { 536 | unsigned char buttons[15]; 537 | float axes[6]; 538 | } GLFWgamepadstate; 539 | 540 | typedef struct GLFWallocator 541 | { 542 | GLFWallocatefun allocate; 543 | GLFWreallocatefun reallocate; 544 | GLFWdeallocatefun deallocate; 545 | void* user; 546 | } GLFWallocator; 547 | 548 | int glfwInit(void); 549 | void glfwTerminate(void); 550 | void glfwInitHint(int hint, int value); 551 | void glfwInitAllocator(const GLFWallocator* allocator); 552 | void glfwGetVersion(int* major, int* minor, int* rev); 553 | const char* glfwGetVersionString(void); 554 | int glfwGetError(const char** description); 555 | GLFWerrorfun glfwSetErrorCallback(GLFWerrorfun cbfun); 556 | int glfwGetPlatform(void); 557 | int glfwPlatformSupported(int platform); 558 | GLFWmonitor** glfwGetMonitors(int* count); 559 | GLFWmonitor* glfwGetPrimaryMonitor(void); 560 | void glfwGetMonitorPos(GLFWmonitor* monitor, int* xpos, int* ypos); 561 | void glfwGetMonitorWorkarea(GLFWmonitor* monitor, int* xpos, int* ypos, int* width, int* height); 562 | void glfwGetMonitorPhysicalSize(GLFWmonitor* monitor, int* widthMM, int* heightMM); 563 | void glfwGetMonitorContentScale(GLFWmonitor* monitor, float* xscale, float* yscale); 564 | const char* glfwGetMonitorName(GLFWmonitor* monitor); 565 | void glfwSetMonitorUserPointer(GLFWmonitor* monitor, void* pointer); 566 | void* glfwGetMonitorUserPointer(GLFWmonitor* monitor); 567 | GLFWmonitorfun glfwSetMonitorCallback(GLFWmonitorfun cbfun); 568 | const GLFWvidmode* glfwGetVideoModes(GLFWmonitor* monitor, int* count); 569 | const GLFWvidmode* glfwGetVideoMode(GLFWmonitor* monitor); 570 | void glfwSetGamma(GLFWmonitor* monitor, float gamma); 571 | const GLFWgammaramp* glfwGetGammaRamp(GLFWmonitor* monitor); 572 | void glfwSetGammaRamp(GLFWmonitor* monitor, const GLFWgammaramp* ramp); 573 | void glfwDefaultWindowHints(void); 574 | void glfwWindowHint(int hint, int value); 575 | void glfwWindowHintString(int hint, const char* value); 576 | GLFWwindow* glfwCreateWindow(int width, int height, const char* title, GLFWmonitor* monitor, GLFWwindow* share); 577 | void glfwDestroyWindow(GLFWwindow* window); 578 | int glfwWindowShouldClose(GLFWwindow* window); 579 | void glfwSetWindowShouldClose(GLFWwindow* window, int value); 580 | const char* glfwGetWindowTitle(GLFWwindow* window); 581 | void glfwSetWindowTitle(GLFWwindow* window, const char* title); 582 | void glfwSetWindowIcon(GLFWwindow* window, int count, const GLFWimage* images); 583 | void glfwGetWindowPos(GLFWwindow* window, int* xpos, int* ypos); 584 | void glfwSetWindowPos(GLFWwindow* window, int xpos, int ypos); 585 | void glfwGetWindowSize(GLFWwindow* window, int* width, int* height); 586 | void glfwSetWindowSizeLimits(GLFWwindow* window, int minwidth, int minheight, int maxwidth, int maxheight); 587 | void glfwSetWindowAspectRatio(GLFWwindow* window, int numer, int denom); 588 | void glfwSetWindowSize(GLFWwindow* window, int width, int height); 589 | void glfwGetFramebufferSize(GLFWwindow* window, int* width, int* height); 590 | void glfwGetWindowFrameSize(GLFWwindow* window, int* left, int* top, int* right, int* bottom); 591 | void glfwGetWindowContentScale(GLFWwindow* window, float* xscale, float* yscale); 592 | float glfwGetWindowOpacity(GLFWwindow* window); 593 | void glfwSetWindowOpacity(GLFWwindow* window, float opacity); 594 | void glfwIconifyWindow(GLFWwindow* window); 595 | void glfwRestoreWindow(GLFWwindow* window); 596 | void glfwMaximizeWindow(GLFWwindow* window); 597 | void glfwShowWindow(GLFWwindow* window); 598 | void glfwHideWindow(GLFWwindow* window); 599 | void glfwFocusWindow(GLFWwindow* window); 600 | void glfwRequestWindowAttention(GLFWwindow* window); 601 | GLFWmonitor* glfwGetWindowMonitor(GLFWwindow* window); 602 | void glfwSetWindowMonitor(GLFWwindow* window, GLFWmonitor* monitor, int xpos, int ypos, int width, int height, int refreshRate); 603 | int glfwGetWindowAttrib(GLFWwindow* window, int attrib); 604 | void glfwSetWindowAttrib(GLFWwindow* window, int attrib, int value); 605 | void glfwSetWindowUserPointer(GLFWwindow* window, void* pointer); 606 | void* glfwGetWindowUserPointer(GLFWwindow* window); 607 | GLFWwindowposfun glfwSetWindowPosCallback(GLFWwindow* window, GLFWwindowposfun cbfun); 608 | GLFWwindowsizefun glfwSetWindowSizeCallback(GLFWwindow* window, GLFWwindowsizefun cbfun); 609 | GLFWwindowclosefun glfwSetWindowCloseCallback(GLFWwindow* window, GLFWwindowclosefun cbfun); 610 | GLFWwindowrefreshfun glfwSetWindowRefreshCallback(GLFWwindow* window, GLFWwindowrefreshfun cbfun); 611 | GLFWwindowfocusfun glfwSetWindowFocusCallback(GLFWwindow* window, GLFWwindowfocusfun cbfun); 612 | GLFWwindowiconifyfun glfwSetWindowIconifyCallback(GLFWwindow* window, GLFWwindowiconifyfun cbfun); 613 | GLFWwindowmaximizefun glfwSetWindowMaximizeCallback(GLFWwindow* window, GLFWwindowmaximizefun cbfun); 614 | GLFWframebuffersizefun glfwSetFramebufferSizeCallback(GLFWwindow* window, GLFWframebuffersizefun cbfun); 615 | GLFWwindowcontentscalefun glfwSetWindowContentScaleCallback(GLFWwindow* window, GLFWwindowcontentscalefun cbfun); 616 | void glfwPollEvents(void); 617 | void glfwWaitEvents(void); 618 | void glfwWaitEventsTimeout(double timeout); 619 | void glfwPostEmptyEvent(void); 620 | int glfwGetInputMode(GLFWwindow* window, int mode); 621 | void glfwSetInputMode(GLFWwindow* window, int mode, int value); 622 | int glfwRawMouseMotionSupported(void); 623 | const char* glfwGetKeyName(int key, int scancode); 624 | int glfwGetKeyScancode(int key); 625 | int glfwGetKey(GLFWwindow* window, int key); 626 | int glfwGetMouseButton(GLFWwindow* window, int button); 627 | void glfwGetCursorPos(GLFWwindow* window, double* xpos, double* ypos); 628 | void glfwSetCursorPos(GLFWwindow* window, double xpos, double ypos); 629 | GLFWcursor* glfwCreateCursor(const GLFWimage* image, int xhot, int yhot); 630 | GLFWcursor* glfwCreateStandardCursor(int shape); 631 | void glfwDestroyCursor(GLFWcursor* cursor); 632 | void glfwSetCursor(GLFWwindow* window, GLFWcursor* cursor); 633 | GLFWkeyfun glfwSetKeyCallback(GLFWwindow* window, GLFWkeyfun cbfun); 634 | GLFWcharfun glfwSetCharCallback(GLFWwindow* window, GLFWcharfun cbfun); 635 | GLFWcharmodsfun glfwSetCharModsCallback(GLFWwindow* window, GLFWcharmodsfun cbfun); 636 | GLFWmousebuttonfun glfwSetMouseButtonCallback(GLFWwindow* window, GLFWmousebuttonfun cbfun); 637 | GLFWcursorposfun glfwSetCursorPosCallback(GLFWwindow* window, GLFWcursorposfun cbfun); 638 | GLFWcursorenterfun glfwSetCursorEnterCallback(GLFWwindow* window, GLFWcursorenterfun cbfun); 639 | GLFWscrollfun glfwSetScrollCallback(GLFWwindow* window, GLFWscrollfun cbfun); 640 | GLFWdropfun glfwSetDropCallback(GLFWwindow* window, GLFWdropfun cbfun); 641 | int glfwJoystickPresent(int jid); 642 | const float* glfwGetJoystickAxes(int jid, int* count); 643 | const unsigned char* glfwGetJoystickButtons(int jid, int* count); 644 | const unsigned char* glfwGetJoystickHats(int jid, int* count); 645 | const char* glfwGetJoystickName(int jid); 646 | const char* glfwGetJoystickGUID(int jid); 647 | void glfwSetJoystickUserPointer(int jid, void* pointer); 648 | void* glfwGetJoystickUserPointer(int jid); 649 | int glfwJoystickIsGamepad(int jid); 650 | GLFWjoystickfun glfwSetJoystickCallback(GLFWjoystickfun cbfun); 651 | int glfwUpdateGamepadMappings(const char* string); 652 | const char* glfwGetGamepadName(int jid); 653 | int glfwGetGamepadState(int jid, GLFWgamepadstate* state); 654 | void glfwSetClipboardString(GLFWwindow* window, const char* string); 655 | const char* glfwGetClipboardString(GLFWwindow* window); 656 | double glfwGetTime(void); 657 | void glfwSetTime(double time); 658 | uint64_t glfwGetTimerValue(void); 659 | uint64_t glfwGetTimerFrequency(void); 660 | void glfwMakeContextCurrent(GLFWwindow* window); 661 | GLFWwindow* glfwGetCurrentContext(void); 662 | void glfwSwapBuffers(GLFWwindow* window); 663 | void glfwSwapInterval(int interval); 664 | int glfwExtensionSupported(const char* extension); 665 | GLFWglproc glfwGetProcAddress(const char* procname); 666 | ]] 667 | 668 | if args.bind_vulkan == true then 669 | ffi.cdef [[ 670 | int glfwVulkanSupported(void); 671 | void glfwInitVulkanLoader(PFN_vkGetInstanceProcAddr loader); 672 | const char** glfwGetRequiredInstanceExtensions(uint32_t* count); 673 | GLFWvkproc glfwGetInstanceProcAddress(VkInstance instance, const char* procname); 674 | int glfwGetPhysicalDevicePresentationSupport(VkInstance instance, VkPhysicalDevice device, uint32_t queuefamily); 675 | VkResult glfwCreateWindowSurface(VkInstance instance, GLFWwindow* window, const VkAllocationCallbacks* allocator, VkSurfaceKHR* surface); 676 | ]] 677 | end 678 | 679 | ----------------------------------------------------------- 680 | -- Functions wrappers 681 | ----------------------------------------------------------- 682 | function funcs.Init() 683 | return clib.glfwInit() 684 | end 685 | 686 | function funcs.Terminate() 687 | clib.glfwTerminate() 688 | end 689 | 690 | function funcs.InitHint(hint, value) 691 | clib.glfwInitHint(aux.get_const(const, hint), aux.get_const(const, value)) 692 | end 693 | 694 | function funcs.InitAllocator(allocator) 695 | if type(allocator) == 'table' then 696 | local callocator = ffi.new('GLFWallocator') 697 | callocator.allocate = aux.wrap_cb(cbs, allocator.allocate, 'allocatefun') 698 | callocator.reallocate = aux.wrap_cb(cbs, allocator.reallocate, 'reallocatefun') 699 | callocator.deallocate = aux.wrap_cb(cbs, allocator.deallocate, 'deallocatefun') 700 | callocator.user = allocator.user 701 | allocator = callocator 702 | end 703 | clib.glfwInitAllocator(allocator) 704 | end 705 | 706 | function funcs.GetVersion() 707 | local major = ffi.new('int[1]') 708 | local minor = ffi.new('int[1]') 709 | local rev = ffi.new('int[1]') 710 | 711 | clib.glfwGetVersion(major, minor, rev) 712 | 713 | local version = { 714 | major = major[0], 715 | minor = minor[0], 716 | rev = rev[0] 717 | } 718 | 719 | return version 720 | end 721 | 722 | function funcs.GetVersionString() 723 | return ffi.string(clib.glfwGetVersionString()) 724 | end 725 | 726 | function funcs.GetError() 727 | local description = ffi.new('const char*[1]') 728 | local err = clib.glfwGetError(description) 729 | return err, ffi.string(description[0]) 730 | end 731 | 732 | function funcs.SetErrorCallback(cbfun) 733 | cbfun = aux.wrap_cb(cbs, cbfun, 'errorfun') 734 | return clib.glfwSetErrorCallback(cbfun) 735 | end 736 | 737 | function funcs.GetPlatform() 738 | return clib.glfwGetPlatform() 739 | end 740 | 741 | function funcs.PlatformSupported(platform) 742 | return clib.glfwPlatformSupported(aux.get_const(const, platform)) 743 | end 744 | 745 | function funcs.GetMonitors(monitors) 746 | local count = ffi.new('int[1]') 747 | local cmonitors = clib.glfwGetMonitors(count) 748 | 749 | monitors = monitors or {} 750 | for i = 1, count do 751 | monitors[i] = cmonitors[i - 1] 752 | end 753 | 754 | return monitors 755 | end 756 | 757 | function funcs.GetPrimaryMonitor() 758 | return clib.glfwGetPrimaryMonitor() 759 | end 760 | 761 | function funcs.GetMonitorPos(monitor) 762 | local xpos = ffi.new('int[1]') 763 | local ypos = ffi.new('int[1]') 764 | 765 | clib.glfwGetMonitorPos(monitor, xpos, ypos) 766 | 767 | return xpos[0], ypos[0] 768 | end 769 | 770 | function funcs.GetMonitorWorkarea(window, out) 771 | local xpos = ffi.new('int[1]') 772 | local ypos = ffi.new('int[1]') 773 | local width = ffi.new('int[1]') 774 | local height = ffi.new('int[1]') 775 | 776 | clib.glfwGetMonitorWorkarea(window, xpos, ypos, width, height) 777 | 778 | out = out or {} 779 | out.xpos = xpos[0] 780 | out.ypos = ypos[0] 781 | out.width = width[0] 782 | out.height = height[0] 783 | 784 | return out 785 | end 786 | 787 | function funcs.GetMonitorPhysicalSize(monitor) 788 | local width = ffi.new('int[1]') 789 | local height = ffi.new('int[1]') 790 | 791 | clib.glfwGetMonitorPhysicalSize(monitor, width, height) 792 | 793 | return width[0], height[0] 794 | end 795 | 796 | function funcs.GetMonitorContentScale(monitor) 797 | local xscale = ffi.new('float[1]') 798 | local yscale = ffi.new('float[1]') 799 | 800 | clib.glfwGetMonitorContentScale(monitor, xscale, yscale) 801 | 802 | return xscale[0], yscale[0] 803 | end 804 | 805 | function funcs.GetMonitorName(monitor) 806 | return aux.string_or_nil(const, clib.glfwGetMonitorName(monitor)) 807 | end 808 | 809 | function funcs.SetMonitorUserPointer(monitor, pointer) 810 | clib.glfwSetMonitorUserPointer(monitor, pointer) 811 | end 812 | 813 | function funcs.GetMonitorUserPointer(monitor) 814 | return clib.glfwGetMonitorUserPointer(monitor) 815 | end 816 | 817 | function funcs.SetMonitorCallback(cbfun) 818 | cbfun = aux.wrap_cb(cbs, cbfun, 'monitorfun') 819 | return clib.glfwSetMonitorCallback(cbfun) 820 | end 821 | 822 | function funcs.GetVideoModes(monitor, modes) 823 | local count = ffi.new('int[1]') 824 | local cmodes = clib.glfwGetVideoModes(monitor, count) 825 | 826 | modes = modes or {} 827 | for i = 0, count - 1 do 828 | modes[i + 1] = { 829 | width = cmodes[i].width; 830 | height = cmodes[i].height; 831 | redBits = cmodes[i].redBits; 832 | greenBits = cmodes[i].greenBits; 833 | blueBits = cmodes[i].blueBits; 834 | refreshRate = cmodes[i].refreshRate; 835 | } 836 | end 837 | 838 | return modes 839 | end 840 | 841 | function funcs.GetVideoMode(monitor) 842 | local cmode = clib.glfwGetVideoMode(monitor) 843 | 844 | local mode = { 845 | width = cmode.width; 846 | height = cmode.height; 847 | redBits = cmode.redBits; 848 | greenBits = cmode.greenBits; 849 | blueBits = cmode.blueBits; 850 | refreshRate = cmode.refreshRate; 851 | } 852 | 853 | return mode 854 | end 855 | 856 | function funcs.SetGamma(monitor, gamma) 857 | clib.glfwSetGamma(monitor, gamma) 858 | end 859 | 860 | function funcs.GetGammaRamp(monitor) 861 | return clib.glfwGetGammaRamp(monitor) 862 | end 863 | 864 | function funcs.SetGammaRamp(monitor, ramp) 865 | clib.glfwSetGammaRamp(monitor, ramp) 866 | end 867 | 868 | function funcs.DefaultWindowHints() 869 | clib.glfwDefaultWindowHints() 870 | end 871 | 872 | function funcs.WindowHint(hint, value) 873 | clib.glfwWindowHint(aux.get_const(const, hint), aux.get_const(const, value)) 874 | end 875 | 876 | function funcs.WindowHintString(hint, value) 877 | clib.glfwWindowHintString(aux.get_const(const, hint), value) 878 | end 879 | 880 | function funcs.CreateWindow(width, height, title, monitor, share) 881 | return clib.glfwCreateWindow(width, height, title, monitor, share) 882 | end 883 | 884 | function funcs.DestroyWindow(window) 885 | clib.glfwDestroyWindow(window) 886 | end 887 | 888 | function funcs.WindowShouldClose(window) 889 | return clib.glfwWindowShouldClose(window) 890 | end 891 | 892 | function funcs.SetWindowShouldClose(window, value) 893 | clib.glfwSetWindowShouldClose(window, value) 894 | end 895 | 896 | function funcs.GetWindowTitle(window) 897 | return aux.string_or_nil(const, clib.glfwGetWindowTitle(window)) 898 | end 899 | 900 | function funcs.SetWindowTitle(window, title) 901 | clib.glfwSetWindowTitle(window, title) 902 | end 903 | 904 | function funcs.SetWindowIcon(window, images) 905 | local cimages = ffi.new('GLFWimage[?]', #images) 906 | 907 | for i = 1, #images do 908 | cimages[i - 1] = images[i] 909 | end 910 | 911 | clib.glfwSetWindowIcon(window, #images, cimages) 912 | end 913 | 914 | function funcs.GetWindowPos(window) 915 | local xpos = ffi.new('int[1]') 916 | local ypos = ffi.new('int[1]') 917 | 918 | clib.glfwGetWindowPos(window, xpos, ypos) 919 | 920 | return xpos[0], ypos[0] 921 | end 922 | 923 | function funcs.SetWindowPos(window, xpos, ypos) 924 | clib.glfwSetWindowPos(window, xpos, ypos) 925 | end 926 | 927 | function funcs.GetWindowSize(window) 928 | local width = ffi.new('int[1]') 929 | local height = ffi.new('int[1]') 930 | 931 | clib.glfwGetWindowSize(window, width, height) 932 | 933 | return width[0], height[0] 934 | end 935 | 936 | function funcs.SetWindowSizeLimits(window, minwidth, minheight, maxwidth, maxheight) 937 | clib.glfwSetWindowSizeLimits(window, minwidth, minheight, maxwidth, maxheight) 938 | end 939 | 940 | function funcs.SetWindowAspectRatio(window, numer, denom) 941 | clib.glfwSetWindowAspectRatio(window, numer, denom) 942 | end 943 | 944 | function funcs.SetWindowSize(window, width, height) 945 | clib.glfwSetWindowSize(window, width, height) 946 | end 947 | 948 | function funcs.GetFramebufferSize(window) 949 | local width = ffi.new('int[1]') 950 | local height = ffi.new('int[1]') 951 | 952 | clib.glfwGetFramebufferSize(window, width, height) 953 | 954 | return width[0], height[0] 955 | end 956 | 957 | function funcs.GetWindowFrameSize(window, out) 958 | local left = ffi.new('int[1]') 959 | local top = ffi.new('int[1]') 960 | local right = ffi.new('int[1]') 961 | local bottom = ffi.new('int[1]') 962 | 963 | clib.glfwGetWindowFrameSize(window, left, top, right, bottom) 964 | 965 | out = out or {} 966 | out.left = left[0] 967 | out.top = top[0] 968 | out.right = right[0] 969 | out.bottom = bottom[0] 970 | 971 | return out 972 | end 973 | 974 | function funcs.GetWindowContentScale(window) 975 | local xscale = ffi.new('float[1]') 976 | local yscale = ffi.new('float[1]') 977 | 978 | clib.glfwGetWindowContentScale(window, xscale, yscale) 979 | 980 | return xscale[0], yscale[0] 981 | end 982 | 983 | function funcs.GetWindowOpacity(window) 984 | return clib.glfwGetWindowOpacity(window) 985 | end 986 | 987 | function funcs.SetWindowOpacity(window, opacity) 988 | clib.glfwSetWindowOpacity(window, opacity) 989 | end 990 | 991 | function funcs.IconifyWindow(window) 992 | clib.glfwIconifyWindow(window) 993 | end 994 | 995 | function funcs.RestoreWindow(window) 996 | clib.glfwRestoreWindow(window) 997 | end 998 | 999 | function funcs.MaximizeWindow(window) 1000 | clib.glfwMaximizeWindow(window) 1001 | end 1002 | 1003 | function funcs.ShowWindow(window) 1004 | clib.glfwShowWindow(window) 1005 | end 1006 | 1007 | function funcs.HideWindow(window) 1008 | clib.glfwHideWindow(window) 1009 | end 1010 | 1011 | function funcs.FocusWindow(window) 1012 | clib.glfwFocusWindow(window) 1013 | end 1014 | 1015 | function funcs.RequestWindowAttention(window) 1016 | clib.glfwRequestWindowAttention(window) 1017 | end 1018 | 1019 | function funcs.GetWindowMonitor(window) 1020 | return clib.glfwGetWindowMonitor(window) 1021 | end 1022 | 1023 | function funcs.SetWindowMonitor(window, monitor, xpos, ypos, width, height, refreshRate) 1024 | clib.glfwSetWindowMonitor(window, monitor, xpos, ypos, width, height, refreshRate) 1025 | end 1026 | 1027 | function funcs.GetWindowAttrib(window, attrib) 1028 | return clib.glfwGetWindowAttrib(window, aux.get_const(const, attrib)) 1029 | end 1030 | 1031 | function funcs.SetWindowAttrib(window, attrib, value) 1032 | clib.glfwSetWindowAttrib(window, aux.get_const(const, attrib), value) 1033 | end 1034 | 1035 | function funcs.SetWindowUserPointer(window, pointer) 1036 | clib.glfwSetWindowUserPointer(window, pointer) 1037 | end 1038 | 1039 | function funcs.GetWindowUserPointer(window) 1040 | return clib.glfwGetWindowUserPointer(window) 1041 | end 1042 | 1043 | function funcs.SetWindowPosCallback(window, cbfun) 1044 | cbfun = aux.wrap_cb(cbs, cbfun, 'windowposfun') 1045 | return clib.glfwSetWindowPosCallback(window, cbfun) 1046 | end 1047 | 1048 | function funcs.SetWindowSizeCallback(window, cbfun) 1049 | cbfun = aux.wrap_cb(cbs, cbfun, 'windowsizefun') 1050 | return clib.glfwSetWindowSizeCallback(window, cbfun) 1051 | end 1052 | 1053 | function funcs.SetWindowCloseCallback(window, cbfun) 1054 | cbfun = aux.wrap_cb(cbs, cbfun, 'windowclosefun') 1055 | return clib.glfwSetWindowCloseCallback(window, cbfun) 1056 | end 1057 | 1058 | function funcs.SetWindowRefreshCallback(window, cbfun) 1059 | cbfun = aux.wrap_cb(cbs, cbfun, 'windowrefreshfun') 1060 | return clib.glfwSetWindowRefreshCallback(window, cbfun) 1061 | end 1062 | 1063 | function funcs.SetWindowFocusCallback(window, cbfun) 1064 | cbfun = aux.wrap_cb(cbs, cbfun, 'windowfocusfun') 1065 | return clib.glfwSetWindowFocusCallback(window, cbfun) 1066 | end 1067 | 1068 | function funcs.SetWindowIconifyCallback(window, cbfun) 1069 | cbfun = aux.wrap_cb(cbs, cbfun, 'windowiconifyfun') 1070 | return clib.glfwSetWindowIconifyCallback(window, cbfun) 1071 | end 1072 | 1073 | function funcs.SetWindowMaximizeCallback(window, cbfun) 1074 | cbfun = aux.wrap_cb(cbs, cbfun, 'windowmaximizefun') 1075 | return clib.glfwSetWindowMaximizeCallback(window, cbfun) 1076 | end 1077 | 1078 | function funcs.SetFramebufferSizeCallback(window, cbfun) 1079 | cbfun = aux.wrap_cb(cbs, cbfun, 'framebuffersizefun') 1080 | return clib.glfwSetFramebufferSizeCallback(window, cbfun) 1081 | end 1082 | 1083 | function funcs.SetWindowContentScaleCallback(window, cbfun) 1084 | cbfun = aux.wrap_cb(cbs, cbfun, 'windowcontentscalefun') 1085 | return clib.glfwSetWindowContentScaleCallback(window, cbfun) 1086 | end 1087 | 1088 | function funcs.PollEvents() 1089 | clib.glfwPollEvents() 1090 | end 1091 | 1092 | function funcs.WaitEvents() 1093 | clib.glfwWaitEvents() 1094 | end 1095 | 1096 | function funcs.WaitEventsTimeout(timeout) 1097 | clib.glfwWaitEventsTimeout(timeout) 1098 | end 1099 | 1100 | function funcs.PostEmptyEvent() 1101 | clib.glfwPostEmptyEvent() 1102 | end 1103 | 1104 | function funcs.GetInputMode(window, mode) 1105 | return clib.glfwGetInputMode(window, aux.get_const(const, mode)) 1106 | end 1107 | 1108 | function funcs.SetInputMode(window, mode, value) 1109 | clib.glfwSetInputMode(window, aux.get_const(const, mode), value) 1110 | end 1111 | 1112 | function funcs.RawMouseMotionSupported() 1113 | return clib.glfwRawMouseMotionSupported() 1114 | end 1115 | 1116 | function funcs.GetKeyName(key, scancode) 1117 | return aux.string_or_nil(const, clib.glfwGetKeyName(key, scancode)) 1118 | end 1119 | 1120 | function funcs.GetKeyScancode(key) 1121 | return clib.glfwGetKeyScancode(key) 1122 | end 1123 | 1124 | function funcs.GetKey(window, key) 1125 | return clib.glfwGetKey(window, aux.get_const(const, key)) 1126 | end 1127 | 1128 | function funcs.GetMouseButton(window, button) 1129 | return clib.glfwGetMouseButton(window, aux.get_const(const, button)) 1130 | end 1131 | 1132 | function funcs.GetCursorPos(window) 1133 | local xpos = ffi.new('int[1]') 1134 | local ypos = ffi.new('int[1]') 1135 | 1136 | clib.glfwGetCursorPos(window, xpos, ypos) 1137 | 1138 | return xpos[0], ypos[0] 1139 | end 1140 | 1141 | function funcs.SetCursorPos(window, xpos, ypos) 1142 | clib.glfwSetCursorPos(window, xpos, ypos) 1143 | end 1144 | 1145 | function funcs.CreateCursor(image, xhot, yhot) 1146 | return clib.glfwCreateCursor(image, xhot, yhot) 1147 | end 1148 | 1149 | function funcs.CreateStandardCursor(shape) 1150 | return clib.glfwCreateStandardCursor(aux.get_const(const, shape)) 1151 | end 1152 | 1153 | function funcs.DestroyCursor(cursor) 1154 | clib.glfwDestroyCursor(cursor) 1155 | end 1156 | 1157 | function funcs.SetCursor(window, cursor) 1158 | clib.glfwSetCursor(window, cursor) 1159 | end 1160 | 1161 | function funcs.SetKeyCallback(window, cbfun) 1162 | cbfun = aux.wrap_cb(cbs, cbfun, 'keyfun') 1163 | return clib.glfwSetKeyCallback(window, cbfun) 1164 | end 1165 | 1166 | function funcs.SetCharCallback(window, cbfun) 1167 | cbfun = aux.wrap_cb(cbs, cbfun, 'charfun') 1168 | return clib.glfwSetCharCallback(window, cbfun) 1169 | end 1170 | 1171 | function funcs.SetCharModsCallback(window, cbfun) 1172 | cbfun = aux.wrap_cb(cbs, cbfun, 'charmodsfun') 1173 | return clib.glfwSetCharModsCallback(window, cbfun) 1174 | end 1175 | 1176 | function funcs.SetMouseButtonCallback(window, cbfun) 1177 | cbfun = aux.wrap_cb(cbs, cbfun, 'mousebuttonfun') 1178 | return clib.glfwSetMouseButtonCallback(window, cbfun) 1179 | end 1180 | 1181 | function funcs.SetCursorPosCallback(window, cbfun) 1182 | cbfun = aux.wrap_cb(cbs, cbfun, 'cursorposfun') 1183 | return clib.glfwSetCursorPosCallback(window, cbfun) 1184 | end 1185 | 1186 | function funcs.SetCursorEnterCallback(window, cbfun) 1187 | cbfun = aux.wrap_cb(cbs, cbfun, 'cursorenterfun') 1188 | return clib.glfwSetCursorEnterCallback(window, cbfun) 1189 | end 1190 | 1191 | function funcs.SetScrollCallback(window, cbfun) 1192 | cbfun = aux.wrap_cb(cbs, cbfun, 'scrollfun') 1193 | return clib.glfwSetScrollCallback(window, cbfun) 1194 | end 1195 | 1196 | function funcs.SetDropCallback(window, cbfun) 1197 | cbfun = aux.wrap_cb(cbs, cbfun, 'dropfun_raw') 1198 | return clib.glfwSetDropCallback(window, cbfun) 1199 | end 1200 | 1201 | function funcs.JoystickPresent(jid) 1202 | return clib.glfwJoystickPresent(aux.get_const(const, jid)) 1203 | end 1204 | 1205 | function funcs.GetJoystickAxes(jid, axes) 1206 | local count = ffi.new('int[1]') 1207 | local caxes = clib.glfwGetJoystickAxes(aux.get_const(const, jid), count) 1208 | 1209 | axes = axes or {} 1210 | for i = 1, count do 1211 | axes[i] = caxes[i - 1] 1212 | end 1213 | 1214 | return axes 1215 | end 1216 | 1217 | function funcs.GetJoystickButtons(jid, buttons) 1218 | local count = ffi.new('int[1]') 1219 | local cbuttons = clib.glfwGetJoystickButtons(aux.get_const(const, jid), count) 1220 | 1221 | buttons = buttons or {} 1222 | for i = 1, count do 1223 | buttons[i] = cbuttons[i - 1] 1224 | end 1225 | 1226 | return buttons 1227 | end 1228 | 1229 | function funcs.GetJoystickHats(jid, hats) 1230 | local count = ffi.new('int[1]') 1231 | local chats = clib.glfwGetJoystickHats(aux.get_const(const, jid), count) 1232 | 1233 | hats = hats or {} 1234 | for i = 1, count do 1235 | hats[i] = chats[i - 1] 1236 | end 1237 | 1238 | return hats 1239 | end 1240 | 1241 | function funcs.GetJoystickName(jid) 1242 | return aux.string_or_nil(const, clib.glfwGetJoystickName(aux.get_const(const, jid))) 1243 | end 1244 | 1245 | function funcs.GetJoystickGUID(jid) 1246 | return aux.string_or_nil(const, clib.glfwGetJoystickGUID(aux.get_const(const, jid))) 1247 | end 1248 | 1249 | function funcs.SetJoystickUserPointer(jid, pointer) 1250 | clib.glfwSetJoystickUserPointer(aux.get_const(const, jid), pointer) 1251 | end 1252 | 1253 | function funcs.GetJoystickUserPointer(jid) 1254 | return clib.glfwGetJoystickUserPointer(aux.get_const(const, jid)) 1255 | end 1256 | 1257 | function funcs.JoystickIsGamepad(jid) 1258 | return clib.glfwJoystickIsGamepad(aux.get_const(const, jid)) 1259 | end 1260 | 1261 | function funcs.SetJoystickCallback(cbfun) 1262 | cbfun = aux.wrap_cb(cbs, cbfun, 'joystickfun') 1263 | return clib.glfwSetJoystickCallback(cbfun) 1264 | end 1265 | 1266 | function funcs.UpdateGamepadMappings(string) 1267 | return clib.glfwUpdateGamepadMappings(string) 1268 | end 1269 | 1270 | function funcs.GetGamepadName(jid) 1271 | return aux.string_or_nil(const, clib.glfwGetGamepadName(aux.get_const(const, jid))) 1272 | end 1273 | 1274 | function funcs.GetGamepadState(jid, state) 1275 | return clib.glfwGetGamepadState(aux.get_const(const, jid), state) 1276 | end 1277 | 1278 | function funcs.SetClipboardString(window, string) 1279 | clib.glfwSetClipboardString(window, string) 1280 | end 1281 | 1282 | function funcs.GetClipboardString(window) 1283 | return aux.string_or_nil(const, clib.glfwGetClipboardString(window)) 1284 | end 1285 | 1286 | function funcs.GetTime() 1287 | return clib.glfwGetTime() 1288 | end 1289 | 1290 | function funcs.SetTime(time) 1291 | clib.glfwSetTime(time) 1292 | end 1293 | 1294 | function funcs.GetTimerValue() 1295 | return clib.glfwGetTimerValue() 1296 | end 1297 | 1298 | function funcs.GetTimerFrequency() 1299 | return clib.glfwGetTimerFrequency() 1300 | end 1301 | 1302 | function funcs.MakeContextCurrent(window) 1303 | clib.glfwMakeContextCurrent(window) 1304 | end 1305 | 1306 | function funcs.GetCurrentContext() 1307 | return clib.glfwGetCurrentContext() 1308 | end 1309 | 1310 | function funcs.SwapBuffers(window) 1311 | clib.glfwSwapBuffers(window) 1312 | end 1313 | 1314 | function funcs.SwapInterval(interval) 1315 | clib.glfwSwapInterval(interval) 1316 | end 1317 | 1318 | function funcs.ExtensionSupported(extension) 1319 | return clib.glfwExtensionSupported(extension) 1320 | end 1321 | 1322 | function funcs.GetProcAddress(procname) 1323 | return clib.glfwGetProcAddress(procname) 1324 | end 1325 | 1326 | if args.bind_vulkan == true then 1327 | function funcs.VulkanSupported() 1328 | return clib.glfwVulkanSupported() 1329 | end 1330 | 1331 | function funcs.InitVulkanLoader(loader) 1332 | clib.glfwInitVulkanLoader(loader) 1333 | end 1334 | 1335 | function funcs.GetRequiredInstanceExtensions(extensions) 1336 | local count = ffi.new('uint32_t[1]') 1337 | local cextensions = clib.glfwGetRequiredInstanceExtensions(count) 1338 | 1339 | extensions = extensions or {} 1340 | for i = 1, count do 1341 | extensions[i] = ffi.string(cextensions[i - 1]) 1342 | end 1343 | 1344 | return extensions 1345 | end 1346 | 1347 | function funcs.GetInstanceProcAddress(instance, procname) 1348 | return clib.glfwGetInstanceProcAddress(instance, procname) 1349 | end 1350 | 1351 | function funcs.GetPhysicalDevicePresentationSupport(instance, device, queuefamily) 1352 | return clib.glfwGetPhysicalDevicePresentationSupport(instance, device, queuefamily) 1353 | end 1354 | 1355 | function funcs.CreateWindowSurface(instance, window, allocator, surface) 1356 | return clib.glfwCreateWindowSurface(instance, window, allocator, surface) 1357 | end 1358 | end 1359 | 1360 | do 1361 | -- Luajit does not allow to call Lua-callbacks 1362 | -- from JIT-compiled C-functions, so we 1363 | -- manually turn off JIT for them 1364 | if is_luajit then 1365 | jit.off(funcs.PollEvents) 1366 | jit.off(funcs.WaitEvents) 1367 | jit.off(funcs.WaitEventsTimeout) 1368 | end 1369 | end 1370 | 1371 | ----------------------------------------------------------- 1372 | -- Extra functions 1373 | ----------------------------------------------------------- 1374 | function funcs.WindowHints(hints) 1375 | for hint,value in pairs(hints) do 1376 | funcs.WindowHint(hint, value) 1377 | end 1378 | end 1379 | 1380 | function funcs.CreateImage(width, height) 1381 | local image = ffi.new('GLFWimage') 1382 | 1383 | -- Allocate pixels if size specified 1384 | if type(width) == 'number' and type(height) == 'number' then 1385 | image.width = width 1386 | image.height = height 1387 | image.pixels = ffi.new('unsigned char[?]', width * height * 4) 1388 | end 1389 | 1390 | return image 1391 | end 1392 | 1393 | 1394 | ----------------------------------------------------------- 1395 | -- Types 1396 | ----------------------------------------------------------- 1397 | local monitor_mt = aux.class() 1398 | monitor_mt.GetPos = funcs.GetMonitorPos 1399 | monitor_mt.GetWorkarea = funcs.GetMonitorWorkarea 1400 | monitor_mt.GetPhysicalSize = funcs.GetMonitorPhysicalSize 1401 | monitor_mt.GetContentScale = funcs.GetMonitorContentScale 1402 | monitor_mt.GetName = funcs.GetMonitorName 1403 | monitor_mt.SetUserPointer = funcs.SetMonitorUserPointer 1404 | monitor_mt.GetUserPointer = funcs.GetMonitorUserPointer 1405 | monitor_mt.GetVideoModes = funcs.GetVideoModes 1406 | monitor_mt.GetVideoMode = funcs.GetVideoMode 1407 | monitor_mt.SetGamma = funcs.SetGamma 1408 | monitor_mt.GetGammaRamp = funcs.GetGammaRamp 1409 | monitor_mt.SetGammaRamp = funcs.SetGammaRamp 1410 | 1411 | local window_mt = aux.class() 1412 | window_mt.Destroy = funcs.DestroyWindow 1413 | window_mt.ShouldClose = funcs.WindowShouldClose 1414 | window_mt.SetShouldClose = funcs.SetWindowShouldClose 1415 | window_mt.GetTitle = funcs.GetWindowTitle 1416 | window_mt.SetTitle = funcs.SetWindowTitle 1417 | window_mt.SetIcon = funcs.SetWindowIcon 1418 | window_mt.GetPos = funcs.GetWindowPos 1419 | window_mt.SetPos = funcs.SetWindowPos 1420 | window_mt.GetSize = funcs.GetWindowSize 1421 | window_mt.SetSizeLimits = funcs.SetWindowSizeLimits 1422 | window_mt.SetAspectRatio = funcs.SetWindowAspectRatio 1423 | window_mt.SetSize = funcs.SetWindowSize 1424 | window_mt.GetFramebufferSize = funcs.GetFramebufferSize 1425 | window_mt.GetFrameSize = funcs.GetWindowFrameSize 1426 | window_mt.GetContentScale = funcs.GetWindowContentScale 1427 | window_mt.GetOpacity = funcs.GetWindowOpacity 1428 | window_mt.SetOpacity = funcs.SetWindowOpacity 1429 | window_mt.Iconify = funcs.IconifyWindow 1430 | window_mt.Restore = funcs.RestoreWindow 1431 | window_mt.Maximize = funcs.MaximizeWindow 1432 | window_mt.Show = funcs.ShowWindow 1433 | window_mt.Hide = funcs.HideWindow 1434 | window_mt.Focus = funcs.FocusWindow 1435 | window_mt.RequestAttention = funcs.RequestWindowAttention 1436 | window_mt.GetMonitor = funcs.GetWindowMonitor 1437 | window_mt.SetMonitor = funcs.SetWindowMonitor 1438 | window_mt.GetAttrib = funcs.GetWindowAttrib 1439 | window_mt.SetAttrib = funcs.SetWindowAttrib 1440 | window_mt.SetUserPointer = funcs.SetWindowUserPointer 1441 | window_mt.GetUserPointer = funcs.GetWindowUserPointer 1442 | window_mt.SetPosCallback = funcs.SetWindowPosCallback 1443 | window_mt.SetSizeCallback = funcs.SetWindowSizeCallback 1444 | window_mt.SetCloseCallback = funcs.SetWindowCloseCallback 1445 | window_mt.SetRefreshCallback = funcs.SetWindowRefreshCallback 1446 | window_mt.SetFocusCallback = funcs.SetWindowFocusCallback 1447 | window_mt.SetIconifyCallback = funcs.SetWindowIconifyCallback 1448 | window_mt.SetMaximizeCallback = funcs.SetWindowMaximizeCallback 1449 | window_mt.SetFramebufferSizeCallback = funcs.SetFramebufferSizeCallback 1450 | window_mt.SetContentScaleCallback = funcs.SetWindowContentScaleCallback 1451 | window_mt.GetInputMode = funcs.GetInputMode 1452 | window_mt.SetInputMode = funcs.SetInputMode 1453 | window_mt.GetKey = funcs.GetKey 1454 | window_mt.GetMouseButton = funcs.GetMouseButton 1455 | window_mt.GetCursorPos = funcs.GetCursorPos 1456 | window_mt.SetCursorPos = funcs.SetCursorPos 1457 | window_mt.SetCursor = funcs.SetCursor 1458 | window_mt.SetKeyCallback = funcs.SetKeyCallback 1459 | window_mt.SetCharCallback = funcs.SetCharCallback 1460 | window_mt.SetCharModsCallback = funcs.SetCharModsCallback 1461 | window_mt.SetMouseButtonCallback = funcs.SetMouseButtonCallback 1462 | window_mt.SetCursorPosCallback = funcs.SetCursorPosCallback 1463 | window_mt.SetCursorEnterCallback = funcs.SetCursorEnterCallback 1464 | window_mt.SetScrollCallback = funcs.SetScrollCallback 1465 | window_mt.SetDropCallback = funcs.SetDropCallback 1466 | window_mt.SetClipboardString = funcs.SetClipboardString 1467 | window_mt.GetClipboardString = funcs.GetClipboardString 1468 | window_mt.MakeContextCurrent = funcs.MakeContextCurrent 1469 | window_mt.SwapBuffers = funcs.SwapBuffers 1470 | 1471 | local cursor_mt = aux.class() 1472 | cursor_mt.Destroy = funcs.DestroyCursor 1473 | 1474 | ffi.metatype('GLFWmonitor', monitor_mt) 1475 | ffi.metatype('GLFWwindow', window_mt) 1476 | ffi.metatype('GLFWcursor', cursor_mt) 1477 | 1478 | types.image = ffi.typeof('GLFWimage') 1479 | types.gamepadstate = ffi.typeof('GLFWgamepadstate') 1480 | 1481 | ----------------------------------------------------------- 1482 | -- Callbacks wrappers 1483 | ----------------------------------------------------------- 1484 | cbs.allocatefun = ffi.typeof('GLFWallocatefun') 1485 | cbs.reallocatefun = ffi.typeof('GLFWreallocatefun') 1486 | cbs.deallocatefun = ffi.typeof('GLFWdeallocatefun') 1487 | 1488 | cbs.errorfun_raw = ffi.typeof('GLFWerrorfun') 1489 | function cbs.errorfun(func) 1490 | return cbs.errorfun_raw(function(error, description) 1491 | func(error, ffi.string(description)) 1492 | end) 1493 | end 1494 | 1495 | cbs.dropfun_raw = ffi.typeof('GLFWdropfun') 1496 | function cbs.dropfun(func) 1497 | return cbs.dropfun_raw(function(window, count, names) 1498 | local t = {} 1499 | for i = 1, count do 1500 | t[i] = ffi.string(names[i - 1]) 1501 | end 1502 | func(window, t) 1503 | end) 1504 | end 1505 | 1506 | cbs.windowposfun = ffi.typeof('GLFWwindowposfun') 1507 | cbs.windowsizefun = ffi.typeof('GLFWwindowsizefun') 1508 | cbs.windowclosefun = ffi.typeof('GLFWwindowclosefun') 1509 | cbs.windowrefreshfun = ffi.typeof('GLFWwindowrefreshfun') 1510 | cbs.windowfocusfun = ffi.typeof('GLFWwindowfocusfun') 1511 | cbs.windowiconifyfun = ffi.typeof('GLFWwindowiconifyfun') 1512 | cbs.windowmaximizefun = ffi.typeof('GLFWwindowmaximizefun') 1513 | cbs.framebuffersizefun = ffi.typeof('GLFWframebuffersizefun') 1514 | cbs.windowcontentscalefun = ffi.typeof('GLFWwindowcontentscalefun') 1515 | cbs.mousebuttonfun = ffi.typeof('GLFWmousebuttonfun') 1516 | cbs.cursorposfun = ffi.typeof('GLFWcursorposfun') 1517 | cbs.cursorenterfun = ffi.typeof('GLFWcursorenterfun') 1518 | cbs.scrollfun = ffi.typeof('GLFWscrollfun') 1519 | cbs.keyfun = ffi.typeof('GLFWkeyfun') 1520 | cbs.charfun = ffi.typeof('GLFWcharfun') 1521 | cbs.charmodsfun = ffi.typeof('GLFWcharmodsfun') 1522 | cbs.monitorfun = ffi.typeof('GLFWmonitorfun') 1523 | cbs.joystickfun = ffi.typeof('GLFWjoystickfun') 1524 | end 1525 | 1526 | ----------------------------------------------------------- 1527 | -- Auxiliary 1528 | ----------------------------------------------------------- 1529 | function aux.get_const(const, value) 1530 | local vtype = type(value) 1531 | 1532 | if vtype == 'number' then 1533 | return value 1534 | end 1535 | 1536 | if vtype == 'string' then 1537 | if const[value] then 1538 | return const[value] 1539 | else 1540 | error('unknown const name', 3) 1541 | end 1542 | end 1543 | 1544 | error('bad const type', 3) 1545 | end 1546 | 1547 | function aux.class() 1548 | local class = {} 1549 | class.__index = class 1550 | return class 1551 | end 1552 | 1553 | function aux.wrap_cb(cbs, cbfun, cbname) 1554 | if type(cbfun) == 'function' then 1555 | return cbs[cbname](cbfun) 1556 | end 1557 | return cbfun 1558 | end 1559 | 1560 | function aux.set_mt_method(t,k,v) 1561 | local mt = getmetatable(t) 1562 | if mt then 1563 | mt[k] = v 1564 | else 1565 | setmetatable(t, { [k] = v }) 1566 | end 1567 | end 1568 | 1569 | function aux.string_or_nil(const, cstr) 1570 | if cstr ~= const.NULL then 1571 | return ffi.string(cstr) 1572 | end 1573 | return nil 1574 | end 1575 | 1576 | 1577 | return setmetatable(mod, { __call = init }) 1578 | --------------------------------------------------------------------------------