Efficiently drawing a grid map

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
Gunroar:Cannon()
Party member
Posts: 1141
Joined: Thu Dec 10, 2020 1:57 am

Re: Efficiently drawing a grid map

Post by Gunroar:Cannon() »

So I could make an atlas of all the tiles used using imagedata, then sort it(?) and then use the sprite batch
...
how? :)
The risk I took was calculated,
but man, am I bad at math.

-How to be saved and born again :huh:
User avatar
darkfrei
Party member
Posts: 1197
Joined: Sat Feb 08, 2020 11:09 pm

Re: Efficiently drawing a grid map

Post by darkfrei »

Gunroar:Cannon() wrote: Sun Apr 18, 2021 1:53 pm So I could make an atlas of all the tiles used using imagedata, then sort it(?) and then use the sprite batch
...
how? :)
I've made such spritesheet and used sprites (quads) from it:
https://love2d.org/forums/viewtopic.php?f=3&t=90857
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
Gunroar:Cannon()
Party member
Posts: 1141
Joined: Thu Dec 10, 2020 1:57 am

Re: Efficiently drawing a grid map

Post by Gunroar:Cannon() »

??Hmmm... Do you get the pictures individually and then put them in one image data, using the data as a spritsheet for a sprite batch? Because that's what I'm asking for.
The risk I took was calculated,
but man, am I bad at math.

-How to be saved and born again :huh:
User avatar
pgimeno
Party member
Posts: 3656
Joined: Sun Oct 18, 2015 2:58 pm

Re: Efficiently drawing a grid map

Post by pgimeno »

No idea of how your project is structured. But you mentioned a grid, so I imagine that the images to draw in that grid are all of the same size. Create an image whose sides are multiples of the horizontal and vertical size respectively, of the images to include, that is able to fit all the images. Then place each image in the big one, allocating a quad at the same time, e.g. in left-to-right order then the next row and so on. Not sure what's complicated about it.

Finally, instead of drawing images, you draw quads of the big image. Or build a spritebatch, your call. Again, hard to know what's best with just the given info.

If your images are of varied sizes, there are algorithms for creating texture atlases automatically. The idea is the same, except for the row-by-row part. I think there have been libs posted in this forum too. Search.
User avatar
Gunroar:Cannon()
Party member
Posts: 1141
Joined: Thu Dec 10, 2020 1:57 am

Re: Efficiently drawing a grid map

Post by Gunroar:Cannon() »

They are of the same size, thnx pgimeno, though can't I order it when the map is loaded, if it's just one big map, then just put it into an imagedata(what's up with me and imageData :crazy: )
The risk I took was calculated,
but man, am I bad at math.

-How to be saved and born again :huh:
User avatar
milon
Party member
Posts: 472
Joined: Thu Jan 18, 2018 9:14 pm

Re: Efficiently drawing a grid map

Post by milon »

pgimeno wrote: Sat Apr 17, 2021 10:56 am A texture atlas is the way to speed it up. SpriteBatch is made mostly for this kind of usage, and requires a texture atlas.
I thought sprite batching became automatic in Love a few versions ago? Am I mistaken about what that meant?
Any code samples/ideas by me should be considered Public Domain (no attribution needed) license unless otherwise stated.
User avatar
pgimeno
Party member
Posts: 3656
Joined: Sun Oct 18, 2015 2:58 pm

Re: Efficiently drawing a grid map

Post by pgimeno »

milon wrote: Mon Apr 19, 2021 6:50 pm I thought sprite batching became automatic in Love a few versions ago? Am I mistaken about what that meant?
I mention that later on.
https://love2d.org/forums/viewtopic.php ... 86#p240186

Manual batching is still available. As I mention in that post, I'm not sure whether it improves performance or not.
User avatar
milon
Party member
Posts: 472
Joined: Thu Jan 18, 2018 9:14 pm

Re: Efficiently drawing a grid map

Post by milon »

pgimeno wrote: Mon Apr 19, 2021 7:07 pm
milon wrote: Mon Apr 19, 2021 6:50 pm I thought sprite batching became automatic in Love a few versions ago? Am I mistaken about what that meant?
I mention that later on.
https://love2d.org/forums/viewtopic.php ... 86#p240186

Manual batching is still available. As I mention in that post, I'm not sure whether it improves performance or not.
Note to self: Get better at reading, lol :crazy:
Thanks!
Any code samples/ideas by me should be considered Public Domain (no attribution needed) license unless otherwise stated.
DrNefario
Prole
Posts: 6
Joined: Thu Apr 08, 2021 7:44 pm

Re: Efficiently drawing a grid map

Post by DrNefario »

You could draw the whole map to a canvas, and then just draw the canvas each frame. The problem there would likely be texture size, depending on how big your map is. Or you could draw a screenful to a canvas, or slightly more than a screenful to give you a bit of scrolling before you need to redraw.

If you really want to use ImageData rather than Canvas, you can generate ImageData from a Canvas. I'm new to Love myself, so I don't totally understand what the advantages are.
User avatar
Gunroar:Cannon()
Party member
Posts: 1141
Joined: Thu Dec 10, 2020 1:57 am

Re: Efficiently drawing a grid map

Post by Gunroar:Cannon() »

DrNefario wrote: Tue Apr 20, 2021 11:44 am You could draw the whole map to a canvas, and then just draw the canvas each frame. The problem there would likely be texture size, depending on how big your map is. Or you could draw a screenful to a canvas, or slightly more than a screenful to give you a bit of scrolling before you need to redraw.

If you really want to use ImageData rather than Canvas, you can generate ImageData from a Canvas. I'm new to Love myself, so I don't totally understand what the advantages are.
Thnx, that's a nice approach. I'll try it.
The risk I took was calculated,
but man, am I bad at math.

-How to be saved and born again :huh:
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 3 guests