For the whole code I posted the link to my repo.
raylib.lua
local c = require "c"
local lib = c.Library("raylib.dll")
if not lib then
error(sys.error)
end
lib.InitWindow = "c(iiZ)"
lib.CloseWindow = "c()"
lib.WindowShouldClose = "c()B"
lib.BeginDrawing = "c()"
lib.EndDrawing = "c()"
lib.ClearBackground = "c(.)"
lib.SetTargetFPS = "c(i)"
---@class Color
---@field r integer 8 bit integer value (0-255)
---@field g integer 8 bit integer value (0-255)
---@field b integer 8 bit integer value (0-255)
---@field a integer 8 bit integer value (0-255)
---Color, 4 components, R8G8B8A8 (32bit)
---@overload fun(r: integer, g: integer, b: integer, a: integer): Color
local Color_t = c.Struct("CCCC", "r", "g", "b", "a")
---@class raylib
---@field LIGHTGRAY Color
---@field GRAY Color
---@field DARKGRAY Color
local raylib = {
LIGHTGRAY = Color_t(c.uchar(200), c.uchar(200), c.uchar(200), c.uchar(255)),
GRAY = Color_t(c.uchar(130), c.uchar(130), c.uchar(130), c.uchar(255)),
DARKGRAY = Color_t(c.uchar(80), c.uchar(80), c.uchar(80), c.uchar(255)),
}
---Creates a Color struct
---@param r integer
---@param g integer
---@param b integer
---@param a integer
---@return Color
function raylib.Color(r, g, b, a)
return Color_t(
c.uchar(r), -- r
c.uchar(g), -- g
c.uchar(b), -- b
c.uchar(a) -- a
)
end
---Initialize window and OpenGL context
---@param width integer
---@param height integer
---@param title string
function raylib.InitWindow(width, height, title)
lib.InitWindow(c.int(width), c.int(height), c.string(title))
end
---Close window and unload OpenGL context
function raylib.CloseWindow()
lib.CloseWindow()
end
---Check if application should close (KEY_ESCAPE pressed or windows close icon clicked)
---@return boolean
function raylib.WindowShouldClose()
return lib.WindowShouldClose()
end
---Set background color (framebuffer clear color)
---@param color Color
function raylib.ClearBackground(color)
assert(type(color) == "Struct" and color.type == "Color_t", "Type not supported")
lib.ClearBackground(color)
end
---Setup canvas (framebuffer) to start drawing
function raylib.BeginDrawing()
lib.BeginDrawing()
end
---End canvas drawing and swap buffers (double buffering)
function raylib.EndDrawing()
lib.EndDrawing()
end
---Set target FPS (maximum)
---@param fps integer
function raylib.SetTargetFPS(fps)
lib.SetTargetFPS(c.int(fps))
end
return raylib
test.lua
local r = require "raylib"
local function main()
r.InitWindow(800, 600, "Test")
r.SetTargetFPS(60)
while not r.WindowShouldClose() do
r.BeginDrawing()
r.ClearBackground(r.Color(200, 200, 200, 255))
r.EndDrawing()
end
r.CloseWindow()
return 0
end
sys.exit(main() or 1)