[Solved] Images sent across threads are blank
Posted: Sun Jun 21, 2015 3:21 am
I have a simple setup where one thread loads images and the main thread fetches them as needed and draws them. (This is to ameliorate loading large images which causes framerate hiccups)
However, the images all appear as blank white when drawn. The dimensions of the white rectangle correspond correctly to the dimensions of the loaded images, so clearly the Image userdata is being passed in some form.
Here's the thread code:
Here's image.lua:
Here's the relevant main thread code:
However, the images all appear as blank white when drawn. The dimensions of the white rectangle correspond correctly to the dimensions of the loaded images, so clearly the Image userdata is being passed in some form.
Here's the thread code:
Code: Select all
require "love.filesystem"
require "love.graphics"
require "love.math"
local function loadImage(winWidth, winHeight)
local o = {}
-- Pick random image
local files = love.filesystem.getDirectoryItems("images")
local i = love.math.random(1, #files)
o.image = love.graphics.newImage('images/' .. files[i])
print("loaded image " .. files[i])
-- scale image
local imgWidth = o.image:getWidth()
local imgHeight = o.image:getHeight()
local xScale = winWidth / imgWidth
local yScale = winHeight / imgHeight
o.scale = math.min(xScale, yScale)
-- center image
o.x = (winWidth - (imgWidth * o.scale)) / 2
o.y = (winHeight - (imgHeight * o.scale)) / 2
return o
end
local channel, winWidth, winHeight = ...
while true do
local myImage = loadImage(winWidth, winHeight)
channel:supply(myImage)
end
Code: Select all
local P = {}
P.Image = {}
function P.Image:new(o)
o = o or {} -- create object if user does not provide one
setmetatable(o, self)
self.__index = self
return o
end
function P.Image:draw()
love.graphics.draw(self.image, self.x, self.y, 0, self.scale, self.scale)
end
image = P
return P
Code: Select all
function P.newPlayer()
-- *snip*
o.channel = love.thread.newChannel()
o.thread = love.thread.newThread("imageThread.lua")
o.thread:start(o.channel, winWidth, winHeight)
o:loadImage()
end
function P.Player:loadImage()
self.image = image.Image:new(self.channel:demand())
end
function P.Player:draw()
self.image:draw()
end