Installing Free Pascal

From SwinBrain

In this article we will look at installing Free Pascal and the tools needed to compile and run SwinGames. These steps are covered in the Getting Started with SwinGame video podcast on iTunesU.

This is a How To article designed to give step-by-step instructions. Search SwinBrain and the external links if you require more detailed information about this topic.


Installing Free Pascal and SwinGame

The first thing we will look at is installing the developer tools that we need to compile our SwinGames. These will include the following:

Following this we will look at the process of compiling standard Free Pascal code, and compiling a Pascal SwinGame.

FPC and SwinGame on Linux

In this section we will outline how to install Free Pascal and the libraries needed to create SwinGames on a Debian based distribution of Linux such as Ubuntu. For other distributions you can use these instructions to determine the libraries that you need to install.

Debian based Linux distributions include a package management system called APT, standing for Advanced Packaging Tool. APT can be used to install programs and libraries, making it easy for you to get the tools you need to get FPC and SwinGame working. In order to install the latest version of Free Pascal you need to start by updating where APT can download packages from. Run the following commands on the console (Terminal).

gpg --keyserver hkp:// --recv-keys 6A11800F
gpg --export 6A11800F | sudo apt-key add -
echo "deb lazarus-stable universe" > ~/lazarus.list
sudo mv ~/lazarus.list /etc/apt/sources.list.d/
sudo chown root:root /etc/apt/sources.list.d/lazarus.list
sudo apt-get update

Now you can install FPC at the console using the following instruction, or via graphical package management systems such as Synaptic.

sudo apt-get install fpc

The next step is to install the libraries that SwinGame depends on. These are as follows:

  • libsdl1.2-dev
  • libsdl-gfx1.2-dev
  • libsdl-image1.2-dev
  • libsdl-mixer1.2-dev
  • libsdl-ttf2.0-dev

You can install these packages using apt-get as shown below, or using Synaptic.

sudo apt-get install curl libsdl1.2-dev libsdl-gfx1.2-dev libsdl-image1.2-dev libsdl-mixer1.2-dev libsdl-ttf2.0-dev

With these packages installed you are done... Ubuntu already comes with a syntax aware text editor, gEdit... Sit back and enjoy.

You can then download the SwinGame project template and create a new 'Hello World' program using the following from the command line. You can enter the commands shown below, or download and run the shell script (you may need to run chmod a+x to make the downloaded file executable).

tar -xzvf SwinGame_3.0_Alpha_FPC.tar.gz
mv Project Template HelloWorld
cd HelloWorld

FPC and SwinGame on MacOS

To start with, you are going to need to install XCode. This is available on the MacOS install disc, but you probably want to download the latest version. This includes a number of developer tools that are needed to create SwinGames, but also includes tools to build applications using Objective-C and Cocoa as well as other languages and APIs.

