XerShade Posted October 3, 2016 Share Posted October 3, 2016 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 More sharing options...
Chief Posted October 3, 2016 Share Posted October 3, 2016 Threads  Edit: Let me clarify, since you already have one thread... use multiple threads. One for updating, one for rendering. Link to comment Share on other sites More sharing options...
XerShade Posted October 4, 2016 Author Share Posted October 4, 2016 Started to work on multi-threading the loop, code can be seen here: https://gitlab.com/XerShade/Esmiylara.Online/blob/master/source/Esmiylara.Online.Client/Engine.cs  I will be re-writing it from scratch later to be more optimized and for two threads instead of one. Suggestions are still appreciated. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now