Problem with glutTimerFunc

glutTimerFunc(100, timer, 1);

Is there is function that can make glutTimerFunc to stop running?

Thank You for your time.

Is the problem that it will only work one time or you want to supend the timer?

// My routine

static void TimeEvent(int te)
{
int timent;
rotateX = rotateX + 5;
if (rotateX > 360) rotateX = 0;
rotateY = rotateY + 5;
if (rotateY > 360) rotateY = 0;
rotateZ = rotateZ + 5;
if (rotateZ > 360) rotateZ = 0;

glutPostRedisplay();
glutTimerFunc( 10, TimeEvent, 1); // Call it’ self to restart the timmer, if you needed it to stop for some reason you could put a if statment if timmer_on = true then restart timeevent. Then to restart the timeevent in another loop just and glutTmerfunc in a statment there.

}

Originally posted by OpenGL_Student:
[b]

glutTimerFunc(100, timer, 1);

Is there is function that can make glutTimerFunc to stop running?

Thank You for your time.[/b]

[This message has been edited by nexusone (edited 02-22-2002).]

[QUOTE]Originally posted by nexusone:
[b]Is the problem that it will only work one time or you want to supend the timer?

I m drawing a box that moves right,
then I made a menu and I get to choose to change to move at an opposite direction.

I m learning the stuff from http://fly.cc.fer.hr/~unreal/theredbook/

#include

#include <stdlib.h>
#include <math.h>

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <GL/glut.h>

GLint tx, ty, angle, moving ,i, state, speed, again;
GLint moving = 0;

void myInit(int *, char **);
void myMenu(int);
void mouseManager(int, int, int, int);

void displayFunc(void);
void timer(int);
void timer2(int);
void timer3(int);

int main(int argc, char **argv){

myInit(&argc, argv);

glutDisplayFunc(displayFunc);
glutMouseFunc(mouseManager);
glutCreateMenu(myMenu);
glutAddMenuEntry(“Speed up”, 1);
glutAddMenuEntry(“Slow down”, 2);
glutAddMenuEntry(“Change direction”, 3);
glutAddMenuEntry(“Quit”, 4);
glutAttachMenu(GLUT_RIGHT_BUTTON);

tx=ty=0;
angle=0;
state=0;
glutMainLoop(); //infinite loop
}

void displayFunc(void){
// int i;
// static done=0;

glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glTranslatef( (107.5 + tx), (125.0), 0.0);
glRotatef(angle, 0 , 0, 1.0);
glTranslatef(-(107.5 + tx), -(125.0), 0.0);
glTranslatef( tx, 0.0, 0.0);

glBegin(GL_POLYGON);

glColor3f(0.0,0.0,1.0);
glVertex2i(100, 100);
glVertex2i(125, 100);
glVertex2i(150, 125);
glVertex2i(125, 150);
glVertex2i(100, 150);
glVertex2i(75, 125);

glEnd();

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glTranslatef( tx, 0.0, 0.0);
glBegin(GL_POLYGON);
glColor3f(1.0,0.0,0.0);
glVertex2i(300, 300);
glVertex2i(325, 300);
glVertex2i(350, 325);
glVertex2i(325, 350);
glVertex2i(300, 350);
glVertex2i(275, 325);

glEnd();

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glTranslatef( tx, 0.0, 0.0);
glBegin(GL_POLYGON);
glColor3f(1.0,0.0,0.0);
glVertex2i(50, 125);
glVertex2i(250, 125);
glVertex2i(250, 225);
glVertex2i(50, 225);

glEnd();
glFlush();

}

void mouseManager (int button, int statem, int x, int y) { speed = 100;
switch(button){
case GLUT_LEFT_BUTTON:
if(statem== GLUT_DOWN)
if(moving==1 | | moving ==2){

moving=0;


}

else {
moving=1;

glutTimerFunc(speed, timer, 1);

}
break;

case GLUT_MIDDLE_BUTTON:
exit(0);
break;
defaut:;
}
}

