Multimedia
  Home arrow Multimedia arrow Page 4 - Using OpenGL with SDL for Game Program...
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
OLM
 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: 5 stars5 stars5 stars5 stars5 stars / 3
    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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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

    PCmover - $15 Off with Coupon Code CJPH7Q

    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.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · In this article I have discussed how to use SDL with OpenGL. Please do comment.
       · Excellent tutorial! I found it very useful.If the above code doesn't work & it...
       · I looked for hours for a good tutorial such as this one. thank you
     

       

    MULTIMEDIA ARTICLES

    - 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

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway