Drawing a Grid

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.
User avatar
Sekaru
Prole
Posts: 17
Joined: Sun Oct 07, 2012 5:09 pm

Drawing a Grid

Post by Sekaru »

So I'm trying to draw a 32x32 grid. Before I was using the .line function to draw it but I couldn't really find any way of colouring the line so I went with using quads.

I've been messing around with this for a while and I keep getting a "world.lua.26: attempt to index global 'columngrid' (a nil value)" and I'm not really sure how to fix it.

I've attached the world.lua I'm doing this in. Thanks in advance!
world.lua
(802 Bytes) Downloaded 131 times
User avatar
Przemator
Party member
Posts: 107
Joined: Fri Sep 28, 2012 6:59 pm

Re: Drawing a Grid

Post by Przemator »

I'm not sure, but if you want to make a grid, should it not be a two-dimensional array? ( grid[x][y] )

But I think you can achieve what you want in a MUCH simpler way. Just use this function:

Code: Select all

grid = love.graphics.newImage('/misc/grid.png')
grid:setWrap("repeat", "repeat")
quad = love.graphics.newQuad(0, 0, 16000, 16000, grid:getWidth(), grid:getHeight())
love.graphics.drawq(grid, quad, 0, 0)
Then create ONE quad that is much bigger than the image.
Last edited by Przemator on Thu Nov 22, 2012 5:59 pm, edited 1 time in total.
User avatar
Sekaru
Prole
Posts: 17
Joined: Sun Oct 07, 2012 5:09 pm

Re: Drawing a Grid

Post by Sekaru »

Przemator wrote:I'm not sure, but if you want to make a grid, should it not be a two-dimensional array? ( grid[x][y] )

But I think you can achieve what you want in a MUCH simpler way. Just use this function:

Code: Select all

grid = love.graphics.newImage('/misc/grid.png')
grid:setWrap("repeat", "repeat")
quad = love.graphics.newQuad(0, 0, 16000, 16000)
love.graphics.drawq(grid, quad, 0, 0)
Then create ONE quad that is much bigger than the image.
I gave that a go and split the drawq off into my rendering function and the rest into a loading function. I'm getting a "Incorrect parameter type: expected userdata" on the drawq line.
User avatar
Przemator
Party member
Posts: 107
Joined: Fri Sep 28, 2012 6:59 pm

Re: Drawing a Grid

Post by Przemator »

I made a small mistake when creating the quad, missed the reference width and height arguments.

Just tested this and it works like a charm:

Code: Select all

function love.load()
	grid = love.graphics.newImage("grid.png")
	grid:setWrap("repeat", "repeat")
	quad = love.graphics.newQuad(0, 0, 16000, 16000, grid:getWidth(), grid:getHeight())
end

function love.draw()
	love.graphics.drawq(grid, quad, 0, 0)
end
User avatar
Sekaru
Prole
Posts: 17
Joined: Sun Oct 07, 2012 5:09 pm

Re: Drawing a Grid

Post by Sekaru »

Przemator wrote:I made a small mistake when creating the quad, missed the reference width and height arguments.

Just tested this and it works like a charm:

Code: Select all

function love.load()
	grid = love.graphics.newImage("grid.png")
	grid:setWrap("repeat", "repeat")
	quad = love.graphics.newQuad(0, 0, 16000, 16000, grid:getWidth(), grid:getHeight())
end

function love.draw()
	love.graphics.drawq(grid, quad, 0, 0)
end
Didn't work for me. I ended up rewriting it and it worked this time:

Code: Select all

world = {
	width   = 16000,
	height  = 16000,
	rows    = 500,
	columns = 500
	}
	
function initWorld()
	grid = love.graphics.newImage('/misc/grid.png')

	columngrid = {}
	columnWidth = world.width/world.columns
	
	for i = 1, world.columns do
		columngrid[i] = love.graphics.newQuad(i*columnWidth, 0, 1, world.height, grid:getWidth(), grid:getHeight())
	end
	
	rowgrid = {}
	rowHeight = world.height/world.rows
	
	for i = 1, world.rows do
		rowgrid[i] = love.graphics.newQuad(0, i*rowHeight, world.width, 1, grid:getWidth(), grid:getHeight())
	end
end

function drawWorld()
	
    for i = 1, world.columns do
		love.graphics.drawq(grid, columngrid[i], i*columnWidth, 0)
	end

	for i = 1, world.rows do
		love.graphics.drawq(grid, rowgrid[i], 0, i*rowHeight)
	end
end
Thanks for your help anyway.
User avatar
Przemator
Party member
Posts: 107
Joined: Fri Sep 28, 2012 6:59 pm

Re: Drawing a Grid

Post by Przemator »

Here, find attached the exact code that I pasted, it works.

What you're doing is really not optimal IMO.
Attachments
grid.love
Grid
(620 Bytes) Downloaded 145 times
User avatar
Kadoba
Party member
Posts: 399
Joined: Mon Jan 10, 2011 8:25 am
Location: Oklahoma

Re: Drawing a Grid

Post by Kadoba »

Another approach is to use love.graphics.rectangle("line").
grid.love
(255 Bytes) Downloaded 259 times
User avatar
Sekaru
Prole
Posts: 17
Joined: Sun Oct 07, 2012 5:09 pm

Re: Drawing a Grid

Post by Sekaru »

Przemator wrote:Here, find attached the exact code that I pasted, it works.

What you're doing is really not optimal IMO.
I'm pretty new to LUA so I'm not really sure how to optimise yet. What you're doing is essentially tiling an image, what I'm doing is drawing lines to create a grid.
Kadoba wrote:Another approach is to use love.graphics.rectangle("line").
Yeah I was using that before but because I couldn't change the image or colour of the lines I went with using Quads.
User avatar
Kadoba
Party member
Posts: 399
Joined: Mon Jan 10, 2011 8:25 am
Location: Oklahoma

Re: Drawing a Grid

Post by Kadoba »

Yeah I was using that before but because I couldn't change the image or colour of the lines I went with using Quads.
Whoops. Didn't see that part. You can change the color of all love.graphics functions by using love.graphics.setColor. Just be sure to set the draw color back to white (255,255,255,255) when you're done.
User avatar
Sekaru
Prole
Posts: 17
Joined: Sun Oct 07, 2012 5:09 pm

Re: Drawing a Grid

Post by Sekaru »

Kadoba wrote:
Yeah I was using that before but because I couldn't change the image or colour of the lines I went with using Quads.
Whoops. Didn't see that part. You can change the color of all love.graphics functions by using love.graphics.setColor. Just be sure to set the draw color back to white (255,255,255,255) when you're done.
Well damn. That would've made it a lot easier. Thanks a lot xD!
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 18 guests