Search in sources :

Example 26 with TextureAtlas

use of com.badlogic.gdx.graphics.g2d.TextureAtlas in project libgdx by libgdx.

the class AtlasIssueTest method create.

public void create() {
    batch = new SpriteBatch();
    batch.setProjectionMatrix(new Matrix4().setToOrtho2D(0, 0, 855, 480));
    atlas = new TextureAtlas(Gdx.files.internal("data/issue_pack"), Gdx.files.internal("data/"));
    sprite = atlas.createSprite("map");
    font = new BitmapFont(Gdx.files.internal("data/font.fnt"), Gdx.files.internal("data/font.png"), false);
    Gdx.gl.glClearColor(0, 1, 0, 1);
}
Also used : TextureAtlas(com.badlogic.gdx.graphics.g2d.TextureAtlas) BitmapFont(com.badlogic.gdx.graphics.g2d.BitmapFont) SpriteBatch(com.badlogic.gdx.graphics.g2d.SpriteBatch) Matrix4(com.badlogic.gdx.math.Matrix4)

Example 27 with TextureAtlas

use of com.badlogic.gdx.graphics.g2d.TextureAtlas in project libgdx by libgdx.

the class AtlasTmxMapLoader method load.

public TiledMap load(String fileName, AtlasTiledMapLoaderParameters parameter) {
    try {
        if (parameter != null) {
            convertObjectToTileSpace = parameter.convertObjectToTileSpace;
            flipY = parameter.flipY;
        } else {
            convertObjectToTileSpace = false;
            flipY = true;
        }
        FileHandle tmxFile = resolve(fileName);
        root = xml.parse(tmxFile);
        ObjectMap<String, TextureAtlas> atlases = new ObjectMap<String, TextureAtlas>();
        FileHandle atlasFile = loadAtlas(root, tmxFile);
        if (atlasFile == null) {
            throw new GdxRuntimeException("Couldn't load atlas");
        }
        TextureAtlas atlas = new TextureAtlas(atlasFile);
        atlases.put(atlasFile.path(), atlas);
        AtlasResolver.DirectAtlasResolver atlasResolver = new AtlasResolver.DirectAtlasResolver(atlases);
        TiledMap map = loadMap(root, tmxFile, atlasResolver);
        map.setOwnedResources(atlases.values().toArray());
        setTextureFilters(parameter.textureMinFilter, parameter.textureMagFilter);
        return map;
    } catch (IOException e) {
        throw new GdxRuntimeException("Couldn't load tilemap '" + fileName + "'", e);
    }
}
Also used : GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) ObjectMap(com.badlogic.gdx.utils.ObjectMap) FileHandle(com.badlogic.gdx.files.FileHandle) TextureAtlas(com.badlogic.gdx.graphics.g2d.TextureAtlas) IOException(java.io.IOException)

Example 28 with TextureAtlas

use of com.badlogic.gdx.graphics.g2d.TextureAtlas in project libgdx by libgdx.

the class AtlasTmxMapLoader method loadTileset.

