The AppDelegate class is the third and final step to your game bootstrap sequence. Here is where you extend the CCApplication object to setup your game specific environment.

    internal class AppDelegate : CCApplication
    {
        public AppDelegate(Game game, GraphicsDeviceManager graphics)
            : base(game, graphics)
        {
            s_pSharedApplication = this;
            DrawManager.InitializeDisplay(game, graphics, DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight);
        }


The call to InitializeDisplay performs the screen orientation setup and does some screen setup that you may have already done in your Game() constructor:

        public static void InitializeDisplay(Game game, GraphicsDeviceManager graphics, DisplayOrientation supportedOrientations)
        {
            m_GraphicsDeviceMgr = graphics;
            SetOrientation(supportedOrientations, false);

#if ANDROID
            graphics.IsFullScreen = true;
#endif

#if WINDOWS_PHONE
            graphics.IsFullScreen = true;
#endif

#if WINDOWS
            game.IsMouseVisible = true;
            graphics.IsFullScreen = false;
#endif
        }


Next, after the base application loads content, it calls ApplicationDidFinishLaunching:

        public override bool ApplicationDidFinishLaunching()
        {
            SimpleAudioEngine.SharedEngine.SaveMediaState();
            CCDirector pDirector = null;
            try
            {
                DrawManager.SetDesignResolutionSize(800f, 480f, ResolutionPolicy.ShowAll);
                CCApplication.SharedApplication.GraphicsDevice.Clear(Color.Black);
                //initialize director
                pDirector = CCDirector.SharedDirector;
                pDirector.SetOpenGlView();

                //turn on display FPS
                pDirector.DisplayStats = false;

                // set FPS. the default value is 1.0/60 if you don't call this
                pDirector.AnimationInterval = 1.0 / 60;
            }
            catch (Exception ex)
            {
                CCLog.Log("ApplicationDidFinishLaunching(): Error " + ex.ToString());
            }
        }

The media player's state is very important to your application. If a user is playing background music and starts to play your game, you don't want to interrupt the background music. The SharedMedia object will keeps information about the current background music so that it can be restored when the game terminates.

This final step sets up the display scale and font scaling. This is all completed with a single call:

DrawManager.SetDesignResolutionSize(800f, 480f, ResolutionPolicy.ShowAll);

The ResolutionPolicy is very important because it determines what your game will do when the design resolution does not match the device resolution. If you pick the ShowAll option then your entire game display will appear.

    public enum ResolutionPolicy
    {
        // The entire application is visible in the specified area without trying to preserve the original aspect ratio. 
        // Distortion can occur, and the application may appear stretched or compressed.
        ExactFit,
        // The entire application fills the specified area, without distortion but possibly with some cropping, 
        // while maintaining the original aspect ratio of the application.
        NoBorder,
        // The entire application is visible in the specified area without distortion while maintaining the original 
        // aspect ratio of the application. Borders can appear on two sides of the application.
        ShowAll,

        UnKnown,
    }


There are two final methods to setup in the AppDelegate. These methods will manage how your application goes into the background on a mobile device and then how it returns to the foreground.

        public override void ApplicationDidEnterBackground()
        {
            base.ApplicationDidEnterBackground();
            CCDirector.SharedDirector.Pause();
        }

        public override void ApplicationWillEnterForeground()
        {
            base.ApplicationWillEnterForeground();
            CCDirector.SharedDirector.ResumeFromBackground();
        }


Note that you need to call the base CCApplication's methods in these two methods. The CCTextureCache needs to be properly managed and this is done in the CCApplication's implementation of the background and foreground methods.

Last edited Jan 12, 2013 at 6:24 AM by totallyevil, version 1

Comments

No comments yet.