I'm trying to clamp my game loop to a specific FPS by employing gettimeofday. It's a very rudimentary game, so it keeps chewing up all my processing power.
Regardless of how low I set my FRAMES_PER_SECOND, it continues to try to run as fast as possible.
I got a pretty good handle on what deWiTTERS has to say about game loops, but am using gettimeofday instead of GetTickCount b/c I'm on a Mac.
Also, I'm running OSX and using C++, GLUT.
This is what my main looks like:
int main (int argc, char **argv)
{
while (true)
{
timeval t1, t2;
double elapsedTime;
gettimeofday(&t1, NULL); // start timer
const int FRAMES_PER_SECOND = 30;
const int SKIP_TICKS = 1000 / FRAMES_PER_SECOND;
double next_game_tick = elapsedTime;
int sleep_time = 0;
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize (windowWidth, windowHeight);
glutInitWindowPosition (100, 100);
glutCreateWindow ("A basic OpenGL Window");
glutDisplayFunc (display);
glutIdleFunc (idle);
glutReshapeFunc (reshape);
glutPassiveMotionFunc(mouseMovement); //check for mouse movement
glutMouseFunc(buttonPress); //check for button press
gettimeofday(&t2, NULL); // stop timer after one full loop
elapsedTime = (t2.tv_sec - t1.tv_sec) * 1000.0; // compute sec to ms
elapsedTime += (t2.tv_usec - t1.tv_usec) / 1000.0; // compute us to ms
next_game_tick += SKIP_TICKS;
sleep_time = next_game_tick - elapsedTime;
if( sleep_time >= 0 )
{
sleep( sleep_time );
}
glutMainLoop ();
}
}
I've tried placing my gettimeofday and sleep functions in multiple locations, but I can't seem to find the sweet spot for them (given that my code is even correct).
That will only ever get called once. You need to put the FPS logic inside the display function I believe because glutMainLoop will never return. (which also means your while loop is not needed. )
edit: or more likely it should go inside your idle function. It has been awhile since I have used glut.
glutMainLoop():
glutMainLoop enters the GLUT event processing loop. This routine should be called at most once in a GLUT program. Once called, this routine will never return. It will call as necessary any callbacks that have been registered.
At the point where you initialise next_game_tick from elapsed_time you haven't yet initialised elapsed_time.
Related
I have a real robot that is ordering my virtual robot in open gl. I want show every movement of my master robot(real robot) in slave (virtual one in open gl) online, so i need to update my glut window continuously, actually as long as real robot moves my virtual one moves too, and all these movement should be online.
I get data from master always with get data function, but I dont know how I should update the window.
Here is my code:
********************************************/
void OnIdle(void){
initSocket();
printf("\n Defining Step Time Parameters and Initial Conditions for solving Dynamic equations\n");
xi=0;
xf=0.1;
printf("\n end value x : %f ",xf);
i=0; yi[i]=0;
i++;yi[i]=-1.570796;
i++;yi[i]=-1.570796;
i++;yi[i]=0;
i++;yi[i]=0;
i++;yi[i]=0;
ndata=2; fi=1;
double counter=0.1;
Eqdifp(v1,v2,v3,v4,v5,v6,xi,xf,yi,ndata,p,fi);
for(int i=0;i<50;i++)
//while(1)
{
getData();
printf("\n");
for(int i=0;i<6; i++)
{
printf("%d = %.3f\n", i,drecvbuf[i]);
}
printf("\n");
yi[0]=v1[ndata];
yi[1]=v2[ndata];
yi[2]=v3[ndata];
yi[3]=v4[ndata];
yi[4]=v5[ndata];
yi[5]=v6[ndata];
printf("my nadata %f\n",v1[ndata]);
counter=counter+0.1;
Eqdifp(v1,v2,v3,v4,v5,v6,xi,xf,yi,ndata,p,fi);
glutPostRedisplay();
}
}
/////////////////////////////////////////////////////
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutInitWindowSize(900,500);
int u=glutCreateWindow("3DOF robot");
myinit();
createMenu();
glutIdleFunc (OnIdle);
glutDisplayFunc(Display);
glutReshapeFunc(reshape);
glutKeyboardFunc(KeyDown);
glutMainLoop();
System::Timers::Timer^ aTimer = gcnew System::Timers::Timer( 100 );
// Hook up the Elapsed event for the timer.
aTimer->Elapsed += gcnew System::Timers::ElapsedEventHandler( OnTimedEvent );
// Set the Interval to 2 seconds (2000 milliseconds).
aTimer->Enabled = true;
return 0;
}
You can call glutPostRedisplay after the update, which schedules the window to be redrawn (using GLUT's display func, of course) as soon as it returns to the message queue, I think.
But this won't work if you are continously polling the robot data in an infinite loop as this continously blocks the program. What you should do is use a timer to schedule the robot update in short intervals, so that between these updates the program can return to the main event loop and redraw the window. Or you can call some function, which tells the framework to visit the event loop. Your code sample doesn't really explain how you do it at the moment (or I'm just not familiar with the functions you call).
GLUT offers you a idle callback (void (*)(void) signature), set through glutIdleFunc. Retrieve the robot input data in the idle handler. Or use a separate thread polling the data, filling data structures; use a semaphore to unlock idle after new data arrived, use a locking with timeout so that your program remains interactive. Pseudocode:
Semaphore robot_data_semaphore;
void wait_for_data(void)
{
SemaphoreLockStatus lock_status =
semaphore_raise_timeout(robot_data_semaphore, RobotDataTimeout);
if( lock_status == SEMAPHORE_RAISED ) {
update_scene_with_robot_data();
semaphore_lower(robot_data_semaphore);
glutPostRedisplay();
}
}
void main(int argc, char *argv[])
{
/* ... */
semaphore_init(robot_data_semaphore);
Thread thread_robot_data_poller = thread_create(robot_data_poller);
glutIdleFunc(wait_for_data);
/* ... */
thread_start(thread_robot_data_poller);
glutMainLoop();
}
I would do the following. Treat glutMainLoop() as your loop and every time you process one getData() you draw it, it will be faster than you think.
What needs to happen for you to get a 'continuous' update is to:
Process data (getData() then your calculations)
Redraw (Display() glut calls this every time it loops)
Other functions defined using glut_____Func()
Back to 1
Glut keeps going until the program is exited.
//called every time glutMainLoop
//do data processing
void OnIdle(void)
{
getData();
printf("\n");
for(int i=0;i<6; i++)
{
printf("%d = %.3f\n", i,drecvbuf[i]);
}
printf("\n");
yi[0]=v1[ndata];
yi[1]=v2[ndata];
yi[2]=v3[ndata];
yi[3]=v4[ndata];
yi[4]=v5[ndata];
yi[5]=v6[ndata];
printf("my nadata %f\n",v1[ndata]);
Eqdifp(v1,v2,v3,v4,v5,v6,xi,xf,yi,ndata,p,fi);
}
//also called every loop of glutMainLoop
void Display()
{
...
//Your previous Display() function just add this:
glutPostRedisplay(); //everytime you are done
// drawing you put it on the screen
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutInitWindowSize(900,500);
int u=glutCreateWindow("3DOF robot");
myinit();
createMenu();
glutIdleFunc (OnIdle);
glutDisplayFunc(Display);
glutReshapeFunc(reshape);
glutKeyboardFunc(KeyDown);
///////////////
// SETUP YOUR INITIAL DATA
System::Timers::Timer^ aTimer = gcnew System::Timers::Timer( 100 );
// Hook up the Elapsed event for the timer.
aTimer->Elapsed += gcnew System::Timers::ElapsedEventHandler( OnTimedEvent );
// Set the Interval to 2 seconds (2000 milliseconds).
aTimer->Enabled = true;
initSocket();
printf("\n Defining Step Time Parameters and Initial Conditions for solving Dynamic equations\n");
xi=0;
xf=0.1;
printf("\n end value x : %f ",xf);
i=0; yi[i]=0;
i++;yi[i]=-1.570796;
i++;yi[i]=-1.570796;
i++;yi[i]=0;
i++;yi[i]=0;
i++;yi[i]=0;
ndata=2; fi=1;
Eqdifp(v1,v2,v3,v4,v5,v6,xi,xf,yi,ndata,p,fi);
//////////////
//Start the Main Loop
glutMainLoop(); //This statement blocks, meaning that until you exit the
// glut main loop no statments past this point will be executed.
return 0;
}
I've tried some OpenGL C++ training.
But I have a logic problem, how can I update my OpenGL Windows window.
It should draw text one, then delay 1-2sec, then draw text 2, but now it draws same time. Can anyone help or give a hint.
void text () {
wait(1);
Sleep(1000);
std::string text_one;
text_one = "Text 1";
glColor3f(1,01, 0);
drawText(text_one.data(), text_one.size(), 050, 150);
glutPostRedisplay();
wait (1)
std::string text_two;
text_two = "Text 2";
glColor3f(1,0, 0);
drawText(text_two.data(), text_two.size(), 250, 150);
}
and here the main
int main(int argc, char **argv) {
// init GLUT and create Window
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(640,640);
glutCreateWindow("Test 001");
// register callbacks
glutDisplayFunc(renderScene);
glutIdleFunc(text);
// enter GLUT event processing cycle
glutMainLoop();
return 1;
}
You should render in renderScene callback. It will be called automatically in you screen refresh rate. If you want some delay you need to implement it inside this callback (functions called from this callback).
So basically you need to re-render everything every 1/60 second.
If you want to implement easy delay you can do something like this:
void renderScene() {
time += deltaTime;
RenderText1();
if (time > delayTime)
RenderText2();
glutSwapBuffers();
}
I'm making a OpenGL game, and I have problem with optimization. When I start it, it does not respond. If in Update() I just put a for loop and _time += 0.1f, I get a blank screen.
void Update(){
for(; ;){
_time += 0.1f;
Render();
}
}
void Render() {
glClearDepth(1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
_colorProgram.use();
GLuint timeLocation = _colorProgram.getUniformLocation("time");
glUniform1f(timeLocation, _time);
_sprite.Render();
_colorProgram.unuse();
glutSwapBuffers();
}
int main(int argc, char** argv) {
std::printf("OpenGL version is %s",glGetString(GL_VERSION));
// Window
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(520, 200);
glutInitWindowSize(800, 600);
glutInitDisplayMode(GLUT_DOUBLE);
glutCreateWindow("OpenGL [ Shader #1 error pt 3 ]");
// Setup GLEW
if( GLEW_OK != glewInit()){
return 1;
} while( GL_NO_ERROR != glGetError() );
// After creating window
Init();
glutDisplayFunc(Render);
Update();
glutMainLoop();
}
The infinite loop in Update() never lets GLUT pump the event queue.
Use glutTimerFunc() or glutIdleFunc() to call Update() instead. That way execution flow periodically returns to GLUT and GLUT can do what it needs to keep the OS happy.
The proper way to run an animation with GLUT is to use a timer function. This way, GLUT can get back to its main loop, and call your display function. You're not supposed to call the display function directly from your own code.
For example, register a timer function during initialization (the first argument is a time in milliseconds):
glutTimerFunc(10, timerFunc, 0);
Then in the timer function:
void timerFunc(int value) {
_time += 0.1f;
glutPostRedisplay();
glutTimerFunc(10, timerFunc, 0);
}
There are two critical pieces in the code fragment above:
You do not call your Render() function directly. Instead, you call glutPostRedisplay() to tell GLUT that a redisplay is needed. It will then call your Render() function because you registered it as the display function with glutDisplayFunc().
You have to register the timer again. glutTimerFunc() fires the timer only once, not periodically. So you have to re-register it every time it fired.
There is one other problem in your code. You have these calls in your main():
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
...
glutInitDisplayMode(GLUT_DOUBLE);
The flags passed in the second call will override the ones from the first call. While GL_RGBA is the default anyway, you will not get a depth buffer because GL_DEPTH is missing in the second call. You can simply remove the second call, since the first one is most likely what you want.
I am trying to pause my GLUT program during its execution by pressing a key on the keyboard. It seems to not recognize my entry. Here are the relevant sections of my code:
static bool paused = false;
void handleKeypress(unsigned char key, //The key that was pressed
int x, int y) { //The current mouse coordinates
switch (key) {
case 27: //Escape key
exit(0); //Exit the program
case 'p':
paused = !paused;
break;
}
}
int main(int argc, char** argv) {
//Initialize GLUT
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(600, 400); //Set the window size
//Create the window
glutCreateWindow("Fractals in Motion");
initRendering(); //Initialize rendering
//Set handler functions for drawing, keypresses, and window resizes
if(!paused)
{
glutDisplayFunc(drawScene); //drawScene draws everything does the real work
glutTimerFunc(10, update, 0); //Add a timer
}
//glutKeyboardFunc(handleKeypress);
glutReshapeFunc(handleResize);
glutMainLoop(); //Start the main loop. glutMainLoop doesn't return.
return 0; //This line is never reached
}
I actually got the skeleton of this code from this very will written tutorial:
http://www.videotutorialsrock.com/opengl_tutorial/basic_shapes/home.php
However, I cannot seem to get the program to pause when I press the 'p' key. If you have better methods please let me know!
its not working because glutKeyboardFunc(handleKeypress) is commented out for some reason. uncomment and it should work.
Your program runs in two stages:
Initialization
Main loop
Everything before glutMainLoop is initialization, telling GLUT all the different settings and callbacks you want to use. During the main loop, GLUT will call all your callbacks and you can draw.
The problem is that you're setting paused during the main loop and checking it during the initialization. Since initialization always happens before the main loop, setting paused won't actually do anything.
The solution is to not rely on checking paused during initialization, but instead modify your callbacks to immediately return if paused is true.
# include<GL/glut.h>
void keys(unsigned char key, int x, int y)
{
if (key == 'a') paused = 1;
if (key == 'A') paused = 0;
glutPostRedisplay();
}
add this function in your program for keyboard function and wherever u are using glutPostRedisplay() in program
add
if(paused == 0)
above it
I have a real robot that is ordering my virtual robot in open gl. I want show every movement of my master robot(real robot) in slave (virtual one in open gl) online, so i need to update my glut window continuously, actually as long as real robot moves my virtual one moves too, and all these movement should be online.
I get data from master always with get data function, but I dont know how I should update the window.
Here is my code:
********************************************/
void OnIdle(void){
initSocket();
printf("\n Defining Step Time Parameters and Initial Conditions for solving Dynamic equations\n");
xi=0;
xf=0.1;
printf("\n end value x : %f ",xf);
i=0; yi[i]=0;
i++;yi[i]=-1.570796;
i++;yi[i]=-1.570796;
i++;yi[i]=0;
i++;yi[i]=0;
i++;yi[i]=0;
ndata=2; fi=1;
double counter=0.1;
Eqdifp(v1,v2,v3,v4,v5,v6,xi,xf,yi,ndata,p,fi);
for(int i=0;i<50;i++)
//while(1)
{
getData();
printf("\n");
for(int i=0;i<6; i++)
{
printf("%d = %.3f\n", i,drecvbuf[i]);
}
printf("\n");
yi[0]=v1[ndata];
yi[1]=v2[ndata];
yi[2]=v3[ndata];
yi[3]=v4[ndata];
yi[4]=v5[ndata];
yi[5]=v6[ndata];
printf("my nadata %f\n",v1[ndata]);
counter=counter+0.1;
Eqdifp(v1,v2,v3,v4,v5,v6,xi,xf,yi,ndata,p,fi);
glutPostRedisplay();
}
}
/////////////////////////////////////////////////////
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutInitWindowSize(900,500);
int u=glutCreateWindow("3DOF robot");
myinit();
createMenu();
glutIdleFunc (OnIdle);
glutDisplayFunc(Display);
glutReshapeFunc(reshape);
glutKeyboardFunc(KeyDown);
glutMainLoop();
System::Timers::Timer^ aTimer = gcnew System::Timers::Timer( 100 );
// Hook up the Elapsed event for the timer.
aTimer->Elapsed += gcnew System::Timers::ElapsedEventHandler( OnTimedEvent );
// Set the Interval to 2 seconds (2000 milliseconds).
aTimer->Enabled = true;
return 0;
}
You can call glutPostRedisplay after the update, which schedules the window to be redrawn (using GLUT's display func, of course) as soon as it returns to the message queue, I think.
But this won't work if you are continously polling the robot data in an infinite loop as this continously blocks the program. What you should do is use a timer to schedule the robot update in short intervals, so that between these updates the program can return to the main event loop and redraw the window. Or you can call some function, which tells the framework to visit the event loop. Your code sample doesn't really explain how you do it at the moment (or I'm just not familiar with the functions you call).
GLUT offers you a idle callback (void (*)(void) signature), set through glutIdleFunc. Retrieve the robot input data in the idle handler. Or use a separate thread polling the data, filling data structures; use a semaphore to unlock idle after new data arrived, use a locking with timeout so that your program remains interactive. Pseudocode:
Semaphore robot_data_semaphore;
void wait_for_data(void)
{
SemaphoreLockStatus lock_status =
semaphore_raise_timeout(robot_data_semaphore, RobotDataTimeout);
if( lock_status == SEMAPHORE_RAISED ) {
update_scene_with_robot_data();
semaphore_lower(robot_data_semaphore);
glutPostRedisplay();
}
}
void main(int argc, char *argv[])
{
/* ... */
semaphore_init(robot_data_semaphore);
Thread thread_robot_data_poller = thread_create(robot_data_poller);
glutIdleFunc(wait_for_data);
/* ... */
thread_start(thread_robot_data_poller);
glutMainLoop();
}
I would do the following. Treat glutMainLoop() as your loop and every time you process one getData() you draw it, it will be faster than you think.
What needs to happen for you to get a 'continuous' update is to:
Process data (getData() then your calculations)
Redraw (Display() glut calls this every time it loops)
Other functions defined using glut_____Func()
Back to 1
Glut keeps going until the program is exited.
//called every time glutMainLoop
//do data processing
void OnIdle(void)
{
getData();
printf("\n");
for(int i=0;i<6; i++)
{
printf("%d = %.3f\n", i,drecvbuf[i]);
}
printf("\n");
yi[0]=v1[ndata];
yi[1]=v2[ndata];
yi[2]=v3[ndata];
yi[3]=v4[ndata];
yi[4]=v5[ndata];
yi[5]=v6[ndata];
printf("my nadata %f\n",v1[ndata]);
Eqdifp(v1,v2,v3,v4,v5,v6,xi,xf,yi,ndata,p,fi);
}
//also called every loop of glutMainLoop
void Display()
{
...
//Your previous Display() function just add this:
glutPostRedisplay(); //everytime you are done
// drawing you put it on the screen
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutInitWindowSize(900,500);
int u=glutCreateWindow("3DOF robot");
myinit();
createMenu();
glutIdleFunc (OnIdle);
glutDisplayFunc(Display);
glutReshapeFunc(reshape);
glutKeyboardFunc(KeyDown);
///////////////
// SETUP YOUR INITIAL DATA
System::Timers::Timer^ aTimer = gcnew System::Timers::Timer( 100 );
// Hook up the Elapsed event for the timer.
aTimer->Elapsed += gcnew System::Timers::ElapsedEventHandler( OnTimedEvent );
// Set the Interval to 2 seconds (2000 milliseconds).
aTimer->Enabled = true;
initSocket();
printf("\n Defining Step Time Parameters and Initial Conditions for solving Dynamic equations\n");
xi=0;
xf=0.1;
printf("\n end value x : %f ",xf);
i=0; yi[i]=0;
i++;yi[i]=-1.570796;
i++;yi[i]=-1.570796;
i++;yi[i]=0;
i++;yi[i]=0;
i++;yi[i]=0;
ndata=2; fi=1;
Eqdifp(v1,v2,v3,v4,v5,v6,xi,xf,yi,ndata,p,fi);
//////////////
//Start the Main Loop
glutMainLoop(); //This statement blocks, meaning that until you exit the
// glut main loop no statments past this point will be executed.
return 0;
}