4 Replies - 670 Views - Last Post: 30 June 2012 - 10:43 AM Rate Topic: -----

#1 Mr_Fraggs  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 89
  • Joined: 17-June 12

Quick Main Loop question

Posted 30 June 2012 - 12:30 AM

Hey everyone, I just have a quick (possibly dumb) question about a games main loop.

Now, i'm using DirectX and C++ here, but I don't think it really matters.

My main loop right now looks like this:

//main loop for game
void Game::gameLoop()
{
	detectInput();
	Game::renderFrame();
	return;
}


As you can see, the function is my main loop. It doesn't really do much yet, but i'm setting up my framework before I start with gameplay.

My question is, when I go to set up a frame rate limiter, do I want to limit the entire game loop, or just the renderFrame() function? All my render frame does is just that, renders a single frame. Would things like scanning for input and updating AI and all that want to be limited to 60 fps? I assume not, but I'm still slightly new to things, so I'd rather get better advice other than my own assumptions. Thanks!

Is This A Good Question/Topic? 1
  • +

Replies To: Quick Main Loop question

#2 v0rtex  Icon User is offline

  • Caffeine: db "Never Enough!"
  • member icon

Reputation: 223
  • View blog
  • Posts: 773
  • Joined: 02-June 10

Re: Quick Main Loop question

Posted 30 June 2012 - 01:34 AM

I think it is normal practice to only limit drawing as it would make little sense if you delayed input, what then if the user is trying to control a sprite and you delayed input so that his current input was not handled correctly or exhibited input lag! So I reckon you should only limit your renderFrame function.

Here is an example of a game loop I used long ago (not very abstracted but its easy to understand):


   Game.DInput_Update();

   Game.Collision_Detection();

   Game.Update_enemyEntities();
   //keep the game running at a steady frame rate
    if (GetTickCount() - start >= 10)
    {
        //reset timing
        start = GetTickCount();

		//update the scrolling view
		Game.UpdateScrollPosition();

		//start rendering
		if (d3ddev->BeginScene())
		{
			
			//draw tiles onto the scroll buffer
			DrawTiles();

			//draw the scroll window onto the back buffer
			DrawScrollWindow();


            spriteobj->Begin(D3DXSPRITE_ALPHABLEND);
            //render spaceship
            Draw_Sprite(spaceship,SCREENW/2,SCREENH/2,frame,64,64,2);
   
            spriteobj->End();

			//stop rendering
			d3ddev->EndScene();
            d3ddev->Present(NULL, NULL, NULL, NULL);
		}
	}

    //to exit 
    if (Key_Down(DIK_ESCAPE))
        gameover = true;

}



Note that there is more to an engine than just rendering a frame and getting input. Also for example remember to check for Collision Detection although that can be done in your detectInput() function

Hope this helps!

This post has been edited by v0rtex: 30 June 2012 - 02:08 AM

Was This Post Helpful? 1
  • +
  • -

#3 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Quick Main Loop question

Posted 30 June 2012 - 07:32 AM

Generally, never bind other subsystems to framerate. As already pointed out you are asking for trouble if the framerate lags.

What you should be doing is having your main loop either constrained to a high number of ticks per second like maybe 100 and have your graphics and other systems reference this global clock to determine when 1/60th of a second has elapsed for instance.

It's the technique I generally use.
Was This Post Helpful? 2
  • +
  • -

#4 Mr_Fraggs  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 89
  • Joined: 17-June 12

Re: Quick Main Loop question

Posted 30 June 2012 - 10:27 AM

Sweet that's what I was thinking. Thanks guys, appreciate the help!
Was This Post Helpful? 0
  • +
  • -

#5 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Quick Main Loop question

Posted 30 June 2012 - 10:43 AM

No problem.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1