PDA

View Full Version : How to add delay in opengl



boydsmith
11-19-2014, 07:16 AM
Hi,

I am looking to create a simple program using delay which works as follows:

1) Draw an array and display the numbers.
2) After a delay of 2 seconds, arrow should point to the first element of the array.

How to do that? Here is the code:



#include <GL/glut.h>
#include<stdio.h>
#include<windows.h>
int i,j,a[100],n;
char c[100][100];
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Do not dick with these functions unless u know what you are doing




void draw_arrow(float x,float y)
{
float h1=8.0,w=8.0;
float z=w/2;
glColor3f(0.0,0.0,0.0);
//glLineWidth(3);
glBegin(GL_POLYGON);
glVertex2f(x,y);
glVertex2f(x+w,y-h1);
glVertex2f(x-w,y-h1);
glEnd();

glColor3f(0.0,0.0,0.0);
//glLineWidth(3);
glBegin(GL_POLYGON);
glVertex2f(x+z,y-h1);
glVertex2f(x-z,y-h1);
glVertex2f(x-z,y-(4*h1));
glVertex2f(x+z,y-(4*h1));
glEnd();

}


void Font(void *font,char *text,int x,int y)
{
glRasterPos2i(x, y);

while( *text != '\0' )
{
glutBitmapCharacter( font, *text );
++text;
}

}



void start_myinit(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glEnable(GL_LIGHT0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

gluOrtho2D(0.0, 800.0, 600.0, 0.0);

glMatrixMode(GL_MODELVIEW);
}
//End of functions which is more or less not edited
///////////////////////////////////////////////////////////////////////////////////////////////////////////////


void display_array()
{
for(i=0;i<n;i++)
{int b=150;
j=150+(50*i);
sprintf(c[i],"%d",a[i]);


glColor3f(1.0,0.0,0.0);
glLineWidth(3);
glBegin(GL_LINE_LOOP);
glVertex2f(j,b);
glVertex2f(j,b+40);
glVertex2f(j+40,b+40);
glVertex2f(j+40,b);
glEnd();


glColor3f(0,0,0);
Font(GLUT_BITMAP_TIMES_ROMAN_24,c[i],j+15,b+15);
}

}

void draw()
{


glClearColor(1.0,1.0,1.0,1.0);
glClear( GL_COLOR_BUFFER_BIT |
GL_DEPTH_BUFFER_BIT );
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,800,600,0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

//Code to be edited from here
display_array();

Sleep(1000);

draw_arrow(10,20);
// Editing Ends



glutSwapBuffers();
glFlush();

}





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

printf("enter n\n");
scanf("%d",&n);
printf("enter ele\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
glutInit(&argc,argv);

glutInitWindowSize( 800, 600 );
glutInitDisplayMode( GLUT_RGB | GLUT_SINGLE | GLUT_DOUBLE);
glutCreateWindow("Selection Sort");
glutDisplayFunc(draw);
start_myinit();

glutMainLoop();
return 0;
}

sueyllam
11-19-2014, 10:57 PM
defining a timer function can help you. See glutTimerFunc() in glut specs...