Multimedia
  Home arrow Multimedia arrow Page 4 - Game Programming with SDL: Getting Started with OpenGL
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? 
Google.com  
MULTIMEDIA

Game Programming with SDL: Getting Started with OpenGL
By: A.P.Rajshekhar
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 25
    2007-07-10


    Table of Contents:
  • Game Programming with SDL: Getting Started with OpenGL
  • OpenGL: Basic Steps
  • SDL-Based Framework: Creating and Testing
  • Adding OpenGL

  • 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


    Game Programming with SDL: Getting Started with OpenGL - Adding OpenGL
    ( Page 4 of 4 )

    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, t
    he 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);
       }

       if(!Initialize())
       {
        
    printf("App init failed: %sn", SDL_GetError() ); exit(1);
       }

       isProgramLooping = true;

       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.



     
     
    >>> 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 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek