Page 1 of 1

Tiles, arrays and quads

Posted: Wed May 13, 2020 12:41 pm
by ricande
I'm trying to read an image as an array.
The image is 600*200px
I need an array in two dimensions. x and y
All tiles are 100px both x and y.

- - - - - - - -
| | | | | | |
- - - - - - - -
| | | | | | |
- - - - - - - -

As a test i would like to write it to the screen.
Here is what i got.

Code: Select all

img = love.graphics.newImage("test.png")
 
for x =0, 5 do
    for y =0, 1 do
        tile[x,y]=love.graphics.newQuad(x*100,y*100,(x*100)+100, (y*100)+100, img:getDimensions())
    end
end

function love.draw()
    for x =0, 5 do
        for y =0, 1 do
            love.graphics.draw(img, tile(x,y), x*100, y*100)
        end
    end

end

Re: Tiles, arrays and quads

Posted: Wed May 13, 2020 1:09 pm
by sphyrth
Here's a re-implementation of your code. Take note of my usage of tile[x][y] compared to your tile[x,y] in your first Loop... and the tile[x] = {} before that.
I also don't know the context of your tile(x,y) in love.draw(), so I also converted it to tile[x][y].

Code: Select all

img = love.graphics.newImage("test.png")
tile = {}
 
for x =0, 5 do
  tile[x] = {}
  for y =0, 1 do
    tile[x][y]=love.graphics.newQuad(x*100,y*100,(x*100)+100, (y*100)+100, img:getDimensions())
  end
end

function love.draw()
  for x =0, 5 do
    for y =0, 1 do
      love.graphics.draw(img, tile[x][y], x*100, y*100)
    end
  end
end


Re: Tiles, arrays and quads

Posted: Wed May 13, 2020 1:42 pm
by ricande
Here's a re-implementation of your code. Take note of my usage of tile[x][y] compared to your tile[x,y] in your first Loop... and the tile[x] = {} before that.
I also don't know the context of your tile(x,y) in love.draw(), so I also converted it to tile[x][y].
You understood me perfectly! <3

Thank you sphyrth. It works now.
But i get tearing.
The 12 tiles are drawn correctly, but then....
Edit: I don't know why the images wont show, but you can "right-click" them and open in new tab.
Image

Here is the original image:
Image

Re: Tiles, arrays and quads

Posted: Thu May 14, 2020 6:34 pm
by zorg
Images aren't showing because you put the imgur pages' URLs into image tags, not the direct links themselves. You wanted these:
https://love2d.org/imgmirrur/wGeM2EJ.jpg
https://love2d.org/imgmirrur/HqwwxqJ.gif
If those are what you put in, then imgur might be doing hotlink-protection, not sure.

As for tearing, there might be some miscalculations this time, i want to doubt that quad-bleeding would be the issue here... although to be honest, i also want to assume that if we knew what you wanted to implement, your end goal, we could suggest a better method to implement it with. :3