Page 1 of 1

[SOLVED] Drawing on canvas works wrong

Posted: Tue Jan 22, 2019 2:04 am
by DGM
Hello again,

More trouble with canvases. Again, this is with v11.2 on 64-bit Windows 10.

I'm attempting to draw a white square with a smaller blue square inside of it to a canvas, then draw the result to the screen. Instead what I get is a solid blue square with no hint of white. Here's the full code of the test program:

Code: Select all

function love.load()
	love.window.setMode(1280, 720)
	love.graphics.setDefaultFilter("nearest", "nearest")

	canvas_1 = love.graphics.newCanvas(500, 500)
end

function love.update()
end

function love.draw()

	love.graphics.setCanvas(canvas_1)
	love.graphics.clear()
	love.graphics.setColor(255/255, 255/255, 255/255, 1)
	love.graphics.rectangle("fill", 0, 0, 499, 499)
	love.graphics.setColor(0/255, 0/255, 255/255, 1)
	love.graphics.rectangle("fill", 100, 100, 300, 300)
	love.graphics.setCanvas()

	love.graphics.clear()
	love.graphics.draw(canvas_1, 350, 100, 0, 1, 1, 0, 0, 0, 0)
end
Any ideas?

Re: Drawing on canvas works wrong

Posted: Tue Jan 22, 2019 2:17 am
by pgimeno
You need to draw the canvas to the screen with white colour, otherwise you're tinting it :)

The last clear() isn't necessary, by the way.

Re: [SOLVED] Drawing on canvas works wrong

Posted: Tue Jan 22, 2019 2:23 am
by DGM
That fixed it, thanks.

But why is the last clear unnecessary? I get that I don't need it here since the image doesn't change, but aren't I normally supposed to clear the screen between frames?

EDIT: One more question. Does the tint apply to everything drawn with the graphics.draw() function? I just realized my forum account lets me edit the docs and I feel like adding a warning.

Re: [SOLVED] Drawing on canvas works wrong

Posted: Tue Jan 22, 2019 6:38 am
by pedrosgali
You get one free clear at the start of the draw loop as love clears the screen for you ready to draw the new frame. As for the tint, yes it applies to everything drawn. You can use this to your advantage though, I draw all my UI assets in greyscale and let the user pick the UI colour.

Re: [SOLVED] Drawing on canvas works wrong

Posted: Tue Jan 22, 2019 11:03 pm
by DGM
pedrosgali wrote: Tue Jan 22, 2019 6:38 am You get one free clear at the start of the draw loop as love clears the screen for you ready to draw the new frame.
But the free clear applies only to the screen and not to the canvases, correct? That makes sense.

As for the tint, yes it applies to everything drawn. You can use this to your advantage though, I draw all my UI assets in greyscale and let the user pick the UI colour.
Thanks. I've added a note to the draw() function doc.

Re: [SOLVED] Drawing on canvas works wrong

Posted: Wed Jan 23, 2019 1:18 am
by pgimeno
Anything drawn with this function will be tinted according to the currently selected color. Set it to pure white to preserve the object's original colors.
Hm, that's only true when using the default shader, which is the one that tints it. When using a custom shader, it depends on whether the shader supports the colour parameter.

Re: [SOLVED] Drawing on canvas works wrong

Posted: Wed Jan 23, 2019 5:28 am
by DGM
Thanks, I've changed the text to mention that. Anything else I should know?

Re: [SOLVED] Drawing on canvas works wrong

Posted: Wed Jan 23, 2019 10:55 am
by pgimeno
LGTM :)