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

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

Introduction

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

Prototype

public AtlasRegion addRegion(String name, TextureRegion textureRegion) 

Source Link

Document

Adds a region to the atlas.

Usage

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

License:Apache License

private void load() {
    try {/*from   ww w.  j  av a 2s .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);
    }
}