Page 1 of 1

[Solved] Canvas drawing problems

Posted: Thu Feb 19, 2015 5:21 am
by apajx
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?

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

Re: Canvas drawing problems

Posted: Thu Feb 19, 2015 6:55 am
by micha
Hi and welcome to the forum.

I played around with your code a little bit.You need to set the drawing color back to white, before you draw the canvas. Put this into the love.draw:

Code: Select all

love.graphics.setColor(255, 255, 255)
Otherwise the canvas itself is drawn with the color black in random cases (namely in those cases, where the last pixel was black).

Re: Canvas drawing problems

Posted: Thu Feb 19, 2015 7:34 am
by s-ol
Try moving all of those drawing calls to a one-time if in love.draw

Re: Canvas drawing problems

Posted: Thu Feb 19, 2015 10:10 am
by micha
S0lll0s wrote:Try moving all of those drawing calls to a one-time if in love.draw
Why should one do that? One can draw to canvases outside the love.draw function. And in this case, the process is only done once. What would be the benefit of moving it into the love.draw and then add additional structure to make sure it is only called once?

Re: Canvas drawing problems

Posted: Thu Feb 19, 2015 2:25 pm
by apajx
Otherwise the canvas itself is drawn with the color black in random cases (namely in those cases, where the last pixel was black).
Wow. Thank you. I feel like a complete idiot. No wonder it was dependent on the random number generator... because it was completely dependent on the color the last rectangle got...

Re: Canvas drawing problems

Posted: Thu Feb 19, 2015 3:41 pm
by s-ol
micha wrote:
S0lll0s wrote:Try moving all of those drawing calls to a one-time if in love.draw
Why should one do that? One can draw to canvases outside the love.draw function. And in this case, the process is only done once. What would be the benefit of moving it into the love.draw and then add additional structure to make sure it is only called once?
To check whether there still could be an issue with that. Glad you got it to work, I didn't think about the actual color either.