   Timing
  Hi! Could anyone help me? I''ve got a problem with timing. . . How can I create a processor-speed independent program, which is running by max. 50 fps, and how may I get the time elapsed since the program start?   RedXIII 
  For FPS cap I would use this:NeHe Lesson 21You would basically get the time before and after the rendering, subtract, then apply a loop to waste cycles.For movement independent speed reguardless of FPS, use this:Timing functions-nehe derived //Global Vars    struct    {  __int64 frequency;  float resolution;  unsigned long mm_timer_start;  unsigned long mm_timer_elapsed;  bool performance_timer;  __int64 performance_timer_start;  __int64 performance_timer_elapsed;    } timer;    float speed_mod=1.0;//Functionsvoid TimerInit(void){memset(&timer, 0, sizeof(timer));if (!QueryPerformanceFrequency((LARGE_INTEGER *) &timer.frequency)){timer.performance_timer= FALSE;timer.mm_timer_start= timeGetTime();timer.resolution= 1.0f/1000.0f;timer.frequency= 1000;timer.mm_timer_elapsed= timer.mm_timer_start;}else{QueryPerformanceCounter((LARGE_INTEGER *) &timer.performance_timer_start);timer.performance_timer = TRUE;timer.resolution = (float)(((double)1.0f)/((double)timer.frequency));timer.performance_timer_elapsed= timer.performance_timer_start;}}float TimerGetTime(){__int64 time;if(timer.performance_timer){QueryPerformanceCounter((LARGE_INTEGER *) &time);return((float)(time-timer.performance_timer_start)*timer.resolution)*1000.0f;}else{return((float)(timeGetTime()-timer.mm_timer_start)*timer.resolution)*1000.0f;}}//Implementation...if(!done){    if (!CreateGLWindow("Name of program here",640,480,16,option[0].fullscreen))    {return 0;    }}TimerInit();...//Windows Loopwhile(!done){if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)){if (msg.message == WM_QUIT){done=TRUE;}else{TranslateMessage(&msg);DispatchMessage(&msg);}}else{    float start=TimerGetTime();if ((active && !DrawScene()) || keys[VK_ESCAPE]){done=TRUE;}else{ Sleep(0);SwapBuffers(hDC);CalculateFrameRate();}speed_mod=(TimerGetTime()-start)/1000.0f;if (keys[VK_F1]){keys[VK_F1]=FALSE;KillGLWindow();option[0].fullscreen=!option[0].fullscreen;if (!CreateGLWindow("Name of program here",640,480,16,option[0].fullscreen)){return 0;}}}}To apply this to movement - do something like this:    for(int loop=0; loop < line_objects; loop++)    {   glPushMatrix(); glLineWidth(5.0); glScalef(0.02,0.02,1); glColor3f(line[loop].r, line[loop].g, line[loop].b); glBegin(GL_LINES);    glVertex3f(line[loop].x_extra,line[loop].y_extra,line[loop].z_extra);    glVertex3f(line[loop].start_x,line[loop].start_y,line[loop].start_z); glEnd();   glPopMatrix();   line[loop].start_x+=line[loop].aug_x*speed_mod;   line[loop].start_y+=line[loop].aug_y*speed_mod;   line[loop].start_z+=line[loop].aug_z*.1*speed_mod;    }    glDisable(GL_BLEND);    glEnable(GL_TEXTURE_2D);   }   break;  Notice the multiplication of speed_mod onto the constant which is added to the variable to obtain the object, in this case the line''s end, new position. ;Yes it works perfectly, although it seems strange because the theory is so simple. ;  Gee, thaks for helping guys. It works as well as I expected. Thanks for your help I was able to fix my program. Best Regards ;  i've always used windows timers to control the render rate,that is set a windows timer to, say, const int GLT_INTERVAL =20;// 1000 msecs / 50 fps = 20msecs per frame - from what i understand, you can't get this timer resolution on win95/98/me, only winnt/xp.and then set up a timer in the initialisation function, like SetTimer(hWnd, GLTIMER, GLT_INTERVAL, NULL);// create the standard timer to send timed event messagesand then just capture and process the events. this leaves windows to distribute the remaining clock-cycles in other applications.this might seem a little much - but i started programming on win3.0  and that was just the best way, but the *only* way!as always, if you want a code sample of this, just ask.   ---craig(at)cod3rz(dot)net   [edited by - craigatcod3rzdotnet on February 13, 2004 8:01:14 AM];  depending on what you are doing that might work, for example for a windowed app like an editor that would make sense, as at least you are getting constant updates, althought hard limiting like that still strikes me as inconsiderate, fi you are going to do it then maybe an option to change it, however overall the generaly ''better'' way to do thing is to render what you need, call swapbuffers() and let windows deal with the rest of the time there while it waits for vsync (and for the love of god dont force vsync off without first asking!)however, if you do feel a need to limit the update rate, how about something a tad more standard like 60hz, not 50hz, although this does lead to the misconception that ''opengl games can only run at 60hz... 