Search in sources :

Example 96 with GdxRuntimeException

use of com.badlogic.gdx.utils.GdxRuntimeException in project gdx-skineditor by cobolfoo.

the class Skin method getDrawable.

/**
	 * Returns a registered drawable. If no drawable is found but a region,
	 * ninepatch, or sprite exists with the name, then the appropriate drawable
	 * is created and stored in the skin.
	 */
public Drawable getDrawable(String name) {
    Drawable drawable = optional(name, Drawable.class);
    if (drawable != null)
        return drawable;
    drawable = optional(name, TiledDrawable.class);
    if (drawable != null)
        return drawable;
    // has rotation or whitespace stripping, use sprite.
    try {
        TextureRegion textureRegion = getRegion(name);
        if (textureRegion instanceof AtlasRegion) {
            AtlasRegion region = (AtlasRegion) textureRegion;
            if (region.splits != null)
                drawable = new NinePatchDrawable(getPatch(name));
            else if (region.rotate || region.packedWidth != region.originalWidth || region.packedHeight != region.originalHeight)
                drawable = new SpriteDrawable(getSprite(name));
        }
        if (drawable == null)
            drawable = new TextureRegionDrawable(textureRegion);
    } catch (GdxRuntimeException ignored) {
    }
    // drawable.
    if (drawable == null) {
        NinePatch patch = optional(name, NinePatch.class);
        if (patch != null)
            drawable = new NinePatchDrawable(patch);
        else {
            Sprite sprite = optional(name, Sprite.class);
            if (sprite != null)
                drawable = new SpriteDrawable(sprite);
            else
                throw new GdxRuntimeException("No Drawable, NinePatch, TextureRegion, Texture, or Sprite registered with name: " + name);
        }
    }
    add(name, drawable, Drawable.class);
    return drawable;
}
Also used : GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) TextureRegion(com.badlogic.gdx.graphics.g2d.TextureRegion) SpriteDrawable(com.badlogic.gdx.scenes.scene2d.utils.SpriteDrawable) AtlasSprite(com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasSprite) Sprite(com.badlogic.gdx.graphics.g2d.Sprite) TiledDrawable(com.badlogic.gdx.scenes.scene2d.utils.TiledDrawable) NinePatch(com.badlogic.gdx.graphics.g2d.NinePatch) TextureRegionDrawable(com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable) Drawable(com.badlogic.gdx.scenes.scene2d.utils.Drawable) SpriteDrawable(com.badlogic.gdx.scenes.scene2d.utils.SpriteDrawable) NinePatchDrawable(com.badlogic.gdx.scenes.scene2d.utils.NinePatchDrawable) TiledDrawable(com.badlogic.gdx.scenes.scene2d.utils.TiledDrawable) TextureRegionDrawable(com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable) AtlasRegion(com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion) NinePatchDrawable(com.badlogic.gdx.scenes.scene2d.utils.NinePatchDrawable)

Example 97 with GdxRuntimeException

use of com.badlogic.gdx.utils.GdxRuntimeException in project bdx by GoranM.

the class AudioStore method findFile.

protected FileHandle findFile(String name) {
    String[] supported = { ".wav", ".mp3", ".ogg" };
    String files = "";
    for (String ext : supported) {
        FileHandle f = Gdx.files.internal(pathRoot + name + ext);
        if (f.exists())
            return f;
        files += name + ext + (ext.equals(".ogg") ? "" : " or ");
    }
    throw new GdxRuntimeException("Could not find " + files + " in " + pathRoot);
}
Also used : GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) FileHandle(com.badlogic.gdx.files.FileHandle)

Example 98 with GdxRuntimeException

use of com.badlogic.gdx.utils.GdxRuntimeException in project gdx-skineditor by cobolfoo.

the class Skin method getSprite.

/**
	 * Returns a registered sprite. If no sprite is found but a region exists
	 * with the name, a sprite is created from the region and stored in the
	 * skin. If the region is an {@link AtlasRegion} then an {@link AtlasSprite}
	 * is used if the region has been whitespace stripped or packed rotated 90
	 * degrees.
	 */
