Example usage for com.badlogic.gdx.graphics.g2d TextureAtlas TextureAtlas

List of usage examples for com.badlogic.gdx.graphics.g2d TextureAtlas TextureAtlas

Introduction

In this page you can find the example usage for com.badlogic.gdx.graphics.g2d TextureAtlas TextureAtlas.

Prototype

public TextureAtlas() 

Source Link

Document

Creates an empty atlas to which regions can be added.

Usage

From source file:com.badlogic.rtm.LevelRenderer.java

License:Apache License

private void load() {
    try {//  w w w  .j ava 2  s.  c  o  m
        tiles = new Texture(Gdx.files.internal("data/tiles-3.png"));
        tiles.setFilter(TextureFilter.MipMapLinearNearest, TextureFilter.Nearest);

        TextureAtlas atlas = new TextureAtlas();
        for (int i = 0; i < 12; i++) {
            TextureRegion region = new TextureRegion(tiles, i % 4 * 64 + 1, i / 4 * 64 + 1, 64, 64);
            atlas.addRegion("" + i, region);
        }
        float uSize = 62.0f / 256.0f;
        float vSize = 62.0f / 256.0f;

        BufferedReader reader = new BufferedReader(
                new InputStreamReader(Gdx.files.internal("data/level.map").read()));
        String line = reader.readLine();
        String tokens[] = line.split(",");
        camera.position.set(Float.parseFloat(tokens[0]), 0, Float.parseFloat(tokens[1]));
        int floors = Integer.parseInt(reader.readLine());
        int walls = Integer.parseInt(reader.readLine());
        float[] floorVertices = new float[floors * 20];
        float[] wallVertices = new float[walls * 20];
        short[] floorIndices = new short[floors * 6];
        short[] wallIndices = new short[walls * 6];

        int idx = 0;
        for (int i = 0, j = 0; i < floors; i++) {
            for (int k = 0; k < 4; k++) {
                tokens = reader.readLine().split(",");
                floorVertices[j++] = Float.parseFloat(tokens[0]);
                floorVertices[j++] = Float.parseFloat(tokens[1]);
                floorVertices[j++] = Float.parseFloat(tokens[2]);
                floorVertices[j++] = 0;
                floorVertices[j++] = 0;
            }

            short startIndex = (short) (i * 4);
            floorIndices[idx++] = startIndex;
            floorIndices[idx++] = (short) (startIndex + 1);
            floorIndices[idx++] = (short) (startIndex + 2);
            floorIndices[idx++] = (short) (startIndex + 2);
            floorIndices[idx++] = (short) (startIndex + 3);
            floorIndices[idx++] = startIndex;

            int type = Integer.parseInt(reader.readLine());
            String textureId = reader.readLine();
            TextureRegion region = atlas.findRegion(textureId);
            float u = region.getU();
            float v = region.getV();

            floorVertices[j - 2] = u + uSize;
            floorVertices[j - 1] = v;
            floorVertices[j - 2 - 5] = u + uSize;
            floorVertices[j - 1 - 5] = v + vSize;
            floorVertices[j - 2 - 10] = u;
            floorVertices[j - 1 - 10] = v + vSize;
            floorVertices[j - 2 - 15] = u;
            floorVertices[j - 1 - 15] = v;
        }

        idx = 0;
        short startIndex = 0;
        for (int i = 0, j = 0; i < walls; i++) {
            tokens = reader.readLine().split(",");
            if (!tokens[1].equals("0")) {
                for (int k = 0; k < 4; k++) {
                    wallVertices[j++] = Float.parseFloat(tokens[0]);
                    wallVertices[j++] = Float.parseFloat(tokens[1]);
                    wallVertices[j++] = Float.parseFloat(tokens[2]);
                    wallVertices[j++] = 0;
                    wallVertices[j++] = 0;
                    if (k < 3)
                        tokens = reader.readLine().split(",");
                }

                wallIndices[idx++] = startIndex;
                wallIndices[idx++] = (short) (startIndex + 1);
                wallIndices[idx++] = (short) (startIndex + 2);
                wallIndices[idx++] = (short) (startIndex + 2);
                wallIndices[idx++] = (short) (startIndex + 3);
                wallIndices[idx++] = startIndex;
                startIndex += 4;

                int type = Integer.parseInt(reader.readLine());
                String textureId = reader.readLine();
                TextureRegion region = atlas.findRegion(textureId);
                float u = region.getU();
                float v = region.getV();

                wallVertices[j - 2] = u + uSize;
                wallVertices[j - 1] = v;
                wallVertices[j - 2 - 5] = u + vSize;
                wallVertices[j - 1 - 5] = v + vSize;
                wallVertices[j - 2 - 10] = u;
                wallVertices[j - 1 - 10] = v + vSize;
                wallVertices[j - 2 - 15] = u;
                wallVertices[j - 1 - 15] = v;
            } else {
                reader.readLine();
                reader.readLine();
                reader.readLine();

                int type = Integer.parseInt(reader.readLine());
                int textureId = Integer.parseInt(reader.readLine());
            }
        }

        floorMesh = new Mesh(true, floors * 4, floors * 6,
                new VertexAttribute(VertexAttributes.Usage.Position, 3, "a_position"),
                new VertexAttribute(VertexAttributes.Usage.TextureCoordinates, 2, "a_texCoord"));
        floorMesh.setVertices(floorVertices);
        floorMesh.setIndices(floorIndices);

        wallMesh = new Mesh(true, walls * 4, walls * 6,
                new VertexAttribute(VertexAttributes.Usage.Position, 3, "a_position"),
                new VertexAttribute(VertexAttributes.Usage.TextureCoordinates, 2, "a_texCoord"));

        wallMesh.setVertices(wallVertices);
        wallMesh.setIndices(wallIndices);

        reader.close();
    } catch (IOException ex) {
        throw new GdxRuntimeException(ex);
    }
}

