Home arrow Multimedia arrow Page 4 - Using OpenGL with SDL for Game Programming

OpenGL with SDL in Action - Multimedia

In the world of gaming, SDL provides the entire necessary infrastructure. This would have become clear from previous articles in this series. Infrastructure is to a game what a skeleton is to a human body. But without muscles, no locomotion is possible. So working with the body analogy, SDL provides the skeletal structure to build the game whereas the flesh, blood and skin are provided by 2D and 3D graphics libraries.

TABLE OF CONTENTS:
  1. Using OpenGL with SDL for Game Programming
  2. OpenGL: What is it?
  3. Initialization: Bringing OpenGL into the Picture
  4. OpenGL with SDL in Action
By: A.P.Rajshekhar
Rating: starstarstarstarstar / 9
May 16, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

The theory is over. It's now time to see some real action. The example application will render a rotating triangle. The includes will contain one more header file.

#include <SDL/SDL.h>
#include <gl/gl.h>

gl.h contains function declarations necessary for working with OpenGL.

Next comes the main() and OpenGL attributes.

int main(int argc, char *argv[])
{
  
SDL_Event event;
  
float theta = 0.0f;
  
SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8 );
  
SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8 );
  
SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE,8 );
  
SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32 );

   :
  
:
}

Next we initialize the video and video mode

int main(int argc, char *argv[])
{
  
SDL_Event event;
  
float theta = 0.0f;
  
SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8 );
  
SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8 );
  
SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE,8 );
  
SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32 );

   SDL_Init(SDL_INIT_VIDEO);
  
SDL_SetVideoMode(600, 300, 0, SDL_OPENGL | SDL_HWSURFACE | SDL_NOFRAME);

   :
  
:
}

The video is initialized to 600x300 resolutions. And the hardware rendering mode is being used. This is done by the SDL_HWSURFACE flag. Hence OpenGL would write on the graphic card's memory instead of mapping it to software memory. After this step, we move into the territory of OpenGL.

int main(int argc, char *argv[])
{
   SDL_Event event;
  
float theta = 0.0f;
  
SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8 );
  
SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8 );
  
SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE,8 );
  
SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32 );

   SDL_Init(SDL_INIT_VIDEO);
  
SDL_SetVideoMode(600, 300, 0, SDL_OPENGL | SDL_HWSURFACE | SDL_NOFRAME);

   glViewport(0, 0, 600, 300);
  
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  
glClearDepth(1.0);
  
glDepthFunc(GL_LESS);
  
glEnable(GL_DEPTH_TEST);
  
glShadeModel(GL_SMOOTH);
  
glMatrixMode(GL_PROJECTION);
  
glMatrixMode(GL_MODELVIEW);
  
:
  
:
}

To start working with OpenGL, the view port is initialized. Then the screen is cleared or rendered with the specified background color. Since the triangle would be rotating in 3D space, the depth has to be set and depth testing has to be enabled. If smooth shading is not used, then the edges would seem jagged. Hence the smooth shading model is used.

This completes the setting up of OpenGL parameters after SDL video initialization. Drawing and rotation is taken care of by the following code in bold:

int main(int argc, char *argv[])
{
  
SDL_Event event;
  
float theta = 0.0f;
  
SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8 );
  
SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8 );
  
SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE,8 );
  
SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32 );

   SDL_Init(SDL_INIT_VIDEO);
  
SDL_SetVideoMode(600, 300, 0, SDL_OPENGL | SDL_HWSURFACE | SDL_NOFRAME);

   glViewport(0, 0, 600, 300);
  
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  
glClearDepth(1.0);
  
glDepthFunc(GL_LESS);
  
glEnable(GL_DEPTH_TEST);
  
glShadeModel(GL_SMOOTH);
  
glMatrixMode(GL_PROJECTION);
  
glMatrixMode(GL_MODELVIEW);

   int done;
  
for(done = 0; !done;)
  
{
     
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

      glLoadIdentity();
     
glTranslatef(0.0f,0.0f,0.0f);
     
glRotatef(theta, 0.0f, 0.0f, 1.0f);
     
:
     
:
   }
}

