Page 1 of 2
Drawing a Grid
Posted: Thu Nov 22, 2012 4:56 pm
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!
Re: Drawing a Grid
Posted: Thu Nov 22, 2012 5:07 pm
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.
Re: Drawing a Grid
Posted: Thu Nov 22, 2012 5:42 pm
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.
Re: Drawing a Grid
Posted: Thu Nov 22, 2012 6:01 pm
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
Re: Drawing a Grid
Posted: Thu Nov 22, 2012 6:19 pm
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.
Re: Drawing a Grid
Posted: Thu Nov 22, 2012 6:35 pm
by Przemator
Here, find attached the exact code that I pasted, it works.
What you're doing is really not optimal IMO.
Re: Drawing a Grid
Posted: Thu Nov 22, 2012 7:47 pm
by Kadoba
Another approach is to use love.graphics.rectangle("line").
Re: Drawing a Grid
Posted: Thu Nov 22, 2012 8:19 pm
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.
Re: Drawing a Grid
Posted: Thu Nov 22, 2012 8:41 pm
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.
Re: Drawing a Grid
Posted: Thu Nov 22, 2012 9:03 pm
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!