From source file:com.jlabarca.texture.TextureCache.java

License:Apache License

/**
 * Load predefined textures./*from  w  w  w.  j  a  v a2  s. c  o m*/
 * 
 * This requires texture definitions to be added to the {@link AppActorTextures} structure.
 */
public void load(List<TextureDefinition> textureDefinitions) {
    if (textureAtlas == null) {
        textureAtlas = new TextureAtlas();
    } else {
        dispose();

        textureAtlas = new TextureAtlas();
    }

    for (TextureDefinition definition : textureDefinitions) {
        Texture texture = new Texture(Gdx.files.internal(definition.getPath()));
        TextureRegion textureRegion = new TextureRegion(texture);

        textureAtlas.addRegion(definition.getName(), textureRegion);
        definitions.put(definition.getName(), definition);
    }
}

From source file:es.danirod.rectball.screens.LoadingScreen.java

License:Open Source License

@Override
public void setUpInterface(Table table) {
    ballsTexture = new Texture("board/normal.png");
    TextureRegion[][] regions = TextureRegion.split(ballsTexture, 256, 256);
    ballAtlas = new TextureAtlas();
    ballAtlas.addRegion("ball_red", regions[0][0]);
    ballAtlas.addRegion("ball_yellow", regions[0][1]);
    ballAtlas.addRegion("ball_blue", regions[1][0]);
    ballAtlas.addRegion("ball_green", regions[1][1]);
    ballAtlas.addRegion("ball_gray", regions[1][2]);

    boardActor = new BoardActor(ballAtlas, board);
    boardActor.setColoured(true);// w  w  w  .j  a va 2  s  . c  o  m
    table.add(boardActor).size(100).align(Align.center);
}

From source file:net.mwplay.cocostudio.ui.particleutil.CCTextureAtlasLoader.java

License:Apache License

@Override
public TextureAtlas load(AssetManager assetManager, String fileName, FileHandle file,
        TextureAtlasLoader.TextureAtlasParameter parameter) {
    TextureAtlas atlas = new TextureAtlas();
    atlas.getTextures().add(texture);/*from   ww  w  .  j  ava  2 s  . c o m*/
    ObjectMap<String, Object> frames = (ObjectMap<String, Object>) map.get("frames");
    for (ObjectMap.Entry<String, Object> entry : frames.entries()) {
        String pageName = entry.key;
        ObjectMap<String, Object> params = (ObjectMap<String, Object>) entry.value;
        Rectangle frame = LyU.parseRect((String) params.get("frame"));
        GridPoint2 offset = LyU.parsePoint((String) params.get("offset"));
        boolean rotated = Boolean.parseBoolean((String) params.get("rotated"));
        GridPoint2 sourceSize = LyU.parsePoint((String) params.get("sourceSize"));
        Rectangle sourceColorRect = LyU.parseRect((String) params.get("sourceColorRect"));
        TextureAtlas.TextureAtlasData.Region region = new TextureAtlas.TextureAtlasData.Region();
        region.name = pageName.substring(0, pageName.lastIndexOf('.'));
        region.rotate = rotated;
        region.offsetX = offset.x;
        region.offsetY = offset.y;
        region.originalWidth = sourceSize.x;
        region.originalHeight = sourceSize.y;
        region.left = (int) frame.x;
        region.top = (int) frame.y;
        region.width = (int) frame.getWidth();
        region.height = (int) frame.getHeight();
        int width = region.width;
        int height = region.height;
        TextureAtlas.AtlasRegion atlasRegion = new TextureAtlas.AtlasRegion(texture, region.left, region.top,
                region.rotate ? height : width, region.rotate ? width : height);
        atlasRegion.index = region.index;
        atlasRegion.name = region.name;
        atlasRegion.offsetX = region.offsetX;
        atlasRegion.offsetY = region.offsetY;
        atlasRegion.originalHeight = region.originalHeight;
        atlasRegion.originalWidth = region.originalWidth;
        atlasRegion.rotate = region.rotate;
        atlasRegion.splits = region.splits;
        atlasRegion.pads = region.pads;
        if (region.flip) {
            atlasRegion.flip(false, true);
        }
        atlas.getRegions().add(atlasRegion);
    }
    texture = null;
    map = null;
    return atlas;
}