#include // used for printing to the terminal using namespace std; // required by gcc3 #include #include "SDL.h" // include the SDL headers #include // include GL headers #include SDL_Surface *mainWindow; int videoFlags; double rotateY = 0.0; // holds the y rotation value double rotateX = 0.0; // holds the x rotation value bool initializeSDL(); // return true on sucess bool createWindow(); // returns true on sucess bool initializeGL(); // return true on sucess void renderScene(); // render our scene 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 // initalize opengl // returns false it theres an error if (!initializeGL()) return 1; // return an error code of 1 bool done = false; // loop variable SDL_Event event; // hold the SDL event // loop until the user quits while (!done) { // see if SDL has recieved and event // the event could be a keyboard/mouse // event, or notification that the // window was closed minimized // this returns true if an event is waiting while (SDL_PollEvent(&event)) { switch (event.type) // what type of event { case SDL_QUIT: // user click on the X to close th e window done = true; // quit break; case SDL_KEYDOWN: // user pressed a key switch (event.key.keysym.sym) // which key ? { case SDLK_q: // q key ? done = true; // quit break; } break; default: break; } } // draw the scene renderScene(); } 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; } bool initializeGL() { glEnable(GL_DEPTH_TEST); // enable depth testing glEnable(GL_CULL_FACE); // enable backface culling glClearColor (0.0, 0.0, 0.0, 1.0); // clear color glViewport(0, 0, 800, 600); // set the viewport to the size of out window glMatrixMode(GL_PROJECTION); // load the projection matrix glLoadIdentity(); // remove all transformations // set the camera in perspective mode gluPerspective(45.0f, // angle 800.0 / 600.0, // ratio of width / height 1.0, // near clipping plane 250.0); // far clipping plane glMatrixMode(GL_MODELVIEW); // go back to the modelview matrix and prepare to draw glLoadIdentity(); // remove all the transformations return true; // everything was created ok } void renderScene() { // clear the color and depth buffers glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // delete our old transformations glLoadIdentity(); // start drawing ! // we are going to draw in trinagles // translate our object back into the screen so we can see it glTranslatef(0.0, 0.0, -25.0); // rotate our object about the y axis glRotatef(rotateY, 0, 1, 0); // rotate our object about the x axis glRotatef(rotateX, 1, 0, 0); glBegin(GL_TRIANGLES); //front face glColor3f(0.0, 0.0, 1.0); // change the color to blue glVertex3f(-5.0, -5.0, 5.0); glVertex3f(5.0, -5.0, 5.0); glVertex3f(5.0, 5.0, 5.0); glVertex3f(5.0, 5.0, 5.0); glVertex3f(-5.0, 5.0, 5.0); glVertex3f(-5.0, -5.0, 5.0); // back face glColor3f(1.0, 0.0, 0.0); // red glVertex3f(5.0, -5.0, -5.0); glVertex3f(-5.0, -5.0, -5.0); glVertex3f(-5.0, 5.0, -5.0); glVertex3f(-5.0, 5.0, -5.0); glVertex3f(5.0, 5.0, -5.0); glVertex3f(5.0, -5.0, -5.0); // right side glColor3f(0.0, 1.0, 0.0); // green glVertex3f(5.0, -5.0, 5.0); glVertex3f(5.0, -5.0, -5.0); glVertex3f(5.0, 5.0, -5.0); glVertex3f(5.0, 5.0, -5.0); glVertex3f(5.0, 5.0, 5.0); glVertex3f(5.0, -5.0, 5.0); // left side glColor3f(1.0, 0.0, 1.0); // purple glVertex3f(-5.0, -5.0, -5.0); glVertex3f(-5.0, -5.0, 5.0); glVertex3f(-5.0, 5.0, 5.0); glVertex3f(-5.0, 5.0, 5.0); glVertex3f(-5.0, 5.0, -5.0); glVertex3f(-5.0, -5.0, -5.0); // top face glColor3f(1.0, 1.0, 0.0); // yellow glVertex3f(-5.0, 5.0, 5.0); glVertex3f(5.0, 5.0, 5.0); glVertex3f(5.0, 5.0, -5.0); glVertex3f(5.0, 5.0, -5.0); glVertex3f(-5.0, 5.0, -5.0); glVertex3f(-5.0, 5.0, 5.0); // bottom face glColor3f(0.0, 1.0, 1.0); // blue-green glVertex3f(-5.0, -5.0, -5.0); glVertex3f(5.0, -5.0, -5.0); glVertex3f(5.0, -5.0, 5.0); glVertex3f(5.0, -5.0, 5.0); glVertex3f(-5.0, -5.0, 5.0); glVertex3f(-5.0, -5.0, -5.0); glEnd(); // done drawing // swap the buffers so we can see what was drawn ! SDL_GL_SwapBuffers(); // increment our Y rotation value rotateY += 0.1; rotateX += 0.1; }