Пример #1
0
        /// <summary>
        /// Update the splash screen.
        /// </summary>
        public void UpdateSplash()
        {
            // Calculate the upper-left corner of the splash screen image
            int x = (graphics.ScreenWidth - splash.Width) >> 1;
            int y = (graphics.ScreenHeight - splash.Height) >> 1;

            // The source region is the entire image
            src.X      = 0;
            src.Y      = 0;
            src.Width  = splash.Width;
            src.Height = splash.Height;

            // Draw the splash screen
            graphics.DrawBitmap(x, y, src, splash);

            // Flip the back buffers
            graphics.Flip();

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

            // Draw the splash screen
            graphics.DrawBitmap(x, y, src, splash);

            // Draw a "Loading" message as this might take a while
            graphics.DrawText(graphics.ScreenWidth >> 1,
                              graphics.ScreenHeight - 50,
                              "Loading...",
                              Color.White, ui.Font, FontDrawOptions.DrawTextCenter);

            // Flip the back buffers
            graphics.Flip();

            if (!introLoaded)
            {
                LoadIntro();
            }

            // delay if the splash screen hasn't been up long enough
            while (sw.DeltaTimeMilliseconds(sw.CurrentTick(), startTick)
                   < TotalSplashTime * 1000.0F)
            {
                Thread.Sleep(0);
            }

            mode = ModeSwitch.UpdateIntro;
        }
Пример #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;
                }
            }
        }