Skip to content

Commit

Permalink
FIX: Parsing imagelayer inside a group tag in TMX file (libgdx#7135)
Browse files Browse the repository at this point in the history
* added failed test for parsing imagelayer inside a group tag.

* fixed the test for parsing imagelayer inside a group tag.

* removed unused group tags from `test.tmx`

* after `./gradlew spotlessApply`

* updated TiledMapGroupLayerWithImagelayerTest

* Rename .tsx.tsx. to .tsx

---------

Co-authored-by: SimonIT <[email protected]>
  • Loading branch information
TMalygin and SimonIT authored Oct 30, 2024
1 parent 6947833 commit 98d64e2
Show file tree
Hide file tree
Showing 13 changed files with 164 additions and 2 deletions.
4 changes: 2 additions & 2 deletions gdx/src/com/badlogic/gdx/maps/tiled/TmxMapLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ protected Array<FileHandle> getDependencyFileHandles (FileHandle tmxFile) {
Array<FileHandle> fileHandles = new Array<FileHandle>();

// TileSet descriptors
for (Element tileset : root.getChildrenByName("tileset")) {
for (Element tileset : root.getChildrenByNameRecursively("tileset")) {
String source = tileset.getAttribute("source", null);
if (source != null) {
FileHandle tsxFile = getRelativeFileHandle(tmxFile, source);
Expand Down Expand Up @@ -144,7 +144,7 @@ protected Array<FileHandle> getDependencyFileHandles (FileHandle tmxFile) {
}

// ImageLayer descriptors
for (Element imageLayer : root.getChildrenByName("imagelayer")) {
for (Element imageLayer : root.getChildrenByNameRecursively("imagelayer")) {
Element image = imageLayer.getChildByName("image");
String source = image.getAttribute("source", null);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<tileset version="1.10" tiledversion="1.10.1" name="Lonesome_Forest_Summer_Tileset.tsx" tilewidth="32" tileheight="32" tilecount="1" columns="1">
<image source="tileset/badlogicsmall.jpg" width="32" height="32"/>
<wangsets>
<wangset name="Unnamed Set" type="mixed" tile="-1">
<wangcolor name="" color="#ff0000" tile="-1" probability="1"/>
<wangcolor name="" color="#00ff00" tile="-1" probability="1"/>
<wangtile tileid="0" wangid="1,1,1,1,1,1,1,1"/>
</wangset>
</wangsets>
</tileset>
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.10" tiledversion="1.10.1" orientation="orthogonal" renderorder="right-down" width="10" height="10" tilewidth="32" tileheight="32" infinite="0" nextlayerid="22" nextobjectid="1">
<tileset firstgid="1" source="Lonesome_Forest_Summer_Tileset.tsx"/>
<layer id="1" name="Tile Layer 1" width="10" height="10">
<data encoding="csv">
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
1,0,0,0,0,0,0,0,0,1,
0,0,0,0,0,0,0,0,0,0,
1,0,0,0,0,0,0,0,0,1
</data>
</layer>
<group id="4" name="Group 2">
<imagelayer id="5" name="Image Layer 2" offsetx="111.654" offsety="-0.8052">
<image source="tileset/test_image2.png" width="112" height="112"/>
</imagelayer>
</group>
<group id="7" name="Group 4">
<group id="8" name="Group 5">
<group id="9" name="Group 6">
<group id="6" name="Group 3"/>
<group id="11" name="Group 7">
<group id="17" name="Group 10">
<group id="19" name="Group 12">
<group id="18" name="Group 11">
<group id="14" name="Group 9">
<imagelayer id="16" name="Image Layer 6" offsetx="230.019" offsety="117.559">
<image source="tileset/test_image6.png" width="112" height="112"/>
</imagelayer>
</group>
</group>
</group>
<group id="20" name="Group 13"/>
<imagelayer id="21" name="Image Layer 7" offsetx="103.871" offsety="230.556">
<image source="tileset/test_image7.png" width="112" height="112"/>
</imagelayer>
</group>
<group id="13" name="Group 8">
<imagelayer id="15" name="Image Layer 5" offsetx="114.607" offsety="118.633">
<image source="tileset/test_image5.png" width="112" height="112"/>
</imagelayer>
</group>
</group>
<imagelayer id="12" name="Image Layer 4" offsetx="0" offsety="118.096">
<image source="tileset/test_image4.png" width="112" height="112"/>
</imagelayer>
</group>
<imagelayer id="10" name="Image Layer 3" offsetx="223.846" offsety="-0.5368">
<image source="tileset/test_image3.png" width="112" height="112"/>
</imagelayer>
</group>
</group>
<group id="2" name="Group 1">
<imagelayer id="3" name="Image Layer 1">
<image source="tileset/test_image.png" width="112" height="112"/>
</imagelayer>
</group>
</map>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@

package com.badlogic.gdx.tests;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.assets.AssetDescriptor;
import com.badlogic.gdx.assets.AssetErrorListener;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.assets.loaders.resolvers.InternalFileHandleResolver;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.maps.tiled.AtlasTmxMapLoader;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TiledMapRenderer;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
import com.badlogic.gdx.tests.utils.GdxTest;
import com.badlogic.gdx.tests.utils.OrthoCamController;
import com.badlogic.gdx.utils.ScreenUtils;

public class TiledMapGroupLayerWithImagelayerTest extends GdxTest {

private TiledMap map;
private TiledMapRenderer renderer;
private OrthographicCamera camera;
private OrthoCamController cameraController;
private AssetManager assetManager;
private BitmapFont font;
private SpriteBatch batch;
String errorMessage;
private String fileName = "data/maps/tiled-groups-imagelayer/test.tmx";

@Override
public void create () {
float w = Gdx.graphics.getWidth();
float h = Gdx.graphics.getHeight();

camera = new OrthographicCamera();
camera.setToOrtho(false, (w / h) * 10, 10);
camera.zoom = 2;
camera.update();

cameraController = new OrthoCamController(camera);
Gdx.input.setInputProcessor(cameraController);

font = new BitmapFont();
batch = new SpriteBatch();

AtlasTmxMapLoader.AtlasTiledMapLoaderParameters params = new AtlasTmxMapLoader.AtlasTiledMapLoaderParameters();
params.forceTextureFilters = true;
params.textureMinFilter = Texture.TextureFilter.Linear;
params.textureMagFilter = Texture.TextureFilter.Linear;

assetManager = new AssetManager();
assetManager.setErrorListener(new AssetErrorListener() {
@Override
public void error (AssetDescriptor asset, Throwable throwable) {
errorMessage = throwable.getMessage();
}
});

assetManager.setLoader(TiledMap.class, new TmxMapLoader(new InternalFileHandleResolver()));
assetManager.load(fileName, TiledMap.class);
}

@Override
public void render () {
ScreenUtils.clear(100f / 255f, 100f / 255f, 250f / 255f, 1f);
camera.update();
assetManager.update(16);
if (renderer == null && assetManager.isLoaded(fileName)) {
map = assetManager.get(fileName);
renderer = new OrthogonalTiledMapRenderer(map, 1f / 32f);
} else if (renderer != null) {
renderer.setView(camera);
renderer.render();
}
batch.begin();
if (errorMessage != null) {
font.draw(batch, "ERROR (OK if running in GWT): " + errorMessage, 10, 50);
System.out.println(errorMessage);
}
font.draw(batch, "FPS: " + Gdx.graphics.getFramesPerSecond(), 10, 20);
batch.end();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ public class GdxTests {
TiledMapAnimationLoadingTest.class,
TiledMapAssetManagerTest.class,
TiledMapGroupLayerTest.class,
TiledMapGroupLayerWithImagelayerTest.class,
TiledMapAtlasAssetManagerTest.class,
TiledMapDirectLoaderTest.class,
TiledMapImageLayerRepeatTest.class,
Expand Down

0 comments on commit 98d64e2

Please sign in to comment.