Monday, March 17, 2014

Creating Splash Screen

We have crated all management classes, now we can start our actual game coding. First we are going to crate splash screen class which will display a splash screen and load resources at end.

Step 1: Loading splash resources.
I have used my company logo image as below,
> put your splash graphic file inside assets/gfx/ folder.
> call this file splash.png
> open the ResourcesManager class, and lets load this graphic. Create fields for the texture region, and splash texture.

public ITextureRegion splash_region;
private BitmapTextureAtlas splashTextureAtlas;

Load our splash file, inside the previously created loadSplashScreen() method.

BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
splashTextureAtlas = new BitmapTextureAtlas(activity.getTextureManager(), 256, 256, TextureOptions.BILINEAR);
splash_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(splashTextureAtlas, activity, "splash.png", 0, 0);
splashTextureAtlas.load();

Now lets prepare the method responsible for unloading our splash texture (which will be executed after completing the process of loading game resources and switching from the splash scene to the menu scene) Do it inside the previously created unloadSplashScreen()

splashTextureAtlas.unload();
splash_region = null;

Done, we've prepared the code responsible for loading/unloading splash resources, now lets move onto creating the splash scene.

Step 2: Creating the splash scene:

create a new class called SplashScene
as an extension, use the previously created abstract class BaseScene.
you will be forced by eclipse to add unimplemented methods (from our superclass)

package com.mindew.fruitecollector;

import org.andengine.engine.camera.Camera;
import org.andengine.entity.sprite.Sprite;
import org.andengine.opengl.util.GLState;

import com.mindew.fruitecollector.BaseScene;
import com.mindew.fruitecollector.SceneManager.SceneType;

/**
 * @author Mateusz Mysliwiec
 * @author www.matim-dev.com
 * @version 1.0
 */
public class SplashScene extends BaseScene
{
    @Override
    public void createScene()
    {

    }

    @Override
    public void onBackKeyPressed()
    {

    }

    @Override
    public SceneType getSceneType()
    {

    }

    @Override
    public void disposeScene()
    {

    }
}

Okay, we have handy and easy to understand methods, lets start with filling getSceneType(), simply return scene type (from the SceneManager`s enum)

@Override
public SceneType getSceneType()
{
    return SceneType.SCENE_SPLASH;
}

Now lets handle the scene creation, we will only need one sprite for our splash.
create a new field for our splash sprite:

private Sprite splash;

lets initialize this sprite, inside createScene() method:

splash = new Sprite(0, 0, resourcesManager.splash_region, vbom)
{
    @Override
    protected void preDraw(GLState pGLState, Camera pCamera)
    {
       super.preDraw(pGLState, pCamera);
       pGLState.enableDither();
    }
};
     
splash.setScale(1.5f);
splash.setPosition(400, 240);
attachChild(splash);

What we just did is initialize our splash sprite, and attached it in the middle of the screen. We also enabled dithering - to improve quality, since its gradient based art (see included link, for more information about dithering)

next step, handle disposing the scene, do it inside the disposeScene() method:

@Override
public void disposeScene()
{
    splash.detachSelf();
    splash.dispose();
    this.detachSelf();
    this.dispose();
}

This will dispose of the splash sprite, and detach it from the scene. Okay, our splash scene is done, now we just have to initialize it inside our activity, more below.

Step 3. Initializing our splash scene:

open our SceneManager class, lets create method responsible for initializing scene. In the parameter we will use OnCreateSceneCallback, because we will need it in the activity.

public void createSplashScene(OnCreateSceneCallback pOnCreateSceneCallback)
{
    ResourcesManager.getInstance().loadSplashScreen();
    splashScene = new SplashScene();
    currentScene = splashScene;
    pOnCreateSceneCallback.onCreateSceneFinished(splashScene);
}

We also will create method responsible for disposing splash scene (to dispose it when it will not be longer needed, which means after we successfully loaded menu resources)

private void disposeSplashScene()
{
    ResourcesManager.getInstance().unloadSplashScreen();
    splashScene.disposeScene();
    splashScene = null;
}

execute the createSplashScene() method inside onCreateScene in your Activity.

public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback) throws IOException
{
    SceneManager.getInstance().createSplashScene(pOnCreateSceneCallback);
}

perform certain tasks while displaying splash (inside onPopulateScene in your Activity)

public void onPopulateScene(Scene pScene, OnPopulateSceneCallback pOnPopulateSceneCallback) throws IOException
{
    mEngine.registerUpdateHandler(new TimerHandler(2f, new ITimerCallback()
    {
            public void onTimePassed(final TimerHandler pTimerHandler)
            {
                mEngine.unregisterUpdateHandler(pTimerHandler);
                // load menu resources, create menu scene
                // set menu scene using scene manager
                // disposeSplashScene();
                // READ NEXT ARTICLE FOR THIS PART.
            }
    }));
    pOnPopulateSceneCallback.onPopulateSceneFinished();
}

What it does:
It will display the splash screen until different tasks have been executed (Loading the menu resources, the menu scene and setting the scene to menu scene.)

At this step, after running our game, you should be able to see your splash scene being displayed (all the time, for now) In the next article, we will cover creating the menu scene and switching to it after loading it. Refer to the next part to continue.

No comments:

Post a Comment