Search in sources :

Example 16 with GdxRuntimeException

use of com.badlogic.gdx.utils.GdxRuntimeException in project libgdx by libgdx.

the class ShapeCache method begin.

/** Initialize ShapeCache for mesh generation
	 * @param primitiveType OpenGL primitive type */
public MeshPartBuilder begin(int primitiveType) {
    if (building)
        throw new GdxRuntimeException("Call end() after calling begin()");
    building = true;
    builder.begin(mesh.getVertexAttributes());
    builder.part(id, primitiveType, renderable.meshPart);
    return builder;
}
Also used : GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException)

Example 17 with GdxRuntimeException

use of com.badlogic.gdx.utils.GdxRuntimeException in project libgdx by libgdx.

the class EllipseShapeBuilder method build.

/** Build an ellipse */
public static void build(MeshPartBuilder builder, float width, float height, float innerWidth, float innerHeight, int divisions, float centerX, float centerY, float centerZ, float normalX, float normalY, float normalZ, float tangentX, float tangentY, float tangentZ, float binormalX, float binormalY, float binormalZ, float angleFrom, float angleTo) {
    if (innerWidth <= 0 || innerHeight <= 0) {
        builder.ensureVertices(divisions + 2);
        builder.ensureTriangleIndices(divisions);
    } else if (innerWidth == width && innerHeight == height) {
        builder.ensureVertices(divisions + 1);
        builder.ensureIndices(divisions + 1);
        if (builder.getPrimitiveType() != GL20.GL_LINES)
            throw new GdxRuntimeException("Incorrect primitive type : expect GL_LINES because innerWidth == width && innerHeight == height");
    } else {
        builder.ensureVertices((divisions + 1) * 2);
        builder.ensureRectangleIndices(divisions + 1);
    }
    final float ao = MathUtils.degreesToRadians * angleFrom;
    final float step = (MathUtils.degreesToRadians * (angleTo - angleFrom)) / divisions;
    final Vector3 sxEx = tmpV1.set(tangentX, tangentY, tangentZ).scl(width * 0.5f);
    final Vector3 syEx = tmpV2.set(binormalX, binormalY, binormalZ).scl(height * 0.5f);
    final Vector3 sxIn = tmpV3.set(tangentX, tangentY, tangentZ).scl(innerWidth * 0.5f);
    final Vector3 syIn = tmpV4.set(binormalX, binormalY, binormalZ).scl(innerHeight * 0.5f);
    VertexInfo currIn = vertTmp3.set(null, null, null, null);
    currIn.hasUV = currIn.hasPosition = currIn.hasNormal = true;
    currIn.uv.set(.5f, .5f);
    currIn.position.set(centerX, centerY, centerZ);
    currIn.normal.set(normalX, normalY, normalZ);
    VertexInfo currEx = vertTmp4.set(null, null, null, null);
    currEx.hasUV = currEx.hasPosition = currEx.hasNormal = true;
    currEx.uv.set(.5f, .5f);
    currEx.position.set(centerX, centerY, centerZ);
    currEx.normal.set(normalX, normalY, normalZ);
    final short center = builder.vertex(currEx);
    float angle = 0f;
    final float us = 0.5f * (innerWidth / width);
    final float vs = 0.5f * (innerHeight / height);
    short i1, i2 = 0, i3 = 0, i4 = 0;
    for (int i = 0; i <= divisions; i++) {
        angle = ao + step * i;
        final float x = MathUtils.cos(angle);
        final float y = MathUtils.sin(angle);
        currEx.position.set(centerX, centerY, centerZ).add(sxEx.x * x + syEx.x * y, sxEx.y * x + syEx.y * y, sxEx.z * x + syEx.z * y);
        currEx.uv.set(.5f + .5f * x, .5f + .5f * y);
        i1 = builder.vertex(currEx);
        if (innerWidth <= 0f || innerHeight <= 0f) {
            if (i != 0)
                builder.triangle(i1, i2, center);
            i2 = i1;
        } else if (innerWidth == width && innerHeight == height) {
            if (i != 0)
                builder.line(i1, i2);
            i2 = i1;
        } else {
            currIn.position.set(centerX, centerY, centerZ).add(sxIn.x * x + syIn.x * y, sxIn.y * x + syIn.y * y, sxIn.z * x + syIn.z * y);
            currIn.uv.set(.5f + us * x, .5f + vs * y);
            i2 = i1;
            i1 = builder.vertex(currIn);
            if (i != 0)
                builder.rect(i1, i2, i4, i3);
            i4 = i2;
            i3 = i1;
        }
    }
}
Also used : GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) VertexInfo(com.badlogic.gdx.graphics.g3d.utils.MeshPartBuilder.VertexInfo) Vector3(com.badlogic.gdx.math.Vector3)

Example 18 with GdxRuntimeException

use of com.badlogic.gdx.utils.GdxRuntimeException in project libgdx by libgdx.

the class FloatTextureData method prepare.

@Override
public void prepare() {
    if (isPrepared)
        throw new GdxRuntimeException("Already prepared");
    this.buffer = BufferUtils.newFloatBuffer(width * height * 4);
    isPrepared = true;
}
Also used : GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException)

