Tiles, arrays and quads

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
ricande
Prole
Posts: 2
Joined: Wed May 13, 2020 12:32 pm

Tiles, arrays and quads

Post 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
The difference between stupidity and genius is that genius has its limits.
sphyrth
Party member
Posts: 260
Joined: Mon Jul 07, 2014 11:04 am
Contact:

Re: Tiles, arrays and quads

Post 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

User avatar
ricande
Prole
Posts: 2
Joined: Wed May 13, 2020 12:32 pm

Re: Tiles, arrays and quads

Post 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
The difference between stupidity and genius is that genius has its limits.
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Tiles, arrays and quads

Post 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
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest