SDL OpenGL won't maximize properly

I am using SDL 1.2.11 on a PowerPC G5 system running Mac OS X 10.4.8.
I wrote the following SDL program which simply draws a quad in a
resizable window:

#include
#include

#include <SDL/SDL.h>
#include <SDL/SDL_opengl.h>

namespace
{
GLdouble g_width = 640.0;
GLdouble g_height = 480.0;
GLdouble g_widthScale = 1.0;
GLdouble g_heightScale = 1.0;
void Render()
{
::glClear(GL_COLOR_BUFFER_BIT);

	::glMatrixMode(GL_PROJECTION);
	::glLoadIdentity();
	GLdouble halfWidth = g_width * g_widthScale * 0.5;
	GLdouble halfHeight = g_height * g_heightScale * 0.5;
	::glOrtho(-halfWidth, halfWidth, -halfHeight, halfHeight, -1.0, 1.0);
	
	::glMatrixMode(GL_MODELVIEW);
	::glLoadIdentity();
	
	::glBegin(GL_QUADS);
	::glVertex2f(-150.0f, 150.0f);
	::glVertex2f(-150.0f, -150.0f);
	::glVertex2f(150.0f, -150.0f);
	::glVertex2f(150.0f, 150.0f);
	::glEnd();
	
	::SDL_GL_SwapBuffers();
}

}

int main
(
int argumentCount,
char* arguments[]
)
{
using std::cerr;

Uint32 initflags = SDL_INIT_VIDEO;
// Initialize the SDL library
if(0 > ::SDL_Init(initflags)){
	cerr << "Couldn't initialize SDL:  " << ::SDL_GetError() << ".\n";
	::exit(1);
}

::SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);

// Set 640x480 video mode
Uint8  video_bpp = 0;
Uint32 videoflags = SDL_SWSURFACE | SDL_OPENGL | SDL_RESIZABLE;
SDL_Surface* screen = ::SDL_SetVideoMode(640, 480, video_bpp,  

videoflags);
if(NULL == screen){
cerr << “Couldn’t set 640x480x” << video_bpp << " video mode: " <<
::SDL_GetError() << “.\n”;
::SDL_Quit();
::exit(2);
}

bool done = false;
SDL_Event event;
while(false == done){
	// Check for events
	while(::SDL_PollEvent(&event)){
		switch(event.type){
			case SDL_VIDEORESIZE:
				::glViewport(0, 0, event.resize.w, event.resize.h);
				g_widthScale = event.resize.w / g_width;
				g_heightScale = event.resize.h / g_height;
				break;
			case SDL_QUIT:
				done = true;
				break;
			default:
				break;
		}
	}
	
	::Render();
}

// Clean up the SDL library
::SDL_Quit();
return(0);

}

When I run the program and maximize the window, SDL only draws in the
upper left corner of the window at the original size of the window.
Also, the rendered quad is only partially visible in the upper right
corner of the area that SDL draws into. If I restore the window back
to it’s original size, the quad still remains only partially visible
in the upper right corner.

I wrote an equivalent native OpenGL program without using the SDL,
and when I maximize that window, the quad remains in the center, and
the entire background is render with the black background color.

What can be done to get the SDL program to behave like the native
OpenGL program?

When you resize the window, you must call SDL_SetVideoMode again. And
you must recreate your opengl context.

I know this is true for switching to full screen, but I’m not 100%
sure for windowed opengl resize, can someone confirm?

Regards–
Kuon
Programmer and sysadmin.

“Computers should not stop working when the users’ brain does.”

Kuon - Nicolas Goy - ??? =?UTF-8?Q?
=E9=9C=8A_=28Goyman.com_SA=29 ?= <kuon goyman.com> writes:

When you resize the window, you must call SDL_SetVideoMode again. And
you must recreate your opengl context.

I know this is true for switching to full screen, but I’m not 100%
sure for windowed opengl resize, can someone confirm?

Regards

I tried calling SDL_SetVideoMode during resize, and I was able to get things
to work more or less. The window would not draw while resizing was taking
place. Also, once I maximized the window, I could not restore the window
back to its original dimensions by clicking on the maximize button again.

Other than that the application appears to render correct for the size of the
window.