[Java Tiled API] Read a .tmx included in a JAR

58 views
Skip to first unread message

Kévin Poupon

unread,
Aug 19, 2014, 7:29:22 PM8/19/14
to mape...@googlegroups.com
Hello,

First, thank you for your great work on this nice software.

I am facing a problem with my game exported as a runnable JAR: I can't find how to read .tmx maps from inside the JAR !
In Eclipse, running the program works well, I can read and render the maps.
When running the JAR, I have trouble accessing the JAR resources and render the maps.

Here is how I do to access a resource (a .tmx map) from the JAR itself:

InputStream is = Content.class.getResourceAsStream("/maps/map1/mymap.tmx");
           
mapReader = new TMXMapReader();
map = mapReader.readMap(is);


From my researches, it seems to be a valid method to reach a resource. I am wondering if the MapReader.readMap(InputStream) works as intended or not. Could you confirm ?
I'm confused because I don't see any other way of reading my map.

Thanks for helping!

Thorbjørn Lindeijer

unread,
Aug 20, 2014, 4:43:25 AM8/20/14
to mape...@googlegroups.com
Hey Kévin,

Unfortunately I have currently no time to check why this method may or
may not work. Maybe the problem is that while it might be able to read
the map file, it won't be able to load the tilesets since it would still
try to access them as regular files?

I'm sorry that libtiled-java is not up to date and I think it's quite a
mess code-wise. I have not gotten around to maintaining it in the past
years.

Glad to hear you enjoy Tiled!

Regards,
Bjørn

Kévin Poupon

unread,
Aug 20, 2014, 9:19:20 PM8/20/14
to mape...@googlegroups.com
Indeed you are right: the .tmx is red but not the associated tileset.

I made a quick but very dirty temporary patch by modifying the tiled.core.TileSet.importTileBitmap(String imgFilename, TileCutter cutter) function.

I changed the way it reads the tileset from File to InputStream but there is still a problem, the function always tries to read the tileset at this location: "./tileset.png".
It could work if my tileset was indeed at the root of the JAR but it isn't the case. So I forced the location to be "/path/to/my/tileset.png". So dirty :(

At least it works, if I find a clean way to read a tileset from a JAR I will update this thread.

Thanks again Bjørn.



 

Reply all
Reply to author
Forward
0 new messages