Search in sources :

Example 6 with Texture

use of com.badlogic.gdx.graphics.Texture in project libgdx by libgdx.

the class PolygonRegionLoader method getDependencies.

/** If the PSH file contains a line starting with {@link PolygonRegionParameters#texturePrefix params.texturePrefix}, an
	 * {@link AssetDescriptor} for the file referenced on that line will be added to the returned Array. Otherwise a sibling of the
	 * given file with the same name and the first found extension in {@link PolygonRegionParameters#textureExtensions
	 * params.textureExtensions} will be used. If no suitable file is found, the returned Array will be empty. */
@Override
public Array<AssetDescriptor> getDependencies(String fileName, FileHandle file, PolygonRegionParameters params) {
    if (params == null)
        params = defaultParameters;
    String image = null;
    try {
        BufferedReader reader = file.reader(params.readerBuffer);
        for (String line = reader.readLine(); line != null; line = reader.readLine()) if (line.startsWith(params.texturePrefix)) {
            image = line.substring(params.texturePrefix.length());
            break;
        }
        reader.close();
    } catch (IOException e) {
        throw new GdxRuntimeException("Error reading " + fileName, e);
    }
    if (image == null && params.textureExtensions != null)
        for (String extension : params.textureExtensions) {
            FileHandle sibling = file.sibling(file.nameWithoutExtension().concat("." + extension));
            if (sibling.exists())
                image = sibling.name();
        }
    if (image != null) {
        Array<AssetDescriptor> deps = new Array<AssetDescriptor>(1);
        deps.add(new AssetDescriptor<Texture>(file.sibling(image), Texture.class));
        return deps;
    }
    return null;
}
Also used : GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) Array(com.badlogic.gdx.utils.Array) FileHandle(com.badlogic.gdx.files.FileHandle) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) Texture(com.badlogic.gdx.graphics.Texture) AssetDescriptor(com.badlogic.gdx.assets.AssetDescriptor)

Example 7 with Texture

use of com.badlogic.gdx.graphics.Texture in project libgdx by libgdx.

the class PolygonSpriteBatch method draw.

@Override
public void draw(TextureRegion region, float width, float height, Affine2 transform) {
    if (!drawing)
        throw new IllegalStateException("PolygonSpriteBatch.begin must be called before draw.");
    final short[] triangles = this.triangles;
    final float[] vertices = this.vertices;
    Texture texture = region.texture;
    if (texture != lastTexture)
        switchTexture(texture);
    else if (//
    triangleIndex + 6 > triangles.length || vertexIndex + SPRITE_SIZE > vertices.length)
        flush();
    int triangleIndex = this.triangleIndex;
    final int startVertex = vertexIndex / VERTEX_SIZE;
    triangles[triangleIndex++] = (short) startVertex;
    triangles[triangleIndex++] = (short) (startVertex + 1);
    triangles[triangleIndex++] = (short) (startVertex + 2);
    triangles[triangleIndex++] = (short) (startVertex + 2);
    triangles[triangleIndex++] = (short) (startVertex + 3);
    triangles[triangleIndex++] = (short) startVertex;
    this.triangleIndex = triangleIndex;
    // construct corner points
    float x1 = transform.m02;
    float y1 = transform.m12;
    float x2 = transform.m01 * height + transform.m02;
    float y2 = transform.m11 * height + transform.m12;
    float x3 = transform.m00 * width + transform.m01 * height + transform.m02;
    float y3 = transform.m10 * width + transform.m11 * height + transform.m12;
    float x4 = transform.m00 * width + transform.m02;
    float y4 = transform.m10 * width + transform.m12;
    float u = region.u;
    float v = region.v2;
    float u2 = region.u2;
    float v2 = region.v;
    float color = this.color;
    int idx = vertexIndex;
    vertices[idx++] = x1;
    vertices[idx++] = y1;
    vertices[idx++] = color;
    vertices[idx++] = u;
    vertices[idx++] = v;
    vertices[idx++] = x2;
    vertices[idx++] = y2;
    vertices[idx++] = color;
    vertices[idx++] = u;
    vertices[idx++] = v2;
    vertices[idx++] = x3;
    vertices[idx++] = y3;
    vertices[idx++] = color;
    vertices[idx++] = u2;
    vertices[idx++] = v2;
    vertices[idx++] = x4;
    vertices[idx++] = y4;
    vertices[idx++] = color;
    vertices[idx++] = u2;
    vertices[idx++] = v;
    vertexIndex = idx;
}
Also used : Texture(com.badlogic.gdx.graphics.Texture)

Example 8 with Texture

use of com.badlogic.gdx.graphics.Texture in project libgdx by libgdx.

the class TideMapLoader method load.

