Read me About Demoscene? Links Contact

Quick guide to setting up SDL on Windows

Hi all,

SDL has proven to be one of the simplest ways of preparing a framework for coding OpenGL. It handles all the borring details of setting up windows and event handlers. Below is a source snippet that illustrates how I code my entry function in Windows programs:





#define FULLSCREEN

#define WIDTH 1024

#define HEIGHT 768



bool ShouldQuit()

{

SDL_Event e;

while (SDL_PollEvent(&e))

{

if (e.type == SDL_QUIT)

{

return true;

}

else if ( e.type == SDL_KEYDOWN && e.key.keysym.sym == SDLK_ESCAPE)

{

return true;

}

}

return false;

}

int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)

{

try

{

if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO) != 0) {

throw SDL_GetError();

}

SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );

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_ALPHA_SIZE, 8 );

SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 32 );

#ifdef FULLSCREEN

SDL_Surface* screen = SDL_SetVideoMode( WIDTH, HEIGHT, 32, SDL_OPENGL | SDL_FULLSCREEN );

#else

SDL_Surface* screen = SDL_SetVideoMode( WIDTH, HEIGHT, 32, SDL_OPENGL );

#endif



while (!ShouldQuit())

{

DrawStuff();

SDL_GL_SwapBuffers();

}

}

catch (const char* msg)

{

MessageBox(0, msg, “Error”, MB_ICONERROR);

}

return 0;

}