PDA

View Full Version : a question......about glutIdleFunc......



pipiloo
02-25-2002, 01:01 PM
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 :-(

satan
02-25-2002, 02:10 PM
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).]

pipiloo
02-26-2002, 03:23 AM
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. :-)

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

Bob
02-26-2002, 04:04 AM
Calling a function with regular intervals in OpenGL is as easy (or hard, depending on experience http://www.opengl.org/discussion_boards/ubb/biggrin.gif ) 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.

pipiloo
02-26-2002, 07:56 AM
thank you!
This is much better than what i have done.

nexusone
02-26-2002, 09:02 AM
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.... http://www.opengl.org/discussion_boards/ubb/smile.gif

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:
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 :-(

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

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

pipiloo
02-26-2002, 09:50 AM
Aha!

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

thank you.

nexusone
02-26-2002, 03:33 PM
Like this http://www.opengl.org/discussion_boards/ubb/smile.gif = ":" + ")"

http://www.opengl.org/discussion_boards/ubb/frown.gif = ":"+"("

Of course you can always do it this way also :-)


Originally posted by pipiloo:
Aha!

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

thank you.

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

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