I was messing around with it, and it works fine now, but the only problem that occurs is when the application first starts. It stalls for a few seconds with a empty transparent screen, then it works normally. I debugged it and found that
initially the accumulator has the value 4000 because update.getDiffSec() returns
a number around 4000, and it then gets stalled in the while(accumlator >= deltaTime) loop because accumlator is around 4000. I would appreacite it if someone helped to find out a reason why initally getDiffSec returns a number around 4000.
GLvoid disp(){ update.start(); static float accumlator = 0.0f; static const float deltaTime = 0.01f; //10 millisecond if(update.getDiffSec() >= deltaTime) { cout<<update.getDiffSec()<<endl; //does not come inside here often than it should accumlator += update.getDiffSec(); //frist returns around 4000 update.reset(); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glTranslatef(0,0,-30); while(accumlator >= deltaTime) { spring.drawSpring(deltaTime); accumlator -= deltaTime; } glBegin(GL_LINES); glVertex3f(spring.getRestLength().getX(),-5,0); glVertex3f(spring.getRestLength().getX(),5,0); glEnd(); glutPostRedisplay(); glutSwapBuffers(); } }
The timer class :
class HighTimer{public: typedef LARGE_INTEGER TimeType; typedef double ReturnType;private: //helpers void _GetTime(TimeType * timeVar){ QueryPerformanceCounter(timeVar); } void _calcFrequency(TimeType* timeVar){ QueryPerformanceFrequency(timeVar); } ReturnType ticksPerSecond(){ return _ticksPerSecond.QuadPart; } private: TimeType _startTime; TimeType _endTime; TimeType _ticksPerSecond;public: HighTimer() { _startTime.QuadPart = 0; _endTime.QuadPart = 0; _ticksPerSecond.QuadPart = 0; _calcFrequency(&_ticksPerSecond); } void start(){ _GetTime(&_endTime); } void reset(){ _startTime = _endTime; } LONGLONG getRawDiff(){ _GetTime(&_endTime); return (_endTime.QuadPart - _startTime.QuadPart); } ReturnType getDiffSec(){ return getRawDiff()/ ticksPerSecond(); } ReturnType getDiffDeci(){ return (getDiffSec() * 10 ); } ReturnType getDiffCenti(){ return ( getDiffSec() * 100 ); } ReturnType getDiffMilli(){ return ( getDiffSec() * 1000 ); } bool hasSecondPassed(){ return getDiffSec() > 1; }};