Page 1 of 1

Help with creating a paint program!

Posted: Sun Apr 26, 2020 9:02 pm
by wujunjun
Hi! This is my first time posting :awesome: . I'm creating a simple paint tool using Love, and the idea is that every time the mouse is clicked and moved, the coordinate of the mouse will be stored in a table (linePoints), and then be connected in the draw function. But the problem is how do you create separate lines and make them all stay on the screen :C . Right now each time I draw a new line the previous will disappear.

Code: Select all

function love.load()
    lineSegment = {} --create lineSegment table
    
    
end

function love.update(dt)

end

function love.draw()
    if love.mouse.isDown("1") == true  then --if mouse is pressed
        for i, p in ipairs (lineSegment) do -- for each linePoints in lineSegments
            if tablelength(linePoints) >= 3 then -- call tablelength function, if length of table is > 3, draw linePoints
                love.graphics.line(linePoints)
            end
        end
end

end

function love.mousepressed( x, y, button, istouch, presses )
    linePoints = {} -- on mouse pressed create new line point table
    table.insert(lineSegment, linePoints) --insert them to lineSegment table so it can be drawn
    if love.mouse.isDown("1") then
        function love.mousemoved( x, y, dx, dy, istouch )--if mouse is hold down and moving
            table.insert(linePoints, x) -- insert x, y, point into linepoints table
            table.insert(linePoints, y)    
        end
    end
end

function love.mousereleased( x, y, button, istouch, presses )
    --linePoints = {}
    table.insert(lineSegment, linePoints) --when mouse is released, insert linePoint into linesegment
    print(tablelength(linePoints).."a")
    print(tablelength(lineSegment).."bbbb")
    
end



function tablelength(t) -- a function to count the length of table
    local count = 0
    for _ in pairs(t) do count = count + 1 end
    return count
end
Thanks!!!

Re: Help with creating a paint program!

Posted: Mon Apr 27, 2020 6:59 am
by pgimeno
Welcome to the forums!

You can use a Canvas, which will persist between frames. You draw the lines to the canvas, and then the canvas to the screen.

You don't really need a table to hold the points; just two variables to hold the X and Y of the last point should suffice.

Re: Help with creating a paint program!

Posted: Mon Apr 27, 2020 10:53 am
by zorg
Alternatively, you can also do it with a table holding all past points you detected, iterating over all of them and drawing them pairwise... in that case, you don't need a canvas, but the age-old tradeoff of memory vs. processing is true here as well; the canvas is fast since you just draw the last line onto it, the table approach is slower because depending on the number of segments you have, the iteration can take a while (note that i'm talking about points in the ten to hundred thousands)

Re: Help with creating a paint program!

Posted: Tue Apr 28, 2020 12:39 am
by wujunjun
pgimeno wrote: Mon Apr 27, 2020 6:59 am Welcome to the forums!

You can use a Canvas, which will persist between frames. You draw the lines to the canvas, and then the canvas to the screen.

You don't really need a table to hold the points; just two variables to hold the X and Y of the last point should suffice.
Ok! I will try that. Thank you!

Re: Help with creating a paint program!

Posted: Tue Apr 28, 2020 12:40 am
by wujunjun
zorg wrote: Mon Apr 27, 2020 10:53 am Alternatively, you can also do it with a table holding all past points you detected, iterating over all of them and drawing them pairwise... in that case, you don't need a canvas, but the age-old tradeoff of memory vs. processing is true here as well; the canvas is fast since you just draw the last line onto it, the table approach is slower because depending on the number of segments you have, the iteration can take a while (note that i'm talking about points in the ten to hundred thousands)
Got it! I'll keep that in mind, thanks!

Re: Help with creating a paint program!

Posted: Tue Apr 28, 2020 12:43 am
by wujunjun
Thanks guys! problem solved.