- SDL Library installed with libraries and header files in the correct folders (use DevPack indicated in the "How To Start" section)
After having very basic and disorganised code sections, we will now focus on getting a little SDL engine on feet. I think there's no point in detailing all the construction of this engine but mainly
how it's structured, how to use it, and how to add stuff to it.
Later on, we will extend this SDL graphics engine to handle sound, networking, open GL and much more. The framework code is fully commented, making it very easy to read through and understand. It is structured around the following elements:
0. Engine config file
The config.h file is there to group all the main settings of your engine together. That's where you can set your screen resolution, pixel colour depth, frames per second... etc... Don't hesitate to add stuff to here to enhance the contorls over your first SDL game
// SDL Video mode information1. The CORE class
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;
// 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
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.
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/game_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</span>
[0] [data/img/ball.bmp] [255 255 255]
[1] [data/img/steel.bmp] [-1 -1 -1]
The 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.
Here's a very simple example. Consider the ball and steel images below, and the 2 different rendering results we obtained:
Note that the ball is on a white background. Therefore, if you want to draw the ball in game, in front of the steel background, you will get the bad side effect of keeping the ball's white background. Using the transparency on the ball image for the white colour [255 255 255] will make all the white pixels of the image transparent and produce the correct effect you see on the 4th screenshot.
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
3. What needs to be done
The engine handles all the basic SDL stuff. Basically, all you need to do is:
Find your bitmap images and fill the game_images.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.
Download the Basic SDL Engine
No comments:
Post a Comment