Compiling the Source Code

From Open Surge Wiki
Revision as of 01:36, 15 April 2021 by Alexandre (Talk | contribs) (Upgrade to Allegro 5.2.7)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Current version

This guide will help you compile Open Surge. Open Surge uses Allegro 5, a game programming library written mostly in C. Allegro 5 is supported on multiple platforms, such as: Windows, Linux and OSX. Regardless of your platform, you'll need CMake and a C compiler.

NOTE: you normally don't need to compile Open Surge from the source code, unless you're an advanced user or a package maintainer.

If you're a package maintainer, it's recommended to create two separate packages for distribution: one for Open Surge and the other for SurgeScript. SurgeScript is a scripting language for games. It's conceived as a separate project by the same author, providing shared and static libraries, and it's used massively in Open Surge. SurgeScript features a runtime engine of its own that doesn't depend on Open Surge.

Cross-compiling

We'll compile a Windows build of the game from a Linux box. We'll first compile Allegro 5 and its dependencies. Next, we'll compile SurgeScript. Finally, we'll compile Open Surge.

Since we're building everything from the sources, the instructions below can be adapted to build the game on other platforms, such as Linux and OSX (there are hints throughout the guide). Also, if you came here from the web, this guide can help you cross-compile Allegro 5 as well.

NOTE: before we begin, create an empty directory for compiling the game and then get in there (cd /path/to/dir). We'll be downloading packages and installing things. Avoid using directories with whitespaces or special characters in the path.

Install the cross-compiler

First of all, we need to install CMake and the cross-compiler. We'll be using MinGW-w64. Get MinGW-w64 from its website and CMake from cmake.org. On Debian-based distributions such as Ubuntu, you can simply run:

sudo apt-get install mingw-w64 cmake

Now, check where the cross-compiler has been installed. Then, run the following commands in a shell (adapt as necessary):

# Name of the cross compiler
# Usually one of the following: i686-w64-mingw32, i686-pc-mingw32, i386-mingw32 (32 bit)
export TOOLSET=i686-w64-mingw32

# Location of the cross compiler
# Usually in /usr (please check) - it should have subdirectories: bin/, include/, lib/
export TOOLCHAIN=/usr/$TOOLSET

We'll also set some environment variables. The programs below must be available in your PATH:

# These tools should be available in your PATH
export CC=$TOOLSET-gcc
export CXX=$TOOLSET-g++
export RANLIB=$TOOLSET-ranlib
export LD=$TOOLSET-ld
export AR=$TOOLSET-ar
export AS=$TOOLSET-as
export STRIP=$TOOLSET-strip

Download Allegro 5

Let's download Allegro 5 (source code). The most recent version of the library can be found at liballeg.org. In this example, we're using Allegro 5.2.7.

wget https://github.com/liballeg/allegro5/releases/download/5.2.7.0/allegro-5.2.7.0.tar.gz
tar xvzf allegro-5.2.7.0.tar.gz
cd allegro-5.2.7.0

In order to compile Allegro 5, we must first install its dependencies. We'll install the dependencies into a special folder called deps. This folder is secretly recognized by Allegro. Set the variables below:

export ALLEGRO_FOLDER="`pwd`"
export ALLEGRO_BUILD_FOLDER="$ALLEGRO_FOLDER/build"
export ALLEGRO_DEPS_FOLDER="$ALLEGRO_FOLDER/deps"
export LDFLAGS="-L$ALLEGRO_DEPS_FOLDER/lib"
export CFLAGS="-I$ALLEGRO_DEPS_FOLDER/include"
export CPPFLAGS="-I$ALLEGRO_DEPS_FOLDER/include"

Let's create the deps folder:

mkdir -p $ALLEGRO_DEPS_FOLDER
cd $ALLEGRO_DEPS_FOLDER

Now we're ready to compile the dependencies.

Compile zlib

zlib can be obtained from http://zlib.net

wget https://zlib.net/zlib-1.2.11.tar.gz
tar xvzf zlib-1.2.11.tar.gz && cd zlib-1.2.11
# ./configure --prefix=$ALLEGRO_DEPS_FOLDER --static && make # skip this (not win32)
sed -e s/"PREFIX ="/"PREFIX = ${TOOLSET}-"/ -i win32/Makefile.gcc
BINARY_PATH=${ALLEGRO_DEPS_FOLDER}/bin \
  INCLUDE_PATH=${ALLEGRO_DEPS_FOLDER}/include \
  LIBRARY_PATH=${ALLEGRO_DEPS_FOLDER}/lib \
  make -f win32/Makefile.gcc install
