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!
Drawing a Grid
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Re: Drawing a Grid
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:
Then create ONE quad that is much bigger than the image.
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)
Last edited by Przemator on Thu Nov 22, 2012 5:59 pm, edited 1 time in total.
Re: Drawing a Grid
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.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:
Then create ONE quad that is much bigger than the image.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)
Re: Drawing a Grid
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:
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
Didn't work for me. I ended up rewriting it and it worked this time: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
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
Re: Drawing a Grid
Here, find attached the exact code that I pasted, it works.
What you're doing is really not optimal IMO.
What you're doing is really not optimal IMO.
- Attachments
-
- grid.love
- Grid
- (620 Bytes) Downloaded 193 times
Re: Drawing a Grid
Another approach is to use love.graphics.rectangle("line").
Re: Drawing a Grid
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.Przemator wrote:Here, find attached the exact code that I pasted, it works.
What you're doing is really not optimal IMO.
Yeah I was using that before but because I couldn't change the image or colour of the lines I went with using Quads.Kadoba wrote:Another approach is to use love.graphics.rectangle("line").
Re: Drawing a Grid
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.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
Well damn. That would've made it a lot easier. Thanks a lot xD!Kadoba wrote: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.Yeah I was using that before but because I couldn't change the image or colour of the lines I went with using Quads.
Who is online
Users browsing this forum: Bing [Bot] and 0 guests