void myMenu(int state ){
int x;
x=100;
i=1;
switch(state){
case 1:
if(state== 1){
moving = 1;
glutTimerFunc(100, timer, 1);

}
   break;

case 2:
if(state==2){
moving = 2;
glutTimerFunc(0, timer, 1);

  }

break;

case 3:
if(state==3){
moving = 1;
glutTimerFunc(x, timer3, 2);

  }
break;

case 4:
exit(0);
}
// displayFunc();

}
void timer(int v){
if(moving==0){

return;
}
if (moving==1)
{
tx=(tx+10);
angle=((angle-15))%360;
//speed = speed*(2);
displayFunc();
glutTimerFunc(100,timer,v);
}
if (moving = 2){
tx=(tx+10);
angle=((angle-15))%360;
//speed = speed*(2);
displayFunc();
glutTimerFunc(500,timer,v);

}
}
void timer2(int v){
if(moving==0)
return;
if (moving==1)
tx=(tx-10);

angle=((angle-30))%360; 
 displayFunc();
 glutTimerFunc(100,timer2,v);
}

void timer3(int v){
if(moving==0)
return;
if (moving==1 | | moving ==2){
tx=(tx-3)%640;

angle=((angle+30))%360; 
 displayFunc();
 glutTimerFunc(100,timer3,v);
}

}
void myInit(int *argc, char **argv){
glutInit(argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(640, 480);
glutInitWindowPosition(100,150);
glutCreateWindow(argv[0]);

glClearColor(1.0,1.0,1.0,0.0); // set clear color to White
glColor3f(1.0f, 0.0f, 0.0f); //set the drawing color to black
glPointSize(2.0);
glMatrixMode(GL_PROJECTION); // virtual camera
glLoadIdentity();
gluOrtho2D(0.0,640.0,0.0,480.0);
}

Each time I call glutTimerFunc again, the speed of the box speeds up even more.
I try changing the speed, but it seems the speed only accumlates.

What can I do to turn the timer off, then call the timmer again with another speed?

Here is the way I would handle it, others on here may have other idea’s.

static void TimerEvent( int te)
{

if (Object_Ticks > Object_Speed)// Adjust the value of Object_Speed to change movement rate.
{
Update_Object(); // Call to move the object, there you put varibles to tell direction and number of units to move. To stop the object, just make the units to move zero.
Object_Ticks = 0;
}else Object_Ticks++;

GlutTimerFunc(10, TimerEvent, 1);
}

This way you could control multiple objects with one TimerEvet!

Originally posted by OpenGL_Student:
[b]Each time I call glutTimerFunc again, the speed of the box speeds up even more.
I try changing the speed, but it seems the speed only accumlates.

What can I do to turn the timer off, then call the timmer again with another speed?[/b]

[This message has been edited by nexusone (edited 02-22-2002).]

[This message has been edited by nexusone (edited 02-22-2002).]

I got it! Thanks!
Its not about how quickly or how slow I call glutTimerFunc!
It works fine.
but the same problem still occur.
the speed still increases.
What can I do to clear the buffer?

I don’t think your problem is a opengl problem but a math problem. I was lost trying to follow your code…

Here is a piece of code for controling the object movement:

Object control routine:

void Update_Object()
{

Object_x = Object_x + Unit * direction_x; Where unit is number of units to move, direction is 1 for positive, -1 negitive, 0 no movement.

if ((Object_x > boundry_X_TOP) | | (Object_x < boundry_X_BOTTOM))
{
direction_x = direction_x * -1; reverse direction when boundry is hit.
}
repeat above but with Y axis.
This goes along with my last example.

Another few things in your code that I could see. Look’s like you translate your object twice, this may part of your problem.

try this along with above code:

glTranslatef( Object_x, Object_y, 0.0);
glRotatef(angle, 0, 0, 1);
Draw_cude();// start drawing your cude here.

On you menu:
switch(state)
{
case SPEED_UP:
Object_Speed–;
break;
case SLOW_DOWN:
Object_Speed++;
break;
case STOP:
direction_x = 0;
direction_y = 0;
break;
case REVERSE:
direction_x = direction_x * -1;
direction_y = direction_y * -1;
break;
}

Originally posted by OpenGL_Student:
[b]I got it! Thanks!
Its not about how quickly or how slow I call glutTimerFunc!
It works fine.
but the same problem still occur.
the speed still increases.
What can I do to clear the buffer?

[/b]

Here is my version of your program, not a complete copy.

#include <GL/glut.h>
#include <stdlib.h>
#include <math.h>

static float Object_x = 0, Object_y = 0, unit = 0.5;
static int direction_x = 1, direction_y = 1;
static int Object_speed = 1, Object_ticks =0;
static float boundry_x_top = 5, boundry_x_bottom = -5, boundry_y_top = 5, boundry_y_bottom = -5;

void init(void)
{
int ix,iy,iz;
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_SMOOTH);
}

void Update_Object()
{

Object_x = Object_x + unit * direction_x;
Object_y = Object_y + unit * direction_y;

// Check boundry’s
if (( Object_x > boundry_x_top ) | | ( Object_x < boundry_x_bottom ))
{
direction_x = direction_x * -1;
}

if (( Object_y > boundry_y_top ) | | ( Object_y < boundry_y_bottom ))
{
direction_y = direction_y * -1;
}

}

static void TimeEvent(int te)
{
int timent;
if (Object_ticks > Object_speed)
{
if (Object_speed > 0) Update_Object();
Object_ticks = 0;
}else Object_ticks++;

glutPostRedisplay();
glutTimerFunc( 10, TimeEvent, 1);

}

void display(void)
{
int i,j;

glClear (GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);

glPushMatrix();
glLoadIdentity();
glTranslatef(Object_x, Object_y, -10);
glBegin(GL_POLYGON);
glVertex2f(-0.5,-0.5);
glVertex2f(0.5,-0.5);
glVertex2f(0.5,0.5);
glVertex2f(-0.5,0.5);
glEnd();
glPopMatrix();

glutSwapBuffers();
}

void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective(60.0,(GLfloat) w/(GLfloat) h, 1.0, 50.0);
glMatrixMode(GL_MODELVIEW);
}

