Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (741)
Games in Android Showcase (225)
games submitted by our members
Games in WIP (823)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  LibGDX drawing of sprites?  (Read 3348 times)
0 Members and 1 Guest are viewing this topic.
Offline EliwoodL

Junior Devvie


Medals: 2
Exp: 8 years



« Posted 2015-04-24 05:17:05 »

So, I have a grid of tiles I wish to render, along with various other objects and bits / pieces atop those tiles.  I've determined that the way I should go about it is to only render the tiles around the player's position, which isn't too hard.

The thing is, LibGDX's SpriteBatch has a maximum size of 5460 sprites per drawing call, correct?  There might well be more than that at any given point, considering the tiles are 8x8 and the screen is 640 x 480, coming up to 4800 sprites in tiles alone.  On top of those might be items and entities and other such nonsense.

Does anyone have any advice for me, whether to use separate spritebatches or something like that?  Would it be more efficient?  Or can I increase the maximum size of a spritebatch and would THAT be more efficient?

Thanks!
Offline Lucasmontec

Junior Devvie


Medals: 3
Exp: 5 years



« Reply #1 - Posted 2015-04-26 19:31:39 »

When rendering large amounts of the same thing you shouldn't have an instance for each object. What I mean is that your map should store only the type of each tile and when drawing you use the tile type to get a texture for that.

You do it like this (pseudo-code):
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
SomeTable tileTypes = new SomeTable<TileType, Texture>(); //Consider HashTable
tileTypes.put(TileType.GRASS, new Texture("grass.png"));
tileTypes.put(TileType.DIRT, new Texture("dirt.png"));
tileTypes.put(TileType.ROCK, new Texture("rocknroll.png"));

public void drawTiles(){
    for(Tile t : tiles){
        batch.draw(tileTypes.get(t.getType()), calculateXforThisTile, calculateYforThisTile);
    }
}


I've tested this for you. Running the following code I've managed to draw 16000 things on one draw call:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
public class BatchDrawingTest extends ApplicationAdapter {
   SpriteBatch         batch;
   OrthographicCamera   camera;
   TextureRegion      thing;

   @Override
   public void create() {
      camera = new OrthographicCamera(800.0f, 600.0f);
      camera.translate(400.0f, 300.0f);
      Assets.load();

      batch = new SpriteBatch();

      thing = new TextureRegion(new Texture(Gdx.files.internal("launcher.png")));
   }

   @Override
   public void render() {
      Gdx.gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
      Gdx.gl.glClear(16384);

      batch.setProjectionMatrix(camera.combined);
      camera.update();

      batch.begin();
      for (int i = 0; i < 16000; i++)
         batch.draw(thing, ((i / 16000f) * 800), (i % 50) * 50, 20, 20);
      batch.end();
   }

   @Override
   public void dispose() {}
}

Try doing something like this. If you need more help just reply here =)
Offline trollwarrior1
« Reply #2 - Posted 2015-04-27 05:25:14 »

When SpriteBatch reaches it's limit, it will automatically flush and begin a new batch. You shouldn't worry about this though, as the batch will need to be flushed on many other occasions, such as changing shaders, changing uniform variables in the shaders and many more.

EDIT---
If your game doesn't have more than 100 draw calls per frame, I don't think you have to worry about anything yet. You could probably have up to 500 or 1000 draw calls and game would still run at  60 fps on not very good machines.
Pages: [1]
  ignore  |  Print  
 
 

 
Ecumene (110 views)
2017-09-30 02:57:34

theagentd (136 views)
2017-09-26 18:23:31

cybrmynd (245 views)
2017-08-02 12:28:51

cybrmynd (241 views)
2017-08-02 12:19:43

cybrmynd (240 views)
2017-08-02 12:18:09

Sralse (254 views)
2017-07-25 17:13:48

Archive (864 views)
2017-04-27 17:45:51

buddyBro (1008 views)
2017-04-05 03:38:00

CopyableCougar4 (1568 views)
2017-03-24 15:39:42

theagentd (1373 views)
2017-03-24 15:32:08
List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05

SF/X Libraries
by SkyAphid
2017-03-02 06:38:56

SF/X Libraries
by SkyAphid
2017-03-02 06:38:32

SF/X Libraries
by SkyAphid
2017-03-02 06:38:05

SF/X Libraries
by SkyAphid
2017-03-02 06:37:51
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!