PDA

View Full Version : Error messages while using glutTimerFunc()



JuanC90
10-16-2015, 03:42 AM
Hello everyone,
I'm trying to change the color of a circle from its ORIGINAL color to RED (then BLUE, then GREEN and repeat) every second.

I tried to achieve it with this function:



void changeColor(int value)
{
glutTimerFunc(1000,changeColor,0);
for (int i=0; i<3; i++)
{C[i]=0;}
C[pos]=1; // int pos is initialized with a value of 0.
pos++;
if(pos>2)
{pos=0;}
glutPostRedisplay();
}


The circle is drawn with glColor3i ( C[0], C[1], C[2] )

Everything looks fine to me, however, I get a "Segmentation fault" every time I compile it.
PD: If I erase the line that says "C[pos]=1" everything works perfect, however, the main objective of the code would still not be achieved.

It seems to be that "pos" is not being updated (but I can't see why).
If any of you can give me some suggestions to fix this code, I'd appreciate it.

JuanC90
10-16-2015, 05:10 PM
Hi again, the problem seems to be produced because I'm using 2 TimerFuncs() and probably I'm not doing it right. (I should have mentioned it)
I tried to use just that piece of code that I posted before and it worked.

The problem starts when I add a second glutTimerFunc() command to call another function with a different "delay". This is what I have in the main function:




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

glutInit(&argc,argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(600,600);
glutInitWindowPosition(425,50);
glutCreateWindow("Test");
glClearColor(0,0,0,1);

glutDisplayFunc(scene); // "scene" draws everything
glutReshapeFunc(...);
glutKeyboardFunc(...);
glutSpecialFunc(...);

glutTimerFunc( 16.66, update, 0 ); // "update" changes the position of the circle
glutTimerFunc( 1000, changeColor, 0 );

glutMainLoop();
return 0; }


I noticed that the value of 'pos' changes every time that glutTimerFunc(16.66,update,0) is called; because of that, when glutTimerFunc(1000,changeColor,0) is first called, pos is equal to 1140457480 (Instead of 0). - This would explain why the window is being closed after 1 sec (It doesn't exist a pos-th element in the matrix C when it is trying to run 'changeColor') - This shouldn't be happening but, somehow, it is.

Another thing that I noticed is that if I add a "//" at the beginning of the 1st TimerFunc() everything works perfect but (of course) 'update' wouldn't update the position of the circle.
After this tests, it seems to be that the problem is produced because of the first TimerFunc() but I don't see how.

Now, after this new info, how could I solve this issue? Is there something important about the usage of multiple timerFunctions that I'm ignoring?