public TiledMap load(String fileName) {
    try {
        FileHandle tideFile = resolve(fileName);
        root = xml.parse(tideFile);
        ObjectMap<String, Texture> textures = new ObjectMap<String, Texture>();
        for (FileHandle textureFile : loadTileSheets(root, tideFile)) {
            textures.put(textureFile.path(), new Texture(textureFile));
        }
        DirectImageResolver imageResolver = new DirectImageResolver(textures);
        TiledMap map = loadMap(root, tideFile, imageResolver);
        map.setOwnedResources(textures.values().toArray());
        return map;
    } catch (IOException e) {
        throw new GdxRuntimeException("Couldn't load tilemap '" + fileName + "'", e);
    }
}
Also used : DirectImageResolver(com.badlogic.gdx.maps.ImageResolver.DirectImageResolver) GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) ObjectMap(com.badlogic.gdx.utils.ObjectMap) FileHandle(com.badlogic.gdx.files.FileHandle) IOException(java.io.IOException) Texture(com.badlogic.gdx.graphics.Texture)

Example 9 with Texture

use of com.badlogic.gdx.graphics.Texture in project libgdx by libgdx.

the class TmxMapLoader method load.

/** Loads the {@link TiledMap} from the given file. The file is resolved via the {@link FileHandleResolver} set in the
	 * constructor of this class. By default it will resolve to an internal file.
	 * @param fileName the filename
	 * @param parameters specifies whether to use y-up, generate mip maps etc.
	 * @return the TiledMap */
public TiledMap load(String fileName, TmxMapLoader.Parameters parameters) {
    try {
        this.convertObjectToTileSpace = parameters.convertObjectToTileSpace;
        this.flipY = parameters.flipY;
        FileHandle tmxFile = resolve(fileName);
        root = xml.parse(tmxFile);
        ObjectMap<String, Texture> textures = new ObjectMap<String, Texture>();
        Array<FileHandle> textureFiles = loadTilesets(root, tmxFile);
        textureFiles.addAll(loadImages(root, tmxFile));
        for (FileHandle textureFile : textureFiles) {
            Texture texture = new Texture(textureFile, parameters.generateMipMaps);
            texture.setFilter(parameters.textureMinFilter, parameters.textureMagFilter);
            textures.put(textureFile.path(), texture);
        }
        DirectImageResolver imageResolver = new DirectImageResolver(textures);
        TiledMap map = loadTilemap(root, tmxFile, imageResolver);
        map.setOwnedResources(textures.values().toArray());
        return map;
    } catch (IOException e) {
        throw new GdxRuntimeException("Couldn't load tilemap '" + fileName + "'", e);
    }
}
Also used : DirectImageResolver(com.badlogic.gdx.maps.ImageResolver.DirectImageResolver) GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) ObjectMap(com.badlogic.gdx.utils.ObjectMap) FileHandle(com.badlogic.gdx.files.FileHandle) IOException(java.io.IOException) Texture(com.badlogic.gdx.graphics.Texture)

Example 10 with Texture

use of com.badlogic.gdx.graphics.Texture in project libgdx by libgdx.

the class OrthoCachedTiledMapRenderer method renderTileLayer.

