Original Post
For some reason when I alt+tab to my desktop while in fullscreen mode, my 3d engine crashes. The line that is causing it is glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); Without GL_DEPTH_BUFFER_BIT though, it does not crash. Does anybody have any idea why this is causing a crash when alt+tabbing from fullscreen? How can I fix it? Here is my OpenGL init code:
void GraphicsSystem::oglinit(){
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); // tell SDL that the GL drawing is going to be double buffered
SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 32); // size of depth buffer
SDL_GL_SetAttribute( SDL_GL_STENCIL_SIZE, 0); // we aren't going to use the stencil buffer
SDL_GL_SetAttribute( SDL_GL_ACCUM_RED_SIZE, 0); // this and the next three lines set the bits allocated per pixel -
SDL_GL_SetAttribute( SDL_GL_ACCUM_GREEN_SIZE, 0); // - for the accumulation buffer to 0
SDL_GL_SetAttribute( SDL_GL_ACCUM_BLUE_SIZE, 0);
SDL_GL_SetAttribute( SDL_GL_ACCUM_ALPHA_SIZE, 0);
glClearColor (0.0f, 0.0f, 0.0f, 0.5f); // Black Background
glClearDepth (1.0f); // Depth Buffer Setup
glDepthFunc (GL_LEQUAL); // The Type Of Depth Testing (Less Or Equal)
glEnable (GL_DEPTH_TEST); // Enable Depth Testing
glShadeModel (GL_SMOOTH); // Select Smooth Shading
glHint (GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Set Perspective Calculations To Most Accurate
glEnable (GL_TEXTURE_2D); // Enable Texture Mapping
// glEnable (GL_CULL_FACE); // Remove Back Face
gr = ((1 + sqrt((double)5)) / 2);
}
My render code where the line that crashes it is:
void GraphicsSystem::Render(std::vector<int> scenegraph,Player* player){
output = fopen("GLErrorLog.txt","w");
fprintf(output,"%s\n",gluErrorString(glGetError()));
fclose(output);
output = fopen("SDLErrorLog.txt","w");
fprintf(output,"%s\n",SDL_GetError());
fclose(output);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear screen and depth buffer
glLoadIdentity(); // reset modelview matrix
glRotatef(player->xangle, 1.0f, 0.0f, 0.0f);
glRotatef(player->yangle, 0.0f, 1.0f, 0.0f);
glRotatef(player->zangle, 0.0f, 0.0f, 1.0f);
glTranslatef(player->x,player->y,player->z);
glColor3f(0.0f,0.6f,0.0f);
glBegin(GL_LINES);
for (float i = -100; i < 100;i++){
glVertex3f(i,0,100);
glVertex3f(i,0,-100);
}
for (float i = -100; i < 100;i++){
glVertex3f(100,0,i);
glVertex3f(-100,0,i);
}
glEnd();
glBegin(GL_POINTS);
glColor3f(1,0,0);
glVertex3f(0,1,gr);
glVertex3f(0,1,-gr);
glVertex3f(0,-1,gr);
glColor3f(0,1,0);
glVertex3f(0,-1,-gr);
glVertex3f(1,gr,0);
glVertex3f(1,-gr,0);
glColor3f(0,0,1);
glVertex3f(-1,gr,0);
glVertex3f(-1,-gr,0);
glVertex3f(gr,0,1);
glColor3f(1,1,1);
glVertex3f(gr,0,-1);
glVertex3f(-gr,0,1);
glVertex3f(-gr,0,-1);
glEnd();
SDL_GL_SwapBuffers();
}
My 3D init code
void GraphicsSystem::oglinit3D(float fov,float aspect,float zNear,float zFar){
glMatrixMode (GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity (); // Reset The Projection Matrix
gluPerspective(fov,aspect,zNear,zFar);
glMatrixMode (GL_MODELVIEW); // Select The Modelview Matrix
//90,1,0.1f,100.0f
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); // Nearest Filtered
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); // Nearest Filtered
}
and the main loop:
window::window(int width, int height,std::string WindowName){
done = false;
if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )
{
fprintf( stderr, "Video initialization failed: %s\n",
SDL_GetError( ) );
SDL_Quit( );
}
atexit(SDL_Quit);
SDL_WM_SetCaption(WindowName.c_str(), NULL);
screen = SDL_SetVideoMode(width, height, 0, SDL_FULLSCREEN|SDL_OPENGL);
if( !screen ) {
fprintf(stderr, "Couldn't create a surface: %s\n",
SDL_GetError());
done = true;
}
SDL_ShowCursor(false);
Player* player = new Player(); //Create new player
GameTimer* timer = new GameTimer(); //Create new timer object
GraphicsSystem* graphics = new GraphicsSystem(); //Create graphics object
GameInput* input = new GameInput(); //Create game input object
GameLogic* logic = new GameLogic(); //Create game logic object
timer->Update();
graphics->oglinit();
graphics->oglinit3D(90,((float)width/(float)height),0.1f,100);
std::vector<int> sg;
while(!done)
{
SDL_Event event;
while ( SDL_PollEvent(&event) )
{
switch (event.type)
{
case SDL_QUIT:
done = true;
break;
case SDL_KEYDOWN:
if ( event.key.keysym.sym == SDLK_ESCAPE ) { done = 1; }
break;
}
}
input->Update();
logic->UpdateGame(input->GetKeyState(),timer->GetDifference(),player,sg);
graphics->oglinit3D(90,(float)(width/height),0.1f,100);
graphics->Render(sg,player);
timer->Update();
}
delete player;
delete timer;
delete graphics;
delete input;
delete logic;
}
(Yes I realize my scenegraph is of type int, just a placeholder.) [Edited by - ScottC on March 9, 2006 3:08:33 PM]