make install
cd ..

Compile libpng

libpng 1.6 can be obtained from http://libpng.org

wget https://downloads.sourceforge.net/project/libpng/libpng16/1.6.36/libpng-1.6.36.tar.gz
tar xvzf libpng-1.6.36.tar.gz && cd libpng-1.6.36
./configure --host=$TOOLSET --target=$TOOLSET --prefix=$ALLEGRO_DEPS_FOLDER --disable-shared --enable-static
make
make install
cd ..

Compile freetype

The FreeType library can be obtained from http://freetype.org

wget http://downloads.sourceforge.net/project/freetype/freetype2/2.9.1/freetype-2.9.1.tar.gz
tar xvzf freetype-2.9.1.tar.gz && cd freetype-2.9.1
./configure --host=$TOOLSET --target=$TOOLSET --prefix=$ALLEGRO_DEPS_FOLDER --disable-shared --enable-static \
  --without-png --without-zlib --without-harfbuzz --with-bzip2=no
make
make install
cd ..

Compile libjpeg

We'll be using libjpeg: http://www.ijg.org/

wget https://www.ijg.org/files/jpegsrc.v9c.tar.gz
tar xvzf jpegsrc.v9c.tar.gz && cd jpeg-9c
./configure --host=$TOOLSET --target=$TOOLSET --prefix=$ALLEGRO_DEPS_FOLDER --disable-shared --enable-static
make
make install
cd ..

You may alternatively use libjpeg-turbo: http://libjpeg-turbo.org

Compile libogg

libogg can be obtained from http://xiph.org

wget http://downloads.xiph.org/releases/ogg/libogg-1.3.3.tar.gz
tar xvzf libogg-1.3.3.tar.gz && cd libogg-1.3.3
# sed -e s/"-mno-ieee-fp"/""/g -i ./configure # skip this (clang only, x86)
./configure --host=$TOOLSET --target=$TOOLSET --prefix=$ALLEGRO_DEPS_FOLDER --disable-shared --enable-static
make
make install
cd ..

Compile libvorbis

libvorbis can also be obtained from http://xiph.org

wget http://downloads.xiph.org/releases/vorbis/libvorbis-1.3.6.tar.gz
tar xvzf libvorbis-1.3.6.tar.gz && cd libvorbis-1.3.6
# sed -e s/"-mno-ieee-fp"/""/g -i ./configure # skip this (clang only, x86)
./configure --host=$TOOLSET --target=$TOOLSET --prefix=$ALLEGRO_DEPS_FOLDER --disable-shared --enable-static \
  --without-ogg --with-ogg=$ALLEGRO_DEPS_FOLDER --with-ogg-libraries=$ALLEGRO_DEPS_FOLDER/lib \
  --with-ogg-includes=$ALLEGRO_DEPS_FOLDER/include
make
make install
cd ..

Optional libraries

Other libraries such as: libflac, libdumb, libopus, libtheora can be added to Allegro, but are currently not required by Open Surge.

libopenal

libopenal is required on OSX, but optional otherwise. We'll be cross-compiling OpenAL Soft, a software implementation of the OpenAL API. It's available at https://kcat.strangesoft.net/

wget https://kcat.strangesoft.net/openal-releases/openal-soft-1.17.2.tar.bz2
tar xvjf openal-soft-1.17.2.tar.bz2 && pushd openal-soft-1.17.2/build
cmake .. -DCMAKE_SYSTEM_NAME=Windows -DCMAKE_C_COMPILER=$(which $CC) -DCMAKE_CXX_COMPILER=$(which $CXX) \
  -DCMAKE_FIND_ROOT_PATH=$TOOLCHAIN -DCMAKE_FIND_ROOT_PATH_MODE_PROGRAM=NEVER -DCMAKE_FIND_ROOT_PATH_MODE_LIBRARY=ONLY \
  -DCMAKE_FIND_ROOT_PATH_MODE_INCLUDE=ONLY -DCMAKE_INSTALL_PREFIX=$ALLEGRO_DEPS_FOLDER -DLIBTYPE=STATIC
make
make install
popd

Compile Allegro 5

