FPC and SDL

From Free Pascal wiki
Jump to navigationJump to search

Introduction

For general information and documentation about SDL visit the SDL homepage. This page is going to explain how to use SDL with Free Pascal, where to obtain the pascal headers etc.

Obtaining FPC headers for SDL

After installing SDL itself on your system you'll need pascal headers to be able to use SDL with FPC.

SDL headers from JEDI-SDL

See JEDI-SDL Home Page.

You can download JEDI-SDLv1.0 Beta..

You can also download and browse current CVS sources, see JEDI-SDL SourceForge CVS for instructions. In short, just do

 cvs -d:pserver:anonymous@cvs.sourceforge.net:/cvsroot/jedi-sdl login 

(when prompted for a password, simply press the Enter key)

 cvs -z3 -d:pserver:anonymous@cvs.sourceforge.net:/cvsroot/jedi-sdl co JEDI-SDLv1.0

After you downloaded (and eventually unpacked) JEDI-SDL sources, you want to be able to use these units in your programs. For the simplest scenario, just add the following lines to your fpc.cfg file:

 -Fi<jedi-sdl-path>/JEDI-SDLv1.0/SDL/Pas
 -Fu<jedi-sdl-path>/JEDI-SDLv1.0/SDL/Pas
 -Fu<jedi-sdl-path>/JEDI-SDLv1.0/SDL_Mixer/Pas
 -Fu<jedi-sdl-path>/JEDI-SDLv1.0/SDL_Image/Pas
 -Fu<jedi-sdl-path>/JEDI-SDLv1.0/SDL_Net/Pas
 -Fu<jedi-sdl-path>/JEDI-SDLv1.0/SDL_ttf/Pas
 -Fu<jedi-sdl-path>/JEDI-SDLv1.0/smpeg/Pas
 -Fu<jedi-sdl-path>/JEDI-SDLv1.0/SFont/Pas
 -Fu<jedi-sdl-path>/JEDI-SDLv1.0/SDL_Sound/Pas
 -Fu<jedi-sdl-path>/JEDI-SDLv1.0/SDLMonoFonts/Pas
 -Fu<jedi-sdl-path>/JEDI-SDLv1.0/SDLSpriteEngine/Pas
 -Fu<jedi-sdl-path>/JEDI-SDLv1.0/Cal3D/Pas
 -Fu<jedi-sdl-path>/JEDI-SDLv1.0/ODE/Pas
 -Fu<jedi-sdl-path>/JEDI-SDLv1.0/SDLCtrls/Pas
 -Fu<jedi-sdl-path>/JEDI-SDLv1.0/SDL_flic/Pas

(where <jedi-sdl-path> is the path where you placed jedi-sdl sources).

Alternatively, you can copy all *.pas and *.inc files from these directories to one chosen directory, and add only this chosen directory to your fpc.cfg.

To test that everything works, you can compile and run one of numerous demo programs included, e.g.

 cd <jedi-sdl-path>/JEDI-SDLv1.0/SDLSpriteEngine/Demos/Oxygene/
 fpc -Sd Oxygene.dpr
 ./Oxygene

SDL headers from FreePascal CVS (deprecated)

Get them using CVS:

cvs -d :pserver:cvs@cvs.freepascal.org:/FPC/CVS login

(password is 'cvs')

cvs -d :pserver:cvs@cvs.freepascal.org:/FPC/CVS -z3 co projects/contrib/sdl

Tips and tricks

There is one more thing I'd like to point out. If you use SDL_mixer you'll notice that it's dependent on smpeg library. This can be a setback since this isn't a standard library and is hard to get (CVS only afaik).
However the SDL_mixer.pas header for it uses smpeg by default even if SDL_mixer itself isn't linked to it.
To remove this you just need to remove smpeg.pas from it's uses and comment out one line in the sources mentioning
mp3s (it's a case near the end). This way your app. won't use smpeg.so/dll anymore.

Also, most examples require that {$mode delphi} is added to compile.

Mac OS X specific issues

For FPC 2.0.2+

With MacOS X 10.4 and fpc 2.0.2 or later you do not have to change rtl/bsd/system.pp any longer. -XMSDL_main does the trick:

1) as before, install sdl, for example using fink.

2) Add the following to your program:

 {$linklib gcc}
 {$linklib SDLmain}
 uses SDL;

3) Then, this command line does it:

 fpc -XMSDL_main -k-L/sw/lib -k-lSDL -k-framework -kOpenGL -k-framework -kCocoa -Fu<pathtosdl.pas files> -Fi<pathtosdl.inc files> YourProgram.pas

If sdl.pas and jedi-sdl.inc are in the directory of your program, you can omit -Fu<pathtosdl.pas files> -Fi<pathtosdl.inc files>.

For FPC 2.0.0-

SDL uses a dirty trick to create a GUI window for drawing things in on several OS'es, such as Mac OS X, Win32 and others. It has a macro inside the SDL_main.h header which redefines the term main into SDL_main, and includes its own main function inside libSDL. This libSDL main function creates the GUI window and does some other setup, and then calls the function SDL_main (i.e., your main program).

Of course, under Pascal such macro trickery does not work. The win32 Pascal ports of SDL contain a translation from C into Pascal of the main function inside libSDL. For Mac OS X, this is not possible because there this main function is written in Objective C.

The only workaround currently is to edit rtl/bsd/system.pp, and change the line

 procedure main(argcparam: Longint; argvparam: ppchar; envpparam: ppchar); cdecl; [public];

into

 procedure SDL_main(argcparam: Longint; argvparam: ppchar; envpparam: ppchar); cdecl; [public];

If you then recompile the RTL, you will obtain an RTL which can be used to compile SDL programs. However, it will only work for SDL programs, since other programs now won't have a main function. We hope to solve this problem in a future version.

Apart from the above, you also have to link against the OpenGL and Cocoa frameworks, and against libgcc. You can do this by adding -k-framework -kOpenGL -k-framework -kCocoa on the command line when compiling SDL programs (and adding {$linklib gcc} in your program).

If you installed the SDL libraries using fink, add -k-L/sw/lib -k-lSDL to your fpc command.

Examples

Creating an empty SDL window:

program sdltest;
{$linklib gcc} {$linklib SDLmain}
uses sdl;
var scr: PSDL_Surface; // Our main screen
begin SDL_Init(SDL_INIT_VIDEO); // Initialize the video SDL subsystem scr:=SDL_SetVideoMode(640, 480, 8, SDL_SWSURFACE); // Create a software window of 640x480x8 and assign to scr SDL_FreeSurface(scr); // Free the surface SDL_Quit; // close the subsystems and SDL end.

You can then compile this code with

 fpc -Fu<pathtosdl.pas files> -Fi<pathtosdl.inc files> example.pas

If you use lazarus, make sure you fill the "other unit files" and "include files" properly.