We will use SDL to open our window and handle keyboard/mouse input.
The first step is to include the correct headers as shown below:
#include <iostream> // used for printing to the terminal
using namespace std; // required by gcc3
#include "SDL.h" // include the SDL headers
int main(int argc, char **argv)
{
return 0;
}
There are a couple variables that we will need to use. It's normally bad to have global variables, but to keep this tutorial simple, we are going to use a couple.
....
#include "SDL.h" // include the SDL headers
SDL_Surface *mainWindow;
int videoFlags;
int main(int argc, char **argv)
....
Every window in SDL is an SDL_Surface. The *mainWindow variable is a pointer to our mainWindow which we will initialize later.
The videoFlags variable contains extra flags that we want to pass to SDL when we create a window. Some example flags would be: fullscreen/window mode, opengl support, hardware acceleration, etc ..
The next step is to initialize SDL. We are going to create a new function called initializeSDL() that will do just that. initializeSDL() returns false if SDL failed for some reason. Let's add the prototype and the call for our new function:
....
int videoFlags;
bool initializeSDL(); // return true on sucess
int main(int argc, char **argv)
....
then create the new function after main
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;
}
Now we need to create another function that will actually create the window. This function is very short and simple
....
bool initializeSDL(); // return true on sucess
bool createWindow(); // returns true on sucess
int main(int argc, char **argv)
....
Call the function in main:
....
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
return 0; // all done
....
Create the actual function after the initializeSDL function:
....
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;
}
....
This opens our SDL/OpenGL window then immediately exits. Let's put a call in the main after we create the window to sleep for 10 seconds, then let the program end. We need to include the header unistd.h to use the sleep function. In the next tutorial, we will initialize openGL and draw the pyramid !
#include <iostream> // used for printing to the terminal
using namespace std; // required by gcc3
#include <unistd.h>
#include "SDL.h" // include the SDL headers
....
if (!createWindow())
return 1; // return an error code of 1
sleep(10);
return 0; // all done
....
To compile this program under Linux use:
g++ -o SDLIntro SDLIntro.cpp `sdl-config --cflags --libs` -lGL -lGLU
Download the source code here