very simple splash screen alternative

  ... tutorials on how to use AndEngine.

very simple splash screen alternative

Postby thepi » Thu Nov 24, 2011 6:00 am

This is my method of implementing splash screen scene (showing splash screen - not black screen) while loading all other game textures and scenes. It is done within one activity, without any overrides or asynctasks.
It is the cleanest and the simplest method I have seen, but might not be conventional. It works though, no black screen is seen while loading game.

1. Inside onLoadResources I loaded only splash texture.
2. Inside onLoadScene I created only splash scene and returned it.
3. Then I created methods loadResources() and loadScenes() and I added all stuff that needs to be loaded for the game inside those two methods.
4. Then in the onLoadComplete I used TimerHandler with minimum delay, after which I called methods from 3. and the first game scene to be shown, in my case it is mMenuScene. TimerHandler is the crucial thing for this to work, without TimerHandler it shows black screen instead of splash. Since I'm not an expert, I cannot explain exactly why this works, maybe some of you can. I hope it will be of use. :roll:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class MainGameActivity extends BaseGameActivity {
  2.  
  3.         // ===========================================================
  4.         // Final Fields
  5.         // ===========================================================
  6.  
  7.  
  8.         // ===========================================================
  9.         // Constants
  10.         // ===========================================================
  11.  
  12.         private static final int CAMERA_WIDTH = 480;
  13.         private static final int CAMERA_HEIGHT = 800;
  14.  
  15.         // ===========================================================
  16.         // Fields
  17.         // ===========================================================
  18.  
  19.         protected Camera mCamera;
  20.  
  21.         // splash scene
  22.         protected Scene mSplashScene;
  23.         private BitmapTextureAtlas mSplashBackgroundTextureAtlas;
  24.         private TextureRegion mSplashBackgroundTextureRegion;
  25.  
  26.         // menu scene
  27.         protected Scene mMenuScene;
  28.         // ...
  29.  
  30.         // ===========================================================
  31.         // Methods for/from SuperClass/Interfaces
  32.         // ===========================================================
  33.  
  34.         @Override
  35.         public Engine onLoadEngine() {
  36.                 this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
  37.                 return new Engine(new EngineOptions(true, ScreenOrientation.PORTRAIT, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera).setNeedsSound(true));
  38.         }
  39.  
  40.         @Override
  41.         public void onLoadResources() {
  42.                 BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
  43.                 mSplashBackgroundTextureAtlas = new BitmapTextureAtlas(512, 1024, TextureOptions.NEAREST);
  44.                 mSplashBackgroundTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mSplashBackgroundTextureAtlas, this, "splash.jpg", 0, 0);
  45.                 mEngine.getTextureManager().loadTextures(mSplashBackgroundTextureAtlas);
  46.         }
  47.  
  48.         @Override
  49.         public Scene onLoadScene() {
  50.                 mSplashScene = new Scene();
  51.                 mSplashScene.setBackgroundEnabled(false);
  52.                 mSplashScene.attachChild(new Sprite(0, 0, mSplashBackgroundTextureRegion));
  53.  
  54.                 return this.mSplashScene;
  55.         }
  56.        
  57.         @Override
  58.         public void onLoadComplete() {
  59.                 mEngine.registerUpdateHandler(new TimerHandler(0.01f, new ITimerCallback() {
  60.                         @Override
  61.                         public void onTimePassed(final TimerHandler pTimerHandler) {
  62.                                 mEngine.unregisterUpdateHandler(pTimerHandler);
  63.                                 loadResources();
  64.                                 loadScenes();
  65.                                 mEngine.setScene(mMenuScene);
  66.                         }
  67.                 }));
  68.         }
  69.         // ===========================================================
  70.         // Methods
  71.         // ===========================================================
  72.  
  73.         public void loadResources() {
  74.                 // menu resources
  75.                 // help resources
  76.                 // pick level resources
  77.                 // game resources
  78.         }
  79.  
  80.         public void loadScenes() {
  81.                 // menu scene
  82.                 // help scene
  83.                 // pick level scene
  84.                 // game scene
  85.         }
  86.  
  87. }
