Jump to content


Main Game Loop


  • You cannot reply to this topic
No replies to this topic

#1 marek-knows.com

    Valued Member

  • Members
  • PipPipPip
  • 189 posts
  • LocationOntario, Canada

Posted 08 August 2008 - 10:51 AM

I've noticed some problems in my game when I have motion on the screen. For instance if I have a space ship flying at a constant speed across the screen, every now and again (every 10-15 seconds) my ship jumps a little FORWARD. My main game loop looks something like this:

     while (!m_bQuit) {

      		// get all messages before rendering a frame

      		if (PeekMessage(&m_Message, NULL, 0, 0, PM_REMOVE)) {

      			if (WM_QUIT == m_Message.message) {

      				m_bQuit = true;

      			}

      			else {

      				TranslateMessage(&m_Message);

      				DispatchMessage(&m_Message);

      			}

      		}

      		else  {

      			//do something in this frame

      			frame();

      		}

      	}



and the frame function

void GameOGL::frame() {


	//update game time

	m_dTimeGame += getDeltaTime();

	

	//physics frame rate (1000 Hz)

	if ((m_dTimeGame - m_dTimeLastPhysics) >= 0.001 ) {


		//Handle keyboard commands

		handleKeyboard();


		countFpsPhysics();


		m_dTimeLastPhysics += m_dPhysicStep;

		if( (m_dTimeGame - m_dTimeLastPhysics) > 0.5) {

			//prevent large jumps

			m_dTimeLastPhysics = m_dTimeGame;

		}

	}


	//render only at desired frame rate 60 Hz

	if ((m_dTimeGame - m_dTimeLastRender) >= 0.016 ) {


		//set background color

		glClearColor(1.0f, 1.0f, 1.0f, 1.0f);


		//clear screen

		if (m_bZBuffer) {

			glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

		}

		else {

			glClear( GL_COLOR_BUFFER_BIT );

		}


		//3D perspective

		setProjMatrix();


		m_pScene->render();


		countFpsRender();


		m_dTimeLastRender = m_dTimeGame;

	}


	return;


} //frame


When my program hiccups I see that my graphics rendering frame rate drops slightly.

Can anyone explain what is going on? How can I update my game loop so that I don't see any hickups in smooth motion across the screen?
3D OpenGL, C++ Game Development Video Tutorials @
www.marek-knows.com





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users