Your first game starts with creating the bootstrapper. This class, typically named in the Program.cs file, is just a Main(), or an Activity if you are on Android. On Windows Phone 7, the device creates a shim that loads your Game class and runs your game.

Program

Next you need a Microsoft.Xna.Framework.Game implementation. Most templates will name your game instance Game1. It doesn't matter what name you choose here, and you can change it after the template creates your game project.

The Game() constructor, which takes no arguments, will look like the following:

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
#if !WINDOWS && !MACOS
            this.graphics.IsFullScreen = true;
#else
            this.graphics.IsFullScreen = false;
#endif
            this.graphics.PreferMultiSampling = true;

            // Frame rate is 30 fps by default for Windows Phone.
            TargetElapsedTime = TimeSpan.FromTicks(333333);
            IsFixedTimeStep = true;

#if WINDOWS || MACOS
            this.IsMouseVisible = true;
#endif
            CCApplication application = new AppDelegate(this, graphics);
            this.Components.Add(application);
            CCDirector.SharedDirector.GamePadEnabled = true;
            application.GamePadButtonUpdate += new CCGamePadButtonDelegate(application_GamePadButtonUpdate);
        }


Note that we are disabling full screen game mode on Windows. This is no longer an acceptable feature of games on Windows, so don't enable full screen game mode. Windows 7 will run your game in FSGM, but you won't see any other windows decorations.

This example is also enabling the GamePad support. On Windows Phone, Android, and iOS, the phone's system keys are mapped to GamePad buttons. The most important of these is the Back button. You will need to add support for exiting or going back through your app using the GamePad Back button.

If you want your game loop to run as fast as it can, then you need to set the game to not be in a fixed time step.

            // Frame rate is 30 fps by default for Windows Phone.
            TargetElapsedTime = TimeSpan.FromTicks(333333);
            IsFixedTimeStep = false;


Now let's handle the GamePad Back button.

        private void ProcessBackClick()
        {
                try
                {
                    CCDirector.SharedDirector.PopScene();
                }
                catch (Exception)
                {
                }
       }

        private void application_GamePadButtonUpdate(CCGamePadButtonStatus backButton, CCGamePadButtonStatus startButton, CCGamePadButtonStatus systemButton, CCGamePadButtonStatus aButton, CCGamePadButtonStatus bButton, CCGamePadButtonStatus xButton, CCGamePadButtonStatus yButton, CCGamePadButtonStatus leftShoulder, CCGamePadButtonStatus rightShoulder, PlayerIndex player)
        {
            if (backButton == CCGamePadButtonStatus.Pressed)
            {
                ProcessBackClick();
            }
        }



CCApplication is the DrawableComponent added to your game canvas. This class also receives the Update(GameTime) call where the GamePad, Keyboard, and Touch state is managed. You need to maintain your own game-specific gamepad status that is used during the Update() call.

Going back to the constructor of our Game, note the following:

            CCApplication application = new AppDelegate(this, graphics);


The AppDelegate class is very important to your game. This is the class that finishes out your game initialization state.

AppDelegate


Last edited Jan 13, 2013 at 7:14 AM by totallyevil, version 4

Comments

No comments yet.