Skip to content

Instantly share code, notes, and snippets.

@sharpobject
Created March 23, 2020 11:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sharpobject/72fe0d6745b9eef8a12276d74b7bafbf to your computer and use it in GitHub Desktop.
Save sharpobject/72fe0d6745b9eef8a12276d74b7bafbf to your computer and use it in GitHub Desktop.
render in a thread
local ffi = require("ffi")
ffi.cdef[[
int SDL_GL_MakeCurrent(void* window,void* context);
void* SDL_GL_GetCurrentContext(void);
void* SDL_GL_GetCurrentWindow(void);
]]
function love.load()
local voidptr_size = ffi.sizeof("void*")
local context = ffi.new("void*[1]")
context[0] = ffi.C.SDL_GL_GetCurrentContext()
local window = ffi.new("void*[1]")
window[0] = ffi.C.SDL_GL_GetCurrentWindow()
ffi.C.SDL_GL_MakeCurrent(window[0], nil)
local channel = love.thread.getChannel("owo")
local contextdata = love.data.newByteData(voidptr_size)
local windowdata = love.data.newByteData(voidptr_size)
ffi.copy(contextdata:getFFIPointer(), context, voidptr_size)
ffi.copy(windowdata:getFFIPointer(), window, voidptr_size)
channel:push(contextdata)
channel:push(windowdata)
love.thread.newThread([=[
local ffi = require"ffi"
ffi.cdef[[
int SDL_GL_MakeCurrent(void* window,void* context);
]]
local voidptr_size = ffi.sizeof("void*")
local channel = love.thread.getChannel("owo")
local context = ffi.new("void*[1]")
local window = ffi.new("void*[1]")
local contextdata = channel:pop()
local windowdata = channel:pop()
ffi.copy(context, contextdata:getFFIPointer(), voidptr_size)
ffi.copy(window, windowdata:getFFIPointer(), voidptr_size)
ffi.C.SDL_GL_MakeCurrent(window[0], context[0])
require"love.graphics"
while true do
love.graphics.setColor(0,0,0,1)
love.graphics.clear()
love.graphics.setColor(1,1,1,1)
love.graphics.print("Hello from another thread!")
love.graphics.present()
end
]=]):start()
end
function love.run()
if love.load then love.load(love.arg.parseGameArguments(arg), arg) end
-- We don't want the first frame's dt to include time taken by love.load.
if love.timer then love.timer.step() end
local dt = 0
-- Main loop time.
return function()
-- Process events.
if love.event then
love.event.pump()
for name, a,b,c,d,e,f in love.event.poll() do
if name == "quit" then
if not love.quit or not love.quit() then
return a or 0
end
end
love.handlers[name](a,b,c,d,e,f)
end
end
-- Update dt, as we'll be passing it to update
if love.timer then dt = love.timer.step() end
if love.timer then love.timer.sleep(0.001) end
end
end
function love.threaderror(_,err)
error(err)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment