Monday, March 17, 2014

Game Classes Setup - ResourcesManager

* Class resources manager

Now we need to create new class that will manager or handle our game resources like graphics, audio, fonts etc. We gonna create references for commonly used objects like camera, context, engine, VertexBufferObjectManager etc.
follow the steps below,

Step 1: Create new class and name it as ResourcesManager

package com.mindew.fruitecollector;

import org.andengine.engine.Engine;
import org.andengine.engine.camera.Camera;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.atlas.bitmap.BuildableBitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.source.IBitmapTextureAtlasSource;
import org.andengine.opengl.texture.atlas.buildable.builder.BlackPawnTextureAtlasBuilder;
import org.andengine.opengl.texture.atlas.buildable.builder.ITextureAtlasBuilder.TextureAtlasBuilderException;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;
import org.andengine.util.debug.Debug;

import com.mindew.fruitecollector.GameActivity;

/**
 * @author Mateusz Mysliwiec
 * @author www.matim-dev.com
 * @version 1.0
 */
public class ResourcesManager
{
    //---------------------------------------------
    // VARIABLES
    //---------------------------------------------
 
    private static final ResourcesManager INSTANCE = new ResourcesManager();
 
    public Engine engine;
    public GameActivity activity;
    public Camera camera;
    public VertexBufferObjectManager vbom;
 
    //---------------------------------------------
    // TEXTURES & TEXTURE REGIONS
    //---------------------------------------------
 
    //---------------------------------------------
    // CLASS LOGIC
    //---------------------------------------------

    public void loadMenuResources()
    {
        loadMenuGraphics();
        loadMenuAudio();
    }
 
    public void loadGameResources()
    {
        loadGameGraphics();
        loadGameFonts();
        loadGameAudio();
    }
 
    private void loadMenuGraphics()
    {
     
    }
 
    private void loadMenuAudio()
    {
     
    }

    private void loadGameGraphics()
    {
     
    }
 
    private void loadGameFonts()
    {
     
    }
 
    private void loadGameAudio()
    {
     
    }
 
    public void loadSplashScreen()
    {
 
    }
 
    public void unloadSplashScreen()
    {

    }
 
    /**
     * @param engine
     * @param activity
     * @param camera
     * @param vbom
     * <br><br>
     * We use this method at beginning of game loading, to prepare Resources Manager properly,
     * setting all needed parameters, so we can latter access them from different classes (eg. scenes)
     */
    public static void prepareManager(Engine engine, GameActivity activity, Camera camera, VertexBufferObjectManager vbom)
    {
        getInstance().engine = engine;
        getInstance().activity = activity;
        getInstance().camera = camera;
        getInstance().vbom = vbom;
    }
 
    //---------------------------------------------
    // GETTERS AND SETTERS
    //---------------------------------------------
 
    public static ResourcesManager getInstance()
    {
        return INSTANCE;
    }
}

There are different methods responsible for loading graphics, fonts and sounds. There is also a prepareManager method, which assigns required parameters for our manager, we have to use this method while initializing the game.

Step 2:Initializing ResourcesManager:

Open your activity class and create a new field for ResourcesManager:

private ResourcesManager resourcesManager;

Initialize the resources manager, it should look like this:

public void onCreateResources(OnCreateResourcesCallback pOnCreateResourcesCallback) throws IOException
{
    ResourcesManager.prepareManager(mEngine, this, camera, getVertexBufferObjectManager());
    resourcesManager = ResourcesManager.getInstance();
    pOnCreateResourcesCallback.onCreateResourcesFinished();
}

Resources manager is ready to be used.

No comments:

Post a Comment