Example 19 with GdxRuntimeException

use of com.badlogic.gdx.utils.GdxRuntimeException in project libgdx by libgdx.

the class KTXTextureData method consumeCustomData.

@Override
public void consumeCustomData(int target) {
    if (compressedData == null)
        throw new GdxRuntimeException("Call prepare() before calling consumeCompressedData()");
    IntBuffer buffer = BufferUtils.newIntBuffer(16);
    // Check OpenGL type and format, detect compressed data format (no type & format)
    boolean compressed = false;
    if (glType == 0 || glFormat == 0) {
        if (glType + glFormat != 0)
            throw new GdxRuntimeException("either both or none of glType, glFormat must be zero");
        compressed = true;
    }
    // find OpenGL texture target and dimensions
    int textureDimensions = 1;
    int glTarget = GL_TEXTURE_1D;
    if (pixelHeight > 0) {
        textureDimensions = 2;
        glTarget = GL20.GL_TEXTURE_2D;
    }
    if (pixelDepth > 0) {
        textureDimensions = 3;
        glTarget = GL_TEXTURE_3D;
    }
    if (numberOfFaces == 6) {
        if (textureDimensions == 2)
            glTarget = GL20.GL_TEXTURE_CUBE_MAP;
        else
            throw new GdxRuntimeException("cube map needs 2D faces");
    } else if (numberOfFaces != 1) {
        throw new GdxRuntimeException("numberOfFaces must be either 1 or 6");
    }
    if (numberOfArrayElements > 0) {
        if (glTarget == GL_TEXTURE_1D)
            glTarget = GL_TEXTURE_1D_ARRAY_EXT;
        else if (glTarget == GL20.GL_TEXTURE_2D)
            glTarget = GL_TEXTURE_2D_ARRAY_EXT;
        else
            throw new GdxRuntimeException("No API for 3D and cube arrays yet");
        textureDimensions++;
    }
    if (glTarget == 0x1234)
        throw new GdxRuntimeException("Unsupported texture format (only 2D texture are supported in LibGdx for the time being)");
    int singleFace = -1;
    if (numberOfFaces == 6 && target != GL20.GL_TEXTURE_CUBE_MAP) {
        // Load a single face of the cube (should be avoided since the data is unloaded afterwards)
        if (!(GL20.GL_TEXTURE_CUBE_MAP_POSITIVE_X <= target && target <= GL20.GL_TEXTURE_CUBE_MAP_NEGATIVE_Z))
            throw new GdxRuntimeException("You must specify either GL_TEXTURE_CUBE_MAP to bind all 6 faces of the cube or the requested face GL_TEXTURE_CUBE_MAP_POSITIVE_X and followings.");
        singleFace = target - GL20.GL_TEXTURE_CUBE_MAP_POSITIVE_X;
        target = GL20.GL_TEXTURE_CUBE_MAP_POSITIVE_X;
    } else if (numberOfFaces == 6 && target == GL20.GL_TEXTURE_CUBE_MAP) {
        // Load the 6 faces
        target = GL20.GL_TEXTURE_CUBE_MAP_POSITIVE_X;
    } else {
        // Load normal texture
        if (target != glTarget && !(GL20.GL_TEXTURE_CUBE_MAP_POSITIVE_X <= target && target <= GL20.GL_TEXTURE_CUBE_MAP_NEGATIVE_Z && target == GL20.GL_TEXTURE_2D))
            throw new GdxRuntimeException("Invalid target requested : 0x" + Integer.toHexString(target) + ", expecting : 0x" + Integer.toHexString(glTarget));
    }
    // KTX files require an unpack alignment of 4
    Gdx.gl.glGetIntegerv(GL20.GL_UNPACK_ALIGNMENT, buffer);
    int previousUnpackAlignment = buffer.get(0);
    if (previousUnpackAlignment != 4)
        Gdx.gl.glPixelStorei(GL20.GL_UNPACK_ALIGNMENT, 4);
    int glInternalFormat = this.glInternalFormat;
    int glFormat = this.glFormat;
    int pos = imagePos;
    for (int level = 0; level < numberOfMipmapLevels; level++) {
        int pixelWidth = Math.max(1, this.pixelWidth >> level);
        int pixelHeight = Math.max(1, this.pixelHeight >> level);
        int pixelDepth = Math.max(1, this.pixelDepth >> level);
        compressedData.position(pos);
        int faceLodSize = compressedData.getInt();
        int faceLodSizeRounded = (faceLodSize + 3) & ~3;
        pos += 4;
        for (int face = 0; face < numberOfFaces; face++) {
            compressedData.position(pos);
            pos += faceLodSizeRounded;
            if (singleFace != -1 && singleFace != face)
                continue;
            ByteBuffer data = compressedData.slice();
            data.limit(faceLodSizeRounded);
            if (textureDimensions == 1) {
            // if (compressed)
            // Gdx.gl.glCompressedTexImage1D(target + face, level, glInternalFormat, pixelWidth, 0, faceLodSize,
            // data);
            // else
            // Gdx.gl.glTexImage1D(target + face, level, glInternalFormat, pixelWidth, 0, glFormat, glType, data);
            } else if (textureDimensions == 2) {
                if (numberOfArrayElements > 0)
                    pixelHeight = numberOfArrayElements;
                if (compressed) {
                    if (glInternalFormat == ETC1.ETC1_RGB8_OES) {
                        if (!Gdx.graphics.supportsExtension("GL_OES_compressed_ETC1_RGB8_texture")) {
                            ETC1Data etcData = new ETC1Data(pixelWidth, pixelHeight, data, 0);
                            Pixmap pixmap = ETC1.decodeImage(etcData, Format.RGB888);
                            Gdx.gl.glTexImage2D(target + face, level, pixmap.getGLInternalFormat(), pixmap.getWidth(), pixmap.getHeight(), 0, pixmap.getGLFormat(), pixmap.getGLType(), pixmap.getPixels());
                            pixmap.dispose();
                        } else {
                            Gdx.gl.glCompressedTexImage2D(target + face, level, glInternalFormat, pixelWidth, pixelHeight, 0, faceLodSize, data);
                        }
                    } else {
                        // Try to load (no software unpacking fallback)
                        Gdx.gl.glCompressedTexImage2D(target + face, level, glInternalFormat, pixelWidth, pixelHeight, 0, faceLodSize, data);
                    }
                } else
                    Gdx.gl.glTexImage2D(target + face, level, glInternalFormat, pixelWidth, pixelHeight, 0, glFormat, glType, data);
            } else if (textureDimensions == 3) {
                if (numberOfArrayElements > 0)
                    pixelDepth = numberOfArrayElements;
            // if (compressed)
            // Gdx.gl.glCompressedTexImage3D(target + face, level, glInternalFormat, pixelWidth, pixelHeight, pixelDepth, 0,
            // faceLodSize, data);
            // else
            // Gdx.gl.glTexImage3D(target + face, level, glInternalFormat, pixelWidth, pixelHeight, pixelDepth, 0, glFormat,
            // glType, data);
            }
        }
    }
    if (previousUnpackAlignment != 4)
        Gdx.gl.glPixelStorei(GL20.GL_UNPACK_ALIGNMENT, previousUnpackAlignment);
    if (useMipMaps())
        Gdx.gl.glGenerateMipmap(target);
    // dispose data once transfered to GPU
    disposePreparedData();
}
Also used : GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) ETC1Data(com.badlogic.gdx.graphics.glutils.ETC1.ETC1Data) IntBuffer(java.nio.IntBuffer) ByteBuffer(java.nio.ByteBuffer) Pixmap(com.badlogic.gdx.graphics.Pixmap)

