Skip to content

Instantly share code, notes, and snippets.

@DenisBelmondo
Created March 30, 2025 00:19
Show Gist options
  • Save DenisBelmondo/59ff6c71cfbac1850004b4f58c571fb6 to your computer and use it in GitHub Desktop.
Save DenisBelmondo/59ff6c71cfbac1850004b4f58c571fb6 to your computer and use it in GitHub Desktop.
#!/usr/bin/env luajit2
local ffi = require 'ffi'
ffi.cdef [[
int glfwInit(void);
void glfwTerminate(void);
void glfwWindowHint(int hint, int value);
void* glfwCreateWindow(int width, int height, const char *title, void *monitor, void *share);
void glfwDestroyWindow(void *window);
void glfwMakeContextCurrent(void *window);
int glfwWindowShouldClose(void *window);
void glfwSwapBuffers(void *window);
void glfwPollEvents(void);
void *glfwGetProcAddress(const char *procname);
]]
local glfw = ffi.load 'glfw'
local GLFW_CONTEXT_VERSION_MAJOR = 0x00022002
local GLFW_CONTEXT_VERSION_MINOR = 0x00022003
local GLFW_OPENGL_PROFILE = 0x00022008
local GLFW_OPENGL_CORE_PROFILE = 0x00032001
local GLFW_OPENGL_FORWARD_COMPAT = 0x00022006
ffi.cdef [[
void glClearColor(float red, float green, float blue, float alpha);
void glClear(unsigned int mask);
void glViewport(int x, int y, int width, int height);
]]
local gl = ffi.load 'GL'
local GL_COLOR_BUFFER_BIT = 0x00004000
local GL_ARRAY_BUFFER = 0x8892
glfw.glfwInit()
glfw.glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3)
glfw.glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3)
glfw.glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE)
glfw.glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, 1)
local window = glfw.glfwCreateWindow(640, 480, 'Learn OpenGL', nil, nil)
if window == nil then
glfw.glfwTerminate()
error 'failed to create glfw window'
end
glfw.glfwMakeContextCurrent(window)
local glGenBuffers = ffi.cast('void(*)(int, unsigned int *)', glfw.glfwGetProcAddress 'glGenBuffers')
local glBindBuffer = ffi.cast('void(*)(int, int)', glfw.glfwGetProcAddress 'glBindBuffer')
local vbo = ffi.new 'unsigned int[1]'
glGenBuffers(1, vbo)
glBindBuffer(GL_ARRAY_BUFFER, vbo[0])
gl.glViewport(0, 0, 640, 480)
while glfw.glfwWindowShouldClose(window) == 0 do
gl.glClearColor(1, 0, 0, 1)
gl.glClear(GL_COLOR_BUFFER_BIT)
glfw.glfwPollEvents()
glfw.glfwSwapBuffers(window)
end
glfw.glfwDestroyWindow(window)
glfw.glfwTerminate()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment