Chapter 2: First steps using SDL (JEDI-SDL)

This is an SDL 1.2 chapter. SDL 1.2 is obsolete since it has been replaced by SDL 2.0. Unless you have good reasons to stay here you may prefer to go for the modern SDL 2.0 :-).

You should be slightly experienced in programming Free Pascal. That means you should be familiar with procedures, functions, loops and usual commands of Free Pascal. If this is not the case you may have problems because this tutorial deals with the features and usage of the SDL library and will not explain basic concepts of Pascal programming. Here I have to set the CRT unit off which was provided along with Turbo Pascal and is also available for Free Pascal by default. It was used in Turbo Pascal for screen and keyboard handling. In many chapters this unit will be used becaused it allows a simple way to recognize if a user pressed a key by a REPEAT … UNTIL keypressed statement. However, this unit is not related to SDL. SDL’s own advanced keyboard handling described in Chapter 6 is not used to keep the code short.

In the original SDL library there are several standard units (called headers in C/C++) whereas in JEDI-SDL all of them got merged to only one unit called “SDL”. To prepare your Free Pascal program for the usage of SDL you just have to call this unit at the uses clause. The SDL unit is the heart of every SDL application!

The different features of SDL (screen handling, audio handling, keyboard handling, cdrom handling, and so on) have to be initilized using the SDL_INIT(flags:UInt32):INTEGER function. You may wonder about the variable type UInt32 of “flags”. The U stands for “unsigned” (values greater or euqal zero only!), the Int stands for “integer” and the number stands for the bit value. So it is an 32 bit unsigned integer (which corresponds to Free Pascal’s longword). Such integer types (which we will see in other chapters again) originate from the original C/C++ SDL code, and for an easier translation to Pascal they were kept.

In the first example we want to initialize the SDL video subsystem for screen handling. The following code example shows the frame of a SDL application.

PROGRAM chap2;
USES SDL;

BEGIN
SDL_INIT(SDL_INIT_VIDEO);

{further code}

SDL_QUIT;
END.

Instead of SDL_INIT_VIDEO you could initilize the respective subsystem by using SDL_INIT_AUDIO, SDL_INIT_CDROM, SDL_INIT_JOYSTICK, SDL_INIT_TIMER, SDL_INIT_NOPARACHUTE, SDL_INIT_EVENTTHREAD, SDL_INIT_EVERYTHING. So if you want to use the features provided by the library for sound handling you should initialize it by using SDL_INIT_AUDIO. If you use SDL_INIT_EVERYTHING all subsystems are initialized. You also can combine several components by the OR keyword, e.g. “SDL_INIT(SDL_INIT_VIDEO OR SDL_INIT_AUDIO)” to initilize video and audio support. If you are running some subsystems already but need to load further ones you would use SDL_INITSUBSYSTEM(flags:UInt32):INTEGER respectivly.

If you look at the exact definition carefully you may wonder about the integer value the function returns. You shoud notice that every SDL function usually returns an error value for you to check if the function runs properly at runtime. There is no general rule what values correspond to which status. Usually values like 0 and 1 correspond to a status “function runs successfully”, values 0 and -1 correspond to a status “function couldn’t be run, something is wrong”. For SDL_INIT the function is run successfully if it returns 0, otherwise there is something wrong. However, in most cases I won’t do error checking to keep code examples short but will mention the error values to be expected.

Every SDL program has to be closed by SDL_QUIT. It cleans up your system. Never forget it! This procedure ensures that all subsystems initilized get unloaded. There is a corresponding procedure to unload specific subsystems defined as SDL_QUITSUBSYSTEM(flags:UInt32 ). The same arguments you use to load subsystems you can use here to unload them.

Now you can try the example program.

Well, we have initilized the video subsystem and released it afterwards. Simple Directmedia Layer deserves its name, isn’t it?

Leave a Reply

Your email address will not be published. Required fields are marked *