glutTimerFunc and for loop

Hi all,
I’m trying to create a simulation where an objects position is updated based on points held in a text file. Reading the points and outputting them to the screen is fine but trying to slow the animation down using glutTimerFunc is just not working!! here is my code so far:

void timer(int flag)
{
glutPostRedisplay();
glutTimerFunc(200, timer, 1);
}

void readstr(FILE *f,char *string)
//this just extracts a 255 character line from my text file
{
do
{
fgets(string, 255, f);
} while ((string[0] == ‘/’) | | (string[0] == ’
'));
return;
}

void move(void)
{
int ver;
int i;
FILE *filein;
char oneline[255];
double pos;

filein = fopen(“frame.dat”, “rt”);
if(!filein)
{
printf("failed to open file: frame.dat
");
exit(0);
}

for (i=0;i<30;i++)
{
readstr(filein,oneline);
printf("%s", oneline);
//this converts the string to a double
pos = atof(oneline);
//updates object position
w_move = pos;
glutTimerFunc(200, timer, 1);
}

I think the problem is because the glutTimerFunc is being called within a for loop, but I need the for loop to iterate through the file!!! has anyone got any suggestions?!
Cheers

Chris

Hi !

The timer is implemented in different ways, but I don’t think it is implmeneted with threads on any platform, so on Windows for example it’s done with the WM_TIMER message, si the timer will not be called until you exit the move() function.

Create the timer in the move function and move the “loop” into the timer() function instead and run through part of the loop at each timer call until you are done.

Mikael

No you don’t need a for loop to read the file, you can do it from the timer routine.
Or you can read the file into a variable which is even better.

First you open the file before entering your timming loop.

void init(void)
{
// Set up every thing here
// open movement file
}

void timer(int flag)
{
if(infile != EOF ) get_next_move();

glutPostRedisplay();
glutTimerFunc(200, timer, 1);
}

then just add to your exit routine close file.

or read file into a variable array.

void timer(int flag)
{
current_position++;
glutPostRedisplay();
glutTimerFunc(200, timer, 1);
}

void display(void)
{

move_array[current_position] //current data to be used.

Thanks!
I’ve got it all working now, cheers for your help

Chris