Jump to content

General Game Loops: Update & Draw


XerShade

Recommended Posts

So I have the game loop for my game coded here. I've been trying to figure out how to make Update() not get lagged out when Draw() takes it's time and slows the game out. Game Loops have always been a weak point so any help would be greatly appreciated.

 

Source Code: https://gitlab.com/XerShade/Esmiylara.Online/blob/master/source/Esmiylara.Online.Client/Engine.cs

using Esmiylara.Online.Client.Components;
using System;
using System.Threading;
using System.Windows.Forms;
using Timer = Esmiylara.Online.Client.Components.Timer;

namespace Esmiylara.Online.Client
{
    /// <summary>
    /// Esmiylara Online game engine.
    /// </summary>
    public partial class Engine
    {
        /// <summary>
        /// The game loop's thread.
        /// </summary>
        private static Thread LoopThread;
        /// <summary>
        /// Tells the engine if it should continue looping.
        /// </summary>
        private static bool DoLoop;

        /// <summary>
        /// [Hook] GameLoopInitialize, called when the game loop is initialized.
        /// </summary>
        public static event EventHandler<EventArgs> GameLoopInitialize
        {
            add { GameLoop.GameLoopInitialize += value; }
            remove { GameLoop.GameLoopInitialize -= value; }
        }
        /// <summary>
        /// [Hook] GameLoopStart, called when the game loop is started.
        /// </summary>
        public static event EventHandler<EventArgs> GameLoopStart
        {
            add { GameLoop.GameLoopStart += value; }
            remove { GameLoop.GameLoopStart -= value; }
        }
        /// <summary>
        /// [Hook] GameLoopUpdate, called when the game loop is updated.
        /// </summary>
        public static event EventHandler<EventArgs> GameLoopUpdate
        {
            add { GameLoop.GameLoopUpdate += value; }
            remove { GameLoop.GameLoopUpdate -= value; }
        }
        /// <summary>
        /// [Hook] GameLoopDraw, called when the game loop is drawn.
        /// </summary>
        public static event EventHandler<EventArgs> GameLoopDraw
        {
            add { GameLoop.GameLoopDraw += value; }
            remove { GameLoop.GameLoopDraw -= value; }
        }
        /// <summary>
        /// [Hook] GameLoopStop, called when the game loop is stopped.
        /// </summary>
        public static event EventHandler<EventArgs> GameLoopStop
        {
            add { GameLoop.GameLoopStop += value; }
            remove { GameLoop.GameLoopStop -= value; }
        }
        /// <summary>
        /// [Hook] GameLoopDispose, called when the game loop is disposed.
        /// </summary>
        public static event EventHandler<EventArgs> GameLoopDispose
        {
            add { GameLoop.GameLoopDispose += value; }
            remove { GameLoop.GameLoopDispose -= value; }
        }

        /// <summary>
        /// Runs the game engine.
        /// </summary>
        public static void Run()
        {
            // Call the loop method.
            Loop();
        }

        /// <summary>
        /// Stops the game engine.
        /// </summary>
        public static void Stop()
        {
            // Tell the engine to stop looping.
            DoLoop = false;
        }

        /// <summary>
        /// Aborts the game engine thread.
        /// </summary>
        public static void Abort()
        {
            // Do the thing!
            LoopThread.Abort();
        }

        /// <summary>
        /// The game engine loop function, do NOT call this directly. EVER.
        /// </summary>
        private static void Loop()
        {
            // Construct a new thread and assign the game code.
            LoopThread = new Thread(() =>
            {
                // Tell the engine to loop.
                DoLoop = true;

                // Construct the game loop's variables.
                GameTime tick = new GameTime();
                Timer tickUpdate = new Timer(1000 / 30);
                Timer tickDraw = new Timer(1000 / 30);

                // Initialize the game.
                GameLoop.Initialize();

                // Start the game.
                GameLoop.Start();

                // The game loop, simple for now.
                while (DoLoop)
                {
                    // Update the GameTime object.
                    tick.Update();

                    // Check to see if we need to update the game.
                    if (tickUpdate.CheckInterval(tick.Value))
                    {
                        // Update the game, do NOT draw any part of the game here.
                        GameLoop.Update(tick);

                        // Update the timer.
                        tickUpdate.Update(tick.Value);
                    }

                    // Check to see if we need to draw the game.
                    if (tickDraw.CheckInterval(tick.Value))
                    {
                        // Draw the game, do NOT draw any part of the game here.
                        GameLoop.Draw(tick);

                        // Draw the timer.
                        tickDraw.Update(tick.Value);
                    }

                    // Update the message pump.
                    Application.DoEvents();
                }

                // Stop the game.
                GameLoop.Stop();

                // Dispose the game.
                GameLoop.Dispose();
            });
            LoopThread.Start();
        }
    }
}

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...