[SOLVED]Help: making a "brush"

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
nice
Party member
Posts: 191
Joined: Sun Sep 15, 2013 12:17 am
Location: Sweden

[SOLVED]Help: making a "brush"

Post by nice »

Hello boys and girls!

I'm currently working on a project that I call "Helium", Helium is going to be a clone of Microsoft paint with brush that's 16x16 big.
And as you might have guessed I need some help, I have successfully managed to figure out how to draw a square and I have made it so that the square moves around by clicking (I'm a beginner at Löve and this is the first time I code something on my own).

I got things started by viewing a another thread that was similar to what I want to do: http://love2d.org/forums/viewtopic.php? ... een#p71772

This is what I need some help with:
1. I don't want to see the square in the corner (it's at 0,0 right now) I only want it to appear when I "draw" on screen.
2. I want to draw several squares on the screen so I can create an "art piece" and they keep their current position when I move on to another.
3. Also I have made a canvas (800x480) that I want to be contain within and I have a temporary UI (800x120) that I don't want you to doodle around on.

OPTIONAL:
I want to be able to draw continuously by holding down the mouse button and drag the mouse cursor up, down, left and right so I can make a straight line if I want

I hope I was clear with my explanation and please feel free to view my code
Attachments
main.lua
(889 Bytes) Downloaded 93 times
Last edited by nice on Sat Sep 13, 2014 1:27 pm, edited 1 time in total.
:awesome: Have a good day! :ultraglee:
User avatar
Plu
Inner party member
Posts: 722
Joined: Fri Mar 15, 2013 9:36 pm

Re: Help: making a "brush"

Post by Plu »

Alright, let's do some pointers then to get you on your way :)


Number 1 can be ignored. It's going to be solved automatically when you do number 2 :)

For number 2; if you want multiple of anything, you need to create a table to store them in. So you'll have to make a table that stores (for now) coordinates of each square you've placed. The simple versions looks like this:

Code: Select all

-- in love.load, we make an empty table:
squares = {}

-- in love.mousepressed, you can add something to the table on click by doing something like this:
local newPosition = {}
newPosition.x = x
newPosition.y = y
table.insert( squares, newPosition )
-- ( this copies the mouse's current x and y coordinates into a new table, and then stores that table in the list of squares )

-- in love.draw, you draw all the squares to the screen:
for key, square in ipairs( squares ) do
  love.graphics.rectangle( "fill", square.x, square.y ) -- this will draw each of them to the screen
end
Because you start with an empty table, you no longer have the problem of a square starting in the top corner.

For 3) you can cheat on making the UI by first drawing all the squares and then drawing a new black rectangle over the area where you don't want people to draw. Make sure it draws in opaque black (or whatever color you want) and go over the whole area where the UI has to go.

As for the optional; in order to allow you to drag around while constantly placing squares, you have to remove your love.mousepressed function (which reacts to single clicks) and instead add, inside your love.update, a check whether a mouse-button is (still) pressed:

Code: Select all

if love.mouse.isDown("l" ) then
 -- insert square here
end

See how far you can get with this :)
User avatar
nice
Party member
Posts: 191
Joined: Sun Sep 15, 2013 12:17 am
Location: Sweden

Re: Help: making a "brush"

Post by nice »

Hey thanks for helping me out it's appreciated!

But to the thing at hand
I copied and pasted in your code (just to see what happened) and it looked fine at first but then I clicked it crashed:

Code: Select all

function love.load()

squares = {}
xPos = 0
yPos = 0


--Draws canvas
canvas = love.graphics.newCanvas(800, 480)

end

-- Draws All The Graphics --
function love.draw()

-- Prints X And Y Position On The Upper Left Screen
local x, y = love.mouse.getPosition()
love.graphics.print("X:" ..x, 10, 10)
love.graphics.print("Y:" ..y, 10, 40)

-- UI: Banner Grey
love.graphics.setColor(195 , 195, 195)
love.graphics.rectangle("fill", 0, 480, 800, 120)

-- Draws A 16x16 Square That Follows The Mouse
love.graphics.rectangle("line", x, y, 16, 16)

-- Draws A 16x16 Square "brush"
love.graphics.rectangle("fill", xPos, yPos, 16, 16)

for key, square in ipairs( squares ) do
  love.graphics.rectangle( "fill", square.x, square.y ) 
end

end

--Mouse control--
function love.mousepressed(x, y, button)

	local newPosition = {}
	newPosition.x = x
	newPosition.y = y
	table.insert( squares, newPosition )
	if button == "l" then
	xPos = x
	yPos = y
	end
end 
I have included a screenshot for you to view of the bluescreen
Attachments
Screen Shot 2014-09-12 at 18.38.20.png
Screen Shot 2014-09-12 at 18.38.20.png (17.8 KiB) Viewed 2974 times
:awesome: Have a good day! :ultraglee:
User avatar
Plu
Inner party member
Posts: 722
Joined: Fri Mar 15, 2013 9:36 pm

Re: Help: making a "brush"

Post by Plu »

Yeah, it's missing the 4th (and 5th) argument to the draw rectangle function, I forgot to include them. You should be able to fix this yourself :) Check your original calls to those functions.
User avatar
nice
Party member
Posts: 191
Joined: Sun Sep 15, 2013 12:17 am
Location: Sweden

Re: Help: making a "brush"

Post by nice »

Plu wrote:Yeah, it's missing the 4th (and 5th) argument to the draw rectangle function, I forgot to include them. You should be able to fix this yourself :) Check your original calls to those functions.
Shouldn't be to hard to figure out, either way thanks for that part of the code! :ultrahappy:
:awesome: Have a good day! :ultraglee:
Post Reply

Who is online

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