[SOLVED] LibGDX TextureAtlas, SpriteBatch combination - nothing displaying?

Ok, I’ve done a lot of looking and can’t really seem to find the answer I’m looking for. My code is at home, but I’ll explain what I’ve been trying to do.

Basically, I have balloons that are created dynamically based on the user’s level, which determines the number of balloons to be launched. In this case, however, I am seeing nothing being displayed on the screen. I have log entries that are created whenever a balloon is assigned and supposed to be drawn, but nothing appears.

I have all the textures for the balloons in a single texturepack.

So ignoring all the features of the balloon class, this is the approach which I had thought was the way to do it:

  • Load a textureatlas and assign it with the balloon.pack
  • Assign a sprite to one of the regions in the file.

call sprite.draw(batch); to draw the sprite on screen (in between batch.begin and batch.end)

Again, I apologise that I don’t have my code on hand but I was hoping that someone may have come across the same issue, but I guess what I’m probably more interested in is the best way to load a texture from a textureatlas, assign it to an entity (which has attributes regarding its position, etc.), and then draw it between spritebatch calls.

I have done a lot of looking but can’t seem to find anything specific to this situation regarding TextureAtlas, which I’m very surprised about. I’m sure I’m missing something obvious. Once I’m home I will post the code example but I am hoping that in the meantime someone will be able to decipher what I’m trying to explain and why I’m an idiot :slight_smile:

Without code we can’t debug your issue. There is no known bug in LibGDX that would cause this.

Yeah, I’m certain it’s something I’ve done or more likely that I’ve forgotten to do.

I will post the code as soon as I can.

Ok, here is the condensed version (without irrelevant bits). I’m simplifying the code to what I am doing without filling the arraylist, etc., as I have tested both ways and both produce the same lack of result.


class WorldRenderer {

World world;
OrthographicCamera cam;
SpriteBatch batch;
Sprite blueBal;
TextureAtlas balAtlas;

Balloons balloon;
ArrayList<Balloons> balloons;

float width, height;

public WorldRenderer (World world) {
this.world = world;

width = Gdx.graphics.getWidth() / 40;
height = Gdx.graphics.getHeight() / 40;

cam = new OrthographicCamera();
cam.setToOrtho(false, width, height);
cam.update;

batch = new SpriteBatch();
batch.setProjectionMatrix(cam.combined);

balAtlas = new TextureAtlas(Gdx.files.internal("images/balloons.pack"));

blueBal = balAtlas.createSprite("blueballoon");
}

public void render(float delta) {
Gdx.gl.glClearColor(0,0,0,1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

balloons = world.getBalloons(); 
// Skipping arraylist bits, let's just assume that balloon is already defined.

sprite.setPosition(3, 3);
sprite.setColor(1, 1, 1, 1);
sprite.setRotation(balloon.getRotation());
sprite.setOrigin(sprite.getWidth() / 2, sprite.getHeight() / 2);
sprite.setScale(balloon.SIZE);

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

batch.begin();
sprite.draw(batch);
batch.end();

}

The render function in WorldRenderer is called from the render method in the GameScreen.class, there are no GL related functions called from GameScreen itself. Could this perhaps have something to do with it?

I have noticed in my perusing of the web that people are using TextureRegions and things, but I can’t seem to find any demonstrations of people loading images from a TextureAtlas directly into a Sprite. There is no animation at this point (each balloon is a static image thus far).

What happens if you remove the atlas and camera and just try putting the balloon on screen? I’d also recommend starting a separate project just to try to draw something onto the screen as easily as humanly possible.

Excellent, thank you! It appears as though I’m doing something wrong with the camera. I’ll have another look, I’m sure it’ll be an obvious solution now… hopefully :).

On the other hand, with the fact that I actually intend on using a static screen, is there even a need to use a camera? Ultimately I’ll want to add lighting and shadows to the game but for my first game I’d rather have something a bit more simple :).

I’m not particularly new to java or programming in general, but this is my first ever project with LibGDX. I’ve been following Dustin Riley’s tutorials somewhat loosely to get the gist of how things work.

I didn’t bother with a camera for my project, you’ll be fine if you don’t actually want the screen to move.

Excellent, thank you.

I will still have a look into working with the camera just purely for education’s sake, but at least now I can at least start playing with the logic :).

Just solved it by process of reverse-elimination - I uncommented the cam related lines in the constructor, and left them commented in render(), and now it works with the cam :).

I guess there is no need to do anything with the cam in the render thread anyway, since I make no changes at that point.