Solved LibGDX Displaying Textures

Hi!

I am looking for a bit of advice on displaying textures in LibGDX. In this case, I am using a sprite batch to display everything. Whenever you have an object, lets say an enemy, each of them individually inside of an array, how do you display them all with one line? I’m assuming like so:


for(int i = 0; i < array.length; i++){
sb.draw(texture, array[i].x, array[i].y, array[i].width, array[i].height); // Each object having an x, y, width, and height of course.
}

While it seems like it would work, is this the conventional way of doing it? I always want to use the proper methods for performance boost.

Thanks!

  • A

You could use an entity system for this:

Create an abstract entity class:

public abstract class Entity {	

	public abstract create();

	public abstract void render(SpriteBatch sb);

	public abstract void update(float dt);
	
	public abstract dispose();
}

Extend the class in you game object:

public class SomeObject extends Entity {

	private float x;
	private float y;
	
	private float width;
	private float height;
	
	private texture someTexture;

    @Override
	public create() {
	
		...
	}

	@Override
    public void render(SpriteBatch sb) {
		
		// Should also do some checking before rendering, if the entity is visible.		
		sb.draw(someTexture, x, y, width, height);
	}

	@Override
    public void update(float dt) {
	
		...
	}
	
	@Override
	public dispose() {
	
		...
	}
}

Create an array of your entities:

private ArrayList<Entity> entities = new ArrayList<>();
// Will be used in update(float dt)
private List<Entity> toBeRemoved = new LinkedList<>();

// Add your entities
entities.add(new SomeObject(...));

Rendering:

public void render(SpriteBatch sb) {

	sb.begin();
	for (Entity e : entities) {	  

		e.render(sb);
	}
	sb.end();
}

Updating:

public void update(float dt) {

	toBeRemoved.clear();
	for (Entity e : entities) {	    

		// Should the entity be removed
		if (e.removePending()) {
		
			toBeRemoved.add(e);
		}
	}
	entities.removeAll(toBeRemoved);
}

Don’t do that. That defeats the point of begin/end and results in terrible performance. You are supposed to begin and end the batch as less as possible, preferably once per tick. So basically begin() the batch, draw all your sprites, and then end() the batch.

syszee what you are doing is fine

Ah, I quickly pulled that out of my old Slick2D game and just threw that in there :smiley: Gonna modify quickly.

So, just keep the sprite batch in the game screen? I’m still having trouble with objects and rendering/updating them individually. I would like to keep collision handling and other properties within the class for each of the objects too, but I’m not sure how to do that. I’m using this method of collision:
https://raw.githubusercontent.com/libgdx/libgdx/master/tests/gdx-tests/src/com/badlogic/gdx/tests/superkoalio/SuperKoalio.java

Essentially, it just checks the adjacent tiles to my position on a layer.

As another option, look into the LibGDX Actor and Stage classes. If you make all of your entities extend Actor, you can add them to the Stage and then updating and rendering is super simple using only stage.act() and stage.draw(). The stage goes through every actor that has been added to it and performs the action (either updating or rendering) so you can update and/or draw everything with only one line just as you want. :slight_smile:

Tekkerue , yea I’ve been suggested that here and there. I’m not really wanting to get into that portion of LibGDX. I’m too settled with sprite batch and such. Thanks for the suggestion though. :slight_smile:

Stage contains a SpriteBatch and does all of the rendering through it, so it’s essentially a helper class that allows things to be handled in a much simpler way. As an example, this is the render method in my Game class.

@Override
public void render () {
	stage.act();
	Gdx.gl.glClearColor(0.10f, 0.10f, 0.08f, 1.0f);
	Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
	stage.draw();
}

That updates and renders absolutely everything in my entire app. I’m fairly new to LibGDX myself, and I started with SpriteBatch and Sprites…but after making this change I haven’t looked back. It’s definitely worth it and not nearly as difficult to use as it might appear at first glance.