#include // used for printing to the terminal using namespace std; // required by gcc3 #include #include "SDL.h" // include the SDL headers SDL_Surface *mainWindow; int videoFlags; bool initializeSDL(); // return true on sucess bool createWindow(); // returns true on sucess int main(int argc, char **argv) { // call our function to initialize SDL // if it return false, SDL could not // be initialized, so quit if (!initializeSDL()) return 1; // return an error code of 1 // call our function to create a window // if it returns false, there was an error if (!createWindow()) return 1; // return an error code of 1 sleep(10); return 0; // all done } bool initializeSDL() // returns true on sucess { // start off initialized SDL, for now // we will only initialize video // (keyboard and mouse is always initialized) if (SDL_Init(SDL_INIT_VIDEO) < 0) // check for an error { cerr << "Failed initializing SDL Video: " << SDL_GetError() << endl; return 1; // return an error } // this is very important, make sure SDL Quits ! atexit(SDL_Quit); // now it is time to setup some video flags // we can pass as many flags as we want // some falgs include whether we want // fullscreen/window mode, hardware acceleration, // and opengl support. Check the SDL (www.libsdl.org) // documentation for more information on the flags // // For our example we want opengl support (SDL_OPENGL), // and a hardware palette (SDL_HWPALETTE) // Later we will check to see if a hardware surface is // supported and also if we have hardware acceleration // videoFlags = SDL_OPENGL; // opengl Support videoFlags |= SDL_HWPALETTE; // we are now going to query the video info // from here we can check whether a hardware // surface is available and if we have // hardware acceleration // const SDL_VideoInfo *videoInfo = SDL_GetVideoInfo(); if (!videoInfo) // check for error { cerr << "Failed getting video info: " << SDL_GetError() << endl; return false; // return failure } // if we are here, we have successfully querried the // video info // let now check to see if we have a hardware surface if (videoInfo->hw_available) videoFlags |= SDL_HWSURFACE; // use a hardware surface else videoFlags |= SDL_SWSURFACE; // fallback to software // check for hardware blitting if (videoInfo->blit_hw) videoFlags |= SDL_HWACCEL; // enable hardware blitting // this next part is for use only if you are using // openGL. This basically sets a couple opengl // attributes, such as whether we want double buffering // which is a must have for opengl or you will get flickering SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); // enable double buffering // set the depth to 24. Idealy this would NOT be hard coded // but for this example, we are going to assume the user // is running at a depth of 24 colors SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24); // set depth buffer SDL_GL_SetAttribute(SDL_GL_ACCUM_RED_SIZE, 0); SDL_GL_SetAttribute(SDL_GL_ACCUM_GREEN_SIZE, 0); SDL_GL_SetAttribute(SDL_GL_ACCUM_BLUE_SIZE, 0); SDL_GL_SetAttribute(SDL_GL_ACCUM_ALPHA_SIZE, 0); // we are now finished setting up SDL // all we need to do now is return true // telling main that everything went ok return true; } bool createWindow() // return true on success { // use SDL_SetVideMode to create the window // this functions takes in the width, height, // depth and video flags // We have hard-coded the width, height, and // depth. This is not ideal for a real program // also make sure that your depth here matches // the depth from the initializeSDL function: // SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24); // // This function returns a reference to and SDL_Surface mainWindow = SDL_SetVideoMode(800, // width 600, // height 24, // depth, make sure it matches ! videoFlags); // define from initializeSDL function if (!mainWindow) // check for errors { cerr << "Failed to create window: " << SDL_GetError() << endl; return false; } // if we are here, the window has been created // this next function gives the window a title SDL_WM_SetCaption("SDL Test Window", "SDL Test Window"); // everything was created OK, so return true return true; }