Getting Started With SDL/OpenGL
Welcome to the first tutorial on programming some OpenGL! Before we go into the specifics of drawing to our graphics device, we need to cover some quick ground in setting the system up to properly communicate with our game.
Due to the fact that OpenGL is a cross-platform library, meaning it can run under several different operating systems as well as different hardware architectures, it only makes a little more sense to try and take advantage of a cross-platform library to handle interaction with the player. Luckily such a technology is available and is known as the “Simple DirectMedia Layer” or just “SDL”.
The SDL Code
Before diving into working with OpenGL to draw with your hardware, you first have to lay the groundwork of playing nice
with the host operating system. There is not that much to worry about as SDL does a lot of the grunt work for you.
We just need the basics to get your game initialized and registered properly with the operating system.
You only have two basic things to worry about: using the main, and your “main loop” which continuously updates your game until you exit it.
Browsing through the project source code will provide a working example of what we are trying to accomplish
with our tiny SDL framework, but I’ll try to explain the basic principles here.
The main workhorse of the sample, is the GameEngineCore object which is a
singleton object
in order to ensure that there’s one and only one instance of GameEngineCore in our application. If you have never
heard or seen a singleton object before, then just think of it as a fancy global object which ensures that
only one instance is used in our application.
You will notice in the included main.cpp file, that we are defining our entry point with the
main function call.
int main(int argc, char* argv[])
{
new GameEngineCore();
MyApp* pApp = new MyApp();
GameEngineCore::getSingletonPtr()->setMainApplication( pApp );
if(!GameEngineCore::getSingletonPtr()->loadGameEngine( “Getting Started” ))
{
goto quit_game; //dear god! a goto!
}
int val = GameEngineCore::getSingletonPtr()->runGameEngine();
quit_game:
delete GameEngineCore::getSingletonPtr();
delete pApp;
return val;
}
When you are developing any and every SDL application, you have to use this function
stub as your main entry point in order to allow SDL to properly initialize itself for the operating system you’re running
on.
Although it is empty for this tutorial, the MyApp class is inherited from the IMainApplication
object which is the only object we actually care about for our game. It is in MyApp where we initialize our
game-specific objects, where we update our objects every game cycle, as well as draw what we need to our graphics
hardware. Finally, when we exit the game, this object also does the housekeeping for the objects we created.
Back to the main.cpp function, we are first declaring our singleton instance of GameEngineCore,
then creating an instance of our MyApp object. We then pass this instance into the GameEngineCore
for it to use internally.
int iWidth = 800;
int iHeight= 600;
int iBits = 16;
//start up SDL
if(SDL_Init( SDL_INIT_EVERYTHING ) < 0 ||
!SDL_GetVideoInfo() )
{
unloadGameEngine();
return false;
}
//create an initial window
SDL_SetVideoMode( 800, 600,
SDL_GetVideoInfo()->vfmt->BitsPerPixel,
SDL_RESIZABLE );
SDL_WM_SetCaption(strWindowTitle.c_str(),
strWindowTitle.c_str());
Via the call to SDL_Init function, the GameEngineCore object initializes itself with
the local operating system, taking care of any os-specific registration or initialization. Once this is completed, the
SDL_SetVideoMode function is used to create a basic window for us.
bool done = false;
SDL_Event event;
while(! done)
{
while( SDL_PollEvent(&event) )
{
switch ( event.type )
{
case SDL_QUIT :
done = true;
break;
default:
break;
}
}
if(m_pMainApp)
{
m_pMainApp->onUpdateApp();
m_pMainApp->onRenderApp();
}
}
unloadGameEngine();
Finally, the program is put into a continuous loop within the runGameEngine method. This method is
responsible for constantly checking to see if you have tried to exit the application (eg. by closing the window). If the
app is still running, then update and draw all the game objects. If you have closed the window, then this loop terminates
and the cleanup of your application and SDL is initiated.
Enjoy the code, and make sure to play with it somewhat to figure the basics out!



This is default description text on Padangan Themes, of course you can change this text via you profile administration.