I am at a very early stage of teaching myself SDL. I've gotten as far as setting up a window. I started with just a blank window that would close as soon as you pressed a key, thus :
SDL_Surface *screen = NULL;
if (SDL_Init(SDL_INIT_EVERYTHING) == -1) return 1;
screen = SDL_SetVideoMode(680, 420, 32, SDL_OPENGL);
if (screen == NULL) return 1;
SDL_Event event;
while (true) {
if (SDL_PollEvent(&event)) {
if (event.type == SDL_KEYDOWN) break;
}
}
SDL_Quit();
return 0;
which worked nicely. You start the program, you get a window, you press a button, it closes. Splendid! Next I thought I'd draw a square in my window. So I included OpenGL, and added a draw-square part to my loop, thusly :
// SDL setup as before
SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8 );
SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 8 );
SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8 );
SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
glViewport(0, 0, 640, 480);
glColor3f(1.0, 1.0, 1.0);
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
SDL_Event event;
while (true) {
if (SDL_PollEvent(&event)) {
if (event.type == SDL_KEYDOWN) break;
}
glBegin(GL_POLYGON); {
glVertex2f(-0.5, -0.5);
glVertex2f(-0.5, 0.5);
glVertex2f(0.5, 0.5);
glVertex2f(0.5, -0.5);
} glEnd();
}
SDL_Quit();
return 0;
The problem is that this code hangs completely! (It doesn't draw any squares, either, but first things first.) I can't even Alt-Tab out of the window; I have to log on from another computer and kill the process. Now, it seems to me that since I'm polling every turn of the loop, my program is catching that Alt-Tab before it can get to the OS. But what I can't figure out is why it doesn't exit the while loop.
Now, I've tested this and I'm fairly sure it's the OpenGL code that's the problem. If I comment out the glBegin to glEnd, the program works as before, even with OpenGL setup in place.
Does anyone have a clue to what I'm doing wrong? I'm using SDL 1.2.9 and gcc 3.2.3.
To win one hundred victories in one hundred battles is not the acme of skill. To subdue the enemy without fighting is the acme of skill.