Beispiel #1
0
        /// <summary>
        /// Load the introduction to the level.
        /// </summary>
        public void LoadIntro()
        {
            DataSet dsIntro = new DataSet();

            Debug.Assert(dsIntro != null,
                         "GameMain.LoadIntro: Failed to initialize intro DataSet");

            dsIntro.Locale = CultureInfo.InvariantCulture;

            // Load the intro xml file
            dsIntro.ReadXml(GetFullPath(@"Data\Intro\intro.xml"));

            // Load the intr resources specified in the xml file
            intro = new Intro(dsIntro, graphics);
            Debug.Assert(intro != null,
                         "GameMain.LoadIntro: Failed to initialize Intro");

            // The intro is loaded at this point
            introLoaded = true;
        }
Beispiel #2
0
        /// <summary>
        /// Start the game.  This method loads the game resources and
        /// runs the main game loop.
        /// </summary>
        public void Run()
        {
            // Load and validate the splash screen
            splash = graphics.CreateBitmap(GetFullPath(@"Data\Splash\splash.bmp"), false);
            Debug.Assert(splash != null,
                         "GameMain.Run: Failed to initialized splash screen");
            Debug.Assert(splash.Width <= graphics.ScreenWidth &&
                         splash.Height <= graphics.ScreenHeight,
                         "GameMain.Run: Splash screen has invalid dimensions");

            // Load the game ui now because it has font information that is
            // needed for drawing the 'Loading...' tag
            DataSet dsUI = new DataSet();

            Debug.Assert(dsUI != null,
                         "GameMain.LoadLevel: Failed to initialize UI DataSet");

            dsUI.Locale = CultureInfo.InvariantCulture;

            // Load the ui xml file
            dsUI.ReadXml(GetFullPath(@"Data\UI\ui.xml"));

            // Load the resources specified in the xml file
            ui = new UserInterface(dsUI, graphics, level);
            Debug.Assert(ui != null,
                         "GameMain.LoadLevel: Failed to initialize UI");

            // Set the current update method as the splash screen updater
            update = new UpdateDelegate(UpdateSplash);

            // Loop until the game is done
            while (!done)
            {
                // Switch the update delegate if a switch was requested.
                switch (mode)
                {
                case ModeSwitch.UpdateLevel:
                    gi.ClearKeyPresses();
                    update    = new UpdateDelegate(UpdateLevel);
                    numFrames = 0;
                    secondsElapsedForCurrentFrames = 0;
                    break;

                case ModeSwitch.UpdateCountdown:
                    intro.Dispose();
                    intro = null;
                    level.Update(gi);
                    update = new UpdateDelegate(UpdateCountdown);
                    break;

                case ModeSwitch.UpdateIntro:
                    LoadLevel();
                    update = new UpdateDelegate(UpdateIntro);
                    splash.Dispose();
                    splash = null;
                    break;
                }

                mode = ModeSwitch.None;

                // Store the tick at which this frame started
                Int64 startTick = sw.CurrentTick();

                // Check if the user pressed the exit key
                if (gi.KeyPressed((int)gi.HardwareKeys[Player.ButtonMap()[
                                                           (int)Player.Buttons.Quit]]))
                {
                    done = true;
                }
                else if (levelLoaded && (level.Done || gi.KeyPressed(
                                             (int)gi.HardwareKeys[Player.ButtonMap()[
                                                                      (int)Player.Buttons.ResetLevel]])))
                {
                    Reset();
                    Application.DoEvents();
                    continue;
                }

                // Update the game
                update();

                // Check for pending events from the OS
                Application.DoEvents();

                // Lock the framerate...
                Int64 deltaMS = sw.DeltaTimeMilliseconds(sw.CurrentTick(), startTick);
                Int64 minMS   = (Int64)(1000.0F * MinSecondsPerFrame);
                Int64 maxMS   = (Int64)(1000.0F * MaxSecondsPerFrame);

                // Check if the frame time was fast enough
                if (deltaMS <= minMS)
                {
                    // Loop until the frame time is met
                    do
                    {
                        if (gi.KeyPressed((int)gi.HardwareKeys[
                                              Player.ButtonMap()[(int)Player.Buttons.Quit]]))
                        {
                            done = true;
                            break;
                        }

                        // Thread.Sleep(0);
                        Application.DoEvents();
                        deltaMS = sw.DeltaTimeMilliseconds(sw.CurrentTick(),
                                                           startTick);
                    } while (deltaMS < minMS);
                }

                // Increment the number of frames
                numFrames++;
                // Increment the overall time for these frames
                if (deltaMS < maxMS)
                {
                    secondsElapsedForCurrentFrames += deltaMS / 1000.0F;
                }
                else
                {
                    secondsElapsedForCurrentFrames += maxMS / 1000.0F;
                }

                // Make sure enough time has elapsed since the last check
                if (level != null && secondsElapsedForCurrentFrames >
                    level.FrameRateCheckRate)
                {
                    currentSecondsPerFrame =
                        secondsElapsedForCurrentFrames / numFrames;
                    numFrames = 0;
                    secondsElapsedForCurrentFrames = 0;
                }
            }
        }