protected void loadTileset(TiledMap map, Element element, FileHandle tmxFile, AtlasResolver resolver) {
    if (element.getName().equals("tileset")) {
        String name = element.get("name", null);
        int firstgid = element.getIntAttribute("firstgid", 1);
        int tilewidth = element.getIntAttribute("tilewidth", 0);
        int tileheight = element.getIntAttribute("tileheight", 0);
        int spacing = element.getIntAttribute("spacing", 0);
        int margin = element.getIntAttribute("margin", 0);
        String source = element.getAttribute("source", null);
        int offsetX = 0;
        int offsetY = 0;
        String imageSource = "";
        int imageWidth = 0, imageHeight = 0;
        FileHandle image = null;
        if (source != null) {
            FileHandle tsx = getRelativeFileHandle(tmxFile, source);
            try {
                element = xml.parse(tsx);
                name = element.get("name", null);
                tilewidth = element.getIntAttribute("tilewidth", 0);
                tileheight = element.getIntAttribute("tileheight", 0);
                spacing = element.getIntAttribute("spacing", 0);
                margin = element.getIntAttribute("margin", 0);
                Element offset = element.getChildByName("tileoffset");
                if (offset != null) {
                    offsetX = offset.getIntAttribute("x", 0);
                    offsetY = offset.getIntAttribute("y", 0);
                }
                Element imageElement = element.getChildByName("image");
                if (imageElement != null) {
                    imageSource = imageElement.getAttribute("source");
                    imageWidth = imageElement.getIntAttribute("width", 0);
                    imageHeight = imageElement.getIntAttribute("height", 0);
                    image = getRelativeFileHandle(tsx, imageSource);
                }
            } catch (IOException e) {
                throw new GdxRuntimeException("Error parsing external tileset.");
            }
        } else {
            Element offset = element.getChildByName("tileoffset");
            if (offset != null) {
                offsetX = offset.getIntAttribute("x", 0);
                offsetY = offset.getIntAttribute("y", 0);
            }
            Element imageElement = element.getChildByName("image");
            if (imageElement != null) {
                imageSource = imageElement.getAttribute("source");
                imageWidth = imageElement.getIntAttribute("width", 0);
                imageHeight = imageElement.getIntAttribute("height", 0);
                image = getRelativeFileHandle(tmxFile, imageSource);
            }
        }
        String atlasFilePath = map.getProperties().get("atlas", String.class);
        if (atlasFilePath == null) {
            FileHandle atlasFile = tmxFile.sibling(tmxFile.nameWithoutExtension() + ".atlas");
            if (atlasFile.exists())
                atlasFilePath = atlasFile.name();
        }
        if (atlasFilePath == null) {
            throw new GdxRuntimeException("The map is missing the 'atlas' property");
        }
        // get the TextureAtlas for this tileset
        FileHandle atlasHandle = getRelativeFileHandle(tmxFile, atlasFilePath);
        atlasHandle = resolve(atlasHandle.path());
        TextureAtlas atlas = resolver.getAtlas(atlasHandle.path());
        String regionsName = name;
        for (Texture texture : atlas.getTextures()) {
            trackedTextures.add(texture);
        }
        TiledMapTileSet tileset = new TiledMapTileSet();
        MapProperties props = tileset.getProperties();
        tileset.setName(name);
        props.put("firstgid", firstgid);
        props.put("imagesource", imageSource);
        props.put("imagewidth", imageWidth);
        props.put("imageheight", imageHeight);
        props.put("tilewidth", tilewidth);
        props.put("tileheight", tileheight);
        props.put("margin", margin);
        props.put("spacing", spacing);
        if (imageSource != null && imageSource.length() > 0) {
            int lastgid = firstgid + ((imageWidth / tilewidth) * (imageHeight / tileheight)) - 1;
            for (AtlasRegion region : atlas.findRegions(regionsName)) {
                // handle unused tile ids
                if (region != null) {
                    int tileid = region.index + 1;
                    if (tileid >= firstgid && tileid <= lastgid) {
                        StaticTiledMapTile tile = new StaticTiledMapTile(region);
                        tile.setId(tileid);
                        tile.setOffsetX(offsetX);
                        tile.setOffsetY(flipY ? -offsetY : offsetY);
                        tileset.putTile(tileid, tile);
                    }
                }
            }
        }
        for (Element tileElement : element.getChildrenByName("tile")) {
            int tileid = firstgid + tileElement.getIntAttribute("id", 0);
            TiledMapTile tile = tileset.getTile(tileid);
            if (tile == null) {
                Element imageElement = tileElement.getChildByName("image");
                if (imageElement != null) {
                    // Is a tilemap with individual images.
                    String regionName = imageElement.getAttribute("source");
                    regionName = regionName.substring(0, regionName.lastIndexOf('.'));
                    AtlasRegion region = atlas.findRegion(regionName);
                    if (region == null)
                        throw new GdxRuntimeException("Tileset region not found: " + regionName);
                    tile = new StaticTiledMapTile(region);
                    tile.setId(tileid);
                    tile.setOffsetX(offsetX);
                    tile.setOffsetY(flipY ? -offsetY : offsetY);
                    tileset.putTile(tileid, tile);
                }
            }
            if (tile != null) {
                String terrain = tileElement.getAttribute("terrain", null);
                if (terrain != null) {
                    tile.getProperties().put("terrain", terrain);
                }
                String probability = tileElement.getAttribute("probability", null);
                if (probability != null) {
                    tile.getProperties().put("probability", probability);
                }
                Element properties = tileElement.getChildByName("properties");
                if (properties != null) {
                    loadProperties(tile.getProperties(), properties);
                }
            }
        }
        Array<Element> tileElements = element.getChildrenByName("tile");
        Array<AnimatedTiledMapTile> animatedTiles = new Array<AnimatedTiledMapTile>();
        for (Element tileElement : tileElements) {
            int localtid = tileElement.getIntAttribute("id", 0);
            TiledMapTile tile = tileset.getTile(firstgid + localtid);
            if (tile != null) {
                Element animationElement = tileElement.getChildByName("animation");
                if (animationElement != null) {
                    Array<StaticTiledMapTile> staticTiles = new Array<StaticTiledMapTile>();
                    IntArray intervals = new IntArray();
                    for (Element frameElement : animationElement.getChildrenByName("frame")) {
                        staticTiles.add((StaticTiledMapTile) tileset.getTile(firstgid + frameElement.getIntAttribute("tileid")));
                        intervals.add(frameElement.getIntAttribute("duration"));
                    }
                    AnimatedTiledMapTile animatedTile = new AnimatedTiledMapTile(intervals, staticTiles);
                    animatedTile.setId(tile.getId());
                    animatedTiles.add(animatedTile);
                    tile = animatedTile;
                }
                String terrain = tileElement.getAttribute("terrain", null);
                if (terrain != null) {
                    tile.getProperties().put("terrain", terrain);
                }
                String probability = tileElement.getAttribute("probability", null);
                if (probability != null) {
                    tile.getProperties().put("probability", probability);
                }
                Element properties = tileElement.getChildByName("properties");
                if (properties != null) {
                    loadProperties(tile.getProperties(), properties);
                }
            }
        }
        for (AnimatedTiledMapTile tile : animatedTiles) {
            tileset.putTile(tile.getId(), tile);
        }
        Element properties = element.getChildByName("properties");
        if (properties != null) {
            loadProperties(tileset.getProperties(), properties);
        }
        map.getTileSets().addTileSet(tileset);
    }
}
Also used : AnimatedTiledMapTile(com.badlogic.gdx.maps.tiled.tiles.AnimatedTiledMapTile) FileHandle(com.badlogic.gdx.files.FileHandle) Element(com.badlogic.gdx.utils.XmlReader.Element) MapProperties(com.badlogic.gdx.maps.MapProperties) IOException(java.io.IOException) Texture(com.badlogic.gdx.graphics.Texture) GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) Array(com.badlogic.gdx.utils.Array) IntArray(com.badlogic.gdx.utils.IntArray) LongArray(com.badlogic.gdx.utils.LongArray) StaticTiledMapTile(com.badlogic.gdx.maps.tiled.tiles.StaticTiledMapTile) AnimatedTiledMapTile(com.badlogic.gdx.maps.tiled.tiles.AnimatedTiledMapTile) IntArray(com.badlogic.gdx.utils.IntArray) TextureAtlas(com.badlogic.gdx.graphics.g2d.TextureAtlas) StaticTiledMapTile(com.badlogic.gdx.maps.tiled.tiles.StaticTiledMapTile) AtlasRegion(com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion)