In this tutorial, we'll compile the static version of Allegro 5 (monolith). YMMV, but we'll be using this configuration for the Windows build of the game. Note that other platforms may not support linking Allegro 5 statically.

export LDFLAGS="-static -static-libgcc -static-libstdc++"
mkdir -p $ALLEGRO_BUILD_FOLDER
cd $ALLEGRO_BUILD_FOLDER
cmake .. -DCMAKE_TOOLCHAIN_FILE="$ALLEGRO_FOLDER/cmake/Toolchain-mingw.cmake" -DWANT_MONOLITH=on -DSHARED=off \
  -DCMAKE_BUILD_TYPE=RelWithDebInfo -DWANT_DOCS=off -DWANT_EXAMPLES=off -DWANT_DEMO=on -DWANT_TESTS=off \
  -DCMAKE_VERBOSE_MAKEFILE=on -DCMAKE_EXE_LINKER_FLAGS="-static -static-libgcc -static-libstdc++"
# Cross fingers and pray.
make -j`nproc`
sudo make install

After compiling Allegro 5, go back to the base folder:

cd $ALLEGRO_FOLDER/..
export LDFLAGS=""

Compile SurgeScript

Compiling SurgeScript should be fairly easy. Get the sources at https://github.com/alemart/surgescript/releases File cmake/toolchain-mingw.cmake tries to automatically locate the cross-compiler (adapt if necessary).

# extract the sources to surgescript/
pushd surgescript
mkdir -p build && cd build
cmake .. -DCMAKE_TOOLCHAIN_FILE=../cmake/toolchain-mingw.cmake
make
sudo make install
popd

You may install SurgeScript into a non-standard folder by modifying CMAKE_INSTALL_PREFIX (via the command-line or using ccmake | cmake-gui). However, we cover only the default setup in this guide.

Compile Open Surge

If everything went well so far, you're ready to build Open Surge! The sources are available at https://github.com/alemart/opensurge/releases Adapt file src/misc/toolchain-mingw.cmake if necessary.

# extract the sources to opensurge/
pushd opensurge
mkdir -p build && cd build
cmake .. -DCMAKE_TOOLCHAIN_FILE=../src/misc/toolchain-mingw.cmake \
  -DCMAKE_SYSROOT=$ALLEGRO_DEPS_FOLDER -DALLEGRO_STATIC=ON -DALLEGRO_MONOLITH=ON -DSURGESCRIPT_STATIC=ON
make
# sudo make install # the Linux build uses this
popd

The Open Surge executable will be available in the opensurge folder.

Legacy version

The instructions below help you compile the legacy version of Open Surge (0.2.0). These instructions are no longer applicable to the current version of Open Surge, but have been kept for historical purposes. Unlike the current version, the legacy version used Allegro 4.

Compiling on Windows

At some point you may want to compile the game/engine on your own, mostly if you are modifying the sources. From here on, when GAMEDIR is mentioned, it means only the directory where you unpacked the game. GAMEDIR stores, among other things, a readme.html page and a file called CMakeLists.txt.

Preparing the environment

We will first need two programs:

Download and install both of them, the latest versions. It will be better if you install MinGW in your C: disk, under C:\MinGW.

Also, make sure your CMake is installed with this option (or the one under) selected:

Cmake install.jpg

Setting the PATH

In order for the compiler to know where to get the liraries, we need to add two variables to your system; right-click on "My Computer", select "Properties", then "Advanced System Settings", and then "Environment Variables".

Now, in the lower box, we select the variable called "Path" and then click "Edit". If it doesn't exist, we should create it.

If it exists, add this at the end:

;C:\MinGW\bin;C:\MinGW\lib

If you're creating it, ignore the ; at the beginning and paste the rest. Give OK to all open dialog boxes.

Getting the dependencies

Now we should download all the dependencies. It is recommendable that you extract the files in an easy to read folder structure. For example, on a main folder called "src", extract each dependency to its separate folder.

Let's go to liballeg.org and download Allegro 4.4. Extract the contents of the file into the folder where we are going to keep the dependencies.

It is now recommended that we compile Allegro. To avoid any errors, we should go to the folder where Allegro was extracted, and delete the CMakeCache.txt file before compiling.

Now it's time to run CMake GUI.

Use this image as a reference on how to configure:

Cmake.jpg

Now we need

Make sure you follow the Generic installation instructions. For ease of access, here are the download links:

