Skip to main content
GameDev.net gamedev.net
🔒 Locked

MFC Gameloop

Started by BrainlessDesigns Sep 1, 2008 at 7:41 AM 1 replies 800+ views
Original Post
BrainlessDesigns
BrainlessDesigns
Hi there! Anybody out there who made a MFC gameloop. My games jerks every few milliseconds / seconds... Here is the code of the application RUN() method: //////////////////////////////////////////////////////////////////////////// int cl_BdGameApp::Run() { MSG msg; // message while(1) { if(m_shutDown) { // user wants to end the game or an error occured ExitInstance(); return 0; } if(PeekMessage(&msg,NULL,0,0,PM_NOREMOVE)) { if(GetMessage(&msg,NULL,0,0) == FALSE) { // there was a WM_QUIT message ExitInstance(); return msg.wParam; } // WM_KEYUP if(msg.message == WM_KEYUP && (msg.wParam == VK_ESCAPE || msg.wParam == VK_F4)) { switch(msg.wParam) { case VK_ESCAPE: // quit app PostQuitMessage(0); break; case VK_F4: // toggle fullscreen m_pGame->ToggleFullscreen(); break; } } else { TranslateMessage(&msg); DispatchMessage(&msg); } } else { if(m_pGameWnd->IsActive()) { m_pGame->Activate(); if(m_pGameWnd->MustRedraw()) { m_pGame->Restore(); // we've to redraw the screen - in cause // of resizing, inactivity... m_pGameWnd->SetRedrawScreen(false); } // draw next frame of the game float timeDiff = m_pGame->GetTimer()->GetFrameTime(); if(!m_pGame->UpdateFrame( timeDiff )) AfxGetMainWnd()->PostMessage(WM_CLOSE,0,0); m_fps = 1.0 / timeDiff; #ifdef _DEBUG TRACE1("m_fps: %d\n",m_fps); #endif } else { m_pGame->Pause(); WaitMessage(); } } } } ////////////////////////////////////////////////////////////////////////////// Anybody an ides what's wrong with it?! Thx 4 ur answers!
__BR4iNL355 D35iGN5__
ChrisPepper1989
ChrisPepper1989
Hi!

ive got a idea why it could be jerking, could you do me a favor though and repost the code with
 ...code ..
?....
************************
EDIT, i didnt really think that through did i...lol
putting the source tags in there, made the code thing happen, oops just check the code bit of:
http://www.gamedev.net/community/forums/faq.asp#tags

im going to leave my little bit of stupidity in above as it amuses me :-)
************************
....it'le just make it easier for me to read and help :)!

Also can you show me a bit of the code within m_pGame->UpdateFrame( timeDiff )?

The two main areas i think could be causing your jerk is either not double buffering properly or if you are only getting this problem in debug then i guarantee its
#ifdef _DEBUG
TRACE1("m_fps: %d\n",m_fps);
#endif
as nearly all trace functions (although ive never used this one) in all languages are horribly horribly slow! although its nice to have FPS count, doing it this way will almost definitely be killing your FPS in debug mode, take it out and see if it improves, if not check/post your draw code, are you using MFC for your drawing aswel?

good luck, chris
BrainlessDesigns
BrainlessDesigns
Hi ChrisPepper1989!

Thanks for your reply. I rendering a fps counter in release mode too. For sure is the TRACE macro slowing down. But I am talking about the release version.

Here the better readable code - btw thx for the advise and the link for formating
code:

///////////////////////////////////////////////////////////////////////////////// Run()		int cl_BdGameApp::Run()		{			MSG msg; // message			while(1)			{				if(m_shutDown)				{					// user wants to end the game or an error occured					ExitInstance();					return 0;				}				if(PeekMessage(&msg,NULL,0,0,PM_NOREMOVE))				{					if(GetMessage(&msg,NULL,0,0) == FALSE)					{						// there was a WM_QUIT message						ExitInstance();						return msg.wParam;					}					// WM_KEYUP					if(msg.message == WM_KEYUP &&					   (msg.wParam == VK_ESCAPE || msg.wParam == VK_F4))					{						switch(msg.wParam)						{							case VK_ESCAPE:								// quit app								PostQuitMessage(0);								break;							case VK_F4:								// toggle fullscreen								m_pGame->ToggleFullscreen();								break;						}					}					else					{						TranslateMessage(&msg);						DispatchMessage(&msg);					}				}				else				{					if(m_pGameWnd->IsActive())					{						m_pGame->Activate();						if(m_pGameWnd->MustRedraw())						{							m_pGame->Restore();							// we've to redraw the screen - in cause							// of resizing, inactivity...							m_pGameWnd->SetRedrawScreen(false);						}						// draw next frame of the game						float timeDiff = m_pGame->GetTimer()->GetFrameTime();						if(!m_pGame->UpdateFrame( timeDiff ))							AfxGetMainWnd()->PostMessage(WM_CLOSE,0,0);						m_fps = 1.0 / timeDiff;#ifdef _DEBUG						TRACE1("m_fps: %d\n",m_fps);#endif					}					else					{						m_pGame->Pause();						WaitMessage();					}				}			}		}


and here the update frame stuff:

///////////////////////////////////////////////////////////////////////////////// UpdateFrame()bool cl_JunkFoodAttack::UpdateFrame( const float timeDiff ){	if(!cl_BdGame::UpdateFrame( timeDiff ))		return false;	// clear backbuffer with black color	m_pDD->ClearBackBuffer();	switch(m_gameState)	{	case GS_INIT_MAINMENU:		// release old module		if(m_pActiveModule)			delete m_pActiveModule;		// build main menu module		m_pActiveModule = new cl_MainMenu(this);		if(!m_pActiveModule)			return false;		if(!m_pActiveModule->InitInstance())			return false;		m_gameState = GS_MAINMENU;		break;	case GS_INIT_GAME:		if(m_pActiveModule)			delete m_pActiveModule;		m_pActiveModule = new cl_InGame(this);		if(!m_pActiveModule)			return false;		if(!m_pActiveModule->InitInstance())			return false;		m_gameState = GS_INGAME;		break;	case GS_INIT_ENTER_NAME:		// release old module		if(m_pActiveModule)			delete m_pActiveModule;		// build new name entry module		m_pActiveModule = new cl_NameEntry(this);		if(!m_pActiveModule)			return false;		if(!m_pActiveModule->InitInstance())			return false;		m_gameState = GS_ENTER_NAME;		break;	case GS_EXIT_GAME:		return false;	}	if(m_gameState == GS_MAINMENU ||	   m_gameState == GS_INGAME ||	   m_gameState == GS_ENTER_NAME)	{		if(m_pActiveModule)			if(!m_pActiveModule->UpdateFrame( timeDiff ))				return false;	}#ifdef _DEBUG	// render fps	int fps = 1.0 / timeDiff;		cl_BdFont* pFont;	if((pFont = GetFont("FONT_BD")) == NULL)		return false;		char cfps[256];	sprintf(cfps,"FPS: %d",fps);	std::string sfps(cfps);	pFont->DrawAligned(sfps,		cl_BdFont::EN_LEFT,		0);#endif	// flip the shit	return m_pDD->Flip();}


class cl_JunkFoodAttack is derived from cl_Game and UpdateFrame does nothing in cl_Game...

If you've an idea what's jerking the you are my hero ;-)

Thx
BR4iNL355 D35iGN5
__BR4iNL355 D35iGN5__

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.