Multimedia
  Home arrow Multimedia arrow Page 4 - Using OpenGL with SDL for Game Programming
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
MULTIMEDIA

Using OpenGL with SDL for Game Programming
By: A.P.Rajshekhar
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 6
    2007-05-16


    Table of Contents:
  • Using OpenGL with SDL for Game Programming
  • OpenGL: What is it?
  • Initialization: Bringing OpenGL into the Picture
  • OpenGL with SDL in Action

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Using OpenGL with SDL for Game Programming - OpenGL with SDL in Action
    ( Page 4 of 4 )

    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
     

       

    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





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    Stay green...Green IT