0
\$\begingroup\$

I am trying to understand the example game, cuboc. GitHub is https://github.com/libgdx/libgdx-demo-cuboc. I have generated my texture atlas but I do not understand the code that uses this texture atlas. Here's the code (you can see on github too here:

package com.badlogic.cubocy;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.FPSLogger;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.SpriteCache;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.glutils.ImmediateModeRenderer20;
import com.badlogic.gdx.math.Vector3;

public class MapRenderer {
...
    private void createAnimations () {
        this.tile = new TextureRegion(new Texture(Gdx.files.internal("data/tile.png")), 0, 0, 20, 20);
        Texture bobTexture = new Texture(Gdx.files.internal("data/bob.png"));
        TextureRegion[] split = new TextureRegion(bobTexture).split(20, 20)[0];
        TextureRegion[] mirror = new TextureRegion(bobTexture).split(20, 20)[0];
        for (TextureRegion region : mirror)
            region.flip(true, false);
        spikes = split[5];
        bobRight = new Animation(0.1f, split[0], split[1]);
        bobLeft = new Animation(0.1f, mirror[0], mirror[1]);
        bobJumpRight = new Animation(0.1f, split[2], split[3]);
        bobJumpLeft = new Animation(0.1f, mirror[2], mirror[3]);
        bobIdleRight = new Animation(0.5f, split[0], split[4]);
        bobIdleLeft = new Animation(0.5f, mirror[0], mirror[4]);
        bobDead = new Animation(0.2f, split[0]);
        split = new TextureRegion(bobTexture).split(20, 20)[1];
        cube = split[0];
        cubeFixed = new Animation(1, split[1], split[2], split[3], split[4], split[5]);
        split = new TextureRegion(bobTexture).split(20, 20)[2];
        cubeControlled = split[0];
        spawn = new Animation(0.1f, split[4], split[3], split[2], split[1]);
        dying = new Animation(0.1f, split[1], split[2], split[3], split[4]);
        dispenser = split[5];
        split = new TextureRegion(bobTexture).split(20, 20)[3];
        rocket = new Animation(0.1f, split[0], split[1], split[2], split[3]);
        rocketPad = split[4];
        split = new TextureRegion(bobTexture).split(20, 20)[4];
        rocketExplosion = new Animation(0.1f, split[0], split[1], split[2], split[3], split[4], split[4]);
        split = new TextureRegion(bobTexture).split(20, 20)[5];
        endDoor = split[2];
        movingSpikes = split[0];
        laser = split[1];
    }

What I do not understand is what is split used for? Like, what is going on here:

split = new TextureRegion(bobTexture).split(20, 20)[1];

And sometimes it looks like split is a method that takes two int's, 20 and 20? Why 20? Othertimes split is used as an Array like in ...

rocketExplosion = new Animation(0.1f, split[0], split[1], split[2], split[3], split[4], split[4]);

... and I just can't follow what's going on here. Thanks!

\$\endgroup\$
2
\$\begingroup\$

From the documentation:

public TextureRegion[][] split(int tileWidth, int tileHeight)

Helper function to create tiles out of this TextureRegion starting from the top left corner going to the right and ending at the bottom right corner. Only complete tiles will be returned so if the region's width or height are not a multiple of the tile width and height not all of the region will be used. This will not work on texture regions returned form a TextureAtlas that either have whitespace removed or where flipped before the region is split.

Parameters:

  • tileWidth - a tile's width in pixels
  • tileHeight - a tile's height in pixels

Returns:

a 2D array of TextureRegions indexed by [row][column].

Looks like the texture is actually a texture atlas, or tile map. The split method takes one texture and splits it into texture tiles. The image has tiles that are 20 by 20 pixels, passing 20,20 into split splits the image into tiles that are 20 pixels by 20 pixels.

So something like

TextureRegion(bobTexture).split(20, 20)[1];

would return the second row of tiles in the sprite sheet.

\$\endgroup\$
1
\$\begingroup\$

this is easy way to create animations, you can add all animation images to one image and than use them separately , for example each image is 20x20 pixel and your animation contains 3 images so the big image will be 60x20 , and if you split it by 20 x20 pixel you can get all the images , and then put those images in animation . Another example. your character have two animations walk right and left, walking left animation contains 2images and walking right animation again 2, so your bug image can contain 4 images (30x30) the big image can be 120x30 or 60x60 , and by splitting this 30x30 you can get animation images separately and walking left can be 0,1 index in the array and right 2,3 .

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.