[Solved] Canvas drawing problems

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
apajx
Prole
Posts: 2
Joined: Thu Feb 19, 2015 5:07 am

[Solved] Canvas drawing problems

Post 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
Last edited by apajx on Thu Feb 19, 2015 3:25 pm, edited 1 time in total.
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Canvas drawing problems

Post 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).
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Canvas drawing problems

Post by s-ol »

Try moving all of those drawing calls to a one-time if in love.draw

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Canvas drawing problems

Post 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?
apajx
Prole
Posts: 2
Joined: Thu Feb 19, 2015 5:07 am

Re: Canvas drawing problems

Post 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...
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Canvas drawing problems

Post 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.

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 2 guests