Search in sources :

Example 16 with TextureAtlas

use of com.badlogic.gdx.graphics.g2d.TextureAtlas in project gdx-skineditor by cobolfoo.

the class MainScreen method refreshResources.

/**
	 * 
	 */
public void refreshResources() {
    TexturePacker.Settings settings = new TexturePacker.Settings();
    settings.combineSubdirectories = true;
    settings.maxWidth = 2048;
    settings.maxHeight = 2048;
    TexturePacker.process(settings, "projects/" + currentProject + "/assets/", "projects/" + currentProject, "uiskin");
    // Load project skin
    if (game.skinProject != null) {
        game.skinProject.dispose();
    }
    game.skinProject = new Skin();
    game.skinProject.addRegions(new TextureAtlas(Gdx.files.local("projects/" + currentProject + "/uiskin.atlas")));
    game.skinProject.load(Gdx.files.local("projects/" + currentProject + "/uiskin.json"));
}
Also used : TextureAtlas(com.badlogic.gdx.graphics.g2d.TextureAtlas) Skin(com.badlogic.gdx.scenes.scene2d.ui.Skin) TexturePacker(com.badlogic.gdx.tools.texturepacker.TexturePacker)

Example 17 with TextureAtlas

use of com.badlogic.gdx.graphics.g2d.TextureAtlas in project gdx-skineditor by cobolfoo.

the class SkinEditorGame method create.

@Override
public void create() {
    opt = new OptionalChecker();
    fm = new SystemFonts();
    fm.refreshFonts();
    // Create projects folder if not already here
    FileHandle dirProjects = new FileHandle("projects");
    if (dirProjects.isDirectory() == false) {
        dirProjects.mkdirs();
    }
    // Rebuild from raw resources, kind of overkill, might disable it for production
    TexturePacker.Settings settings = new TexturePacker.Settings();
    settings.combineSubdirectories = true;
    TexturePacker.process(settings, "resources/raw/", ".", "resources/uiskin");
    batch = new SpriteBatch();
    skin = new Skin();
    atlas = new TextureAtlas(Gdx.files.internal("resources/uiskin.atlas"));
    skin.addRegions(new TextureAtlas(Gdx.files.local("resources/uiskin.atlas")));
    skin.load(Gdx.files.local("resources/uiskin.json"));
    screenMain = new MainScreen(this);
    screenWelcome = new WelcomeScreen(this);
    setScreen(screenWelcome);
}
Also used : WelcomeScreen(org.shadebob.skineditor.screens.WelcomeScreen) FileHandle(com.badlogic.gdx.files.FileHandle) TextureAtlas(com.badlogic.gdx.graphics.g2d.TextureAtlas) Skin(com.badlogic.gdx.scenes.scene2d.ui.Skin) TexturePacker(com.badlogic.gdx.tools.texturepacker.TexturePacker) SpriteBatch(com.badlogic.gdx.graphics.g2d.SpriteBatch) MainScreen(org.shadebob.skineditor.screens.MainScreen)

Example 18 with TextureAtlas

use of com.badlogic.gdx.graphics.g2d.TextureAtlas 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 19 with TextureAtlas

use of com.badlogic.gdx.graphics.g2d.TextureAtlas in project commons-gdx by gemserk.

the class SpriteResourceBuilder method build.

@Override
public Sprite build() {
    if (textureId != null) {
        Texture texture = resourceManager.getResourceValue(textureId);
        int w = width != 0 ? width : texture.getWidth();
        int h = height != 0 ? height : texture.getHeight();
        Sprite sprite = new Sprite(texture, x, y, w, h);
        SpriteUtils.transformSprite(sprite, scale, cx, cy, flop, flip, rotate90, clockwise);
        return sprite;
    } else if (textureAtlasId != null) {
        if (spriteRegion == null) {
            TextureAtlas textureAtlas = resourceManager.getResourceValue(textureAtlasId);
            try {
                spriteRegion = textureAtlas.createSprite(regionId, regionIndex);
            } catch (Exception e) {
                throw new RuntimeException("Failed to load AtlasRegion " + regionId + " with index " + regionIndex + " from TextureAtlas " + textureAtlasId, e);
            }
            if (spriteRegion == null)
                throw new RuntimeException("AtlasRegion " + regionId + " with index " + regionIndex + " from TextureAtlas " + textureAtlasId + " not found");
        }
        // note that this resource will not be updated if the resource of the texture atlas changed...
        Sprite sprite = null;
        if (trySpriteAtlas) {
            if (spriteRegion instanceof AtlasSprite)
                sprite = new AtlasSprite(((AtlasSprite) spriteRegion).getAtlasRegion());
            else
                sprite = new Sprite(spriteRegion);
        } else {
            sprite = new Sprite(spriteRegion);
        }
        SpriteUtils.transformSprite(sprite, scale, cx, cy, flop, flip, rotate90, clockwise);
        return sprite;
    }
    throw new RuntimeException("failed to create sprite neither textureId nor textureAtlasId specified");
}
Also used : AtlasSprite(com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasSprite) Sprite(com.badlogic.gdx.graphics.g2d.Sprite) TextureAtlas(com.badlogic.gdx.graphics.g2d.TextureAtlas) AtlasSprite(com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasSprite) Texture(com.badlogic.gdx.graphics.Texture)

