Using SDL under Free Pascal

 

This article will provide you with a basic introduction into using the Simple Direct Media Library (SDL) with Free Pascal. For those of you who don't know what Free Pascal is, it is a cross platform compiler based on the Object Pascal language. The IDE is very much like Borland's Turbo Pascal 7.0 (TP7), and it has many of the same features, such as integrated debugging. It also includes some features more common in modern IDE’s like simple code completion.

 

Liking Free Pascal with SDL is advantageous as SDL is also a cross platform games development library, which gives developers a standard interface to the many different API's available under different platforms. For example, under Windows SDL would utilize DirectX but under Linux it would interface with X Windows or some other graphics library. What you end up with is code that could with only minor changes run on many operating systems.

 

Setting up Free Pascal to use JEDI-SDL

 

Before trying to use SDL you will need to make some changes to the way Free Pascal compiles and links. Firstly go to the Options | Compiler menu item, and open the compiler switches page. On the Syntax tab you will need to ensure that the following options are set ON.

Delphi 2 extensions

Delphi compatibility

Now move to the Assembler Tab and switch Assembler Reader to Intel style Assembler and the Assembler Output to precoff. Finally add FPC to the conditional defines box at the bottom of the page. Then close the compiler switches page.

 

Now go to Options | Linker. The only thing you need to do on this page is set the Preferred Library Type to Smart Libraries. Close the linker page and open Options | Directories. In the unit directories box you will need to ensure that the path where the JEDI-SDL units exists is added.

 

That’s it for the settings in Free Pascal. You are now ready to write your first program.

 

Basic Program Structure

 

The basic structure of a Free Pascal program is exactly the same as it is in TP7 and Delphi. The unit begins with program <name> and contains a set of begin and end statements.

 

program test;

 

begin

end.

 

You will soon realise that programs written in Free Pascal will usually port directly to Delphi, although the reverse may not always be true. Between the begin and end statements is where we will initialise the SDL library and create a screen. All the other aspects of the Pascal language are the same; this includes the way you declare procedures and functions.

 

Before heading into SDL I would like to point out that for the most part Free Pascal uses the extension .pp for it's units, however you can make use of .pas and .dpr if you wish, it may also make conversion simpler.

 

Initialising SDL

 

Before trying to initialise SDL, we must first add a few units to the uses clause. The uses clause is very much like the include statement in C and C++, in order to use SDL we must add SDL to the uses clause of our main program and any other units that use the library.

 

program test;

 

uses SDL, SDLUtils;

 

begin

end;

 

Notice that we have also included SDLUtils. This is a utility unit provided by the JEDI-SDL team, which includes some useful procedures and functions.

 

Creating a Window

 

Creating a window can be very simple or complex depending on whether you want to check the video modes you want to use first. For this example we are going to assume that and video mode of 640x480 is available in 16bit mode. Before creating a window we need to initialise the SDL video system using SDL_Init and passing SDL_INIT_VIDEO. Once this is complete we can then call the SDL_SetVideoMode function to create our screen. We will need a variable to store the reference to the surface that SDL_SetVideoMode creates, we can then test this variable to see if the screen was created.

 

Now depending on whether you created the window with the SDL_DOUBLEBUF flag or not, you can either call SDL_Flip or SDL_UpdateRect ot refresh the screen. For more information on double buffering see the JEDI-SDL documentation.

 

Handling events

 

Now that we know how to create and screen, we need to be able to handle input events. SDL provides a mechanism for checking mouse, joystick and keyboard events, as well as checking for window events. In the SDLTest program at the end of this document we use this facility to check when the user closes the window. The important function in this case is SDL_PollEvent, this function queries SDL’s event queue and returns an event if one occurred. In our little demo we check the type of this event against SDL_QUITEV.

 

Shutting Down SDL

 

Closing SDL is simple, a call to SDL_Quit will shut down all open systems (Video, Audio etc). However as good programmers we always clean up after ourselves (don’t we), we free the _screen variable using SDL_FreeSurface function prior to calling SDL_Quit.

 

Final comments

 

This document has only given a brief introduction into using SDL. More information can be found on the JEDI-SDL web site at

http://www.delphi-jedi.org/Jedi:TEAM_SDL_HOME . The listing for the Free Pascal SDLTest program is listed below, for details on how to use the functions not covered in this document check out the documentation that comes with the JEDI-SDL headers. The JEDI-SDL library comes with many demos of how to use SDL, unfortunately these demos are not 100% compatible with Free Pascal and might not work. But the team is working on the problems and hopefully we will be able to put together a complete set of demos that will compile under Delphi, Kylix and Free Pascal. If you are interested in helping on this project head over to http://www.delphi-jedi.org/Jedi:TEAM_SDL_HOME.

 

-------------------------------------

SDLTest program listing

 

program SDLtest;

 

uses SDL, SDLUtils;

 

var

  _screen: PSDLSurface;

  event : TSDL_Event;

 

const

    Done: Boolean = False;

 

begin

   If SDL_Init( SDL_INIT_VIDEO) < 0 then

   begin

       exit;

   end;

   _screen := SDL_SetVideoMode( 640, 480, 16, SDL_DOUBLEBUF );

   if _screen <> nil then

   begin

       repeat

          while SDL_PollEvent( @event ) = 1 do

          begin

             if event._type = SDL_QUITEV then

                 Done := True;            

          end;

          SDL_Flip(_screen);

       Until Done;

       SDL_FreeSurface(_screen);

   end;

   SDL_Quit;

end;