package dk.dionysus.hjarl; // Change this to your own package if you are adding it to your own project.
import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.IEntity;
import org.anddev.andengine.entity.modifier.AlphaModifier;
import org.anddev.andengine.entity.modifier.DelayModifier;
import org.anddev.andengine.entity.modifier.IEntityModifier.IEntityModifierListener;
import org.anddev.andengine.entity.modifier.LoopEntityModifier;
import org.anddev.andengine.entity.modifier.LoopEntityModifier.ILoopEntityModifierListener;
import org.anddev.andengine.entity.modifier.SequenceEntityModifier;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.background.ColorBackground;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.atlas.bitmap.source.IBitmapTextureAtlasSource;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.ui.activity.BaseGameActivity;
import org.anddev.andengine.ui.activity.BaseSplashActivity;
import org.anddev.andengine.util.modifier.IModifier;
import org.anddev.andengine.util.modifier.LoopModifier;
import android.app.Activity;
import android.content.Intent;
import android.widget.Toast;
public class GameSplashActivity extends BaseGameActivity {
private static final int CAMERA_WIDTH = 470;
private static final int CAMERA_HEIGHT = 270;
private Camera camera;
private BitmapTextureAtlas texture;
private TextureRegion pictureRegion;
//private final String backgroundImage = "gfx/AndEngineText.png"; // from the original script
@Override
public Engine onLoadEngine() {
this.camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
final EngineOptions engineOptions = new EngineOptions(true,
ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy( // you need to set this to PORTRAIT instead of LANDSCAPE if you want to change the orientation.
CAMERA_WIDTH, CAMERA_HEIGHT), this.camera);
return new Engine(engineOptions);
}
@Override
public void onLoadResources() {
texture = new BitmapTextureAtlas(512, 512, TextureOptions.BILINEAR); // For storing the splash screen texture
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/"); // We will expect all images to be be in the assets/gfx folder
pictureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.texture, this, "startbox.png", 0, 0); // The first splash screen image is added here.
mEngine.getTextureManager().loadTexture(this.texture); //Loading the texture into memory. Easy to forget when adding new textures for new graphics. You can load several textures at once if you use loadTextures instead, and then put a , between each texture to load.
}
@Override
public Scene onLoadScene() {
final Scene scene = new Scene(1); // The scene where our camera viewpoint will focus on, and were we will be placing our splash screen.
scene.setBackground(new ColorBackground(0, 0, 0)); // Change the scenes background color to contain no red, green or blue. Making it black.
final int x = (CAMERA_WIDTH - this.pictureRegion.getWidth()) / 2;
final int y = (CAMERA_HEIGHT - this.pictureRegion.getHeight()) / 2;
// ^^ A little math trick for centering our picture region on the camera.
final Sprite picture = new Sprite(x, y, this.pictureRegion); // Make a sprite with the picture region as its graphic.
// A sprite is a game object, and makes it for us to add modifiers to it.
// The sprite is what we use to show our splashscreen image and move it.
picture.setAlpha(0); // makes our picture sprite transparent
// I took this entity modifier code from the AndEngineExamples (because the original was old AndEngine code)
final LoopEntityModifier entityModifier =
new LoopEntityModifier(
new IEntityModifierListener() {
@Override
public void onModifierStarted(final IModifier<IEntity> pModifier, final IEntity pItem) {
GameSplashActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
// Any code here will run at the same time as the splash screen runs, or at least the first modifier.
// Toast for debugging
// Toast.makeText(GameSplashActivity.this, "Sequence started.", Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onModifierFinished(final IModifier<IEntity> pEntityModifier, final IEntity pEntity) {
GameSplashActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
// Any code here will run at the same time as the splash screen finishes
// Toast for debugging
// Toast.makeText(GameSplashActivity.this, "Sequence finished.", Toast.LENGTH_SHORT).show();
Intent menu = new Intent(GameSplashActivity.this, ScrollMenu.class); // Creates an intent in this activity, to prepare the next class
startActivity(menu); // Runs the menu activity
finish(); // Ends this activity to stop it from hogging precious Android memory.
}
});
}
},
2, // Loop count. Want more loops to add more splash screens or something else? Just change this number.
new ILoopEntityModifierListener() {
@Override
public void onLoopStarted(final LoopModifier<IEntity> pLoopModifier, final int pLoop, final int pLoopCount) {
GameSplashActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
// Do something while the loop is rolling
// A toast message that is useful for debugging.
//Toast.makeText(GameSplashActivity.this, "Loop: '" + (pLoop + 1) + "' of '" + pLoopCount + "' started.", Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onLoopFinished(final LoopModifier<IEntity> pLoopModifier, final int pLoop, final int pLoopCount) {
GameSplashActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
// Toast.makeText(GameSplashActivity.this, "Loop: '" + (pLoop + 1) + "' of '" + pLoopCount + "' finished.", Toast.LENGTH_SHORT).show();
if (pLoop == 0) { //pLoop is the number of times this modifier has been loop.
texture.clearTextureAtlasSources(); // Clear the first splash screen from our texture
BitmapTextureAtlasTextureRegionFactory.createFromAsset(texture, GameSplashActivity.this, "AndEngineText.png", 0, 0); // Load the second splash screen onto the texture instead
}
if (pLoop == 1) { //pLoop is the number of times this modifier has been looped.
texture.clearTextureAtlasSources(); // Clear the splash screen from our texture
BitmapTextureAtlasTextureRegionFactory.createFromAsset(texture, GameSplashActivity.this, "startbox2.png", 0, 0); // Load this splash screen onto the texture instead
}
if (pLoop == 2) { //pLoop is the number of times this modifier has been looped.
texture.clearTextureAtlasSources(); // Clear the splash screen from our texture
BitmapTextureAtlasTextureRegionFactory.createFromAsset(texture, GameSplashActivity.this, "startbox3.png", 0, 0); // Load this splash screen onto the texture instead
}
if (pLoop == 3) { //pLoop is the number of times this modifier has been looped.
texture.clearTextureAtlasSources(); // Clear the splash screen from our texture
BitmapTextureAtlasTextureRegionFactory.createFromAsset(texture, GameSplashActivity.this, "AndEngineText.png", 0, 0); // Load this splash screen onto the texture instead
}
if (pLoop == 4) {
// You could do something here which would happen after the fourth loop. If you change the loop count this could become the third loop.
}
}
});
}
},
// These are the modifiers added to each splash screen. Making them fade in and out.
new SequenceEntityModifier(
new DelayModifier(1),
new AlphaModifier(0.5f, 0, 1),
new DelayModifier(2),
new AlphaModifier(0.5f, 1, 0),
new DelayModifier(0.5f)
)
);
return scene;
}
@Override
public void onLoadComplete() {}
}