Example 20 with TextureAtlas

use of com.badlogic.gdx.graphics.g2d.TextureAtlas in project commons-gdx by gemserk.

the class LibgdxResourceBuilder method splitLoadingTextureAtlas.

public void splitLoadingTextureAtlas(final String id, final String file) {
    FileHandle packFile = Gdx.files.internal(file);
    final TextureAtlasData textureAtlasData = new TextureAtlasData(packFile, packFile.parent(), false);
    Array<Page> pages = textureAtlasData.getPages();
    final String pageTextureSuffix = "_generated_page_";
    for (int i = 0; i < pages.size; i++) {
        Page page = pages.get(i);
        FileHandle textureFile = page.textureFile;
        resource(id + pageTextureSuffix + i, texture2(textureFile).format(page.format).useMipMaps(page.useMipMaps).magFilter(page.magFilter).minFilter(page.minFilter));
    }
    resourceManager.add(id, new DataLoader<TextureAtlas>() {

        @Override
        public TextureAtlas load() {
            Array<Page> pages = textureAtlasData.getPages();
            for (int i = 0; i < pages.size; i++) {
                Page page = pages.get(i);
                try {
                    String textureResourceId = id + pageTextureSuffix + i;
                    page.texture = resourceManager.getResourceValue(textureResourceId);
                    if (page.texture == null)
                        throw new RuntimeException("The resource " + textureResourceId + " was not found");
                } catch (Exception e) {
                    throw new RuntimeException("Error while loading page for textureAtlas " + id + " - page: " + page.textureFile.path(), e);
                }
            }
            return new TextureAtlas(textureAtlasData);
        }

        @Override
        public void unload(TextureAtlas atlas) {
            atlas.dispose();
        }
    });
}
Also used : Array(com.badlogic.gdx.utils.Array) TextureAtlasData(com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData) GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) FileHandle(com.badlogic.gdx.files.FileHandle) TextureAtlas(com.badlogic.gdx.graphics.g2d.TextureAtlas) Page(com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData.Page) GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException)

Aggregations

TextureAtlas (com.badlogic.gdx.graphics.g2d.TextureAtlas)33 Texture (com.badlogic.gdx.graphics.Texture)11 BitmapFont (com.badlogic.gdx.graphics.g2d.BitmapFont)11 SpriteBatch (com.badlogic.gdx.graphics.g2d.SpriteBatch)10 GdxRuntimeException (com.badlogic.gdx.utils.GdxRuntimeException)7 Sprite (com.badlogic.gdx.graphics.g2d.Sprite)6 TextureRegion (com.badlogic.gdx.graphics.g2d.TextureRegion)6 FileHandle (com.badlogic.gdx.files.FileHandle)5 AtlasRegion (com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion)4 Skin (com.badlogic.gdx.scenes.scene2d.ui.Skin)4 Array (com.badlogic.gdx.utils.Array)4 Animation (com.badlogic.gdx.graphics.g2d.Animation)3 AtlasSprite (com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasSprite)3 ShapeRenderer (com.badlogic.gdx.graphics.glutils.ShapeRenderer)3 TexturePacker (com.badlogic.gdx.tools.texturepacker.TexturePacker)3 InputAdapter (com.badlogic.gdx.InputAdapter)2 Pixmap (com.badlogic.gdx.graphics.Pixmap)2 Page (com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData.Page)2 ShaderProgram (com.badlogic.gdx.graphics.glutils.ShaderProgram)2 StaticTiledMapTile (com.badlogic.gdx.maps.tiled.tiles.StaticTiledMapTile)2