With XCode installed all that is left to do is to install the Free Pascal Compiler. If you have a recent Mac you will want to download the intel version (for example Open the downloaded disk image, and install the Free Pascal package.

You should now have all of the components that you need to compile SwinGame projects, the last think you need to get is a text editor. MacOS includes a TextEdit program, but what you really want is an editor that is able to perform syntax highlighting. One of the most popular text editors for developers on the Mac is TextMate, while this is not free it is definitely worth paying for. If you want a free alternative you can try TextWrangler. Once you have either of these installed you are ready to go!

To get SwinGame for Mac, download the Free Pascal DMG containing SwinGame from the SwinGame website. Copy the Project Template directory, and all the files and folders within it to a directory (with no spaces in the path). Rename Project Template to the name of your project (once again, no spaces). Alternatively, download and run the shell script (you may need to run chmod a+x to make the downloaded file executable), this script will download a new SwinGame template and rename it to your requested project name.

FPC and SwinGame on Windows

Do not install Free Pascal on a path with spaces! So not in Program Files for example...

On Windows you can start by downloading and installing the Free Pascal Compiler. You need to install the i386 version, even if you are running on a 64bit machine. This installer contains the actual compiler. If you want to create 64bit binaries you can also install the 64bit compiler extensions. Download and run the installer, following the prompts.

After the installer completes you need to update your path. This is an Environment Variable that is used by the operating system to locate programs you run from the command line. The Setting Environment Variables How To article shows you how to set environment variables. Make sure that this variable includes the path to the free pascal compiler. For example, if you installed Free Pascal at c:\apps\fpc then you will need to add c:\apps\fpc\2.4.0\bin\i386-win32 to the path variable. Double check the exact path using Windows Explorer, the directory you want contains a program called fpc.exe.

Next you need to install a unix shell command line interpreter. This is needed to run the scripts used to compile your SwinGames. MSYS provides a minimal unix shell you can use on Windows. Download the current version of the MinGW installer, that includes the MSYS components. Download and run the installer, follow the prompts and make sure you select the MSYS component on the options screen. The MinGW installer should install a MinGW Shell program in your Start menu. You will need to compile your SwinGames from this command prompt. You can get more help on the commands you can use in the MinGW Shell from the Getting started with Bash article.

Place wget's bin directory in the path
Download and install the wget for Windows. The wget program allows you to download files from the command line and is used by the script. Once you have downloaded and installed this you need to add wget's bin directory to the path. The image to the right shows this on Windows 7. See the Setting Environment Variables How To article for further details.

The last thing you need to install is a syntax aware text editor such as Notepad++. Once this is installed you are ready to go... though you may need to reboot for some of these settings to work correctly.

For step by step instructions see Install FPC for SwinGame on Windows.

Using the Command Line

In order to work with your SwinGame project you will need to learn to use the Unix command line environment. To help you get started have a look at the Getting started with Bash article.

Compiling Pascal Code

Do not place your code in a directory with spaces in its name! So not in Documents and Settings\Documents for example...

Now that you have the tools installed, lets use them to build and run a program.

  1. Open your text editor.
  2. Create a new document, and save it as HelloWorld.pas to a directory on your machine.
  3. Open a Terminal (console) window (MSYS on Windows)
  4. Navigate to a directory using the cd command (see below)
  5. Compile the program using the fpc command. We supply this command with the -S2 option to enable more advanced syntax, and also provide the name of the file to compile.
  6. Run the program
In Windows you need to use /c/ to locate the C: drive. For example cd /c/Temp/LearnProg to move into the c:\Temp\LearnProg directory.

These steps are illustrated below. This example assumes that you have saved your code into a directory called LearnProg in Documents of your home directory.

cd ~/Documents/LearnProg
fpc -S2 HelloWorld.pas
Tip: ~ in Unix is your home directory. For example: /Users/fred if your username is fred.

Compiling a SwinGame project

Do not place your SwinGame project in a directory with spaces in its name!

Now you are ready to get started programming with SwinGame. SwinGame for Free Pascal comes as a project template. There is nothing additional to install, and each time you want to create a new game its best to start from an fresh version of these templates. The first thing that you will need to do is download the latest version of SwinGame 3.0.

The SwinGame 3 file that you have downloaded is a zip file that contains an empty project template. To get started extract this template to your source code directory, and rename the project template directory to the name of the program you want to create, for example HelloWorld.

  • Download the SwinGame 3.0 template
  • Extract the files to your source code directory
  • Rename the project template' directory to the name of the program you are creating (make sure there are no spaces in the path)
  • Open a Terminal window (MSYS in Windows)
  • Navigate to the directory you just created
  • Run the file, this will compile your project
  • Open the GameMain.pas file from the src directory...

The project template directory contains a number of subdirectories. These are:

  • bin: this directory will be created when you run the script. This is the directory that contains the programs you compile.
  • lib: contains the SwinGame library code and any libraries it needs. SwinGame is written in Pascal so you can examine its code if you are interested.
  • Resources: contains the images, sounds, animations, maps etc. that are associated with your game. These are copied into the bin directory when you run the script.
  • src: this is the directory with your source code. The main code must be in the GameMain.pas. This is the file that the script compiles (using Free Pascal).

[edit]Learn Programming: Getting Started | Calling Procedures | Calling Functions | ...
Links: Topic Brains
Return to Swin Brain