Game loop

A place for users of OGRE to discuss ideas and experiences of utilitising OGRE in their games / demos / applications.
Ephon
Gnoblar
Posts: 18
Joined: Tue Jan 18, 2005 8:33 pm

Game loop

Post by Ephon »

Just wonderering:

Does the order in which things get done matter when it comes to the game loop?

Take for instance:

Code: Select all

while(!shutdown)
{
    DoInput()
    DoGameLogic()
    DoRender()
}
versus

Code: Select all

while(!shutdown)
{
    DoRender()
    DoGameLogic()
    DoInput()
}
Does it matter that rendering comes before inputhandling?

And must the framelistener notification(framestarted/frameended) be like this:

Code: Select all

while(!shutdown)
{
    Root::_frameStarted()
    DoRender()
    DoGameLogic()
    DoInput()
    Root::_frameEnded()
}
or is it just as good to put the framelistener notification inside DoRender():

Code: Select all

DoRender()
{
    Root::renderOneFrame()
}
All this is with regard to timing issues and game experience etc.
User avatar
Emmeran
Goblin
Posts: 272
Joined: Wed Jun 02, 2004 11:47 am
Location: Erlangen

Post by Emmeran »

ok... let's see...

as long as u don't use global variables u changed in one of the other functions u should be able to order the functions as u like.

the frameStarted() event will be fired automatically by Root::renderOneFrame()

--
i don't know if u knew but u can create an instance of framelistener like this:

Code: Select all

class someClass : public Ogre::FrameListener
{
     bool frameStarted(FrameEvent &evt);
     bool frameEnded(FrameEvent &evt);
};

bool someClass::frameStarted(FrameEvent &evt)
{
     //do what u like to be done before rendering
     return true;
}

bool someClass::frameEnded(FrameEvent &evt)
{
     //do what u like to be done after rendering
     return true;
}
edit:
the function frameStarted and frameEnded will be called automatically, if u have registered the framelistener with Ogre. I think u have to do something like this:

Code: Select all

myPointerToSomeClass = new someClass();
Root::registerFrameListener(myPointerToSomeClass);
look in the api if i'm wrong

good luck,
Emmeran
User avatar
jacmoe
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 20570
Joined: Thu Jan 22, 2004 10:13 am
Location: Denmark
x 179

Post by jacmoe »

Generally, you don't have to worry about Ogre doing it's magic - after Root::startRendering() it happens automatically.
For your own stuff, you can use framelisteners to execute code on a frame by frame basis.
That can be input, AI, etc.

Forget about renderOneFrame(), etc. The only time when you want to update the renderloop manually, is when writing gui tools (MFC for instance).

Feel free to search around for more on this subject - there's a lot on that in the Ogre fora. :)
/* Less noise. More signal. */
Ogitor Scenebuilder - powered by Ogre, presented by Qt, fueled by Passion.
OgreAddons - the Ogre code suppository.
alphabeta
Kobold
Posts: 34
Joined: Mon Jan 03, 2005 5:07 am

Post by alphabeta »

[quote="jacmoe"]Forget about renderOneFrame(), etc. The only time when you want to update the renderloop manually, is when writing gui tools (MFC for instance).quote]

That's not entirely true, if you're writing a framework independant of Ogre you'll need to use renderOneFrame() in your own loop
User avatar
Kristian
Hobgoblin
Posts: 542
Joined: Sun Jan 12, 2003 7:35 pm
Location: Copenhagen, Denmark

Post by Kristian »

I take it that you dont want to use the Ogre::Root::startRendering and Ogre::Framelistener's and manually make your own game loop ?!?
In that case it would be most correct to render something after you've done your logic and simulating of the world it would be more natural to render what you have in the current step/"world state" and not the previous step. So yes this means that you will have a loop that loops somethink like this:

void loop () {

while(1) {
handleInput();
doGameLogic();
doRenderWorld();
}
}
Kristian, crying out loud: "If it works. Don't fix it!" :p
HCA
HCA2
GuppyLife
User avatar
tuan kuranes
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 2653
Joined: Wed Sep 24, 2003 8:07 am
Location: Haute Garonne, France
x 4

Post by tuan kuranes »

Here's an interesting thread about this :
http://groups-beta.google.com/group/com ... b5a4bb9589