- SDL Library installed with libraries and header files in the correct folders (use DevPack indicated in the "How to Start" section)
- SDL_Image library installed correctly. You can get ALL the Devpacks required HERE. Installation has to be done in a specific order (e.g you won't get SDL_Image installed until you have libpng Devpack installed ... etc...)
- SDL_TTF library to render fonts on screen. You can get the SDL_TTF DevPack HERE
- SDL_Mixer library to play sounds. You can get the SDL_Mixer DevPack HERE
This SDL Game engine is a follow up and improvement of the GDO Basic SDL Engine. It is an enhanced version since it handles more data types (jpeg, bmp, tga and png image formats) in addition to sound. It also implements the SDL TTF library, letting you display custom fonts on screen.
0. Engine config file
The config.h file Has a few extra features compared with the SDL Basic engine. It now lets you set your audio sampling frequency, the maximum number of sounds to be loaded, the maximum number of fonts to initialize...
// SDL Video mode information
const int SCREEN_WIDTH = 1024;
const int SCREEN_HEIGHT = 768;
const int SCREEN_BPP = 32;
// SDL Image structure information. Define the maximum number of images the game can load
const int IMAGE_COUNT = 128;
// SDL TTF Font structure information. Defines the maximum number of fonts loaded
const int FONT_COUNT = 8;
// SDL Audio information
const int AUDIO_SOUND_COUNT = 32;
const int AUDIO_SAMPLE_FRQ = 22050; // Sampling frequency of audio
const int AUDIO_CHUNK_SIZE = 4096; // This value determines the size of the memory chunks
// used for storage and playback of samples. A value of
// 4096 should be suitable for most games. Increasing
// the value will decrease the CPU load but will also
// decrease responsiveness of playback. If you find the
// mixer's play/stop response times to be too slow, you
// may wish to decrease this value.
// Timer definitions. This forces the game to wait at least T_FRAME_INTERVAL milliseconds
// between each frame. It is the only way to get the game to NOT run faster on a faster
// computer... Indeed, without this test, the game loop would run as fast as the computer
// processors allows it
const int T_FRAME_INTERVAL = 25; // time interval (ms) used for FPS rendering
1. The CORE class
As the name states it, the CORE class handles the game engine core. It's this class that have an infinite loop function that we call the gameLoop() function in this framework. It will loop for
upcoming user events (mouse or keyboard inputs), perform the actions that correspond to those events and then render the graphics. This class hasn't changed in any way since the basic engine. It simply handles the audio class in addition to the video class...
2. The GRAPHICS class
This class will give you full image control. When it's instanciated, it automatically calls its loadImageData() function. This function simply scans through the data/bitmap_images.dat file for image information as below:
# This file defines all the images loaded by the game # They are assigned in a static array and limited to 128 images by default # # If you need to re-assign the upper limit, you need to edit IMAGE_COUNT # field in config.h and recompile the game
# Syntax to define an image # [image_number] [image_file_path] [RGB transparency key colour] # Also, note that all bright pink colours will be used for the transparency colour key # This means RGB colour [255, 0, 255] will be transparent in game # On the other hand, setting this to [-1 -1 -1] will disable transparency for that image [0] [data/img/ball.bmp] [255 255 255] [1] [data/img/steel.bmp] [-1 -1 -1]
This enhanced framework has a new config file to handle non bitmap images. It's in this file that you'll specify all your jpeg, png, tiff... images to load in addition to whether or not you want to use their alpha channel
# This file defines all the images loaded by the gameThe engine supports RGB transparency key colour for each image loaded. This colour can be specified in the image data file shown just above. RGB image transparency is something new to those who have only been through the first 2 SDL tutorials. Setting that value to [-1 -1 -1] will just disable transparency for that image. For each image, you can specify a colour which will be "substracted" to your image, making that colour transparent in game.
# They are assigned in a static array and limited to 128 images by default
#
# If you need to re-assign the upper limit, you need to edit IMAGE_COUNT
# field in config.h and recompile the game
# Syntax to define an image
# [image_number] [image_file_path] [alpha]
# Alpha has to be set to 0 all the time, unless your image has
# a transparency layer in which case you have to set alpha to 1
# Make sure the image number you enter here DO NOT interfere with those
# define in the bitmap image ressource definition file !!!</span>
<span class='comment'># Loads tux.png with its alpha channel -> transparent background</span>
[11] [data/img/tux.png] [1]
<span class='comment'># Loads tux.png without its alpha channel -> white opaque background</span>
[12] [data/img/tux.png] [0]
The engine converts any non bitmap image to a surface on which alpha channel is omitted or not depending on the settings in the data/images.dat file. (The user can choose to load the image alpha channel or not). Note that alpha channel handling is heavier than simple opaque image blitting. You therefore must set the alpha switch only on images that really are transparent !
The graphics class supports 8, 16, 24 and 32 bits per pixel colour depth raw pixel writing and line drawing functions
Pixel reading functions allow advanced collisions detection using "pixel perfect" collision models directly onto an SDL Surface
Truetype font blitting. The rendertext() function in the Graphics class will let you blit a Truetype font directly onto an SDL Surface
3. The AUDIO class
The audio class is still in a basic status. Indeed, it only lets you handle WAV files in this engine version. A better version will be released later to provide support for other music formats such as MP3. Loading audio files is just as easy as loading images. It is once again done via a config file which is extremely basic: you just specify a sound slot number to know in which array cell the sample has to be stored, then a path to the wav file you wish to load...
# This file defines all the sounds loaded by the game
# Syntax to define a sound
# [sound_number] [sound_path]
#
# The audio sample format must be 16 bits audio
[0] [data/sound/online.wav]
4. What needs to be done
The engine handles all the basic SDL stuff. Basically, all you need to do is:
Find your images and fill the game_images.dat and images.dat config files to get them loaded
Find your wav sound samples and fill the sounds_wav.dat config file to get them loaded
Find your fonts and fill the fonts.dat config file to get them loaded
Create your game objects class that is instanciated and controlled by CORE class
Update the gameLoop() function in the CORE class to get your object data and render it. Note that you can do this the other way round and have all your objects handle a CORE class instance pointed to render themselves. This will only require a little more adaptation.
The sample provided with this framework will simply display the Tux PNG image on screen (once with and once without its alpha channel), will let you move a bitmap ball around the screen with its transparency key colour handling, and will play a sound when the left mouse button is clicked... very simple, but that's how you learn :)
Download the Advanced SDL Engine
No comments:
Post a Comment