GameMIDlet.java

/*
 * Copyright © 2012 Nokia Corporation. All rights reserved.
 * Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation.
 * Oracle and Java are trademarks or registered trademarks of Oracle and/or its
 * affiliates. Other product and company names mentioned herein may be trademarks
 * or trade names of their respective owners.
 * See LICENSE.TXT for license information.
 */

package com.nokia.example.spacemission;

import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class GameMIDlet
        extends MIDlet
        {

    private Display display;
    private GameCanvasView gameCanvas;
    private SplashScreenView splashScreen;
    private static String FRAMEWORK_INSTRUCTIONS = "framework/instructions.png";
    private static String FRAMEWORK_PAUSED = "framework/paused.png";
    private static String FRAMEWORK_WELLDONE = "framework/gameover_welldone.png";
    private static String FRAMEWORK_BADLUCK = "framework/gameover_badluck.png";
    private static String GAME_WEAPON = "game/weapon.png";
    private static String GAME_FIRED = "game/fired.png";
    private static int TIMEOUT = 4000;
    private static String SPLASH_1 = "splashscreen/companyimage.png";
    private static String SPLASH_2 = "splashscreen/splashimage.png";
    private GameSoundManager soundManager;
    private static String MIDI_MUSIC = "sounds/music.mid";
    private static String SOUND_SHOT = "sounds/shot.amr";


    public GameMIDlet() {
        this.display = Display.getDisplay(this);
        String[] midiFilenames = {MIDI_MUSIC};
        String[] gameSoundsFilenames = {SOUND_SHOT};
        soundManager = new GameSoundManager(midiFilenames, gameSoundsFilenames);

        Image[] images = new Image[2];
        images[0] = loadImage(SPLASH_1);
        images[1] = loadImage(SPLASH_2);

        Image frameworkPaused = loadImage(FRAMEWORK_PAUSED);
        Image frameworkInstructions = loadImage(FRAMEWORK_INSTRUCTIONS);
        Image frameworkWellDone = loadImage(FRAMEWORK_WELLDONE);
        Image frameworkBadLuck = loadImage(FRAMEWORK_BADLUCK);
        Image gameWeapon = loadImage(GAME_WEAPON);
        Image gameFired = loadImage(GAME_FIRED);

        this.gameCanvas = new GameCanvasView(this, soundManager, frameworkInstructions, frameworkPaused,
                                             frameworkWellDone, frameworkBadLuck, gameWeapon, gameFired);
        this.splashScreen = new SplashScreenView(display, gameCanvas, images, TIMEOUT);
    }

    protected void startApp() throws MIDletStateChangeException {
        this.display.setCurrent(splashScreen);
        soundManager.playToneSequence();
    }

    protected void pauseApp() {
        // TODO Auto-generated method stub
    }

    protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
        this.gameCanvas.stopGame();
        this.gameCanvas = null;
        this.splashScreen = null;
        this.display = null;
    }

    private Image loadImage(String image) {
        Image splashImage = null;
        try {
            splashImage = Image.createImage("/" + image);
        }
        catch (Exception e) {
            System.out.println("Couldn't load image " + image);
        }
        return splashImage;
    }
}