Jump to content

Spriter with LibGDX?


siba2893

Recommended Posts

  • 3 months later...

I just started using LibGdx (and Spriter).

Today 1.5.4. of libGdx was released.

 

I just wanted to quickly let anyone searching know, that Trixt0r's runtime still works.

(Just saying this, because I wasn't sure because the last github commit was 5 month ago, or something like that.)

 

The way I went for now: I added the com.brashmonkey.spriter sources directly to my libgdx project (this way there is no problem with gradle) and I also use three of the classes from test directory (LibGdxAtlasLoader, LibGdxDrawer and LibGdxLoader).

 

So far it is working fine. I have my first spriter animation on screen and it's running.

Link to comment
Share on other sites

Hi guys, we're making an android game and we had no problems running our tests using this library on our DesktopLauncher but it crashes when we try it from our .apk

All the resources are synchronized and I've made a lot of trials and errors until I'm certain the error comes from this line of code :
 

SCMLReader scml = new SCMLReader(handle.read());

The problem is not the handle because when I recover handle.exists() in Android it says "true", but just when I add this line of code it crashes.

Do you know what could be the cause?
Link to comment
Share on other sites

SCMLReader scml = new SCMLReader(handle.read());

The problem is not the handle because when I recover handle.exists() in Android it says "true", but just when I add this line of code it crashes.

Do you know what could be the cause?

 

 

"Crashing" is a bit too vague - I've used Trixt0r's lib for some personal pet projects to play with and can say reading scml's worked just fine for me (on physical devices). It would be near impossible to guess what the problem is without some more information. I assume you've debuged the apk with Eclipse/DDMS (or the equivalent in Android Studio), debug window should print out some kind of exception error or whatever which should be giving you an idea of the problem (?)

Link to comment
Share on other sites

I'd really like to have a good logcat to look at but i've tried debugging the android version before and it just never works, it throws a lot of errors without much sense while working fine on a real device (I'm talking about situations in which I'm not using this library).

 

I thought it could be some problem on my project so I tried to build a new one with gradle from scratch, copy / paste the relevant code from the SpriterTests\src\com\brashmonkey\spriter\tests\backend\LibGdxTest.java into my main class in the core project and when I run it from the DesktopLauncher it works fine, when I export the .apk and try to run it from my phone it just crashes.

 

And the problem still lies within the SCMLReader instantation, but I have no idea what could be causing the crashing.

 

PS : If somebody has a simple project test which runs fine on Android it would be super useful to have a look at the code to spot where lies the problem.

Link to comment
Share on other sites

Okay guys, I've discovered what was the problem, I tried to pack all the classes from the library into a one single .jar for convenience and strangely that worked in the Desktop but not in Android, so yeah... if you want to use it in Android don't try this and just copy / paste the .java classes into your project.

Link to comment
Share on other sites

  • 3 months later...

Hi

 

My Problem:

I Draw a coin and in sprinter, make it go up and down. When I use the Trixt0r's runtime it uses 34 MB of Ram and when I unload the coin the memory use doesnt change, but if a load it again the memory use increase 34 MB more.

 

 When I to load the coin as Texture with the assetManager and make the animation on the code, the memory utilization doesnt change.

 

What is the real problem. Well, I have two menus with two animated characters each menu. If I open the two menus and then go to the main scene, At this point I am using 186 MB of Ram, If I open 12 times the coins, that are drop items, my memory utilization goes to   300 to 594 MB of Ram vs if i use the Textures with assetManager the Ram Utilization continues on 186 to 190 MB of Ram. Whell I realy like Spriter and I am going to make more monsters and i dont what to animate all of then in the code.

 

I am making something wrong?

 

My code:
    private static Player player;
    private static Mydrawer drawer;
    FileHandle scmlHandle;
    SCMLReader reader;
    Data data;
    LibGdxLoader loader;

 

public MyActor(String image, Gamestage stage, float X, float Y, int type){

if(type==2){

            scmlHandle = Gdx.files.internal("data/spriter/"+image+"/"+image+".scml");
            reader = new SCMLReader(scmlHandle.read());
            data = reader.getData();
            
            loader = new LibGdxLoader(data);
            loader.load(scmlHandle.file());
            drawer = new Mydrawer(loader, stage.getBatch());
            player = new Player(data.getEntity(0));
            player.setPosition(X, Y);

}

}

 

on render

 

player.update();

drawer.draw(player);
          

Link to comment
Share on other sites

Hi Roberto,

 

I've only been working with this particular API for a few weeks now and have been heavily customizing it for my purposes, so please take this with a grain of salt though as I'm only speaking on my personal experience which may or may not be best practice.

 

The loading of the SCML "data" object in this implementation is where a lot of memory gets used and you have to be careful how often you're loading that data. The larger the Spriter files (more entities/animations per project), the more memory gets used up and it escalates pretty quickly I've found. I've actually done extensive changes to the original code base in this area which has significantly improved it for my purposes, but has resulted in several features being dropped from the original implementation (for now) and unfortunately a slightly "broken" feeling to the implementation as a whole. I am however considering creating a new Java API based upon the one the folks here at Spriter have been working on (once it is ready); but that is likely still a bit of time away yet.

 

One immediate thing I found is that if you're going to use multiple of the same SCML, instead of loading a new one for each entity, you should create a cache of the loaded "data" instances (actually the loaders that contain it). The "data" can be shared without causing any problems with multiple entities as long as you create new "Player" instances that use the data (all the location etc are stored in the player, not the data object). This should help reduce the overhead when using multiples, especially since you're looking at that many.

 

What I've done is create an ObjectMap of LibGdxLoader instances keyed on the file name. Then any time I load another, I simply look up in the map and if it was already loaded, I returned the same loader instance (which contains only the one set of sprites). Pass that loader into your "MyActor" and then create the MyDrawer/Player instances from that. Once you are entirely done with the SCML and don't intend to use them anymore, carefully dispose of it and any references to it. If you don't clear out all references, the Java GC cannot free the memory and will hold onto it forever. Funny enough, despite the GC, memory management in Java can still be a tricky thing to work with. 

 

So something quick and dirty like this:

 

    public final ObjectMap<String, LibGdxLoader > spriterLoaders = new ObjectMap<String, LibGdxLoader >();
 

Then, create your load like before, but only if not in the map above:

 

        if (!spriterLoaders.containsKey(scmlFile)) {
 

            scmlHandle = Gdx.files.internal("data/spriter/"+image+"/"+image+".scml");
            reader = new SCMLReader(scmlHandle.read());
            data = reader.getData();

            loader = new LibGdxLoader(data);

            loader.load(scmlHandle.file());

 

           spriterLoaders.put(scmlFile, loader);
      }
 
Hopefully that's enough to get the point across. As stated earlier though, take it with a grain of salt since I've only been working with it for a few weeks now.
 
Also, if you happen to have multiple entities in the same SCML file, you'd look up using the same file name and you'd end up with the same shared "data" (which actually represents the entire SCML file data). So having several in the same file can be helpful as long as you're using them all. If you have a bunch there though and you're not using them on a regular basis, I'd consider moving them into a separate file to reduce the memory stamp of that particular "data" object. (hope that makes sense)
 

I also found that using their LibGdxAtlasLoader helped reduce some memory as well but mileage might vary there depending on how many sprites/parts you're trying to load.

 

Good luck !

Link to comment
Share on other sites

Hi Roberto,

 

If I open 12 times the coins, that are drop items, my memory utilization goes to   300 to 594 MB of Ram 

          

 

Well this makes sense. Why do you then open it 12 times? Load the data once, and cache it anywhere.

 

These lines

scmlHandle = Gdx.files.internal("data/spriter/"+image+"/"+image+".scml");reader = new SCMLReader(scmlHandle.read());data = reader.getData();            loader = new LibGdxLoader(data);loader.load(scmlHandle.file());drawer = new Mydrawer(loader, stage.getBatch());

have to be called only once, when you are doing your whole asset loading. You are wasting a lot of memory if you are loading the SCML file again on each coin creation.

 

If I get back into the code, I will try to add some utility classes which should prevent you guys from doing such things.

 

- Trixt0r

Link to comment
Share on other sites

Thank you Smilne and Trixt0r. That was my mistake, I was loading the data to many times. I need to check the part of dispose the reference, for the monsters, I am going to have to many monsters types and I am not going to use them all at the same time. Just one or two by scene.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...