Hello,
I’m a DirectX programmer but I’m trying to move away from Windows. So I’ve been learning GL/GLUT and it’s been pretty simple so far. Right now I’m trying to get it to switch from windowed->fullscreen and vice versa via a menu( I’m not sure if one must maintain resources under OGL, like in DX, so for the moment I’m completely ignoring that train of thought ). However, when I click to make the switch everything goes ape. The resolution changes, but then the screen flickers wildly - sometimes going black for a few seconds and no input is recognised by the program anymore. I have to CTRL+Backspace out and run an init 3 followed by startx in order to get out of it. I’m sure I’m doing something newbish. Could someone please scan their eyes over this code and let me know if they can see anything stupid? - I’ve commented the bad lines. Thanks.
#include <GL/gl.h>
#include <GL/glut.h>
#include <stdlib.h>
int Handle;
int Prim;
GLfloat Rot;
void Menu(int value)
{
if(value == 0)
{
glutDestroyWindow(Handle);
exit(0);
}
else
{
Prim = value;
}
glutPostRedisplay();
}
void CreateMenu()
{
int submenuid = glutCreateMenu(Menu);
glutAddMenuEntry("Teapot", 2);
glutAddMenuEntry("Cube", 3);
glutAddMenuEntry("Torus", 4);
int submenuid0 = glutCreateMenu(Menu);
glutAddMenuEntry("Teapot", 5);
glutAddMenuEntry("Cube", 6);
glutAddMenuEntry("Torus", 7);
int submenuid1 = glutCreateMenu(Menu);
glutAddMenuEntry("800x600x32", 9);
glutAddMenuEntry("1024x768x32", 10);
glutAddMenuEntry("1280x1024x32", 11);
int menuid = glutCreateMenu(Menu);
glutAddMenuEntry("Clear", 1);
glutAddSubMenu("Draw As Wire", submenuid);
glutAddSubMenu("Draw As Solid", submenuid0);
glutAddSubMenu("Fullscreen", submenuid1);
glutAddMenuEntry("Windowed", 8);
glutAddMenuEntry("Quit", 0);
glutAttachMenu(GLUT_RIGHT_BUTTON);
}
void EnableLight()
{
glClearDepth(1);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
}
//------------------------------------------------------------------------------
void keyboard(unsigned char key, int x, int y)
{
if(key == 'q')
{
glutDestroyWindow(Handle);
exit(0);
}
}
void draw()
{
glTranslatef(0, 0, -1);
//glRotatef(Rot, 0, 0, 1);
glRotatef(Rot, 1, 0, 0);
//glRotatef(Rot, 0, 1, 0);
if(Prim == 1) glutPostRedisplay();
if(Prim == 2) glutWireTeapot(0.5f);
if(Prim == 3) glutWireCube(0.5f);
if(Prim == 4) glutWireTorus(0.3f, 0.6f, 100, 100);
if(Prim == 5) glutSolidTeapot(0.5f);
if(Prim == 6) glutSolidCube(0.5f);
if(Prim == 7) glutSolidTorus(0.3f, 0.6f, 100, 100);
//if(Prim == 8) glutLeaveGameMode();
if(Prim == 9)
{
//glutGameModeString("800x600:32@75");
//glutEnterGameMode();
}
if(Prim == 10)
{
//glutGameModeString("1024x768:32@75");
//glutEnterGameMode();
}
if(Prim == 11)
{
//glutGameModeString("1280x1024:32@75");
//glutEnterGameMode();
}
}
void display()
{
glClearColor (0.0,0.0,0.0,1.0);
glClearDepth(1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
draw();
Rot++;
glutSwapBuffers();
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45, (GLfloat)w / (GLfloat)h, 1.0, 100.0);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char **argv)
{
Prim = 1;
Rot = 0.000000001;
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutInitWindowSize(800, 600);
glutInitWindowPosition(100, 100);
Handle = glutCreateWindow("learn learn learn");
glutDisplayFunc(display);
glutIdleFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
EnableLight();
CreateMenu();
glutMainLoop();
return 0;
}