Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Managing assets between desktop and device version in libgdx

I'm building a little Android game using libgdx. For now I have a copy of the game's assets in the desktop project folder and the Android project folder. For some strange reason I have to access those files differently in each of the two versions.

This works fine in the desktop app but gives me a FileNotFound exception in the android app:

Texture texture = new Texture(Gdx.files.internal("assets/someImage.png"));

If I remove the "assets" from the filename it's the other way round (Android works, desktop crashes):

Texture texture = new Texture(Gdx.files.internal("someImage.png"));

I'm not sure what the problem is. The folder structure is exactly the same for both projects... What is the right way to this with libgdx?

like image 796
Boris Avatar asked Mar 27 '12 15:03

Boris


4 Answers

You should be storing all of your assets in the Android assets folder and linking your desktop project to that folder. There is a quick description of this at http://www.badlogicgames.com/wordpress/?p=1537

EDIT: The official Project Setup tutorial describes how to perform this as well. It's found at http://code.google.com/p/libgdx/wiki/ProjectSetup#Asset_folder_setup

Aside from project setup I believe that your second method is the correct way of referencing the assets from both projects. After you fix your setup it should work properly in both environments.

like image 68
Deano Avatar answered Nov 18 '22 20:11

Deano


Assets folder of desktop project can be also chosen with Gradle - add this line to build.gradle in desktop folder:

sourceSets.main.resources.srcDirs = [ "../android/assets/" ]

Or, if you didn't generate an Android project:

sourceSets.main.resources.srcDirs = [ "../core/assets/" ]

This should properly link the folder in both Eclipse and IntelliJ when running eclipse/idea Gradle tasks. Compared to creating links or manually adding the folder to source, I think this is the easiest to set up and manage by far.

like image 42
Czyzby Avatar answered Nov 18 '22 21:11

Czyzby


After a lot of experimentation I discovered a way to get this to work:

For the android module, Gdx.files.internal is rooted in the android assets folder. For the desktop module, Gdx.files.internal is rooted in the toplevel project folder.

So if you put a symbolic link from files or directories in your android assets folder to just underneath your top level project folder (ie, parallel to android, core, desktop, etc. dirs) then the Gdx.files.internal will work for both.

% cd project
% ln -s android/assets/sound sound
% ln -s android/assets/images images

Bonus tip - if using windows, you can use the mklink command at the cmd shell to create these:

mklink /d sound d:\project\android\assets\sound
mklink /d images d:\project\android\assets\images

(note windows cant handle relative paths in symlinks. also you have to run cmd as administrator)

make sure to sync your files if you are doing this while running, too.

like image 3
Mad Ned Avatar answered Nov 18 '22 21:11

Mad Ned


libGDX Wiki extract:

Asset folder setup
The Android project has a sub-folder named assets, which was created automatically. Files available to the Android application must be placed here. This is problematic, because these same files must be available to the desktop application. Rather than maintain two copies of all the files, the desktop project can be configured to find the assets in the Android project:

Click the Source tab, click Link Source, Browse, select the "assets" folder from your Android project and click OK.
Specify "assets" for the folder name and click Finish then OK.

Note: If your desktop and Android projects are in the same parent folder, you can use "PARENT-1-PROJECT_LOC/gamename-android/assets" for the location of the linked assets folder, where "gamename-android" is the name of your Android project. This is better than a hard-coded path if you plan on sharing your projects with others.

like image 1
MonoThreaded Avatar answered Nov 18 '22 19:11

MonoThreaded