Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (121)
games submitted by our members
Games in WIP (577)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Better way to Manage Assets... Need a tip :)  (Read 476 times)
0 Members and 1 Guest are viewing this topic.
Offline Andre Lopes
« Posted 2013-12-16 02:51:21 »

Hi Guys.. Im trying to optimize my game, and well, its going well Smiley
Im usint TexturePacker2 to optimze the images in game, i dont know well how should i manage them or pack. But i was able to pack the images and gen the atlas.

Anyway,
Heres the Class that i built following tutorials :

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
package br.assets;

import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.utils.Disposable;


public class AssetManagement implements Disposable {

    private static final AssetManagement assetManagement = new AssetManagement();

    public static AssetManagement getAssetManager() {
        return assetManagement;
    }
   
    private AssetManager assetManager;

   
    public AssetManagement() {}

   
    public void init(AssetManager assetManager) {
        this.assetManager = assetManager;
        //load texture atlas
        assetManager.load("", TextureAtlas.class);
        //start loading assets and wait until finished
        assetManager.finishLoading();
    }

    @Override
    public void dispose() {
        assetManager.dispose();

    }

}



The tutorial suggests that i do this to load :

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
@Override
public void create () {

Assets.instance.init(new AssetManager());


@Override
public void resume () {
Assets.instance.init(new AssetManager());
}


Now i ask, if im creating a new AssetManager, and replacing the old one, the old AssetManager wont dispose what he already had...  Wouldnt that cause memory leak?

I mean , in resume he creates a new AssetManager();

I was thinking in making in this way :

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
package br.assets;

import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.utils.Disposable;

public class AssetManagement implements Disposable {

    private static final AssetManagement assetManagement = new AssetManagement();
    private final AssetManager assetManager;

    public AssetManagement() {
        assetManager = new AssetManager();
    }

    public static AssetManagement getInstance() {
        return assetManagement;
    }

    public void init() {

        //load texture atlas
        assetManager.load("packed/archerresources.atlas", TextureAtlas.class);
        //start loading assets and wait until finished
        assetManager.finishLoading();
    }

    @Override
    public void dispose() {
        assetManager.dispose();

    }

}



When i pause/resume, i could do this :

1  
2  
3  
4  
5  
6  
7  
public void resume() {
            AssetManagement.getInstance().init();
        }

        public void pause() {
            AssetManagement.getInstance().dispose();
        }



Am i thinking wrong or im correct?

Its 1 AM here so , im sorry if im lacking something lol
Offline UprightPath
« Reply #1 - Posted 2013-12-16 12:48:13 »

Creating a new AssetManager with out calling its dispose method will in eventual memory issues. Many of the objects that it loads are not lost during the pause/resume of a mobile application. Creating a new one will cause it to reload everything that is not lost during the context switch as well (I believe that this includes any sounds, music and non-image files that were loaded up, though some of these may require a bit of processing. I may be wrong about everything that is saved. I might be wrong about this, though!)

 The AssetManager class already provides some support for the pause/resume. The AssetManager can be set to reload the assets that are required to load when a resume is called by doing: Texture.setAssetManager(AssetManager manager).

Then in your screen/application listener class, you can have the resume method either wait for the AssetManager to reload or you can cause it to switch to a loading screen until it's complete (Look at the very end of: https://github.com/libgdx/libgdx/wiki/Managing-your-assets )

Offline Andre Lopes
« Reply #2 - Posted 2013-12-16 16:06:07 »

Creating a new AssetManager with out calling its dispose method will in eventual memory issues. Many of the objects that it loads are not lost during the pause/resume of a mobile application. Creating a new one will cause it to reload everything that is not lost during the context switch as well (I believe that this includes any sounds, music and non-image files that were loaded up, though some of these may require a bit of processing. I may be wrong about everything that is saved. I might be wrong about this, though!)

 The AssetManager class already provides some support for the pause/resume. The AssetManager can be set to reload the assets that are required to load when a resume is called by doing: Texture.setAssetManager(AssetManager manager).

Then in your screen/application listener class, you can have the resume method either wait for the AssetManager to reload or you can cause it to switch to a loading screen until it's complete (Look at the very end of: https://github.com/libgdx/libgdx/wiki/Managing-your-assets )

I understand...
But i want to use AssetManager with My TexturePack2... I already packed all my images , i just need to know whats the best way to load them and dispose...
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline UprightPath
« Reply #3 - Posted 2013-12-16 17:09:46 »

Something simple like this. But, a lot depends on how you're controlling things: whether you're just implementing ApplicationListener or making use of Game and Screens.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
public class MainClassName implements ApplicationListener {
private final AssetManager assetManager;


public void create() {
assetManager = new AssetManager();
Texture.setAssetManager(assetManager);
// ... Load operations here.
}

public void resume() {
if (assetManager.update()) {
doneLoading();
}
}

public void render(float delta) {
if (assetManager.update()) {
doneLoading();
} else {
// ... Draw Loading thing!
}
}

public void dispose() {
assetManager.dispose();
}

public void doneLoading() {
// ... Get your texture assets here.
// ... Change to your main game screen.
}

}

Offline Andre Lopes
« Reply #4 - Posted 2013-12-16 22:38:36 »

I see. I just want to create a class that i can call from any class to get an asset that i need. Like an image or sound.

I generated a .atlas with texturepacker 2, so now i want a class to manage all that.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

theagentd (20 views)
2014-10-25 15:46:29

Longarmx (53 views)
2014-10-17 03:59:02

Norakomi (47 views)
2014-10-16 15:22:06

Norakomi (35 views)
2014-10-16 15:20:20

lcass (39 views)
2014-10-15 16:18:58

TehJavaDev (68 views)
2014-10-14 00:39:48

TehJavaDev (69 views)
2014-10-14 00:35:47

TehJavaDev (61 views)
2014-10-14 00:32:37

BurntPizza (74 views)
2014-10-11 23:24:42

BurntPizza (46 views)
2014-10-11 23:10:45
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!