Full game tutorial - part 3. Resources Manager:
This class will be responsible for loading/unloading our game resources (art, fonts, audio) it will also provide references to the most commonly needed objects (camera, context, engine, VertexBufferObjectManager) this class will use singleton holder, which means we will be able to access this class from the global level.
package com.matimdev.manager; 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.matimdev.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 separat methods responsible for loading graphics, fonts and sounds, also methods responsible for unloading textures and loading them again (needed for memory saving purposes while switching between menu and game scenes) There is also a prepareManager method, which assigns required parameters for our manager, we have to use this method while initializing our game.
1. Initializing our ResourcesManager:
Open your activity class, create a new field for ResourcesManager:
Open your activity class, create a new field for ResourcesManager:
private ResourcesManager resourcesManager;
Initialize the resources manager and pass the required parameters inside onCreateResources, it should look like this:
public void onCreateResources(OnCreateResourcesCallback pOnCreateResourcesCallback) throws IOException { ResourcesManager.prepareManager(mEngine, this, camera, getVertexBufferObjectManager()); resourcesManager = ResourcesManager.getInstance(); pOnCreateResourcesCallback.onCreateResourcesFinished(); }
Done, our resources manager is ready to be used, now refer to the next article. Introducing scene management, using the SceneManager class.
HTML Comment Box is loading comments...