Question

I am refactorying my game i would like to use the AltasTmxMapLoader to increase the renderingperformance of my TiledMaps. I stuck at adding the atlas property into the map. Where do i need to put it in this map for example?

<?xml version="1.0" encoding="UTF-8"?>
<map version="1.0" orientation="orthogonal" width="10" height="10" tilewidth="32" tileheight="32">
 <tileset firstgid="1" name="tile2" tilewidth="32" tileheight="32">
  <image source="tile2.png" width="512" height="512"/>
 </tileset>
 <tileset firstgid="257" name="mountain" tilewidth="32" tileheight="32">
  <image source="mountain.png" width="512" height="512"/>
 </tileset>
 <tileset firstgid="513" name="pubdlcnt" tilewidth="32" tileheight="32">
  <image source="pubdlcnt.png" width="512" height="512"/>
 </tileset>
 <tileset firstgid="769" name="snowWit" tilewidth="32" tileheight="32">
  <image source="snowWit.png" width="512" height="512"/>
 </tileset>
 <tileset firstgid="1025" name="tile2" tilewidth="32" tileheight="32">
  <image source="tile2.png" width="512" height="512"/>
 </tileset>
 <tileset firstgid="1281" name="tree+rock" tilewidth="32" tileheight="32">
  <image source="tree+rock.png" width="512" height="512"/>
  <tile id="129">
   <properties>
    <property name="blocked" value=""/>
   </properties>
  </tile>
  <tile id="161">
   <properties>
    <property name="move" value=""/>
   </properties>
  </tile>
 </tileset>
 <tileset firstgid="1537" name="trees" tilewidth="32" tileheight="32">
  <image source="trees.png" width="512" height="512"/>
 </tileset>
 <tileset firstgid="1793" name="trees2" tilewidth="32" tileheight="32">
  <image source="trees2.png" width="512" height="512"/>
 </tileset>
 <layer name="background1" width="10" height="10">
  <data encoding="base64" compression="zlib">
   eJzzZ2Bg8B/FgwYDAFyQHt0=
  </data>
 </layer>
 <layer name="background" width="10" height="10">
  <data encoding="base64" compression="zlib">
   eJxjYGBgmISEGfDQi5AwAx76KBSPAtxgIysEowMAMnYKLw==
  </data>
 </layer>
 <layer name="background" width="10" height="10">
  <data encoding="base64" compression="zlib">
   eJybxMDAcBqIJxGgFwHxbSJoUsA1IL6OR/4XlD4KxSMBAAA87BJ0
  </data>
 </layer>
 <layer name="foreground1" width="10" height="10">
  <data encoding="base64" compression="zlib">
   eJxjYCAOzCdCzTwofQyIj+NR94pIO0m1l5pgoOwFACVaBi8=
  </data>
 </layer>
 <layer name="blocked" width="10" height="10">
  <data encoding="base64" compression="zlib">
   eJxrYmVgaKIiJgUQ0kOqeUMJLGKFYHQAAKpXDXA=
  </data>
 </layer>
</map>

I already tried it by adding it to the map tag and also to the tileset tags but this isnt the right solution for it. I already noticed, that i need to put the atlas into the same folder as the tmx files but i really wonder where to put the tag.


Exception in thread "main" java.lang.NoClassDefFoundError: com/badlogic/gdx/util
s/GdxRuntimeException
        at java.lang.Class.getDeclaredMethods0(Native Method)
        at java.lang.Class.privateGetDeclaredMethods(Class.java:2531)
        at java.lang.Class.getMethod0(Class.java:2774)
        at java.lang.Class.getMethod(Class.java:1663)
        at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:494)
        at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:486)

Caused by: java.lang.ClassNotFoundException: com.badlogic.gdx.utils.GdxRuntimeEx
ception
        at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
        ... 6 more
Was it helpful?

Solution

It is not as easy as just putting the atlas property to the source.

That property would not be used, because the tiles described in the xml don't reference the atlas at all.

What you need is to let the TiledMapPacker run over your files. It will preprocess your map, create an optimized atlas file and add the property to the xml. Only then you can use the AltasTmxMapLoader to load that file correctly.

See my posts here for an explanation of how I got that to work.

The tools in the official distributions of libgdx should include all necessary things. I got it to run with this command:

java -classpath "gdx.jar";"gdx-natives.jar";"gdx-backend-lwjgl.jar";"gdx-backend-lwjgl-natives.jar";"gdx-tools.jar";"gdx-tiled-preprocessor.jar" com.badlogic.gdx.tiledmappacker.TiledMapPacker "processed/input" "processed/output" "--strip-unused"

To run it from code put this inside of the core-main Class/project:

    Settings settings = new Settings();
    settings.maxWidth = 2048; //modify if needed
    settings.maxHeight = 2048; //modify if needed
    settings.fast = true; //fast should be fine here!
    //all tiles have a 1px padding. better for not getting artifacts
    TiledMapPacker pack = new TiledMapPacker();
    try
    {
        pack.processMaps(
                new File(
                        "PATH-TO-INTPUT"),
                new File(
                        "PATH-TO-OUTPUT"),
                settings);
    }
    catch (IOException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

OTHER TIPS

I recently went through this issue with libGDX 1.5.5 and attempted to use the TiledMapPacker class.

However, according to the official forums, as February 2015 this is no longer supported.

With libGDX 1.5.5, I went through the following steps to use an atlas/packed tile map in my project:

Either create a new project or make sure that the Tools extension has been added to your project (I opted for a new project). I also was developing for Android and Desktop.

In the Android assets directory I created a raw and packed directory, and put the sprite PNGs and Tiled .tmx file.

In the .tmx file, add a Map level property called atlas and set it to a file name that makes sense for the project. You'll use this later.

If you open the .tmx file in Android Studio (or your editor), you'll find something like the following at the top of the file:

<map version="1.0" orientation="isometric" renderorder="left-up" width="5" height="5" tilewidth="128" tileheight="64" nextobjectid="1">
 <properties>
  <property name="atlas" value="tiledexampletiles.atlas"/>
 </properties>
 <tileset firstgid="1" name="City - Roads" tilewidth="133" tileheight="123">

In the Desktop project you'll want the following import in the DesktopLauncher.java file:

import com.badlogic.gdx.tools.texturepacker.TexturePacker;

Now use TexturePacker.process() to actually generate your atlas.

public class DesktopLauncher {
    public static void main (String[] arg) {
        TexturePacker.Settings settings = new TexturePacker.Settings();
        // If your images are numbered, but not for animation, you'll probably need this.
        settings.useIndexes = false;

        TexturePacker.process(settings, "raw", "packed", "tiledexampletiles.atlas");

        LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();

        new LwjglApplication(new LibGDXToolsApp(), config);
    }
}

Run the Desktop application and it should generate the necessary atlas file and png in the Android assets directory.

If you need to configure an application, the steps to configure an application in Android Studio 1.1.x are:

  1. Select Run > Edit Configurations
  2. Click the + in the upper left corner and select Application.
  3. Populate the Name (I usually use Desktop), point the Main class to your DesktopLauncher (for example com.jamesrskemp.libgdxToolsApp.desktop.DesktopLauncher), set your working directory to your android assets directory (C:\path\to\LibGDXToolsApp\android\assets), and finally your 'Use classpath of mod' to desktop.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top