PDA

View Full Version : About Menu



LovingYou
12-12-2007, 05:29 PM
hello, guys.

I have a question about using menu. I made a sample program to test it but it does not work perfectly.

Using right button of mouse, there are 3 menu. So, after reading a input file using "input" button, if we press the button, "draw", it shows the input data on screen. However, it does not work well. If the drawing part is out of switch in display(), it works. If the drawing part is in switch, I cannot see anything.

I'm trying to know, but I need your help.

Here is my code:

#include <stdio.h>
#include <iostream>
#include <OpenGL/glu.h>
#include <OpenGL/gl.h>
#include <GLUT/glut.h>

int screenwidth = 640;
int screenheight = 480;

int something;
int first = 1;

static int data[10];
static int old[10];

void Menu(int entryID)
{
something = entryID;
glutPostRedisplay();
}

void input(char* filename)
{
FILE *fp;

int i = 0;
int j = 0;
char tdata[10];
int temp[10];

printf("read files");

fp = fopen("test.txt", "r");

printf("open successfully");
while(fgets(tdata, sizeof(tdata), fp))
{
temp[i] = atoi(tdata);
i++;
}

for(j = i; j<10;j++)
temp[j] = 0;

for(i=0;i<10;i++)
{
data[i] = temp[i];
printf("data[%d] = %d\n", i, data[i]);
}

fclose(fp);
}

void draw()
{
int i;

glColor3d( 1.0, 0.0, 0.0);

glBegin(GL_LINES);
for(i=0;i<10;i++)
{
if(i == 9)
{
glVertex2d(i-5, data[i]);
glVertex2d(i-4, data[0]);
}
else
{
glVertex2d(i-5, data[i]);
glVertex2d(i-4, data[i+1]);
}
}
glEnd();
}

void display()
{
glClear(GL_COLOR_BUFFER_BIT);

glColor3f(1.0, 1.0, 1.0);

// coordinate
glBegin(GL_LINES);
glVertex2d(-100, 0);
glVertex2d(100, 0);

glVertex2d(0, -100);
glVertex2d(0, 100);
glEnd();

switch(something)
{
case 0:
break;
case 1:
input("test.txt");
printf("Input files\n");
something = 0;
break;
case 2:
glBegin(GL_LINES);
glVertex2d(-100, -100);
glVertex2d(100, 100);
glEnd();
draw();
printf("Draw files\n");
something = 0;
break;
case 3:
exit(0);
}

glFlush();
}

void specialkeyboard(int key, int x, int y)
{
if((glutGetModifiers() == GLUT_ACTIVE_ALT) && (key == 4))
exit(0);
}

void resize(int w, int h)
{
glViewport(0, 0, w, h);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// -min to max
gluOrtho2D((GLdouble)screenwidth*(-1.0), (GLdouble)screenwidth, (GLdouble)screenheight*(- 1.0), (GLdouble)screenheight);

glMatrixMode(GL_MODELVIEW);
}

void init(void)
{
// bg color
glClearColor(0.0, 0.0, 0.0, 0.0);
}

void MenuSet()
{
GLint MyMainMenuId = glutCreateMenu(Menu);
glutAddMenuEntry("Input",1);
glutAddMenuEntry("Draw Data",2);
glutAddMenuEntry("Exit",3);
glutAttachMenu(GLUT_RIGHT_BUTTON);
}

void timer(int extra)
{
glutPostRedisplay();
glutTimerFunc(100, timer, 0);
}

int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutInitWindowSize(screenwidth, screenheight);
glutInitWindowPosition(50, 50);

glutCreateWindow("MENU");

MenuSet();

glutDisplayFunc(display);

glutReshapeFunc(resize);
glutSpecialFunc(specialkeyboard);

init();

glutTimerFunc(0, timer, 0);

glutMainLoop();
}