void my_menu(int value)
{

switch (value) {
case 'f':
    Object_speed--;
	if (Object_speed &lt; 0) Object_speed = 1;
    break;
case 's':
    Object_speed++;
	if (Object_speed &gt; 100) Object_speed = 100;
    break;
case 'h':
    Object_speed = 0;
    break;
case 'r':
    direction_x = direction_x * -1;
	direction_y = direction_y * -1;
    break;
case 'x':
    exit(0);
	break;
}

}

int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
glutSetWindowTitle(“GLobject”);
init ();
glutCreateMenu(my_menu);
glutAddMenuEntry(“Faster”, ‘f’);
glutAddMenuEntry(“Slower”, ‘s’);
glutAddMenuEntry(“Halt”, ‘h’);
glutAddMenuEntry(“Reverse”, ‘r’);
glutAddMenuEntry(“Exit”, ‘x’);
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutTimerFunc( 10, TimeEvent, 1);
glutMainLoop();
return 0;
}

Thanks for your help!

I understand now.

Originally posted by nexusone:
[b]Here is my version of your program, not a complete copy.

// Check boundry’s
if (( Object_x > boundry_x_top ) | | ( Object_x < boundry_x_bottom ))
{
direction_x = direction_x * -1;
}

if (( Object_y > boundry_y_top ) | | ( Object_y < boundry_y_bottom ))
{
direction_y = direction_y * -1;
}

}
[/b]

I know that I might be splitting hairs here, but direction_y = direction_y * -1? Why not direction_y = -direction_y and skip the extra multiplication ?

Habbit maybe, I do a lot of cutting a pasting code and varibles around.
I had a direction varible that I used in the other part of the code and when I pasted the code, I replaced that varible with a fixed number.

Also using -direction may not be as clear to a new programmer as to that is happening in the code as direction * -1…

Originally posted by Furrage:
I know that I might be splitting hairs here, but direction_y = direction_y * -1? Why not direction_y = -direction_y and skip the extra multiplication ?

[This message has been edited by nexusone (edited 02-27-2002).]

How can I get answer of each question?
I have problem with stop of glutTimerFunc
thank in advance

Dear All
I am going to buy a laptop for using C++ Opengl. My program is heavy and I don’t know which laptop is better (High speed)?
My experience shows AMD with nVidia is better but in PC. I want to know about laptop.
Your answer will be high appreciated.
Hoshang

Dear All
I have some codes in C++ OpenGl. They can be run in my PC (AMD 2.4 with Graphic cart NVIDIA, Win XP) with abou 8 FPS but changed my Windows in Xp and Vista or 7 the speed is very low about 2.5 FPS. I have bought a new PC( Intel 3.6, i7, with Win 7) but the speed is also low 0.052 PFS . I changed the windows but speed is same. I don’t know what I should do?

Please help me.
shahinkey@yahoo.com