The focus is brought to the point of origin by translating it. Then rotation function is provided with the theta value through which the triangle has to be rotated.

int main(int argc, char *argv[])
{
  
SDL_Event event;
  
float theta = 0.0f;
  
SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8 );
  
SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8 );
   SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE,8 );
   SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32 );

   SDL_Init(SDL_INIT_VIDEO);
  
SDL_SetVideoMode(600, 300, 0, SDL_OPENGL | SDL_HWSURFACE | SDL_NOFRAME);

   glViewport(0, 0, 600, 300);
  
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  
glClearDepth(1.0);
  
glDepthFunc(GL_LESS);
  
glEnable(GL_DEPTH_TEST);
  
glShadeModel(GL_SMOOTH);
  
glMatrixMode(GL_PROJECTION);
  
glMatrixMode(GL_MODELVIEW);

   int done;
  
for(done = 0; !done;)
  
{
     
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

     glLoadIdentity();
    
glTranslatef(0.0f,0.0f,0.0f);
    
glRotatef(theta, 0.0f, 0.0f, 1.0f);
    
glBegin(GL_TRIANGLES);
    
glColor3f(1.0f, 0.0f, 0.0f);
    
glVertex2f(0.0f, 1.0f);
    
glColor3f(0.0f, 1.0f, 0.0f);
    
glVertex2f(0.87f, -0.5f);
    
glColor3f(0.0f, 0.0f, 1.0f);
    
glVertex2f(-0.87f, -0.5f);
    
glEnd();

     theta += .5f;
    
SDL_GL_SwapBuffers();
    
:
    
:
  
}
}

The triangle is drawn by specifying the vertices. Then the theta value is increased. Next the event handling part comes into play.

int main(int argc, char *argv[])
{
  
SDL_Event event;
  
float theta = 0.0f;
  
SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8 );
  
SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8 );
  
SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE,8 );
  
SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32 );

   SDL_Init(SDL_INIT_VIDEO);
  
SDL_SetVideoMode(600, 300, 0, SDL_OPENGL | SDL_HWSURFACE | SDL_NOFRAME);

   glViewport(0, 0, 600, 300);
  
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  
glClearDepth(1.0);
  
glDepthFunc(GL_LESS);
  
glEnable(GL_DEPTH_TEST);
  
glShadeModel(GL_SMOOTH);
  
glMatrixMode(GL_PROJECTION);
  
glMatrixMode(GL_MODELVIEW);

   int done;
  
for(done = 0; !done;)
  
{
    
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

     glLoadIdentity();
    
glTranslatef(0.0f,0.0f,0.0f);
    
glRotatef(theta, 0.0f, 0.0f, 1.0f);
    
glBegin(GL_TRIANGLES);
    
glColor3f(1.0f, 0.0f, 0.0f);
    
glVertex2f(0.0f, 1.0f);
    
glColor3f(0.0f, 1.0f, 0.0f);
    
glVertex2f(0.87f, -0.5f);
    
glColor3f(0.0f, 0.0f, 1.0f);
    
glVertex2f(-0.87f, -0.5f);
    
glEnd();

     theta += .5f;
    
SDL_GL_SwapBuffers();
    
SDL_PollEvent(&event);
    
if(event.key.keysym.sym == SDLK_ESCAPE)
      
done = 1;
   }
}

That's it. This is how SDL and OpenGL work together. Though this section covers the technical details of OpenGL, the complete steps for using OpenGL are not discussed. That will be the topic of the next discussion.



 
 
>>> More Multimedia Articles          >>> More By A.P.Rajshekhar
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

MULTIMEDIA ARTICLES

- Basic Lighting in OpenGL and SDL Game Progra...
- Working with Colors in OpenGL for Game Progr...
- Animation in OpenGL for Game Programming usi...
- Game Programming with SDL: Getting Started w...
- Using OpenGL with SDL for Game Programming
- Learning Sound for Game Programming using SDL
- Game Programming using SDL: Raw Graphics and...
- Game Programming using SDL: Getting Started
- Network Radio With Icecast
- Learning To SMILe

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: