Articles » Amiga OS 4 Articles » OpenGL/MiniGL Templates » A Basic SDL Based OpenGL Application

A Basic SDL Based OpenGL Application

Unaltered, this template renders a rotating coloured triangle. Nothing fancy; nothing particularly original; this template simply provides a makefile (and Visual Studio project for Windows) and an SDL framework providing the bare essentials in order to display an animation. The locations at which you should insert code, or replace existing code with your own code have been highlighed using comments. Just search for "insert" or "replace." The rest of this article covers the basic sections of the template code.

This template includes a project file for Visual Studio 2008 (i.e., Windows), and a makefile for Amiga OS 4.x+. Users of other systems should be able to modify the makefile for their systems easily.

NOTE: For more serious work, I would recommend using the frame-rate independent template, as it is more versatile, and does not hog CPU time. However, this template is the easiest to get started with.

Initialization and Opening a Window

In SDL, initializing and opening an OpenGL window is achieved as follows:

// Initialize
SDL_Init(SDL_INIT_VIDEO);

// Enable double-buffering
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);

// Create a OpenGL window
screen = SDL_SetVideoMode(WIDTH, HEIGHT, 0, SDL_OPENGL |
SDL_RESIZABLE);
if(!screen)
{
printf("Couldn't set %dx%d GL video mode: %s\n", WIDTH,
HEIGHT, SDL_GetError());
SDL_Quit();
exit(2);
}
SDL_WM_SetCaption(WINDOW_TITLE, WINDOW_TITLE);

The one difference between a plain SDL application, and an OpenGL one, is the SDL_OPENGL flag passed in the SDL_SetVideoMode() function call; everything else is the same.

The Main Loop (Handling Events)

Unlike GLUT, SDL requires that the programmer write their own main loop. This gives more flexibility at the cost of writing more code. The loop must check for events and redraw the screenwhen appropriate. For example, this template's main loop is:

BOOL done = FALSE;
while(!done)
{
SDL_Event event;
    // Rotates the triangle (this could be replaced with custom
// processing code)
animate();

// Respond to any events that occur
while(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_VIDEORESIZE:
screen = SDL_SetVideoMode(event.resize.w,
event.resize.h, 0,
SDL_OPENGL | SDL_RESIZABLE);
if(screen)
{
reshape(screen->w, screen->h);
}
else
{
; // Oops, we couldn't resize for some reason.
// This should never happen
}
break;

case SDL_QUIT:
done = TRUE;
break;

// ## INSERT CODE TO HANDLE ANY OTHER EVENTS HERE ##
}
}

// Check for escape
keys = SDL_GetKeyState(NULL);
if( keys[SDLK_ESCAPE] ) {
done = TRUE;
}

// Draw the screen
render();
}

Exiting the Program

SDL needs to perform clean up operations before the application/game exit. This is performed via a single function call:

SDL_Quit(); 

Compiling

Simply download (here) and decompress the archive to a directory. Windows users can open the Visual Studio solution file. Amiga users should open a shell (console) window and enter that directory, and type gmake. Provided that you have the SDK and the MiniGL header files installed, it will compile and produce a binary called GLUT-basic. You can run the template application by typing GLUT-basic and pressing enter. An example of the DOS commands is:

cd SDL-GL-basic
gmake
SDL-GL-basic

Pressing ESCAPE quits the template application.

IMPORTANT: Amiga developers should use gmake, not make, to build this template. 

Download

Learning More About OpenGL

Whilst online tutorials and template code such as the above are important aids to learning OpenGL programming, having the right books is equally important. A list of recommended books can be found here.

 





Articles » Amiga OS 4 Articles » OpenGL/MiniGL Templates » A Basic SDL Based OpenGL Application