Chapter 1. Getting Started with SDL

Simple DirectMedia Layer (SDL) is a cross-platform multimedia library created by Sam Oscar Latinga. It provides low-level access to input (via mouse, keyboard, and gamepads/joysticks), 3D hardware, and the 2D video frame buffer. SDL is written in the C programming language, yet has native support for C++. The library also has bindings for several other languages such as Pascal, Objective-C, Python, Ruby, and Java; a full list of supported languages is available at http://www.libsdl.org/languages.php.

SDL has been used in many commercial games including World of Goo, Neverwinter Nights, and Second Life. It is also used in emulators such as ZSNES, Mupen64, and VisualBoyAdvance. Some popular games ported to Linux platforms such as Quake 4, Soldier of Fortune, and Civilization: Call to Power utilize SDL in some form.

SDL is not just used for games. It is useful for all manner of applications. If your software needs access to graphics and input, chances are that SDL will be a great help. The SDL official website has a list of applications that have been created using the library (http://www.libsdl.org/applications.php).

In this chapter we will cover the following:

  • Getting the latest SDL build from the Mercurial repository
  • Building and setting up SDL in Visual C++ 2010 Express
  • Creating a window with SDL
  • Implementing a basic game class

Why use SDL?

Each platform has its own way of creating and displaying windows and graphics, handling user input, and accessing any low-level hardware; each one with its own intricacies and syntax. SDL provides a uniform way of accessing these platform-specific features. This uniformity leads to more time spent tweaking your game rather than worrying about how a specific platform allows you to render or get user input, and so on. Game programming can be quite difficult, and having a library such as SDL can get your game up and running relatively quickly.

The ability to write a game on Windows and then go on to compile it on OSX or Linux with little to no changes in the code is extremely powerful and perfect for developers who want to target as many platforms as possible; SDL makes this kind of cross-platform development a breeze. While SDL is extremely effective for cross-platform development, it is also an excellent choice for creating a game with just one platform in mind, due to its ease of use and abundance of features.

SDL has a large user base and is being actively updated and maintained. There is also a responsive community along with a helpful mailing list. Documentation for SDL 2.0 is up-to-date and constantly maintained. Visiting the SDL website, libsdl.org, offers up lots of articles and information with links to the documentation, mailing list, and forums.

Overall, SDL offers a great place to start with game development, allowing you to focus on the game itself and ignore which platform you are developing for, until it is completely necessary. Now, with SDL 2.0 and the new features it brings to the table, SDL has become an even more capable library for game development using C++.

Note

The best way to find out what you can do with SDL and its various functions is to use the documentation found at http://wiki.libsdl.org/moin.cgi/CategoryAPI. There you can see a list of all of SDL 2.0's functions along with various code examples.

What is new in SDL 2.0?

The latest version of SDL and SDL 2.0, which we will be covering in this book, is still in development. It adds many new features to the existing SDL 1.2 framework. The SDL 2.0 Roadmap (wiki.libsdl.org/moin.cgi/Roadmap) lists these features as:

  • A 3D accelerated, texture-based rendering API
  • Hardware-accelerated 2D graphics
  • Support for render targets
  • Multiple window support
  • API support for clipboard access
  • Multiple input device support
  • Support for 7.1 audio
  • Multiple audio device support
  • Force-feedback API for joysticks
  • Horizontal mouse wheel support
  • Multitouch input API support
  • Audio capture support
  • Improvements to multithreading

While not all of these will be used in our game-programming adventures, some of them are invaluable and make SDL an even better framework to use to develop games. We will be taking advantage of the new hardware-accelerated 2D graphics to make sure our games have excellent performance.

Migrating SDL 1.2 extensions

SDL has separate extensions that can be used to add new capabilities to the library. The reason these extensions are not included in the first place is to keep SDL as lightweight as possible, with the extensions serving to add functionality only when necessary. The next table shows some useful extensions along with their purpose. These extensions have been updated from their SDL1.2/3 Versions to support SDL 2.0, and this book will cover cloning and building them from their respective repositories as and when they are needed.

Name

Description

SDL_image

This is an image file loading library with support for BMP, GIF, PNG, TGA, PCX, and among others.

SDL_net

This is a cross-platform networking library.

SDL_mixer

This is an audio mixer library. It has support for MP3, MIDI, and OGG.

SDL_ttf

This is a library supporting the use of TrueType fonts in SDL applications.

SDL_rtf

This is a library to support the rendering of the Rich Text Format (RTF).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.96.94