2

I have all of my images for a libgdx project in a single texture.

I'm about to add some nice particle effects, but the documentation implies that each type of emitter requires a separate graphics file for its particle.

Is that true? Or, is there a way of specifying a region of a texture to be used as the particle image, so that I may still keep all my images in that single file?

Bumpy
  • 1,290
  • 1
  • 22
  • 32

1 Answers1

3

Yes it can but you need to have the texture inside of an TextureAtlas. Take a look at this article for it.

here is an Example where i even use a TextureAtlas:

m_effect = new ParticleEffect();
m_effect.load(Gdx.files.internal("particle/effects/lightning.p"), this.getAtlas());

Or in 2 steps:

m_effect.loadEmitters(Gdx.files.internal("particle/effects/lightning.p"));
m_effect.loadEmitterImages(this.getAtlas());

here is whatt he LoadEmitterImage does:

public void loadEmitterImages (TextureAtlas atlas) {
    for (int i = 0, n = emitters.size; i < n; i++) {
        ParticleEmitter emitter = emitters.get(i);
        String imagePath = emitter.getImagePath();
        if (imagePath == null) continue;
        String imageName = new File(imagePath.replace('\\', '/')).getName();
        int lastDotIndex = imageName.lastIndexOf('.');
        if (lastDotIndex != -1) imageName = imageName.substring(0, lastDotIndex);
        Sprite sprite = atlas.createSprite(imageName); /// <---- here it creates a Sprite with a textureregion
        if (sprite == null) throw new IllegalArgumentException("SpriteSheet missing image: " + imageName);
        emitter.setSprite(sprite);
    }
}

src from git

bemeyer
  • 6,154
  • 4
  • 36
  • 86
  • An excellent example, thanks BennX. This appears to be adding more than one image per effect (which is not quite what I asked, but EVEN BETTER!) On the other hand, it appears that the multiple images have to live in a texture Atlas, which would still be a separate file from my manually-managed texture (which was my original complaint). But I'm assuming that since it comes down to just adding a sprite (`emitter.addSprite()`), I should be able to create my own sprites from my own texture, and have all the particle images in the same texture as my other images. Does that sound reasonable? – Bumpy Mar 21 '14 at 05:00
  • I would recomend to use one Atlas for all of your textures. It increases your rendering performance by decreasing the texture binding to the obengl context. If you have just one big texture (the atlas) you just bind that to draw everything instead of binding x texture in one rendering. But yes it allows you to have more particle effects in one texture and creates a sprite from it which is just an extendet `TextureRegion`. But start using it with the effects and see if you can manage to integrate it for all textures. – bemeyer Mar 21 '14 at 10:48
  • Thanks again. To clarify, though, even though I'm not using a TextureAtlas, I still only have ONE SINGLE texture in which I'm manually managing co-ords of TextureRegions. This should be the same, in terms of texture binding speed, right? The reason is that I'm finding it simpler to edit the images in one .png and manage the coords myself, rather that edit a whole bunch of .pngs, and continually have to compile an Atlas. Although, I've asked a question elsewhere about what to do with really large textures. Do you suggest a single Atlas for EVERY image, including really large BG images as well? – Bumpy Mar 21 '14 at 11:09
  • Also, in the code above, you are only specifying the Atlas - not the names of the regions within the Atlas for the emitters to use. How do you specify which images in the Atlas your particle emitter uses? Or have I missed something really obvious? – Bumpy Mar 21 '14 at 11:12
  • You dont need to specify them since you chose a texture while creating the particle effect. That name will be used to find the region inside of the Atlas. So no need to do it manually just take care that you add the same texture to the atlas – bemeyer Mar 21 '14 at 11:13
  • Again thanks. After this, I think TextureAtlas is becoming more attractive than managing texture regions manually. – Bumpy Mar 21 '14 at 23:15
  • yes it will be important later on if you have alot of textures. youll have a way better performance – bemeyer Mar 22 '14 at 12:00