Multimedia
  Home arrow Multimedia arrow Page 3 - 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 - Initialization: Bringing OpenGL into the Picture
    ( Page 3 of 4 )

    In SDL all the sub-systems are initialized via SDL_Init(). OpenGL, being a part of the graphics subsystem, is not directly initialized in this manner. For initializing OpenGL, these three steps should be followed: 

    1. Set OpenGL Attributes
    2. Specify use of Double Buffering
    3. Set the Video Mode

    Of these, the second one is optional as it is used only when double buffering is a requirement. Let's have a detailed look at all of them.

    Setting the OpenGL Attributes

    Before initializing the video, it is better to set up the OpenGL attributes. These attributes are passed to OpenGL via SDL calling the SDL_GL_SetAttribute() function. The parameters are the OpenGL attribute and their values. The most common attributes passed to this function are:

    1. SDL_GL_RED_SIZE:

    It sets the size of the red component of the frame buffer. The value is in bits. The most commonly used value is 5. Similar parameters exist for blue and green components which are SDL_GL_BLUE_SIZE and SDL_GL_GREEN_SIZE respectively. To set green component to a bit value of 4 the code would be:

    SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 4 );

    2. SDL_GL_BUFFER_SIZE:

    To set the size of the frame buffer this attribute is passed with the required Buffer size in bits. It is greater than or equal to the combined value i.e. sum of the red, green, blue and alpha components. If the requirement is 24 bit color depth and an alpha channel of 32 bits then each color component must be given the size value of 8 and the frame buffer must be given a size of 32. In code it would be:

    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 );

    3. SDL_GL_DEPTH_SIZE:

    This attribute controls the size of the depth buffer or Z buffer. Normally graphics cards provide 16-bit or 24-bit depth. If the value is set higher than what is available, the operation will fail. To make it more clear, check out the following code:

    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE,16);

    4. SDL_GL_ACCUM_RED_SIZE:

    To set the size of the red component's accumulation buffer, this attribute is used.

    The value is specified in bits. SDL_GL_ACCUM_BLUE_SIZE, SDL_GL_ACCUM_GREEN_SIZE controls the size of blue and green components' accumulation buffer. In code it would be:

    SDL_GL_SetAttribute(SDL_GL_ACCUM_RED_SIZE,5);

    You are probably wondering right now whether these attributes could be set after initializing the video mode. The answer is no. The reason is that these settings have to be initialized before invoking and configuring the video mode. Next we have to set up the video mode.

    Setting up double buffering

    This aspect must also to be covered before setting up the video mode as this attribute goes as a flag parameter to the SDL_GL_SetAttribute. The attribute is SDL_GL_DOUBLEBUFFER and the value is either 1 or 0. The point to be kept in mind is that when working in conjunction with OpenGL, the flag specifying the double buffer must be passed as an attribute to the SDL_GL_SetAttribute function and not to the SDL_Set_VideoMode(). In code this would be:

    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1);

    would set the double buffering to one state.

    Setting the Video Mode

    Once the OpenGL attributes are set, setting the video mode is similar to the setting of video mode as described in previous tutorials. For more details have a look at the second article in this series. The only difference comes in flags being sent to the SDL_Set_VideoMode(). Apart from other required flags the SDL_OPENGL would also be set i.e.

    int flags=0;
    flags= SDL_OPENGL | SDL_FULLSCREEN;

    Setting the SDL_OPENGL flag is a must.

    That covers all the required steps, Now let's see OpenGL at play.



     
     
    >>> 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 1 Hosted by Hostway
    Stay green...Green IT