Difference between revisions of "FPC and SDL"

From Free Pascal wiki
Jump to navigationJump to search
m
(4 intermediate revisions by the same user not shown)
Line 8: Line 8:
  
 
After installing SDL itself on your system you'll need pascal headers to be able to use SDL with FPC.<br>
 
After installing SDL itself on your system you'll need pascal headers to be able to use SDL with FPC.<br>
There are 2 known(to me ) sources. 1st is the original JEDI SDL project at [http://www.delphi-jedi.org/Jedi:TEAM_SDL_HOME this page].<br>
+
You can download from CVS(deprecated)
These headers were ment for Delphi and while they work with delphi compatibility mode (-Sd) I'd suggest<br>
 
(from personal experience) to use the modified FPC version.<br>
 
 
 
This can be obtained via CVS like so:
 
  
 
  cvs -d :pserver:cvs@cvs.freepascal.org:/FPC/CVS login
 
  cvs -d :pserver:cvs@cvs.freepascal.org:/FPC/CVS login
Line 20: Line 16:
 
  cvs -d :pserver:cvs@cvs.freepascal.org:/FPC/CVS -z3 co projects/contrib/sdl
 
  cvs -d :pserver:cvs@cvs.freepascal.org:/FPC/CVS -z3 co projects/contrib/sdl
  
 +
OR
  
This will download the FPC - SDL headers into the directory projects. The important part is in the<br>
+
[http://www.delphi-jedi.org/Jedi:TEAM_SDL_HOME this page].<br>
''basesdl'' directory. You can use these headers directly with FPC without modification.
+
Unpack the v1.0 SDL sources into a directory. Copy all *.pas files (SDL/Pas; SDL_Image/Pas etc)<br>
 +
into some directory you can access with relative paths to your project.<br>
 +
For example make a "basesdl" directory in your project and copy all the pas files there.<br>
  
 
==Tips and tricks==
 
==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
 
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 IMHO).<br>
+
on smpeg library. This can be a setback since this isn't a standard library and is hard to get (CVS only afaik).<br>
 
However the SDL_mixer.pas header for it uses smpeg by default even if SDL_mixer itself isn't linked to it.<br>
 
However the SDL_mixer.pas header for it uses smpeg by default even if SDL_mixer itself isn't linked to it.<br>
 
To remove this you just need to remove smpeg.pas from it's uses and comment out one line in the sources mentioning<br>
 
To remove this you just need to remove smpeg.pas from it's uses and comment out one line in the sources mentioning<br>
 
mp3s (it's a case near the end). This way your app. won't use smpeg.so/dll anymore.
 
mp3s (it's a case near the end). This way your app. won't use smpeg.so/dll anymore.
 +
 +
==Mac OS X specific issues==
 +
 +
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 <tt>main</tt> into <tt>SDL_main</tt>, and includes its own <tt>main</tt> function inside libSDL. This libSDL main function creates the GUI window and does some other setup, and then calls the function <tt>SDL_main</tt> (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 <tt>-k-framework -kOpenGL -k-framework -kCocoa</tt> on the command line when compiling SDL programs (and adding <tt>{$linklib gcc}</tt> in your program).
  
 
== Examples ==
 
== Examples ==

Revision as of 21:08, 26 January 2005

Introduction

I assume that you already have atleast minimal knowlidge of what SDL is and it's basic functions.
For those who want to learn more I suggest to 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.
You can download from CVS(deprecated)

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

OR

this page.
Unpack the v1.0 SDL sources into a directory. Copy all *.pas files (SDL/Pas; SDL_Image/Pas etc)
into some directory you can access with relative paths to your project.
For example make a "basesdl" directory in your project and copy all the pas files there.

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.

Mac OS X specific issues

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).

Examples

Creating an empty SDL window:

program sdltest;
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.