I wanted to draw some functions using love.graphics.points
it works, I can see the individual points, but it's not persistent, a new point replace the previous one, I wanted it to draw some curves or lines, like in a graphical.
function love.load()
x, y = 0, 0
t=0
end
function love.update(dt)
z=math.abs(math.sin(t)/math.cos(y+t))
t = t +1
end
function love.draw()
love.graphics.points(t,10+z*44)
end
local canvas, x, y, t
function love.load()
x, y = 0, 0
t = 0
canvas = love.graphics.newCanvas()
end
function love.update(dt)
love.graphics.setCanvas(canvas)
local z = math.abs(math.sin(t)/math.cos(y+t))
love.graphics.rectangle("fill", t, 10 + z*44, 1, 1)
t = t + 1
love.graphics.setCanvas()
end
function love.draw()
love.graphics.draw(canvas)
end
Edit:
ReFreezed wrote: ↑Fri Jul 17, 2020 11:26 pm
You may want to replace the default love.run as it's calling love.graphics.clear every frame.
Don't do that. There are good reasons for that to happen. Typically love.graphics.present uses double (or triple) buffering, and if you don't completely overwrite each buffer before it's drawn to the screen, the results are undefined. Furthermore, due to this, you may have half the pixels drawn to one of the buffers and the other half drawn to the other; and that's assuming that it's a double buffer and not a triple buffer.
pgimeno wrote: ↑Sat Jul 18, 2020 10:14 am
Well, the obvious way is to draw the plot to a canvas.
Note that canvases will still be cleared if the window is resized. The safest way is to either draw each point every frame or to use an ImageData object.
function love.resize(w, h)
local oldcanvas = canvas
canvas = love.graphics.newCanvas(w, h)
love.graphics.setCanvas(canvas)
love.graphics.draw(oldcanvas)
love.graphics.setCanvas()
oldcanvas:release()
end
This has the problem that if the plot exceeded the screen limit, there will be a part that is blank. If you want to solve that, you can just create the initial canvas with the maximum size supported and forget about resizing, e.g.
Wiki wrote:Changing the display mode may have side effects: for example, canvases will be cleared and values sent to shaders with Shader:send will be erased. Make sure to save the contents of canvases beforehand or re-draw to them afterward if you need to.
I guess I should have said "may" instead of "will", unless the wiki is plainly wrong. Canvases don't get cleared in my tests either. *shrug*
Well, is there a difference? The only way to resize the window through code is setMode and updateMode. Maybe it is that canvases won't ever get cleared if the window size is the only property that has changed when calling setMode/updateMode, and that dragging borders to resize is actually safe too. I'm just wildly speculating here.