PDA

View Full Version : Glut+subwindow+Scroll Mouse = Segmentation fault



zane
09-08-2005, 03:22 PM
Hi,

I meet the problem when I use scroll mouse in the subwindow created by glut. When I scroll up the scroll button, code crash. Other mouse buttons have no problem. Scroll down the scroll button has no problem too. If you run the attached code, move mouse to the center and scroll up the mouse, you will know what I mean.

I don't know whether it is a bug of glut or not. Does anyone know how to fix this problem?

Thanks.

-------------------------------------------------
//
//gcc -O3 -o mouse mouse.c -L/usr/X11R6/lib -lGL -lGLU -lX11 -lglut -lXmu
//
#include <GL/glut.h>
#include <stdlib.h>

static GLfloat spin = 0.0;

void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glRotatef(spin, 0.0, 0.0, 1.0);
glColor3f(0.0, 1.0, 0.0);
glRectf(-0.5, -0.5, 0.5, 0.5);
glPopMatrix();

glutSwapBuffers();
}

void mdisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glRectf(-40.0, -40.0, 40.0, 40.0);

glutSwapBuffers();
}

void spinDisplay(void)
{
spin = spin + 2.0;
if (spin > 360.0)
spin = spin - 360.0;
glutPostRedisplay();
}

void reshape(int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-50.0, 50.0, -50.0, 50.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

void mouse(int button, int state, int x, int y)
{
switch (button) {
case GLUT_LEFT_BUTTON:
if (state == GLUT_DOWN)
glutIdleFunc(spinDisplay);
break;
case GLUT_MIDDLE_BUTTON:
if (state == GLUT_DOWN)
glutIdleFunc(NULL);
break;
default:
break;
}
}

int subwin(int mwin)
{
int swin;

swin = glutCreateSubWindow (mwin,50,50,150,150);

glClearColor (0.0, 0.0, 1.0, 0.0);
glutDisplayFunc(display);
glutMouseFunc(mouse);
return 0;
}

int mainwin()
{
int mwin;

glutInitWindowSize (250, 250);
glutInitWindowPosition (100, 100);

mwin = glutCreateWindow ("main");
glClearColor (0.0, 0.0, 0.0, 0.0);

glutDisplayFunc(mdisplay);
glutReshapeFunc(reshape);

return mwin;
}

int main(int argc, char** argv)
{
int mwin,swin;

glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);

mwin = mainwin();

subwin(mwin);

glutMainLoop();
return 0;
}

Hlz
09-08-2005, 06:28 PM
Looks OK to me. Have you tried using a dummy idle func instead of passing NULL to glutIdleFunc? In Windows this is not a problem, but I don't know about Linux.

Hlz

zane
09-08-2005, 08:15 PM
Even I delete everything in function "mouse", the code will crash if I use scroll up the scroll button, which is button = 3.(the callback of left button=0, right button=1, middle/scroll button=2, scroll up middle button=3, scroll down middle button = 4).

If there is no subwindow, the code will not crash for any buttons.

Hlz
09-08-2005, 08:34 PM
That's freaky.

I hate to say bug, until I've exhausted everything, but it sure looks like one here, somewhere. I just ran it on my Wintel box and it's fine...

P.S. What's the debugger telling you, if anything?