1
\$\begingroup\$

I am confused about the argument "m" of this function.

I also need to know how to make this work. I am working on a simple 2d game and I am really new to programming. I don't know how to make things work in this Timer() function. I want to animate an object in a specific direction. I know the coordinates of the direction but after a lot of hit and trial, my object moves in a random direction. I have used sin and cos for the movement. But my real problem is about the functionality of this Timer() function. Can anyone here help me about it? Any help will be appreciated.

float dt = 0, lastframe = 0;

void Timer(int m) {
    dt = (m - lastframe) / 1000.0;
    lastframe = m;

    glutPostRedisplay();
    glutTimerFunc(1000.0 / FPS, Timer, m + 1);
}
\$\endgroup\$
  • \$\begingroup\$ Are you sure that you copied the code correctly? If yes, does that eve work? Because the function Timer is being called in the last line without any parameters passed, and considering that the function Timer does not have a default value, it should throw you an error. Besides, knowing what the functions glutPostRedisplay and glutTimerFunc are supposed to do would help. It looks like m is trying to count the number of frames elapsed, but I can't be sure before you reply with further details. \$\endgroup\$ – MAnd Nov 30 '15 at 8:54
  • \$\begingroup\$ @MAnd For free functions, Timer is the same as &Timer, that is, forming a function pointer. The intent and effect here is to once the callback completes, re-register itself for the next tick. \$\endgroup\$ – Lars Viklund Nov 30 '15 at 9:36
  • \$\begingroup\$ Yes i have copied the code correctly and it works but after every click, the speed of my object incresases. glutPostRedisplay() actually calls the Display function to redraw the whole canvas i.e update the display. glutTimerFunc is something which i don't know about myself. I think i should read up more on this matter. Can you tell me what does a tick mean? \$\endgroup\$ – Abdul Mateen Nov 30 '15 at 10:26
1
\$\begingroup\$

According, the value passed to timer function, is the one you provided when registering the timer as the third parameter. Read here for more information. It's mainly for the purpose of tagging different calls to timer functions, so that you can do different things based on tags.

If you want to keep track of actual time passed since last frame, you've got to keep track of time yourself; With mechanisms like std::chrono::high_resolution_clock.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.