September 18th, 2012, 09:36 AM
Game loop stutter
For a school project we must make a pretty basic game in c#. So far we have created a gameloop and a basic draw function.
Our problem now is that the game randomly stutters. Sometimes it runs perfectly smooth when I run it, other times it stutters pretty bad.
Here is the gameloop:
And here is the paint method:
long currentMS = stopwatch.ElapsedMilliseconds;
while (stopwatch.ElapsedMilliseconds - currentMS < 16)
Does anyone have any idea what could cause the stutters?
protected override void OnPaint(PaintEventArgs e)
Graphics g = view.CreateGraphics();
September 22nd, 2012, 11:11 AM
Re: Game loop stutter
First, keep in mind that Windows is a message based non-preemptive multitasking system. That means that Windows sends messages to the application's message pump (the message pump is hidden from you in managed code - look at a c++ program listing for a Windows program to see how it works). The message pump then processes the message (for instance WM_PAINT) calling the appropriate callback or raising the appropriate event in your code (for instance OnPaint). Your code then executes and returns execution to Windows. Windows queues up these messages for your appication. The idea is that a well behaved Windows program does it's thing in a timely manner, then returns execution back to Windows, however, some programs don't play nice under these rules. If a program executes a long running loop without returning to Windows (your call to Application.DoEvents does this - it temporarily returns execution to Windows so that Windows can do its housekeeping and allow other programs to run a timeslice), it can delay the processing of the queued messages for other programs.
So the first thing to check is that there are no "misbehaved" programs running at the same time as yours.
I don't see anything in the code that you posted that would inherently cause an occasional stutter. That being said, check the Update and Invalidate functions for problems.
If these methods do what their name indicates, Update updates the screen and invalidate invalidates the portion of the screen that needs to be updated so Windows will redraw it. You should probably move the call to Invalidate inside the Update method. Update should first check to see if the screen actually needs to be updated and return immediately if not. It should update the screen if required then make the call to invalidate at the end before returning.
Last edited by CGKevin; September 22nd, 2012 at 11:18 AM.
September 24th, 2012, 11:50 AM
Re: Game loop stutter
Thank you Kevin, you realy made us think about what our program was doing and how it all worked.
We fixed the stutters, the problem was the draw function and not the loop.
The draw function is now located in the class "View" and instead of "Graphics g = view.CreateGraphics();" we now use "Graphics g = e.Graphics;".
That also means that instead of "Invalidate();", we use "view.Invalidate();".
To make it even better, we put the gameloop in a seperate thread, so we dont have to use "Application.DoEvents();".
Click Here to Expand Forum to Full Width