CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Hybrid View

  1. #1
    Join Date
    Sep 2012
    Posts
    2

    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:
    Code:
    while (this.Created)
    {
    	Application.DoEvents();
    	Update();
    	Invalidate();
    	long currentMS = stopwatch.ElapsedMilliseconds;
    	while (stopwatch.ElapsedMilliseconds - currentMS < 16)
    	{
    		System.Threading.Thread.Sleep(1);
     	}
    }
    And here is the paint method:
    Code:
    protected override void OnPaint(PaintEventArgs e)
    {
    	Graphics g = view.CreateGraphics();
    	g.Clear(Color.Black);
    	g.FillRectangle(Brushes.White, player.hitbox);
    }
    Does anyone have any idea what could cause the stutters?

  2. #2
    Join Date
    Jul 2012
    Posts
    90

    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 10:18 AM.

  3. #3
    Join Date
    Sep 2012
    Posts
    2

    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();".

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured