Eclipse C++ installing SDL & freeglut

Installing OpenGL for C++ Elcipse Linux:

Install SDL lib which provides low level access to audio, keyboard, mouse, joystick, 3D hardware via OpenGL

Install freeglut which is a alternative to OpenGL

sudo apt-get install libsdl1.2-dev libsdl-image1.2-dev libsdl-mixer1.2-dev libsdl-ttf2.0-dev
sudo apt-get install freeglut3 freeglut3-dev gcc g++

While in Eclipse create a new C++ project choosing File -> New -> C++ Project, choose Hello World with gcc compiler.

When new project is opened, right click project in project explorer and choose properties. Under C/C++ Build -> Settings choose GCC C++ Linker -> Libraries include glut, GL, GLU, SDL by Add.. function

Code example:

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

#define window_width  640
#define window_height 480

bool key[321];

bool events()
  SDL_Event event;
  if( SDL_PollEvent(&event) ){
   switch( event.type ){
     case SDL_KEYDOWN : key[ event.key.keysym.sym ]=true ;   break;
     case SDL_KEYUP   : key[ event.key.keysym.sym ]=false;   break;
     case SDL_QUIT    : return false; break;
 return true;

void main_loop_function(){
  float angle;
  float y_offset;
     while( events() ){
       glTranslatef(0,0, -10);
       glRotatef(angle, 0, 0, 1);
       glColor3ub(255, 000, 000); glVertex2f(-1,  1+y_offset);
       glColor3ub(000, 255, 000); glVertex2f( 1,  1+y_offset);
       glColor3ub(000, 000, 255); glVertex2f( 1, -1+y_offset);
       glColor3ub(255, 255, 000); glVertex2f(-1, -1+y_offset);

       if( key[SDLK_RIGHT] ){ angle-=0.5; }
       if( key[SDLK_LEFT ] ){ angle+=0.5; }
       if( key[SDLK_UP]){ y_offset+=0.2; }
       if( key[SDLK_DOWN]){ y_offset-=0.2; }

void GL_Setup(int width, int height) {
  glViewport( 0, 0, width, height );
  glMatrixMode( GL_PROJECTION );
  glEnable( GL_DEPTH_TEST );
  gluPerspective( 45, (float)width/height, 0.1, 100 );
  glMatrixMode( GL_MODELVIEW );

int main(){
  const SDL_VideoInfo* info = SDL_GetVideoInfo();
  if (info->hw_available) {vidFlags |= SDL_HWSURFACE;}
    else {vidFlags |= SDL_SWSURFACE;}
  int bpp = info->vfmt->BitsPerPixel;
  SDL_SetVideoMode(window_width, window_height, bpp, vidFlags);
  GL_Setup(window_width, window_height);

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s