public Sprite getSprite(String name) {
    Sprite sprite = optional(name, Sprite.class);
    if (sprite != null)
        return sprite;
    try {
        TextureRegion textureRegion = getRegion(name);
        if (textureRegion instanceof AtlasRegion) {
            AtlasRegion region = (AtlasRegion) textureRegion;
            if (region.rotate || region.packedWidth != region.originalWidth || region.packedHeight != region.originalHeight)
                sprite = new AtlasSprite(region);
        }
        if (sprite == null)
            sprite = new Sprite(textureRegion);
        add(name, sprite, NinePatch.class);
        return sprite;
    } catch (GdxRuntimeException ex) {
        throw new GdxRuntimeException("No NinePatch, TextureRegion, or Texture registered with name: " + name);
    }
}
Also used : GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) TextureRegion(com.badlogic.gdx.graphics.g2d.TextureRegion) AtlasSprite(com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasSprite) Sprite(com.badlogic.gdx.graphics.g2d.Sprite) AtlasSprite(com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasSprite) AtlasRegion(com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion)

Example 99 with GdxRuntimeException

use of com.badlogic.gdx.utils.GdxRuntimeException in project commons-gdx by gemserk.

the class LibgdxResourceBuilder method animation.

public void animation(final String id, final String textureAtlasId, final String prefix, final int sf, final int ef, final boolean loop, final boolean removeAlias, final float time, final float... times) {
    resourceManager.addVolatile(id, new DataLoader<Animation>() {

        class DuplicatedSpritesRemover {

            float[] times;

            Sprite[] frames;

            public void removeDuplicates(Sprite[] frames, float[] times) {
                ArrayList<Sprite> newSprites = new ArrayList<Sprite>();
                ArrayList<FloatValue> newTimes = new ArrayList<FloatValue>();
                Sprite lastSprite = null;
                int i = 0;
                FloatValue frameTime = null;
                do {
                    Sprite sprite = frames[i];
                    if (SpriteUtils.isAliasSprite(lastSprite, sprite)) {
                        frameTime.value += times[i];
                    } else {
                        newSprites.add(sprite);
                        lastSprite = sprite;
                        frameTime = new FloatValue(times[i]);
                        newTimes.add(frameTime);
                    }
                    i++;
                } while (i < frames.length);
                this.frames = new Sprite[newSprites.size()];
                this.times = new float[newTimes.size()];
                newSprites.toArray(this.frames);
                for (i = 0; i < newTimes.size(); i++) this.times[i] = newTimes.get(i).value;
            }
        }

        FrameAnimationImpl cachedFrameAnimation = null;

        Animation cachedAnimation = null;

        @Override
        public Animation load() {
            if (cachedAnimation == null) {
                TextureAtlas textureAtlas = resourceManager.getResourceValue(textureAtlasId);
                Array<Sprite> sprites = null;
                try {
                    sprites = textureAtlas.createSprites(prefix);
                } catch (GdxRuntimeException e) {
                    throw new RuntimeException("Failed to create animation " + id + " from texture atlas " + textureAtlasId, e);
                }
                if (sprites.size == 0)
                    throw new IllegalArgumentException("Failed to create animation " + id + ", no regions found for prefix " + prefix);
                int endFrame = ef;
                int startFrame = sf;
                if (endFrame == -1)
                    endFrame = sprites.size - 1;
                if (startFrame == -1)
                    startFrame = 0;
                Sprite[] frames = new Sprite[endFrame - startFrame + 1];
                int frameNumber = startFrame;
                if (endFrame >= sprites.size) {
                    throw new IllegalArgumentException("Failed to create animation " + id + ", end frame " + endFrame + " couldn't be greater than sprites quantity " + sprites.size);
                }
                int framesCount = frames.length;
                float[] newTimes = new float[framesCount];
                // newTimes[0] = 0.001f * (float) time;
                newTimes[0] = time;
                float lastTime = newTimes[0];
                for (int i = 1; i < framesCount; i++) {
                    if (i < times.length) {
                        // newTimes[i] = ((float) times[i]) * 0.001f;
                        newTimes[i] = times[i];
                        lastTime = newTimes[i];
                    } else
                        newTimes[i] = lastTime;
                }
                for (int i = 0; i < frames.length; i++) {
                    Sprite sprite = sprites.get(frameNumber);
                    if (sprite instanceof AtlasSprite)
                        frames[i] = new AtlasSprite(((AtlasSprite) sprite).getAtlasRegion());
                    else
                        frames[i] = new Sprite(sprite);
                    frameNumber++;
                }
                if (removeAlias) {
                    int framesBeforeRemoval = frames.length;
                    DuplicatedSpritesRemover duplicatedSpritesRemover = new DuplicatedSpritesRemover();
                    duplicatedSpritesRemover.removeDuplicates(frames, newTimes);
                    frames = duplicatedSpritesRemover.frames;
                    newTimes = duplicatedSpritesRemover.times;
                    Gdx.app.log("commons-gdx", "[" + id + "] frames removed: " + (framesBeforeRemoval - frames.length));
                }
                cachedFrameAnimation = new FrameAnimationImpl(newTimes);
                cachedFrameAnimation.setLoop(loop);
                cachedAnimation = new Animation(frames, cachedFrameAnimation);
            }
            Sprite[] frames = new Sprite[cachedAnimation.getFramesCount()];
            for (int i = 0; i < frames.length; i++) {
                Sprite sprite = cachedAnimation.getFrame(i);
                if (sprite instanceof AtlasSprite)
                    frames[i] = new AtlasSprite(((AtlasSprite) sprite).getAtlasRegion());
                else
                    frames[i] = new Sprite(sprite);
            }
            return new Animation(frames, new FrameAnimationImpl(cachedFrameAnimation));
        }
    });
}
Also used : AtlasSprite(com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasSprite) Sprite(com.badlogic.gdx.graphics.g2d.Sprite) ArrayList(java.util.ArrayList) AtlasSprite(com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasSprite) Array(com.badlogic.gdx.utils.Array) GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) FrameAnimationImpl(com.gemserk.animation4j.FrameAnimationImpl) GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) TextureAtlas(com.badlogic.gdx.graphics.g2d.TextureAtlas) Animation(com.gemserk.animation4j.gdx.Animation) FloatValue(com.gemserk.commons.values.FloatValue)

