Page 1 of 1

[SOLVED]How to draw an image in a table?

Posted: Thu Jan 05, 2017 2:33 am
by Taaaaum
Well ... I'm very new to game development and I started this a few hours ago to be honest. I've been reading docs in Lua and Love docs to try to produce something and what I'm trying to do is a simple board and I'd like to put a chunk inside the table index. The following code is showing you ... but the image is positioning at the edge of the window.

What am I trying to do is ok or is there another easy way out of it?

Code: Select all

Tabuleiro = {}
vazio = nil
casax, casay = 0,0;
Piece1= {x = 0, y = 0, img = nil}

function IniciaTabuleiro()
	for i = 1, 10 do
		Tabuleiro[i] = {}
		for j = 1, 10 do
			Tabuleiro[i][j] = 0 -- Inicia o tabuleiro
		end
	end
	
end 

function love.load(args)	
		vazio = love.graphics.newImage('vazio.png') -- Salva img na memoria
		Piece1.img = love.graphics.newImage('aviao1.png')
		
		IniciaTabuleiro()
		
end

function love.update(dt)
	if love.keyboard.isDown('escape') then
		love.event.push('quit') -- Fecha quando aperta ESC
	end
	Tabuleiro.x = 5
	Tabuleiro.y = 5
end

function love.draw(dt) 
	for x = 1, 10 do
		for y = 1, 10 do
					
					Tabuleiro[x][y] = love.graphics.draw(vazio, y * 50 - 50, x * 50 - 50) -- Desenha o tabuleiro
					
	
		end
	end

	Tabuleiro[2][3] = love.graphics.draw(Piece1.img, 450, 400)	
end

Re: How to draw an image in a table?

Posted: Thu Jan 05, 2017 8:49 am
by nyenye
Hii, welcome to the forums.

From what I see you are trying to make some sort of chess game? What I'd do is draw the empty board as a whole image. Then on your matrix you keep note of your pieces and theyr position, and draw each one on top of the board.

You will have to know width and height of the cells and draw acordingly.

Hope it helps.

Re: How to draw an image in a table?

Posted: Thu Jan 05, 2017 10:28 am
by zorg
First, a tip: Posting english code snippets amounts for faster/better help. :3

Now, onto the issues: (from smaller to bigger)
- anything = nil will only ever be a placeholder, it doesn't make sense to have it, unless you want a visual reminder of those variables.
- Same with someTable.somefield = nil
- You don't -technically- need to initialize the inner tables to 0, only those that hold tables themselves, but you might have a good reason to do it anyway.

- Why are you setting Tabuleiro.x and y to 5 every update?

- love.graphics.draw draws an image on the screen, it has no return value. You'd either want to store images in the table slots, or some other data that can tell the drawing code what to draw in love.draw. I'm guessing you want to do the latter.

So, here's a bit of a rewrite; not saying it'll work, i haven't tested it myself, but it should show how things would work more correctly.

Code: Select all

-- Use locals, then it makes sense to have nil variables.
local Tabuleiro = {}
local vazio -- don't even need the explicit "= nil" written out! :3
local casax, casay = 0,0;
local Piece1 = {x = 0, y = 0, img = nil}

function IniciaTabuleiro()
   for i = 1, 10 do
      Tabuleiro[i] = {}
      -- As long as Tabuleiro[i][j] is not a table you'd accidentally index into, you don't need to initialize it.
      -- But since you have an empty image...
      Tabuleiro[i][j] = vazio
   end
   
end 

function love.load(args)   
      vazio = love.graphics.newImage('vazio.png') -- Salva img na memoria
      Piece1.img = love.graphics.newImage('aviao1.png')
      IniciaTabuleiro()
      Tabuleiro[2][3] = Piece1.img
end

function love.update(dt)
   if love.keyboard.isDown('escape') then
      love.event.push('quit') -- Fecha quando aperta ESC
   end
end

function love.draw(dt) 
   for x = 1, #Tabuleiro do -- This way you get the max number from the table itself.
      for y = 1, #Tabuleiro[x] do -- Same here
         love.graphics.draw(Tabuleiro[x][y], (y-1) * 50, (x-1) * 50) -- Desenha o tabuleiro
      end
   end  
end

Re: How to draw an image in a table?

Posted: Thu Jan 05, 2017 11:47 am
by Taaaaum
Thanks zorg and nyenye, you too were very fast and helpfull.


- Why are you setting Tabuleiro.x and y to 5 every update? It's easy to answer, I was TOTALLY drunk of sleep, and just doing things without think.

- I knew that I can store images in table because Lua properties is very dinamic about tables, I just don't knew how to do and you made it clear so now I can sleep early today.