Example 29 with TextureAtlas

use of com.badlogic.gdx.graphics.g2d.TextureAtlas in project libgdx by libgdx.

the class TextureLoaderPanel method initializeComponents.

@Override
protected void initializeComponents() {
    super.initializeComponents();
    JButton atlasButton = new JButton("Open Atlas");
    JButton textureButton = new JButton("Open Texture");
    JButton defaultTextureButton = new JButton("Default Texture");
    final JCheckBox genMipMaps = new JCheckBox("Generate MipMaps");
    final JComboBox minFilterBox = new JComboBox(new DefaultComboBoxModel(TextureFilter.values()));
    final JComboBox magFilterBox = new JComboBox(new DefaultComboBoxModel(TextureFilter.values()));
    minFilterBox.setSelectedItem(editor.getTexture().getMinFilter());
    magFilterBox.setSelectedItem(editor.getTexture().getMagFilter());
    ActionListener filterListener = new ActionListener() {

        public void actionPerformed(ActionEvent event) {
            editor.getTexture().setFilter((TextureFilter) minFilterBox.getSelectedItem(), (TextureFilter) magFilterBox.getSelectedItem());
        }
    };
    minFilterBox.addActionListener(filterListener);
    magFilterBox.addActionListener(filterListener);
    atlasButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            File file = editor.showFileLoadDialog();
            if (file != null) {
                TextureAtlas atlas = editor.load(file.getAbsolutePath(), TextureAtlas.class, null, null);
                if (atlas != null) {
                    editor.setAtlas(atlas);
                }
            }
        }
    });
    textureButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            File file = editor.showFileLoadDialog();
            if (file != null) {
                TextureParameter params = new TextureParameter();
                params.genMipMaps = genMipMaps.isSelected();
                params.minFilter = (TextureFilter) minFilterBox.getSelectedItem();
                params.magFilter = (TextureFilter) magFilterBox.getSelectedItem();
                Texture texture = editor.load(file.getAbsolutePath(), Texture.class, null, params);
                if (texture != null) {
                    editor.setTexture(texture);
                }
            }
        }
    });
    defaultTextureButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            editor.setTexture(editor.assetManager.get(FlameMain.DEFAULT_BILLBOARD_PARTICLE, Texture.class));
        }
    });
    contentPanel.add(new JLabel("Min. Filter"), new GridBagConstraints(0, 0, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(6, 0, 0, 0), 0, 0));
    contentPanel.add(minFilterBox, new GridBagConstraints(1, 0, 1, 1, 1, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(6, 0, 0, 0), 0, 0));
    contentPanel.add(new JLabel("Mag. Filter"), new GridBagConstraints(0, 1, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(6, 0, 0, 0), 0, 0));
    contentPanel.add(magFilterBox, new GridBagConstraints(1, 1, 1, 1, 1, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(6, 0, 0, 0), 0, 0));
    contentPanel.add(genMipMaps, new GridBagConstraints(0, 2, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(6, 0, 0, 0), 0, 0));
    contentPanel.add(atlasButton, new GridBagConstraints(0, 3, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(6, 0, 0, 0), 0, 0));
    contentPanel.add(textureButton, new GridBagConstraints(1, 3, 1, 1, 1, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(6, 0, 0, 0), 0, 0));
    contentPanel.add(defaultTextureButton, new GridBagConstraints(2, 3, 1, 1, 1, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(6, 0, 0, 0), 0, 0));
}
Also used : GridBagConstraints(java.awt.GridBagConstraints) Insets(java.awt.Insets) JComboBox(javax.swing.JComboBox) ActionEvent(java.awt.event.ActionEvent) JButton(javax.swing.JButton) JLabel(javax.swing.JLabel) DefaultComboBoxModel(javax.swing.DefaultComboBoxModel) TextureFilter(com.badlogic.gdx.graphics.Texture.TextureFilter) Texture(com.badlogic.gdx.graphics.Texture) JCheckBox(javax.swing.JCheckBox) ActionListener(java.awt.event.ActionListener) TextureAtlas(com.badlogic.gdx.graphics.g2d.TextureAtlas) TextureParameter(com.badlogic.gdx.assets.loaders.TextureLoader.TextureParameter) File(java.io.File)

