今回は、ResourcesManager.java、画像などのリソースを一括管理するクラスです。

package com.chabaori.sample;

import org.andengine.engine.Engine;
import org.andengine.engine.camera.BoundCamera;
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.region.ITextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;
														
public class ResourcesManager{
	
	private static final ResourcesManager INSTANCE = new ResourcesManager();
	
	public Engine engine;
	public MainActivity activity;
	public BoundCamera camera;
	public VertexBufferObjectManager vbom;

	private BitmapTextureAtlas splashTextureAtlas;
	public ITextureRegion splash_region;
	private BitmapTextureAtlas backgroundTextureAtlas;
	public ITextureRegion background_region;
	private BitmapTextureAtlas spriteTextureAtlas;
	public ITextureRegion sprite_region;

	public void loadSplashScreen(){
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
        splashTextureAtlas = new BitmapTextureAtlas(activity.getTextureManager(), 256, 256, TextureOptions.BILINEAR);
        splash_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(splashTextureAtlas, activity, "splash.png", 0, 0);
        splashTextureAtlas.load();
        backgroundTextureAtlas = new BitmapTextureAtlas(activity.getTextureManager(), 512, 1024, TextureOptions.BILINEAR);
        background_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(backgroundTextureAtlas, activity, "background.png", 0, 0);
        backgroundTextureAtlas.load();
	}
	
	public void unloadSplashScreen(){
		splashTextureAtlas.unload();
		splashTextureAtlas = null;
		splash_region = null;
	}
	
	public void loadResources(){
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
        spriteTextureAtlas = new BitmapTextureAtlas(activity.getTextureManager(), 256, 256, TextureOptions.BILINEAR);
        sprite_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(spriteTextureAtlas, activity, "sprite.png", 0, 0);
        spriteTextureAtlas.load();
	}
	
	public static void prepareManager(Engine engine, MainActivity activity, BoundCamera camera, VertexBufferObjectManager vbom){
		getInstance().engine = engine;
		getInstance().activity = activity;
		getInstance().camera = camera;
		getInstance().vbom = vbom;
	}
	
	public static ResourcesManager getInstance(){
		return INSTANCE;
	}
}

 画像は、background.png と splash.png、sprite.png を assets フォルダ内に gfx フォルダを作って置いておきます。
background splash sprite

Comments are closed.

Post Navigation