HomeMultimedia Page 4 - Game Programming with SDL: Getting Started with OpenGL
Adding OpenGL - Multimedia
SDL helps to provide a framework on which to build games. OpenGL is the graphic library most commonly used with this framework. This article shows you how these two technologies work together.
Next come the OpenGL parts -- creating an OpenGL window. In other words, this section deals with initializing OpenGL. But the initialization needs updating as it is created. Hence the reshape function :
void ReshapeGL(int width, int height) // reshape the window when it's moved or resized { glViewport(0,0,(GLsizei)(width),(GLsizei)(height)); // reset the current viewport glMatrixMode(GL_PROJECTION); // select the projection matrix glLoadIdentity(); // reset the projection matrix gluPerspective(45.0f,(GLfloat)(width)/(GLfloat)(height),1.0f,100.0f); // calculate the aspect ratio of the window glMatrixMode(GL_MODELVIEW); // select the modelview matrix glLoadIdentity(); // reset the modelview matrix return; }
bool CreateWindowGL(int W, int H, int B, Uint32 F) // This Code Creates Our OpenGL Window { SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5 ); // In order to use SDL_OPENGLBLIT we have to SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 5 ); // set GL attributes first SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5 ); SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 ); SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); // colors and double buffering
if(!(Screen = SDL_SetVideoMode(W, H, B, F))) // SDL_SetVideoMode to create the window { return false; // if it fails, return false }
SDL_FillRect(Screen, NULL, SDL_MapRGBA(Screen->format,0,0,0,0)); ReshapeGL(SCREEN_W, SCREEN_H); // calling reshape as the window is created return true; // return true (initialization successful) }
I will be discussing the APIs used in the resize function in the next article. Next is the draw function. It also contains the test code:
void Draw3D(SDL_Surface *S) // OpenGL drawing code here { glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear screen and depth buffer. Screen color has been cleared at init glLoadIdentity(); // reset the modelview matrix glBegin(GL_TRIANGLES); glVertex3f( 0.0f, 1.0f, 0.0f); glVertex3f(-1.0f,-1.0f, 0.0f); glVertex3f( 1.0f,-1.0f, 0.0f); glEnd();
glFlush(); // flush the gl rendering pipelines
return; }
Now we move on to the main(). It contains the keyboard handling code. It checks for each key press and processes it accordingly.
int main(int argc, char **argv) { SDL_Event E; // and event used in the polling process Uint8 *Keys; // a pointer to an array that will // contain the keyboard snapshot Uint32 Vflags; // video flags
Screen = NULL; Keys = NULL; Vflags = SDL_HWSURFACE|SDL_OPENGLBLIT;//a hardware // surface and special openglblit mode // so we can even blit 2d graphics in our opengl scene
if(SDL_Init(SDL_INIT_VIDEO)<0)// initializing the sdl // library, the video subsystem
{ printf("Unable to open SDL: %sn", SDL_GetError() );// if sdl can't //be initialized exit(1); }
atexit(SDL_Quit);// sdl's been inited, now making // sure that sdl_quit will be // called in case of exit()
if(!CreateWindowGL(SCREEN_W, SCREEN_H, SCREEN_BPP, Vflags)) // video flags are set, creating the window { printf("Unable to open screen surface: %sn", SDL_GetError() ); exit(1); }
if(!InitGL(Screen))// calling the OpenGL init function { printf("Can't init GL: %sn", SDL_GetError() ); exit(1); }
while(isProgramLooping)// and while it's looping { if(SDL_PollEvent(&E)) { switch(E.type)// and processing it {
case SDL_QUIT://check whether it's a quit event? { isProgramLooping = false; break; }
case SDL_VIDEORESIZE:// or it's a resize event? { ReshapeGL(E.resize.w, E.resize.h); break; }
case SDL_KEYDOWN:// check which key has been pressed { Keys = SDL_GetKeyState(NULL); break; } }
}
Draw3D(Screen); SDL_GL_SwapBuffers(); // and swap the buffers (since double buffering is being used) }
Deinitialize(); exit(0); // And finally exit() so calling call sdl_quit
return 0; }
That brings us to the end of this discussion. This time it was a bit lengthy. But the framework that has been developed will work as the foundation for developing functionalities like lighting, texture mapping, animation and so on. The next topic will be using timers in animating the triangle just drawn. Till next time.