I have a problem with exporting Tiled map file.
I create map and , suggest a tutorial, I exporting tmx file and import it in Eclipse project assets.
In the code I did this:
tiledMap = new TmxMapLoader().load("map.tmx");
tiledMapRenderer = new OrthogonalTiledMapRenderer(tiledMap);
My problem is that when I execute it I get this :
Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: Couldn't load file: tileset 2/grass-tiles-2-small.png
at com.badlogic.gdx.graphics.Pixmap.<init>(Pixmap.java:140)
at com.badlogic.gdx.graphics.glutils.FileTextureData.prepare(FileTextureData.java:64)
at com.badlogic.gdx.graphics.Texture.load(Texture.java:130)
at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:121)
at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:100)
at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:96)
at com.badlogic.gdx.maps.tiled.TmxMapLoader.load(TmxMapLoader.java:116)
at com.badlogic.gdx.maps.tiled.TmxMapLoader.load(TmxMapLoader.java:101)
at com.mygdx.game.MyGdxGame.create(MyGdxGame.java:43)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:136)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)
Caused by: com.badlogic.gdx.utils.GdxRuntimeException: File not found: tileset 2/grass-tiles-2-small.png (Internal)
at com.badlogic.gdx.files.FileHandle.read(FileHandle.java:136)
at com.badlogic.gdx.files.FileHandle.readBytes(FileHandle.java:220)
at com.badlogic.gdx.graphics.Pixmap.<init>(Pixmap.java:137)
What do I for fix it?
As I see in libGdx wiki , there is two option to load tmx files. Make sure your files under the assets folder. You can see in libGdx Wiki how to load tmx files. Also there is similar question in here.
Tiled is referencing its tileset image source in the .tmx file. So the tileset grass-tiles-2-small.png you used is not embedded and has to be loaded, too. Fortunately this is done automatically by resolving the image dependencies for you.
So additionally to map.tmx you have to make sure that all used image files are reachable by copying them to your assets folder. It has to look like this: assets/tileset 2/grass-tiles-2-small.png. Refresh your Eclipse project after doing so, just in case it didn't notice.
If you already did that, another source of the problem could be the whitespace in your directory name. Try to rename tileset 2 to tileset_2 and create a new .tmx file.
It simply failed to load you texture grass-tiles-2-small.png. I usually open up the TMX/XML file and set the filenames for the tilesheets to just the filenames manually without a path.
Example:
<tileset firstgid="1" name="grass" tilewidth="32" tileheight="32">
<image source="grass-tiles-2-small.png" width="256" height="256"/>
</tileset>
You should put the textures in the same folder within your project as the map.tmx resides preferably Android/assets/../.. and use the Gdx.files.internal to load your map. internal just returns a regular file handle.
tiledMap = new TmxMapLoader().load(Gdx.files.internal("yourmap.tmx"));
The TMX just looks in the same folder for the right textures to use since we didn't specify a path in the tmx/xml.
Thoughts:
I really hate the LibGDX default TmxLoader. It would work for really simple things but you have no way of sorting depth when sprites overlap since the only way to draw is to draw a complete layer at once. If you need a overlap on a player you need to be able to draw tile by tile/object by object. I wrote my own XML parser for Tiled maps and it surprised me how simple it was. I have a pretty complicated map and it just took me about 30 lines of code to load everything into an array ready to draw it within LibGDX.
Related
I am attempting to create a game in libgdx using the tool overlap2d, and everything works fine except one key thing: every time I add text to the scene, I get an error stating that it can't find the corresponding font file. I have the file, I've made sure it's spelled right, and I've tried running this with it in my assets folder and just about every other folder in my project, but it just won't work. What do I need to do or where do I need to put this for libgdx to be able to find my font?
Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: File not found: freetypefonts\Calibri.ttf (Internal)
at com.badlogic.gdx.files.FileHandle.read(FileHandle.java:136)
at com.badlogic.gdx.files.FileHandle.length(FileHandle.java:602)
at com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator.<init>(FreeTypeFontGenerator.java:90)
at com.uwsoft.editor.renderer.resources.ResourceManager.loadFont(ResourceManager.java:319)
at com.uwsoft.editor.renderer.resources.ResourceManager.loadFonts(ResourceManager.java:312)
at com.uwsoft.editor.renderer.resources.ResourceManager.loadAssets(ResourceManager.java:205)
at com.uwsoft.editor.renderer.resources.ResourceManager.initAllResources(ResourceManager.java:100)
at com.uwsoft.editor.renderer.SceneLoader.<init>(SceneLoader.java:63)
at com.mygdx.game.MyGdxGame.create(MyGdxGame.java:21)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:146)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:123)
Try opening the project in editor again, and pressing export again, the ttf file probably did not get exported first time around.
I am attempting to load an image called Default.png stored within the project and draw it onto a canvas. I am well aware of ImageIO.read however no matter what path I give it, I can't seem to load it. Where should I put the image? I have tried putting it in a separate folder calles "res," putting it into assets.author.mypackagename.textures, but no matter what I do I cannot seem to find the right location and how to access it. Any help is appreciated, comment for further specifics.
Actually the resources are loaded in the classpath relative to the current package. If /com/daniel/project/src/ is in your classpath, and images are in /com/daniel/project/src/image then use:
ImageIO.read( ClassLoader.getSystemResource( "image/Default.png" ) );
But the src folder is not included in the classpath by IDEs generally. Try adding the image to the bin folder.
If You have it in a separate folder called res you can load the image by doing this:
ImageIO.read(this.getClass().getResource("/Default.png"));
you can also do something like:
ImageIO.read(new File("res/Default.png"));
The second method doesn't need the picture to be in another folder, but for me it's cleaner that way.
I'm new to development and would like to clarify a couple of simple points,
When I create a new drawable xml file I place it in the "drawable-hdpi" folder and only that folder, I can access it fine like so....
android:src="#drawable/button_border"
but is that the correct correct way? or should they be in their own folder in the res folder, or just the file placed in the res folder and not in any particular sub-folder?
also if I wanted to create a arrays.xml file what folder should that be created in.
Thank you.
Your arrays should go to the values folder, so res/values/arrays.xml, as per the documentation.
As for the images, it is a good practice to provide relevant images in each drawable folder according to the screen sizes you plan to support for your app, or some of your users will end up seeing badly sized images, more details here. You can use the Android Asset Studio to generate icons, for example.
The different xml file like attr.xml, colors.xml, arrays.xml, dimens.xml, font.xml, strings.xml, styles.xml should be kept in the res/values folder.
The others custom xml file should be keep in the drawable folder.
I am developing a little game where I use some pictures for the sprites etc etc.
It works just fine when I load it from the disc like this
Image image = new Image("C:\\AppleMan.png");
but how can I load it from a foloder within the project. I am using eclipse as IDE and the language is Java :)
I took a screenshot of a sample project so you can see how I have importet the picture
So I want to load the picture from that resource folder like this pseudo code
Image image = getResource("Resources/AppleMan.png");
but that simply doesn't work.
Any help appreciated :)
1) You should add Resources folder to classpath
2) You should locate file absolutely, i.e. "/Resources/AppleMan.png"
P.S.
3) Sorry, also note that getResource returns URL: http://docs.oracle.com/javase/6/docs/api/java/lang/Class.html#getResource%28java.lang.String
You're passing a relative path when you were passing a absolute path before. Add the path of the Eclipse workspace.
For example, if your workspace is at C:\Workspace, you need to put
Image image = getResource("C:\\Workspace\\HowToGetResources\\Resources\\AppleMan.png");
I know, I know, this has been asked before. But every resource I've looked at uses IconImages while I just have plain Images.
Is there a different solution? Please help as I've been stuck researching and trying to figure this out for days now with no progress.
Image Floor = Toolkit.getDefaultToolkit().getImage("Floor.PNG");
EDIT: If I was to make sure the jar wouldn't compress and I created a seperate directory in the jar for images and put the correct file path, would this code work?
Toolkit#getImage(String s) looks for a file and likely your image is in the Jar and is now a resource not a file. Look to using resources for this.
Note that ImageIO.read(...) can accept an InputStream parameter the Class class has a method, getResourceAsStream(...) which can put the resource into a Stream that ImageIO can read. Give that a try.
Also, are you getting any error messages when you try what you're doing?
Make sure you know what your current directory is, and how it relates to the position of the files in your jar.
Here's how I would handle it.
1) Require there to be a file called "images.txt" in the directory with your jar (or bundle it into the jar.)
2) Make a file called "images.txt" with a format like `FLOOR:C:\\images\\floor.png`
3) Load this file into memory on load.
4) Load your images based on the entries in the file
This will give you the advantage of changing your images without changing your code if it's defined outside the jar :)
It's not loading because you're not putting the path to the images in the declaration. It expects the images to be wherever the jar is (notice there's no directories there)
You need to offload the definition of the file names to a file, or at the very least guarantee the relative position of the files.
Another good option is to put the images in the jar itself, say in an img directory, and reference them there. But then changes to the images require a new jar, which may not be desired for development purposes.
The getImage call is looking in the file system working directory, not inside the Jar file. This is why the jar file loads the images successfully when they are placed in the same directory outside the jar file. If the images are bundled in the jar file, they are no longer file system files to be accessed, but rather Jar file resources. There is a different way to load these, but sorry, I don't know it off the top of my head.
Check the extension of files. I had this problem because the extension was "PNG", when I changed it to "png", everything was ok.
You can't expect a JAR file to magically know where your images are. If you put a JAR file alone on the desktop, it's going to look for the files on the desktop! The code
getImage("Floor.png")
searches the current directory of the JAR (or source project) by default and you'd expect that if the JAR was in the same directory, it would work. If the JAR is on the desktop how does it know where Floor.png is? Of course, you can specify a hard-coded path
getImage("C:\Some Folder Path\Floor.png")
but then Floor.png has to be in C:\Some Folder Path\ for the JAR to work properly.
It sounds like what you really want to do is keep the images in the JAR file (which acts like a ZIP file). The tutorial on doing that is here:
http://download.oracle.com/javase/tutorial/uiswing/components/icon.html#getresource
And I know for ImageIcon you use: new javax.swing.ImageIcon(getClass().getResource("myimage.jpeg") but I have not found anything similar for plain Image.
<head-desk /> You should really get into reading the JavaDocs. Otherwise you are 'coding by magic'. Which generally won't work.
URL urlToImage = getClass().getResource("myimage.jpeg");
// If you need to support Java 1.3
Image image = Toolkit.getDefaultToolKit().getImage(urlToImage);
// If your users have dragged their JRE into this millennium
BufferedImage bufferedImage = ImageIO.read(urlToImage);