[Solved] Canvas drawing problems
Posted: Thu Feb 19, 2015 5:21 am
In the below code i'm trying to draw rectangles to a canvas at random positions. However, seemingly dependent on the random value, the canvas drawing to the screen appears to fail (even though the code path through the if is definitely hit). There is a 4 x 4 square that should appear on the screen no matter what, however, sometimes the screen will be completely black. I say sometimes because it's not always the case that this happens, and you might need to run the code a couple times to experience the completely black screen. At least in my experience the black screen occurs more often than not. I could find a specific seed that always fails for me but the black screen happens often enough (for me) that I didn't think it necessary.
I've fiddled with this code at great length to try and discover what the hell was going on. Switching from points to rectangles, to spacing by 2 (which does seem to reduce the likelihood of the problem), etc. What exactly is going on?
I've fiddled with this code at great length to try and discover what the hell was going on. Switching from points to rectangles, to spacing by 2 (which does seem to reduce the likelihood of the problem), etc. What exactly is going on?
Code: Select all
xSize = 500
ySize = 500
local firstCanvas
local currentCanvas
function love.load()
love.window.setMode(xSize, ySize)
firstCanvas = love.graphics.newCanvas(xSize, ySize)
currentCanvas = firstCanvas
love.graphics.setCanvas(currentCanvas)
white_count = 0
black_count = 0
for i = 10, xSize - 10 do
for j = 10, ySize - 10 do
if love.math.random() < .45 then
white_count = white_count + 1
love.graphics.setColor(255, 255, 255)
love.graphics.rectangle("fill", i, j, 2, 2)
else
black_count = black_count + 1
love.graphics.setColor(0, 0, 0)
love.graphics.rectangle("fill", i, j, 2, 2)
end
end
end
love.graphics.rectangle("fill", 50, 50, 2, 2)
love.graphics.rectangle("fill", 50, 52, 2, 2)
love.graphics.rectangle("fill", 52, 50, 2, 2)
love.graphics.rectangle("fill", 52, 52, 2, 2)
print(white_count .. " " .. black_count)
love.graphics.setCanvas()
love.graphics.draw(currentCanvas)
end
function love.update(dt)
if dt < 1/30 then
love.timer.sleep(1/30 - dt)
end
end
function love.draw()
love.graphics.draw(currentCanvas)
end