Example 20 with GdxRuntimeException

use of com.badlogic.gdx.utils.GdxRuntimeException in project libgdx by libgdx.

the class Skin method get.

/** Returns a named resource of the specified type.
	 * @throws GdxRuntimeException if the resource was not found. */
public <T> T get(String name, Class<T> type) {
    if (name == null)
        throw new IllegalArgumentException("name cannot be null.");
    if (type == null)
        throw new IllegalArgumentException("type cannot be null.");
    if (type == Drawable.class)
        return (T) getDrawable(name);
    if (type == TextureRegion.class)
        return (T) getRegion(name);
    if (type == NinePatch.class)
        return (T) getPatch(name);
    if (type == Sprite.class)
        return (T) getSprite(name);
    ObjectMap<String, Object> typeResources = resources.get(type);
    if (typeResources == null)
        throw new GdxRuntimeException("No " + type.getName() + " registered with name: " + name);
    Object resource = typeResources.get(name);
    if (resource == null)
        throw new GdxRuntimeException("No " + type.getName() + " registered with name: " + name);
    return (T) resource;
}
Also used : GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException)

Aggregations

GdxRuntimeException (com.badlogic.gdx.utils.GdxRuntimeException)202 IOException (java.io.IOException)40 FileHandle (com.badlogic.gdx.files.FileHandle)14 Array (com.badlogic.gdx.utils.Array)13 Texture (com.badlogic.gdx.graphics.Texture)12 TextureRegion (com.badlogic.gdx.graphics.g2d.TextureRegion)11 AtlasRegion (com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion)9 InputStream (java.io.InputStream)9 Pixmap (com.badlogic.gdx.graphics.Pixmap)8 Sprite (com.badlogic.gdx.graphics.g2d.Sprite)8 TextureAtlas (com.badlogic.gdx.graphics.g2d.TextureAtlas)7 AtlasSprite (com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasSprite)7 BufferedInputStream (java.io.BufferedInputStream)7 File (java.io.File)7 OutputStream (java.io.OutputStream)7 VertexAttribute (com.badlogic.gdx.graphics.VertexAttribute)6 LifecycleListener (com.badlogic.gdx.LifecycleListener)5 ByteBuffer (java.nio.ByteBuffer)5 BitmapFont (com.badlogic.gdx.graphics.g2d.BitmapFont)4 NinePatch (com.badlogic.gdx.graphics.g2d.NinePatch)4