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