Thread: why the hell does this little bit of code make a difference? (windows message loop)

  1. #1
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753

    why the hell does this little bit of code make a difference? (windows message loop)

    Here is my now working message loop, thanks to Hunter2's old space shooter game. I am using PeekMessage, and his little nifty Sleep() thingy which I don't understand:
    Code:
    while(!done)
    	{
    		DWORD start, timeElapsed;
    		start = GetTickCount();
    
    		while(PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE))
    		{
    			TranslateMessage(&Msg);
    			DispatchMessage(&Msg);
    		}
    
    		MainFunc(hWnd);
    
    		timeElapsed = GetTickCount() - start;
    		if(timeElapsed < FRAME_DELAY)
    			Sleep(FRAME_DELAY - timeElapsed);
    	}
    if I don't have that if-statement in there, the timeElapsed one, it doesn't work good. if i keep it in it works great. could anyone explain what exactly that is doing?

    lol im sure you could, Hunter

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    its trying to keep your frame rate reasonably constant. It basically says if you havent got to your frame delay setting then wait until you do.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Just starting Windows Programming, School me!
    By Shamino in forum Windows Programming
    Replies: 17
    Last Post: 02-22-2008, 08:14 AM
  2. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM
  3. OpenGL and Windows
    By sean345 in forum Game Programming
    Replies: 5
    Last Post: 06-24-2002, 10:14 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. opengl code not working
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 02-14-2002, 10:01 PM