Example 30 with TextureAtlas

use of com.badlogic.gdx.graphics.g2d.TextureAtlas in project Entitas-Java by Rubentxu.

the class Mariano method create.

@Override
public GameEntity create(Engine engine, Context<GameEntity> context) {
    BasePhysicsManager physics = engine.getManager(BasePhysicsManager.class);
    BodyBuilder bodyBuilder = physics.getBodyBuilder();
    TextureAtlas textureAtlas = assetsManager.getTextureAtlas(atlas);
    Array<TextureAtlas.AtlasRegion> heroWalking = textureAtlas.findRegions("Andando");
    Array<TextureAtlas.AtlasRegion> heroJump = textureAtlas.findRegions("Saltando");
    Array<TextureAtlas.AtlasRegion> heroFall = textureAtlas.findRegions("Cayendo");
    Array<TextureAtlas.AtlasRegion> heroIdle = textureAtlas.findRegions("Parado");
    Map<String, Animation<TextureRegion>> animationStates = Collections.createMap(String.class, Animation.class);
    animationStates.put("walking", new Animation(0.02f, heroWalking, Animation.PlayMode.LOOP));
    animationStates.put("jump", new Animation(0.02f * 7, heroJump, Animation.PlayMode.NORMAL));
    animationStates.put("fall", new Animation(0.02f * 5, heroFall, Animation.PlayMode.NORMAL));
    animationStates.put("idle", new Animation(0.02f * 4, heroIdle, Animation.PlayMode.LOOP));
    Body bodyPlayer = bodyBuilder.fixture(bodyBuilder.fixtureDefBuilder().boxShape(0.35f, 1).density(1)).type(BodyDef.BodyType.DynamicBody).fixedRotation().position(0, 5).mass(1).build();
    GameEntity entity = ((GameContext) context).getPlayerEntity().addRigidBody(bodyPlayer).addAnimations(animationStates, animationStates.get("idle"), 0).addCharacter("Mariano", StateCharacter.IDLE, false).addMovable(7, 8).addTextureView(null, new Bounds(0.9f, 1.15f), false, false, 1, 1, Color.WHITE);
    return entity;
}
Also used : GameEntity(com.indignado.games.states.game.gen.GameEntity) TextureAtlas(com.badlogic.gdx.graphics.g2d.TextureAtlas) Bounds(com.ilargia.games.logicbrick.data.Bounds) Animation(com.badlogic.gdx.graphics.g2d.Animation) BasePhysicsManager(com.ilargia.games.entitas.egdx.base.managers.BasePhysicsManager) BodyBuilder(ilargia.egdx.util.BodyBuilder) Body(com.badlogic.gdx.physics.box2d.Body)

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