Bright Hub
 
Matthew Casperson's Hubfolio

SDL Programming Tutorial - Getting Started

Article by Matthew Casperson (4,911 pts )
Published on Dec 14, 2009

In this tutorial we will look at getting a basic SDL application up and running, as well as creating the framework that will support our game as it progresses.

EngineManager.cpp / EngineManager.h

We will start with the EngineManager class. This class will be responsible for initialising SDL, maintaining the objects that will make up the game and distributing events.

The first thing we need to do is call SDL_Init. This loads the SDL library, and initialises any of the subsystems that we specify. In this case we have specified that everything be initialised by supplying the SDL_INIT_EVERYTHING flag. You could choose to initialise only the subsystems you need (audio, video, input etc), but since we will be making use of most of these subsystems as the game progresses, initialising everything now saves some time. [code]

If SDL, and it’s subsystems, were loaded and initialised correctly we then create a window. The SCREEN_WIDTH, SCREEN_HEIGHT and BITS_PER_PIXEL define the size of the window and the colour depth. These values are defined in the Constants.h file. The next parameter is a collection of options that further specify how the window is to work.

The SDL_HWSURFACE option tells SDL to place the video surface in video memory (i.e. the memory on your video card). Most systems have dedicated video cards with plenty of memory, and certainly enough to hold our 2D game.

The SDL_DOUBLEBUF option tells SDL to set up two video surfaces, and to swap between the two with a call to SDL_Flip(). This stops the visual tearing that can be associated with the monitor is refreshing while the video memory is being written to. It is slower than a single buffered rendering scheme, but again most systems are fast enough for this not to make any difference to the performance.

The SDL_ANYFORMAT option tells SDL that if it can’t set up a window with the requested colour depth that it is free to use the best colour depth available to it. We have requested a 32 bit colour depth, but some desktops may only be running at 16 bit. This means that our application won’t fail just because the desktop is not set to 32 bit colour depth. [code]

Search More About:

Follow Matthew Casperson
Receive weekly updates from Matthew Casperson
 
Bright Hub - Science & Technology Articles, Buyer's Guides, How-To Tips and Software Reviews
About Bright Hub | Contact Us | Advertise with Us | Become a Writer | RSS | Site Map | Terms of Use | Privacy Policy | Copyright Policy
©2010 Bright Hub Inc. All rights reserved. Page copy protected against web site content infringement by Copyscape