http://opensnc.sourceforge.net/alfont/AlFont209.rar (Original Alfont)

http://opensnc.sourceforge.net/alfont/alfont.c (Modified Alfont, conveniently prepared for download).

Now, after downloading and extracting the Original Alfont, replace alfont/src/alfont.c with the modified alfont.c you just downloaded.

Next, follow the compilation process described in alfont/README.txt: with a text editor, modify Makefile and set TARGET=MINGW32_STATIC

Now you should

  • Compile Alfont.

After running make, copy alfont/lib/mingw32/libalfont.a and alfont/include/* to the MingW directories, normally C:/MingW/lib , and C:/MingW/include .

You should now be ready for the final step.

Compiling Open Surge

When using MinGW, make sure both lib\ and bin\ folders are listed on your PATH. If MinGW is installed on C:\MinGW\, simply open a Command Prompt (cmd) and type:

set PATH=%PATH%;C:\MinGW\bin;C:\MinGW\lib

On the same Command Prompt, please go to GAMEDIR and run the following commands:

cmake -G "MinGW Makefiles" .
mingw32-make

Note that little '.' at the end of the first command. If everything works well, opensurge.exe will be in GAMEDIR.

Cross-compiling (advanced)

We'll learn how to compile the Windows version from a Linux box. This is more advanced and more complete (we show you how to compile everything, including the libs). You can adapt these instructions to compile Open Surge in other systems (particularly on *nix). If you came here from the web, you can follow these instructions to cross compile Allegro 4.4 as well:

Install MinGW

First of all, we need to install the cross-compiler. Get MinGW on its website or, on Debian-based distributions, simply run:

sudo apt-get install mingw-w64-i686-dev

Now, check where the cross-compiler has been installed. Then, run the following commands (adapt as necessary):

TOOLSET="i686-w64-mingw32"
MINGDIR="/usr/i686-w64-mingw32"

Note: the 32-bit version of the compiler is required for Allegro 4.4.

Install CMake

Get CMake if you haven't yet. Download it on its website or, on Debian-based distributions, run:

sudo apt-get install cmake

We'll use this tool to help us compile some things.

Compile zlib

Compiling zlib is pretty straighforward:

wget https://zlib.net/zlib-1.2.11.tar.gz
tar xvzf zlib-1.2.11.tar.gz
cd zlib-1.2.11
sed -e s/"PREFIX ="/"PREFIX = ${TOOLSET}-"/ -i win32/Makefile.gcc
make -f win32/Makefile.gcc
sudo BINARY_PATH=${MINGDIR}/bin \
    INCLUDE_PATH=${MINGDIR}/include \
    LIBRARY_PATH=${MINGDIR}/lib \
    make -f win32/Makefile.gcc install
cd ..

Compile libpng

You'll need libpng version 1.2:

wget https://download.sourceforge.net/libpng/libpng-1.2.59.tar.gz
tar xvzf libpng-1.2.59.tar.gz
cd libpng-1.2.59
./configure \
    CC="${TOOLSET}-gcc" \
    AR="${TOOLSET}-ar" \
    STRIP="${TOOLSET}-strip" \
    RANLIB="${TOOLSET}-ranlib" \
    CFLAGS="-I${MINGDIR}/include/" \
    LDFLAGS="-L${MINGDIR}/lib/" \
    --disable-shared \
    --target=$TOOLSET \
    --host=$TOOLSET \
    --prefix=$MINGDIR
make
sudo make install

Compile libogg

Run this:

wget http://downloads.xiph.org/releases/ogg/libogg-1.3.3.tar.gz
tar xvzf libogg-1.3.3.tar.gz
cd libogg-1.3.3
./configure \
    CC="${TOOLSET}-gcc" \
    AR="${TOOLSET}-ar" \
    STRIP="${TOOLSET}-strip" \
    RANLIB="${TOOLSET}-ranlib" \
    CFLAGS="-I${MINGDIR}/include/" \
    LDFLAGS="-L${MINGDIR}/lib/" \
    --disable-shared \
    --target=$TOOLSET \
    --host=$TOOLSET \
    --prefix=$MINGDIR
make
sudo make install

Compile libvorbis

Run that:

wget http://downloads.xiph.org/releases/vorbis/libvorbis-1.3.6.tar.gz
tar xvzf libvorbis-1.3.6.tar.gz 
cd libvorbis-1.3.6
./configure \
    CC="${TOOLSET}-gcc" \
    AR="${TOOLSET}-ar" \
    STRIP="${TOOLSET}-strip" \
    RANLIB="${TOOLSET}-ranlib" \
    CFLAGS="-I${MINGDIR}/include/" \
    LDFLAGS="-L${MINGDIR}/lib/" \
    --disable-shared \
    --target=$TOOLSET \
    --host=$TOOLSET \
    --prefix=$MINGDIR
make
sudo make install

Install the DirectX SDK for MinGW

This will install the DirectX SDK for MinGW:

wget https://download.tuxfamily.org/allegro/files/dx70_mgw.zip
sudo unzip -a -d$MINGDIR dx70_mgw.zip

Compile Allegro 4.4.2

To compile Allegro 4.4, first run:

wget https://github.com/liballeg/allegro5/releases/download/4.4.2/allegro-4.4.2.zip
unzip -a allegro-4.4.2.zip 
cd allegro

In cmake/Toolchain-mingw.cmake, adjust CMAKE_C_COMPILER, CMAKE_CXX_COMPILER and CMAKE_FIND_ROOT_PATH to ${which i686-w64-mingw32-gcc}, ${which i686-w64-mingw32-g++} and $MINGDIR, respectively (evaluate these values yourself and write their paths on the toolchain file). Additionally, set CMAKE_RC_COMPILER to ${which i686-w64-mingw32-windres} and add the command link_libraries("-static-libgcc") to the file. The result will be like this:

#
# Values to be placed in cmake/Toolchain-mingw.cmake
#
set(CMAKE_C_COMPILER /usr/bin/i686-w64-mingw32-gcc)
set(CMAKE_CXX_COMPILER /usr/bin/i686-w64-mingw32-g++)
set(CMAKE_FIND_ROOT_PATH /usr/i686-w64-mingw32)
set(CMAKE_RC_COMPILER /usr/bin/i686-w64-mingw32-windres)
link_libraries("-static-libgcc")

Then, run:

mkdir build && cd build
cmake \
    -DCMAKE_TOOLCHAIN_FILE=../cmake/Toolchain-mingw.cmake \
    -DCMAKE_INSTALL_PREFIX=$MINGDIR \
    -DWANT_EXAMPLES=ON \
    -DWANT_ALLEGROGL=OFF \
    ..
make -j`nproc`
sudo make install

You're done compiling Allegro! Additionally, run:

sudo ln -s $MINGDIR/lib/liballeg44.dll.a $MINGDIR/lib/liballeg.dll.a

Compile Modified Alfont 2.0.9

Compiling Modified Alfont is a bit more tricky. See its website for more information. Run the commands:

wget http://opensnc.sourceforge.net/alfont/AlFont209.rar
unrar x AlFont209.rar
cd alfont
wget http://opensnc.sourceforge.net/alfont/alfont.c -O src/alfont.c
sh ./fixunix.sh
sed -e s/"TARGET=DJGPP_STATIC"/"TARGET=MINGW32_STATIC"/ -i Makefile
sed -e s/"CC=gcc"/"CC=${TOOLSET}-gcc"/ -i Makefile
make
sudo install -m 755 lib/mingw32/libalfont.a ${MINGDIR}/lib
sudo install -m 644 include/alfont*.h ${MINGDIR}/include

Compile Open Surge

If you've managed to come so far, congratulations. We're almost there!

Get the source code of the game and unpack it to $HOME/opensurge (for example). Then:

cd ~/opensurge
mkdir build && cd build
cmake \
  -DCMAKE_SYSTEM_NAME=Windows \
  -DCMAKE_FIND_ROOT_PATH=$MINGDIR \
  -DCMAKE_RC_COMPILER=${TOOLSET}-windres \
  -DCMAKE_C_COMPILER=${TOOLSET}-gcc \
  -DCMAKE_FIND_ROOT_PATH_MODE_LIBRARY=ONLY \
  -DCMAKE_FIND_ROOT_PATH_MODE_INCLUDE=ONLY \
  ..
make

If everything went well, the opensurge executable will be in the opensurge folder. Make sure to include the Allegro DLL in the folder:

cp $MINGDIR/bin/alleg44.dll ~/opensurge

That's it!

Testing

Optional: if you have wine installed in your system, you can test the executable by running

wine ~/opensurge/opensurge.exe