Parsed in 0.041 seconds, using GeSHi 1.0.8.4


Edit: I added other parts of activity, so it will be clearer what I'm writing about. ;)
Last edited by thepi on Sun Dec 04, 2011 5:51 am, edited 1 time in total.
Go and trap them... Trap Balls on Google Play
Play & reminisce... Treasure Island LCD Retro
thepi
 
Posts: 456
Joined: Sun Oct 09, 2011 9:30 pm

Re: another splash screen alternative

Postby RealMayo » Thu Nov 24, 2011 6:46 am

Thanks! I'm going to try this.
User avatar
RealMayo
 
Posts: 1689
Joined: Sat Sep 03, 2011 9:25 pm
Location: Chicago, IL

Re: another splash screen alternative

Postby kblood » Thu Nov 24, 2011 11:30 am

I am wondering, does this splash screen activity extend the BaseSplashActivity?

Looks very nice, so I linked this one in my own tutorial.
kblood
 
Posts: 333
Joined: Wed May 25, 2011 10:13 pm

Re: another splash screen alternative

Postby thepi » Thu Nov 24, 2011 6:15 pm

Thanks! It does not extend BaseSplashActivity. Only one activity is used, the main activity that extends BaseGameActivity.
Go and trap them... Trap Balls on Google Play
Play & reminisce... Treasure Island LCD Retro
thepi
 
Posts: 456
Joined: Sun Oct 09, 2011 9:30 pm

Re: another splash screen alternative

Postby kblood » Sat Dec 03, 2011 1:53 pm

thepi wrote:Thanks! It does not extend BaseSplashActivity. Only one activity is used, the main activity that extends BaseGameActivity.


I suspected that is how you did it . I guess a downside could be that it does not animate the splash screen, but it is still amazing how its so simple, and not using the BaseSplashActivity, while also preloading the next activity.
kblood
 
Posts: 333
Joined: Wed May 25, 2011 10:13 pm

Re: another splash screen alternative

Postby RealMayo » Sat Dec 03, 2011 7:08 pm

kblood wrote:amazing how its so simple, and not using the BaseSplashActivity, while also preloading the next activity.

Yes! I'm successfully using it in my project and it works great!
User avatar
RealMayo
 
Posts: 1689
Joined: Sat Sep 03, 2011 9:25 pm
Location: Chicago, IL

Re: very simple splash screen alternative

Postby thepi » Sun Dec 04, 2011 6:07 am

I just renamed the topic title from "another" to "very simple" since you guys confirmed it. ;)
I also added some source, making it more complete.

@kblood: You are right about the lack of splash screen animation. I'm just guessing, what if I'd create another separate TimerHandler for animation before that very short one, do you think it would be possible to do some animation there?
Or I'll just try and play with it tomorrow.
Go and trap them... Trap Balls on Google Play
Play & reminisce... Treasure Island LCD Retro
thepi
 
Posts: 456
Joined: Sun Oct 09, 2011 9:30 pm

Re: very simple splash screen alternative

Postby kblood » Tue Dec 06, 2011 11:35 am

thepi wrote:I just renamed the topic title from "another" to "very simple" since you guys confirmed it. ;)
I also added some source, making it more complete.

@kblood: You are right about the lack of splash screen animation. I'm just guessing, what if I'd create another separate TimerHandler for animation before that very short one, do you think it would be possible to do some animation there?
Or I'll just try and play with it tomorrow.


Possibly, but if you want to add animations, it might be easier to use the BaseSplashActivity extension, since it has the animation as part of it. Otherwise it would become less simple to add animations. In the tutorial I am making, I show how the creator of Space Assault created his own animations, but he also had to implement some more stuff to do so. I just realized I had not put the code itself into my tutorial, I just linked some files with it. But I just fixed that, although its probably too much code to get a good overview in a browser:

