title: GLUT animation with glTimerFunc
body: <p>I've been trying out some simple drawings and animations with freeglut (on Linux in virtual machine). Until now everything built and worked just fined. My latest try was moving a square around with <code>glTimerFunc</code>. Although it built without any errors with <code>gcc stack.c -lGL -lglut -o stack</code>, the animation itself doesn't work. I've looked at every example of an animation with glut I could find, but didn't see any problems with my code. Could anyone explain to me what my mistake is?</p>
  
  <p>(edit: see below for working code)</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;GL/freeglut.h&gt;
  
  int dx = 0;
  
  #define TIMERSECS 100
  
  void animate(int value) {
    glutTimerFunc(TIMERSECS, animate, 1);
    if (dx &gt; 0.5) {
      dx = -0.5;
    }
    else {
      dx += 0.1;
    }
    glutPostRedisplay();
  }
  
  void display(void) {
    glClear(GL_COLOR_BUFFER_BIT);
  
    glColor3f(0.0, 0.0, 0.5);
  
    glBegin(GL_POLYGON);
      glVertex2d(-0.5+dx, 0.5);
      glVertex2d(-0.5+dx, -0.5);
      glVertex2d(0.5+dx, -0.5);
      glVertex2d(0.5+dx, 0.5);
    glEnd();
  
    glutSwapBuffers();
  }
  
  void initialize(void) {
    glClearColor(1.0, 1.0, 1.0, 1.0);
    glShadeModel(GL_SMOOTH);
  }
  
  void main(int argc, char *argv[]) {
  
    glutInit(&amp;argc, argv);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(500, 500);
  
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
    glutCreateWindow(argv[0]);
  
    initialize();
    glutDisplayFunc(display);
    glutTimerFunc(TIMERSECS, animate, 0);
    glutPostRedisplay();
  
    glutMainLoop();
  }
  </code></pre>
  
  <p>EDIT</p>
  
  <p>@datenwolf: I looked at the <a href="https://stackoverflow.com/questions/11072951/simple-2d-animation-in-glut">other question</a> you answered more carefully this time and took some of the code from there, works great!</p>
  
  <p>Here's the new version:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;GL/freeglut.h&gt;
  
  int factor=100; // factor the animation is slowed down by
  
  double dx = 0;
  
  void animate(double speed);
  
  static double ftime(void) {
      struct timeval t;
      gettimeofday(&amp;t, NULL);
  
      return 1.0*t.tv_sec + 1e-6*t.tv_usec;
  }
  
  static double last_T;
  
  static void idle(void) {
    const double now_T = ftime();
    const double delta_T = now_T - last_T;
    last_T = now_T;
  
    const double speed = delta_T * 60;
  
    animate(speed);
  
    glutPostRedisplay();
  }
  
  void animate(double speed) {
    if (dx &gt; 1.5) {
      dx = -1.5;
    }
    else {
      dx += speed/factor;
    }
    glutPostRedisplay();
  }
  
  void display(void) {
    glClear(GL_COLOR_BUFFER_BIT);
  
    glColor3f(0.0, 0.0, 0.5);
  
    glBegin(GL_POLYGON);
      glVertex2d(-0.5+dx, 0.5);
      glVertex2d(-0.5+dx, -0.5);
      glVertex2d(0.5+dx, -0.5);
      glVertex2d(0.5+dx, 0.5);
    glEnd();
  
    glutSwapBuffers();
  }
  
  void initialize(void) {
    glClearColor(1.0, 1.0, 1.0, 1.0);
    glShadeModel(GL_SMOOTH);
  }
  
  void main(int argc, char *argv[]) {
  
    glutInit(&amp;argc, argv);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(500, 500);
  
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
    glutCreateWindow(argv[0]);
  
    initialize();
    glutDisplayFunc(display);
    glutIdleFunc(idle);
    glutPostRedisplay();
  
    glutMainLoop();
  }
  </code></pre>
  
  <p>Dankeschön für deine Hilfe!</p>