Hi,
I just started learning OpenGL. Though the beginner’s section here seems to be much more advanced, I hope you can help me solve this small problem of mine.
Just thought I’d put to use all the functions I’d learnt in a stack simulation program. I can’t seem to get the ‘POP Button’ to work. The mouse function just does not seem to work. What am I doing wrong here?
#include <string.h>
#include <stdio.h>
#include <glut.h>
int flag=0;
int top=-1;
float W=100,H=120;
char a[6];
void stackdisp();
void myinit()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,W,0,H);
glMatrixMode(GL_MODELVIEW);
}
void display1()
{
glClearColor(1,1,1,1);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1,0,0);
glLineWidth(2.0);
glBegin(GL_LINES); /* Stack Frame Box*/
glVertex2f(20.0,20.0);
glVertex2f(40.0,20.0);
glVertex2f(40.0,20.0);
glVertex2f(40.0,100.0);
glVertex2f(40.0,100.0);
glVertex2f(20.0,100.0);
glVertex2f(20.0,100.0);
glVertex2f(20.0,20.0);
glEnd();
glColor3f(0,0,0);
glBegin(GL_LINES); /*Divider Line*/
glVertex2f(50.0,0.0);
glVertex2f(50.0,120.0);
glEnd();
glColor3f(0.4,0.6,0.2); //lime green
glBegin(GL_POLYGON); /* POP BOX*/
glVertex2f(70.0,20.0);
glVertex2f(85.0,20.0);
glVertex2f(85.0,35.0);
glVertex2f(70.0,35.0);
glEnd();
glColor3f(0.3,0.1,0.4); //purple
glBegin(GL_POLYGON); /*FLAG BOX*/
glVertex2f(60.0,60.0);
glVertex2f(60.0,90.0);
glVertex2f(90.0,90.0);
glVertex2f(90.0,60.0);
glEnd();
glColor3f(1,0,0); //TEXT COLOR RED
if(flag == 1) /*OVERFLOW TEXT*/
{
char flow[15] = "OVERFLOW!";
glRasterPos2f(61.0,80.0);
for(int i=0;i<strlen(flow);i++)
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,flow[i]);
}
if(flag == 2) /*UNDERFLOW TEXT*/
{
char flow[15] = "UNDRFLOW!";
glRasterPos2f(61.0,65.0);
for(int i=0;i<strlen(flow);i++)
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,flow[i]);
}
char p[5] = "POP!";
glRasterPos2f(72.0,25.0);
for(int i=0;i<strlen(p);i++)
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,p[i]);
if(top>=0)
{
stackdisp();
}
glFlush();
}
void stackdisp()
{ float q=20.0,x=20.0,y;
for(int i=0;i<=top;i++)
{
y=q*i+20;
if(i==0){glColor3f(1,1,0);}
if(i==1){glColor3f(1,0,1);}
if(i==2){glColor3f(0,1,1);}
if(i==3){glColor3f(0,0,0.8);}
glBegin(GL_POLYGON);
glVertex2f(x,y);
glVertex2f(x+20.0,y);
glVertex2f(x+20.0,y+20.0);
glVertex2f(x,y+20.0);
glEnd();
}
float m=30.0,n=30;
glColor3f(0,0,0);
for(int i=0;i<=top;i++)
{
glRasterPos2f(m,n+(i*20.0));
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,a[i]);
}
}
void mymouse(int button, int state, int x,int y)
{
if((button == GLUT_RIGHT_BUTTON) && (state == GLUT_DOWN))
exit(0);
if((button == GLUT_LEFT_BUTTON) && (state == GLUT_DOWN))
{ if((x>70 && x<85) && ((H-y)>20 && (H-y)<35))
{
if(top>-1)
--top;
}
}
glutPostRedisplay();
}
void mykey(unsigned char key,int x,int y)
{
if(top>=3)
{
flag=1;
}
else
{
a[++top]=key;
}
glutPostRedisplay();
}
int main()
{
glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE);
glutInitWindowPosition(100,100);
glutInitWindowSize(500,500);
glutCreateWindow("First!");
myinit();
glutDisplayFunc(display1);
glutMouseFunc(mymouse);
glutKeyboardFunc(mykey);
glutMainLoop();
}