a question......about glutIdleFunc......

Hi everyone. i am trying to write a small game using opengl.
now i have a function called “void updateFrame(void)”.
it should be called 5 times per second.
how can i call it every 200 ms exactly?

in the function main, i wrote:

int main(int argc, char ** argv) {

glutIdleFunc(callupdate); // when os is idle, function callupdate is called…

glutDisplayFunc(updateframe);

return 0;
}

and in the function callupdate, i wrote:

void callupdate(void) {
static long systemtime=getSystemTime();
// static variable
long currenttime=getSystemTime();
while (currenttime-systemtime<200) {
// function updateframe should be called later
Sleep(currenttime-systemtime);
}
systemtime=getSystemTime(); // update static variable
updateframe();
return;
}

Yes, it does work. but, is it a good solution?
and are there any other methods which are better?

PS. i hope that i have expressed myself clearly.
i am not very good at english :frowning:

i don’t see where your opengl problems are
this is no c forum

and by the way do you want to run your game at 5fps???

p.s.:i hate c/c++

[This message has been edited by satan (edited 02-25-2002).]

hello satan.

yes this program works. but i wonder whether there is a better solution.

5fps is only an example. Perhaps 10 fps or 20 fps is better. I just want to know how to make a function called at regular intervals in opengl. i am new to opengl, and some functions in the lib glut are special to me.

anyway, thank you for your reply. :slight_smile:

PS. I have a good friend. I do not like his girlfriend, but i do not care that he talks about her. :slight_smile:

Calling a function with regular intervals in OpenGL is as easy (or hard, depending on experience ) as calling any other function with regular intervals. The problem itself has nothing to do with OpenGL.

Anyways, with GLUT, you can use the timer function. It can trigger a callback after a given time.

First, in your main function, or wherever you set you callback functions.

glutTimerFunc(200, timerCallback, 123);

200 is 200 ms untill next callback to timerCallback.
123 is a number that identifies that particular callback. If you only have one timer going at any given time to that callback function, don’t care about it.

Then, then callback can look like this.

void timerCallback(int value)
{
glutPostRedisplay();
glutTimerFunc(200, timerCallback, 123);
}

Post a redisplay message, and start a new timer to trigger another callback in 200 milliseconds.

thank you!
This is much better than what i have done.

You don’t want to use glutidlefunc, since any keyboard or mouse movent will keep it from being call. Thus the name idle function! This call is used for say if a player has stopped moving for some amount of time the mouse or keyboard, make stupid faces at him…

You need to use the glutTimerFunc( time in milli seconds, routine to call, int number)

glutTimerfunc( 10, My_timer_event, 1);
glutMainLoop();

static void My_timer_event(int te)
{
/* update player and other objects pos.

glutTimerfunc( 10, My_timer_event, 1); //Timer is a one shot have to restart it!!! important you have this…
glutPostredisplay();
}

Originally posted by pipiloo:
[b]Hi everyone. i am trying to write a small game using opengl.
now i have a function called “void updateFrame(void)”.
it should be called 5 times per second.
how can i call it every 200 ms exactly?

in the function main, i wrote:

int main(int argc, char ** argv) {

glutIdleFunc(callupdate); // when os is idle, function callupdate is called…

glutDisplayFunc(updateframe);

return 0;
}

and in the function callupdate, i wrote:

void callupdate(void) {
static long systemtime=getSystemTime();
// static variable
long currenttime=getSystemTime();
while (currenttime-systemtime<200) {
// function updateframe should be called later
Sleep(currenttime-systemtime);
}
systemtime=getSystemTime(); // update static variable
updateframe();
return;
}

Yes, it does work. but, is it a good solution?
and are there any other methods which are better?

PS. i hope that i have expressed myself clearly.
i am not very good at english :-([/b]

[This message has been edited by nexusone (edited 02-26-2002).]

[This message has been edited by nexusone (edited 02-26-2002).]

Aha!

Now i know not only how to update frames at regular intervals, but also how to make a stupid face correctly.:slight_smile:

thank you.

Like this = “:” + “)”

= “:”+“(”

Of course you can always do it this way also :slight_smile:

Originally posted by pipiloo:
[b]Aha!

Now i know not only how to update frames at regular intervals, but also how to make a stupid face correctly.:slight_smile:

thank you.[/b]

[This message has been edited by nexusone (edited 02-26-2002).]

[This message has been edited by nexusone (edited 02-26-2002).]