Hey guys, I'm new here. I just started learning c++ and I'm having a few problems. I have looked for one online but I found none that really worked for me so I tried to make my own. I'm trying to make a constant frame rate at 30 frames per second. I have a code working and it does stay at 30 frames and all but then when you move the window or resize the window, it starts to lag and it drops and sometimes goes all the way down to 1 for me. Here is the code that handles the main loop(located in my WinMain of course):

MSG msg;
	while(running)
	{
		while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) == true)
		{
			if (msg.message == WM_QUIT)
			{
				running=false;
				break;
			}
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		if (dwFrames<30)
		{
			dwFrames+=1;
		}
		dwCurr = GetTickCount();
		dwElapse = dwCurr - dwLast;
		if (dwElapse >= 1000)
		{
			dwFPS = dwFrames;
			dwLast = dwCurr;
			dwFrames = 0;
		}
		InvalidateRect(hWnd, 0, TRUE);
		Sleep(32);
	}
    return (int) msg.wParam;
}

And this is where the fps is drawn:

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    PAINTSTRUCT ps;
    HDC hdc;
    TCHAR greeting[] = _T("Hello, World!");

    switch (message)
    {
    case WM_PAINT:
        hdc = BeginPaint(hWnd, &ps);
        TextOut(hdc,
            5, 5,
            greeting, _tcslen(greeting));
	TCHAR buff[100];
	TextOut(hdc,5,25,buff,wsprintf(buff,L"%d",dwFPS));               //FPS Drawn here

        EndPaint(hWnd, &ps);
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
        break;
    }

    return 0;
}

Here is the link for people to test out the exe: http://host-a.net/DaSpirit/Frame%20Rate.exe
For the .cpp file, download it here: http://host-a.net/DaSpirit/frame%20rate%20main.cpp
I would appreciate anyone helping me improve the code possibly fix the problems. And yes, I know about Delta Timing and such but I don't want to use that as it makes things harder for me. I just need a steady frame rate of 30 frames per second with no lag whatsoever. Thanks!

Sorry, solved my problem on my own. It wasn't lagging. I ran the program a couple of times and noticed that when you move or resize the window, the program sort of like freezes. When it freezes, it isn't doing the main loop, therefore GetTickCount() finds the count after it freezes, which makes the program think it's lagging. Oh well, hopefully this will help someone with the same problem on day!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.