Cheapest Tile based system?

Anything libgdx related goes here!

Cheapest Tile based system?

Postby Dark_Cron » Mon Jun 03, 2013 2:47 pm

Hello there DarkCron here,
just a quick question since I just started out programming with libgdx (again) and wanted to make a small tile-based game to get back into it...
I was wondering, since I'm not a pro at programming yet, but I do know how to a little bit of programming...
I'm trailing off, well for a tile based map say 50x50 tiles (16/32/64 pixels width, height) which I've done before, but does one "convert" every tile in a sprite or just draw plane 2d textures? I worked without the sprites before (don't know why), but now I'm actually using sprites and well it's more useful and way more easier to get around with to program, but it looks like it's resources heavier then what I used before... Is it?
I'm so sorry if this sounds pretty vague, I have really no idea which things in libgdx can be resource heavy and which don't... Of course I read some examples in the wiki, but I was just wondering.

Cheers.

EDIT: of course, I do know that making a tile map static with only the dynamic tiles being sprites is better, but hey.
Dark_Cron
 
Posts: 22
Joined: Sat Mar 16, 2013 7:12 pm

Re: Cheapest Tile based system?

Postby mzechner » Mon Jun 03, 2013 9:57 pm

Sprites aren't to terribly memory heavy. 4 vertices, each has 5 floats, so we are talking about 20 floats, or 80 bytes per sprite, vertex data only. Then a reference to a texture(region), etc. So a sprite weighs in at about 100 bytes in total. Drawing a sprite with a spritebatch can be super fast, as it's just an array copy.

The alternative of just keeping texture regions around for tiles, plus an array the tells you which tile uses which texture region is probably cheaper in terms of memory. Drawing wise it's a tiny little less efficient than a sprite, but nothing to worry about.

I generally don't use sprites for tile data, even if they are easier to handle. Check out the source code of the tile map renderers of libgdx on github on how they do it.
mzechner
Site Admin
 
Posts: 4715
Joined: Sat Jul 10, 2010 3:50 pm

Re: Cheapest Tile based system?

Postby Doofus » Tue Jun 04, 2013 12:28 pm

performance wise, if I have my own "tile system" so to speak and dont use tmx for example, what would you recommend for performance ?
Lets assume, I have like 6 layers in a tilemap. So rendering them all in a spritebatch with TextureRegions or sprites - is that fast enough ?
Well talking about PC here primarily.
Doofus
 
Posts: 125
Joined: Sat May 05, 2012 11:35 am

Re: Cheapest Tile based system?

Postby tonyp7 » Tue Jun 04, 2013 2:58 pm

In my game I wrote my own tilemap renderer. What I do is that first I render the static bottom layer in a spritecache. On top of that I render dynamically the tiles where characters can be in front or behind as I can't cache that.

As the majority of your tiles should be some sort of background using a cache makes sense.

Performance wise I believe it is the best option.
tonyp7
 
Posts: 329
Joined: Fri Feb 17, 2012 1:02 pm

Re: Cheapest Tile based system?

Postby Doofus » Tue Jun 04, 2013 3:32 pm

Ah yes SpriteCache. I believe you mean because some things never change you can cache them for better performance.

Not really sure how SpriteCache works and the difference to SpriteBatch. Is there a wiki tutorial for it ? :D
Doofus
 
Posts: 125
Joined: Sat May 05, 2012 11:35 am

Re: Cheapest Tile based system?

Postby tonyp7 » Wed Jun 05, 2013 1:36 am

SpriteCache is MUCH more performant because you don't need to build and send your vertices to the GPU at every frame.

I do it like that:
Code: Select all
@Override
   public void render(float dt) {

      
      if(firstFrame){
         firstFrame = false;
         
         if(tilemap.cachedGroundIndex == -1){
            //generate the sprite cache
             Game.spriteCache.setProjectionMatrix(Game.cam.combined);
             Game.spriteCache.beginCache();
             tilemap.render(Game.spriteCache);
             tilemap.cachedGroundIndex = Game.spriteCache.endCache();
         }
         
          
          //initial position of the camera at the center of the map
          Game.cam.zoom = 1.0f;
          Game.cam.position.set(tilemap.displaySize.x + tilemap.displaySize.width / 2.0f, tilemap.displaySize.y + tilemap.displaySize.height / 2.0f, 0.0f);
          Game.cam.update();
          updateCamRectange();
          

          
      }


and then:

Code: Select all
      //render the cache
      Game.spriteCache.setProjectionMatrix(Game.cam.combined);
      Game.spriteCache.begin();
      tilemap.renderCache(Game.spriteCache);
      Game.spriteCache.end();


tilemap.renderCache is just:

Code: Select all
public void renderCache(SpriteCache spriteCache) {
      spriteCache.draw(cachedGroundIndex);
      
   }


On a 50x50 tilemap you effectively save 50x50x4=10000 vertices for each frame. That's a lot.
tonyp7
 
Posts: 329
Joined: Fri Feb 17, 2012 1:02 pm

Re: Cheapest Tile based system?

Postby Doofus » Wed Jun 05, 2013 10:16 am

I'm not sure that I understand WHY we can actually do a cache here.
If you have a sidescroller or top down like zelda, and you move to the side, everything scrolls. When everything scrolls everything changes... right?
So what do we cache exactly?
Doofus
 
Posts: 125
Joined: Sat May 05, 2012 11:35 am

Re: Cheapest Tile based system?

Postby tonyp7 » Wed Jun 05, 2013 10:22 am

you translate your camera, not your tiles!

only the camera moves. if you move your tiles at every frame you will very quickly hit some performance issues.
tonyp7
 
Posts: 329
Joined: Fri Feb 17, 2012 1:02 pm

Re: Cheapest Tile based system?

Postby Sharp » Wed Jun 05, 2013 3:00 pm

tonyp7 wrote:you translate your camera, not your tiles!

only the camera moves. if you move your tiles at every frame you will very quickly hit some performance issues.

And how do you work with different layers? And dynamic tiles?
Image
Sharp
 
Posts: 47
Joined: Tue Apr 23, 2013 9:50 am

Re: Cheapest Tile based system?

Postby tonyp7 » Thu Jun 06, 2013 1:52 am

It's quite simple really. In tiled you define some layer that need to be cached, because you know they will always be behind dynamic elements no matter what. I labelled them level0x. For example level0, level0b, level0c.

In your game, you detect the layer index level when static elements ends.

- You do a loop from 0 to end of static layers and draw it in a spritecache. (only once)
- You then loop from the beginning of your dynamic tiles and draw them using a regular spritebatch.

Here only the dynamic stuff drawn:
Image

Here + the spritecache
Image

And lastly, the camera translated and zoomed: no problem with the sprite cache being static at all:
Image
tonyp7
 
Posts: 329
Joined: Fri Feb 17, 2012 1:02 pm

Next

Return to Libgdx

Who is online

Users browsing this forum: dmg46664, Google [Bot], Yahoo [Bot] and 4 guests