Skip to content

Instantly share code, notes, and snippets.

@creationix
Created September 13, 2011 06:52
Show Gist options
  • Save creationix/1213280 to your computer and use it in GitHub Desktop.
Save creationix/1213280 to your computer and use it in GitHub Desktop.
Using SDL from luajit's built-in ffi module
-- load the luajit ffi module
local ffi = require "ffi"
-- Parse the C API header
-- It's generated with:
--
-- echo '#include <SDL.h>' > stub.c
-- gcc -I /usr/include/SDL -E stub.c | grep -v '^#' > ffi_SDL.h
--
ffi.cdef(io.open('ffi_SDL.h', 'r'):read('*a'))
-- Load the shared object
local SDL = ffi.load('SDL')
-- Define some constants that were in declares
local SDL_INIT_VIDEO = 0x20
-- Make an easy constructor metatype
local rect = ffi.metatype("SDL_Rect", {})
-- Create the window
SDL.SDL_Init(SDL_INIT_VIDEO)
SDL.SDL_WM_SetCaption("SDL Test", "SDL Test");
local screen = SDL.SDL_SetVideoMode(1024, 768, 0, 0)
-- Set up our event loop
local gameover = false
local event = ffi.new("SDL_Event")
while not gameover do
-- Draw 8192 randomly colored rectangles to the screen
for i = 0,0x2000 do
local r = rect(math.random(screen.w)-10, math.random(screen.h)-10,20,20)
local color = math.random(0x1000000)
SDL.SDL_FillRect(screen, r, color)
end
-- Flush the output
SDL.SDL_Flip(screen)
-- Check for escape keydown or quit events to stop the loop
if (SDL.SDL_PollEvent(event)) then
local etype=event.type
if etype == SDL.SDL_QUIT then
-- close button clicked
gameover = true
break
end
if etype == SDL.SDL_KEYDOWN then
local sym = event.key.keysym.sym
if sym == SDL.SDLK_ESCAPE then
-- Escape is pressed
gameover = true
break
end
end
end
end
-- When the loop finishes, clean up, print a message, and exit
SDL.SDL_Quit();
print("Thanks for Playing!");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment