Help with creating a paint program!

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
wujunjun
Prole
Posts: 5
Joined: Sun Apr 26, 2020 8:50 pm

Help with creating a paint program!

Post 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!!!
User avatar
pgimeno
Party member
Posts: 3656
Joined: Sun Oct 18, 2015 2:58 pm

Re: Help with creating a paint program!

Post 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.
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Help with creating a paint program!

Post 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)
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
wujunjun
Prole
Posts: 5
Joined: Sun Apr 26, 2020 8:50 pm

Re: Help with creating a paint program!

Post 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!
wujunjun
Prole
Posts: 5
Joined: Sun Apr 26, 2020 8:50 pm

Re: Help with creating a paint program!

Post 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!
wujunjun
Prole
Posts: 5
Joined: Sun Apr 26, 2020 8:50 pm

Re: Help with creating a paint program!

Post by wujunjun »

Thanks guys! problem solved.
Post Reply

Who is online

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