Example 100 with GdxRuntimeException

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

the class TiledMapPacker method writeUpdatedTMX.

private void writeUpdatedTMX(TiledMap tiledMap, FileHandle tmxFileHandle) throws IOException {
    Document doc;
    DocumentBuilder docBuilder;
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    try {
        docBuilder = docFactory.newDocumentBuilder();
        doc = docBuilder.parse(tmxFileHandle.read());
        Node map = doc.getFirstChild();
        while (map.getNodeType() != Node.ELEMENT_NODE || map.getNodeName() != "map") {
            if ((map = map.getNextSibling()) == null) {
                throw new GdxRuntimeException("Couldn't find map node!");
            }
        }
        setProperty(doc, map, "atlas", settings.tilesetOutputDirectory + "/" + settings.atlasOutputName + ".atlas");
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(doc);
        outputDir.mkdirs();
        StreamResult result = new StreamResult(new File(outputDir, tmxFileHandle.name()));
        transformer.transform(source, result);
    } catch (ParserConfigurationException e) {
        throw new RuntimeException("ParserConfigurationException: " + e.getMessage());
    } catch (SAXException e) {
        throw new RuntimeException("SAXException: " + e.getMessage());
    } catch (TransformerConfigurationException e) {
        throw new RuntimeException("TransformerConfigurationException: " + e.getMessage());
    } catch (TransformerException e) {
        throw new RuntimeException("TransformerException: " + e.getMessage());
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) File(java.io.File) TransformerException(javax.xml.transform.TransformerException)

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