@Override
public void renderTileLayer(TiledMapTileLayer layer) {
    final float color = Color.toFloatBits(1, 1, 1, layer.getOpacity());
    final int layerWidth = layer.getWidth();
    final int layerHeight = layer.getHeight();
    final float layerTileWidth = layer.getTileWidth() * unitScale;
    final float layerTileHeight = layer.getTileHeight() * unitScale;
    final float layerOffsetX = layer.getOffsetX() * unitScale;
    // offset in tiled is y down, so we flip it
    final float layerOffsetY = -layer.getOffsetY() * unitScale;
    final int col1 = Math.max(0, (int) ((cacheBounds.x - layerOffsetX) / layerTileWidth));
    final int col2 = Math.min(layerWidth, (int) ((cacheBounds.x + cacheBounds.width + layerTileWidth - layerOffsetX) / layerTileWidth));
    final int row1 = Math.max(0, (int) ((cacheBounds.y - layerOffsetY) / layerTileHeight));
    final int row2 = Math.min(layerHeight, (int) ((cacheBounds.y + cacheBounds.height + layerTileHeight - layerOffsetY) / layerTileHeight));
    canCacheMoreN = row2 < layerHeight;
    canCacheMoreE = col2 < layerWidth;
    canCacheMoreW = col1 > 0;
    canCacheMoreS = row1 > 0;
    float[] vertices = this.vertices;
    for (int row = row2; row >= row1; row--) {
        for (int col = col1; col < col2; col++) {
            final TiledMapTileLayer.Cell cell = layer.getCell(col, row);
            if (cell == null)
                continue;
            final TiledMapTile tile = cell.getTile();
            if (tile == null)
                continue;
            count++;
            final boolean flipX = cell.getFlipHorizontally();
            final boolean flipY = cell.getFlipVertically();
            final int rotations = cell.getRotation();
            final TextureRegion region = tile.getTextureRegion();
            final Texture texture = region.getTexture();
            final float x1 = col * layerTileWidth + tile.getOffsetX() * unitScale + layerOffsetX;
            final float y1 = row * layerTileHeight + tile.getOffsetY() * unitScale + layerOffsetY;
            final float x2 = x1 + region.getRegionWidth() * unitScale;
            final float y2 = y1 + region.getRegionHeight() * unitScale;
            final float adjustX = 0.5f / texture.getWidth();
            final float adjustY = 0.5f / texture.getHeight();
            final float u1 = region.getU() + adjustX;
            final float v1 = region.getV2() - adjustY;
            final float u2 = region.getU2() - adjustX;
            final float v2 = region.getV() + adjustY;
            vertices[X1] = x1;
            vertices[Y1] = y1;
            vertices[C1] = color;
            vertices[U1] = u1;
            vertices[V1] = v1;
            vertices[X2] = x1;
            vertices[Y2] = y2;
            vertices[C2] = color;
            vertices[U2] = u1;
            vertices[V2] = v2;
            vertices[X3] = x2;
            vertices[Y3] = y2;
            vertices[C3] = color;
            vertices[U3] = u2;
            vertices[V3] = v2;
            vertices[X4] = x2;
            vertices[Y4] = y1;
            vertices[C4] = color;
            vertices[U4] = u2;
            vertices[V4] = v1;
            if (flipX) {
                float temp = vertices[U1];
                vertices[U1] = vertices[U3];
                vertices[U3] = temp;
                temp = vertices[U2];
                vertices[U2] = vertices[U4];
                vertices[U4] = temp;
            }
            if (flipY) {
                float temp = vertices[V1];
                vertices[V1] = vertices[V3];
                vertices[V3] = temp;
                temp = vertices[V2];
                vertices[V2] = vertices[V4];
                vertices[V4] = temp;
            }
            if (rotations != 0) {
                switch(rotations) {
                    case Cell.ROTATE_90:
                        {
                            float tempV = vertices[V1];
                            vertices[V1] = vertices[V2];
                            vertices[V2] = vertices[V3];
                            vertices[V3] = vertices[V4];
                            vertices[V4] = tempV;
                            float tempU = vertices[U1];
                            vertices[U1] = vertices[U2];
                            vertices[U2] = vertices[U3];
                            vertices[U3] = vertices[U4];
                            vertices[U4] = tempU;
                            break;
                        }
                    case Cell.ROTATE_180:
                        {
                            float tempU = vertices[U1];
                            vertices[U1] = vertices[U3];
                            vertices[U3] = tempU;
                            tempU = vertices[U2];
                            vertices[U2] = vertices[U4];
                            vertices[U4] = tempU;
                            float tempV = vertices[V1];
                            vertices[V1] = vertices[V3];
                            vertices[V3] = tempV;
                            tempV = vertices[V2];
                            vertices[V2] = vertices[V4];
                            vertices[V4] = tempV;
                            break;
                        }
                    case Cell.ROTATE_270:
                        {
                            float tempV = vertices[V1];
                            vertices[V1] = vertices[V4];
                            vertices[V4] = vertices[V3];
                            vertices[V3] = vertices[V2];
                            vertices[V2] = tempV;
                            float tempU = vertices[U1];
                            vertices[U1] = vertices[U4];
                            vertices[U4] = vertices[U3];
                            vertices[U3] = vertices[U2];
                            vertices[U2] = tempU;
                            break;
                        }
                }
            }
            spriteCache.add(texture, vertices, 0, NUM_VERTICES);
        }
    }
}
Also used : TextureRegion(com.badlogic.gdx.graphics.g2d.TextureRegion) TiledMapTileLayer(com.badlogic.gdx.maps.tiled.TiledMapTileLayer) TiledMapTile(com.badlogic.gdx.maps.tiled.TiledMapTile) Cell(com.badlogic.gdx.maps.tiled.TiledMapTileLayer.Cell) Texture(com.badlogic.gdx.graphics.Texture)

Aggregations

Texture (com.badlogic.gdx.graphics.Texture)162 SpriteBatch (com.badlogic.gdx.graphics.g2d.SpriteBatch)59 TextureRegion (com.badlogic.gdx.graphics.g2d.TextureRegion)56 BitmapFont (com.badlogic.gdx.graphics.g2d.BitmapFont)31 Pixmap (com.badlogic.gdx.graphics.Pixmap)27 Stage (com.badlogic.gdx.scenes.scene2d.Stage)23 OrthographicCamera (com.badlogic.gdx.graphics.OrthographicCamera)20 Sprite (com.badlogic.gdx.graphics.g2d.Sprite)17 Image (com.badlogic.gdx.scenes.scene2d.ui.Image)14 TextureAtlas (com.badlogic.gdx.graphics.g2d.TextureAtlas)11 Skin (com.badlogic.gdx.scenes.scene2d.ui.Skin)11 GdxRuntimeException (com.badlogic.gdx.utils.GdxRuntimeException)11 Material (com.badlogic.gdx.graphics.g3d.Material)10 ShaderProgram (com.badlogic.gdx.graphics.glutils.ShaderProgram)10 PerspectiveCamera (com.badlogic.gdx.graphics.PerspectiveCamera)9 VertexAttribute (com.badlogic.gdx.graphics.VertexAttribute)8 ColorAttribute (com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute)8 ShapeRenderer (com.badlogic.gdx.graphics.glutils.ShapeRenderer)8 Actor (com.badlogic.gdx.scenes.scene2d.Actor)8 Label (com.badlogic.gdx.scenes.scene2d.ui.Label)8