main.lua
Code: Select all
-- new thread
thread = love.thread.newThread('thread','thread.lua')
thread:start()
function love.update(dt)
msg_table = msg_table or {}
-- process messages when other thread is "done"
if thread:get('done') then
msg_table[1] = thread:get('number')
msg_table[2] = thread:get('string')
msg_table[3] = tostring(thread:get('condition'))
-- concat our messages into a long string
message = table.concat(msg_table,'\n')
-- get image data from the other thread
imageData = imageData or thread:get('image')
-- initialize image
image = love.graphics.newImage(imageData)
end
if thread:get('hasCounter') then
msg_table[4] = thread:get('counter')
message = table.concat(msg_table,'\n')
end
end
function love.draw()
if image then
love.graphics.draw(image,50,50)
end
if message then
love.graphics.print(message,10,10)
end
end
Code: Select all
-- load modules b/c new thread
require 'love.filesystem'
require 'love.image'
require 'love.timer'
-- get this thread's id
thisThread = love.thread.getThread()
-- load image file, userdata can be sent also!
file = love.image.newImageData('love.png')
-- send messages to this thread's inbox
thisThread:set('image',file)
thisThread:set('number',1337)
thisThread:set('string','a string message')
thisThread:set('condition',true)
thisThread:set('done',true)
time0 = love.timer.getTime()
counter = 0
-- count per second and send counter to inbox
while true do
timeD = love.timer.getTime() - time0
if timeD > 1 then
counter = counter + 1
thisThread:set('counter',counter)
thisThread:set('hasCounter',true)
time0 = love.timer.getTime()
end
end