help needed drawing lines

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
jurassicjordan
Prole
Posts: 1
Joined: Fri Mar 03, 2017 3:55 am

help needed drawing lines

Post by jurassicjordan »

I'm trying to make a sort of "drawing" program that simulates the LJN video art. I've done this before in Scratch (link here: https://scratch.mit.edu/projects/198480274/), but I want to port it to Löve. I have a basic cursor working, but It to leave a line behind when pressing a key. I thought of just creating a trail of rectangles, but the tutorial I'm learning the code to place said rectangles (https://www.youtube.com/watch?v=FUiz1kL0QtI) is very outdated and doesn't work(and yes i know about the changed space key thing, the problem is with the "for _,v do". Plus I imagine that generating that many rectangles will destroy the FPS. I'm a newcomer to Löve2D and I thought this would be a simple starter game to make. For the record, I don't care about saving Images, or using the mouse, I want to strictly use arrow keys or a joystick. Please help.
oh and here is what I have so far for code, its just a cursor.

Code: Select all

function love.load()
 cursor = {}
 cursor.x = 385
 cursor.y = 285
 cursor.pixel = {}
 cursor.draw = function()
	pixel = {}
	pixel.x = cursor.x
	pixel.y = cursor.y
	table.insert(cursor.pixel, pixel)
end
end


function love.draw(dt)
 love.graphics.setColor( 100, 100, 100)
 love.graphics.rectangle("fill", cursor.x, cursor.y, 3, 3)
end


function love.update(dt)
 --cursor movement
	if love.keyboard.isDown("up") then
		cursor.y = cursor.y - 20
	elseif love.keyboard.isDown("down") then
		cursor.y = cursor.y + 20
	end
	
	if love.keyboard.isDown("left") then
		cursor.x = cursor.x - 20
	elseif love.keyboard.isDown("right") then
		cursor.x = cursor.x + 20
	end
	--thx drunken_munki for help with diagonals
	
 --cursor bounds
	if cursor.y < 0 then
		cursor.y= 1
	end
	
	if cursor.y > 600 then
		cursor.y= 599
	end
	
	if cursor.x < 0 then
		cursor.x= 1
	end
	
	if cursor.x > 800 then
		cursor.x= 799
	end
	
 --drawing control
 	if love.keyboard.isDown("space") then
	table.insert(cursor.pixel, pixel)
	end
end
User avatar
kbmonkey
Party member
Posts: 138
Joined: Tue Sep 01, 2015 12:19 pm
Location: Sydney
Contact:

Re: help needed drawing lines

Post by kbmonkey »

Every call to love.draw clears the screen and keeping a table of points to redraw each time is expensive.

What you need is a canvas. Simply draw on the canvas in memory and the draw the canvas to the screen.

The other thing I added was factoring your movement out by dt. This is important because then your game works the same speed for all computers (slow and fast alike).

Code: Select all

local mycanvas = nil
function drawOnCanvas(x, y)

    -- make a new canvas (this only happens once)
    if not mycanvas then
        mycanvas = love.graphics.newCanvas()
        print("making new canvas")
    end

    -- redirect any drawing to this canvas
    love.graphics.setCanvas(mycanvas)

    -- do the drawing
    love.graphics.setColor(255, 255, 255)
    love.graphics.rectangle("fill", x, y, 3, 3)

    -- reset the game canvas
    love.graphics.setCanvas()

end

function love.load()
    cursor = {}
    cursor.x = 385
    cursor.y = 285
    cursor.pixel = {}
end


function love.draw(dt)
    if mycanvas then
        love.graphics.setColor(255, 255, 255)
        love.graphics.draw(mycanvas)
    end
    love.graphics.setColor( 100, 100, 100)
    love.graphics.rectangle("fill", cursor.x, cursor.y, 3, 3)
end


function love.update(dt)
 --cursor movement
    if love.keyboard.isDown("up") then
        cursor.y = cursor.y - 100 * dt
    elseif love.keyboard.isDown("down") then
        cursor.y = cursor.y + 100 * dt
    end

    if love.keyboard.isDown("left") then
        cursor.x = cursor.x - 100 * dt
    elseif love.keyboard.isDown("right") then
        cursor.x = cursor.x + 100 * dt
    end
    --thx drunken_munki for help with diagonals

 --cursor bounds
    if cursor.y < 0 then
        cursor.y= 1
    end

    if cursor.y > 600 then
        cursor.y= 599
    end

    if cursor.x < 0 then
        cursor.x= 1
    end

    if cursor.x > 800 then
        cursor.x= 799
    end

    --drawing control
    if love.keyboard.isDown("space") then
        drawOnCanvas(cursor.x, cursor.y)
    end
end

User avatar
kbmonkey
Party member
Posts: 138
Joined: Tue Sep 01, 2015 12:19 pm
Location: Sydney
Contact:

Re: help needed drawing lines

Post by kbmonkey »

Oh ps - welcome to the forums!
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 52 guests