tutorials/galaga-like-game-space-assault-t5652.html#p25257

I guess this is the most important part:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         public Scene onLoadScene() {
  2.  
  3.                 final Scene scene = new Scene(1);
  4.                 scene.setBackground(new ColorBackground(0, 0, 0));
  5.  
  6.                 final int x = (CAMERA_WIDTH - this.pictureRegion.getWidth()) / 2;
  7.                 final int y = (CAMERA_HEIGHT - this.pictureRegion.getHeight()) / 2;
  8.  
  9.                 final Sprite picture = new Sprite(x, y, this.pictureRegion);
  10.                 picture.setAlpha(0); // makes our picture sprite transparent
  11.  
  12.                 // I took this entity modifier code from the AndEngineExamples
  13.                         final LoopEntityModifier entityModifier =
  14.                                 new LoopEntityModifier(
  15.                                                 new IEntityModifierListener() {
  16.                                                         @Override
  17.                                                         public void onModifierStarted(final IModifier<IEntity> pModifier, final IEntity pItem) {
  18.                                                                 GameSplashActivity.this.runOnUiThread(new Runnable() {
  19.                                                                         @Override
  20.                                                                         public void run() {
  21.                                                                                 // Any code here will run at the same time as the splash screen runs
  22.                                                                                
  23.                                                                                 // Toast for debugging
  24.                                                                                 // Toast.makeText(GameSplashActivity.this, "Sequence started.", Toast.LENGTH_SHORT).show();
  25.                                                                         }
  26.                                                                 });
  27.                                                         }
  28.  
  29.                                                         @Override
  30.                                                         public void onModifierFinished(final IModifier<IEntity> pEntityModifier, final IEntity pEntity) {
  31.                                                                 GameSplashActivity.this.runOnUiThread(new Runnable() {
  32.                                                                         @Override
  33.                                                                         public void run() {
  34.                                                                                 // Any code here will run at the same time as the splash screen finishes
  35.                                                                                
  36.                                                                                 // Toast for debugging
  37.                                                                                 // Toast.makeText(GameSplashActivity.this, "Sequence finished.", Toast.LENGTH_SHORT).show();
  38.                                                                                 Intent menu = new Intent(GameSplashActivity.this, MenuActivity2.class); // Creates an intent in this activity, to prepare the next class
  39.                                                                                 startActivity(menu); // Runs the menu activity
  40.                                                                                 finish(); // Ends this activity to stop it from hogging precious Android memory.
  41.                                                                         }
  42.                                                                 });
  43.                                                         }
  44.                                                 },
  45.                                                 2, // Loop count. Want more loops to add more splash screens or something else? Just change this number.
  46.                                                 new ILoopEntityModifierListener() {
  47.                                                         @Override
  48.                                                         public void onLoopStarted(final LoopModifier<IEntity> pLoopModifier, final int pLoop, final int pLoopCount) {
  49.                                                                 GameSplashActivity.this.runOnUiThread(new Runnable() {
  50.                                                                         @Override
  51.                                                                         public void run() {
  52.                                                                                 // Do something while the loop is rolling
  53.                                                                                
  54.                                                                                 // A toast message that is useful for debugging.
  55.                                                                                 // Toast.makeText(GameSplashActivity.this, "Loop: '" + (pLoop + 1) + "' of '" + pLoopCount + "' started.", Toast.LENGTH_SHORT).show();
  56.                                                                         }
  57.                                                                 });
  58.                                                         }
  59.  
  60.                                                         @Override
  61.                                                         public void onLoopFinished(final LoopModifier<IEntity> pLoopModifier, final int pLoop, final int pLoopCount) {
  62.                                                                 GameSplashActivity.this.runOnUiThread(new Runnable() {
  63.                                                                         @Override
  64.                                                                         public void run() {
  65.                                                                                 // Toast.makeText(GameSplashActivity.this, "Loop: '" + (pLoop + 1) + "' of '" + pLoopCount + "' finished.", Toast.LENGTH_SHORT).show();
  66.                                                                                
  67.                                                                                 if (pLoop == 0) { //pLoop is the number of times this modifier has been loop.
  68.                                                                                         texture.clearTextureAtlasSources(); // Clear the first splash screen from our texture
  69.                                                                                         BitmapTextureAtlasTextureRegionFactory.createFromAsset(texture, GameSplashActivity.this, "mercury.png", 0, 0); // Load the second splash screen onto the texture instead
  70.                                                                                 }
  71.                                                                                 else {
  72.                                                                                         // You could do something here which would happen after the second loop. If you change the loop count this could become the third loop.
  73.                                                                                        
  74.                                                                                 }
  75.                                                                         }
  76.                                                                 });
  77.                                                         }
  78.                                                 },
  79.                                                 new SequenceEntityModifier(
  80.                                                         new DelayModifier(1),
  81.                                                         new AlphaModifier(0.5f, 0, 1),
  82.                                                         new DelayModifier(2),
  83.                                                         new AlphaModifier(0.5f, 1, 0),
  84.                                                                         new DelayModifier(0.5f)
  85.                                                        
  86.                                                                
  87.                                                 )
  88.                                 );
  89.  
  90.                         picture.registerEntityModifier(entityModifier); // We attach our modifier to our Picture sprite
  91.                        
  92.  
  93.                 scene.attachChild(picture); // We attach our picture sprite to the active scene
Parsed in 0.045 seconds, using GeSHi 1.0.8.4


All those modifiers is what is needed to make animations not done by the BaseSplashActivity.
kblood
 
Posts: 333
Joined: Wed May 25, 2011 10:13 pm

Re: very simple splash screen alternative

Postby thepi » Tue Dec 06, 2011 8:48 pm

Actually I did make my first splash screen with BaseSplashActivity (without the loop, though). But since I have many textures to load and I could not find a simple way to do so, without showing the black screen for a couple of seconds while loading textures I started experimenting.
So I came up with this simple and clean way of splash screen, doing exactly what I wanted.

Kblood, in the example you gave, I guess you can load all the textures in onLoopStart, but how can you tell when all textures are loaded and all sprites are added to screens, so you can stop the splash loop and begin with first game screen?
Go and trap them... Trap Balls on Google Play
Play & reminisce... Treasure Island LCD Retro
thepi
 
Posts: 456
Joined: Sun Oct 09, 2011 9:30 pm

Re: very simple splash screen alternative

Postby kblood » Tue Dec 06, 2011 11:10 pm

thepi wrote:Actually I did make my first splash screen with BaseSplashActivity (without the loop, though). But since I have many textures to load and I could not find a simple way to do so, without showing the black screen for a couple of seconds while loading textures I started experimenting.
So I came up with this simple and clean way of splash screen, doing exactly what I wanted.

Kblood, in the example you gave, I guess you can load all the textures in onLoopStart, but how can you tell when all textures are loaded and all sprites are added to screens, so you can stop the splash loop and begin with first game screen?


It would have to be changed a bit. Making the load resources become implemented between two of the animations sequences on one of the splash screens.

Maybe
loadResources(); at the first splash screen, before it goes black and loadScenes(); at the second splash screen before it fades to black. Maybe the modifier loop would have to be split in two. But as I said earlier, it would stop being a simple splash screen when adding all these modifiers.

But when looking closer at your script, it seems that it is supposed to do the splash screen in the same activity as the the menu and the game itself is loaded? I am not sure, but I intents makes the next activity load before running it. But I have been wondering why it does not setup the intent earlier then. I guess I would have to study Android itself some more to figure that out.

How to load resources in between different activities.
kblood
 
Posts: 333
Joined: Wed May 25, 2011 10:13 pm

Next

Return to Tutorials

Who is online

Users browsing this forum: No registered users and 29 guests