The Allegro Wiki is migrating to github at


From Allegro Wiki
Revision as of 15:10, July 16, 2007 by Matthew Leverton (talk | contribs) (cleaned a few things up)
Jump to: navigation, search



Please note that this article is not meant to be a comprehensive guide on D. It is only meant to help you get D working so that you can use DAllegro, the bindings that let you use Allegro with D. For more information on the D language, visit D specific resources.

"D is a systems programming language. Its focus is on combining the power and high performance of C and C++ with the programmer productivity of modern languages like Ruby and Python. Special attention is given to the needs of quality assurance, documentation, management, portability and reliability.

"The D language is statically typed and compiles directly to machine code. It's multiparadigm, supporting many programming styles: imperative, object oriented, and metaprogramming. It's a member of the C syntax family, and its appearance is very similar to that of C++."

Why Use D?

In a nutshell, D is like a very clean C++. It has garbage collection, a simplified reference system, dynamic arrays (and strings as a consequence), a no-headache file dependency system, foreach iterators, etc. If you fit in one of the groups, you'll probably like D:

  • You use C, but would like to use features like dynamic arrays, strings, and clean object oriented designs.
  • You use C++, but dislike it's ugly, verbose syntax and would rather using something more simple and intuitive.
  • You use Java or .NET, but would like to use something that is "lighter" and compiled to machine code.

In no way should you think that D is a magical language that is going to solve all of your problems. It's really not radically different from any of the above languages. Nothing here is trying to convince you that you should use D. If you're reading this article, you're probably already curious enough to give it a try.

You should be warned that many D related things are not "production" quality in terms of stability or reliability. While the official compiler is well tested, many of the 3rd party tools and libraries are works in progress. As long as you can put up with a few weird bugs here and there, you probably won't even notice it. And if you give it a fair try, you'll probably find that D + DAllegro is very nice for making games.

There's far too much to cover here, so for a detailed list of all of D's features, you should read the official documentation.

Available Compilers

There are a few compilers available:

  1. dmd: the official Digital Mars' compiler. Works with Windows and any modern 32-bit Linux distribution. If possible, it is recommended that you use this compiler. (For Windows, you also need the Digital Mars' C package for the linker.)
  2. gdc: a D front end to gcc. If you have an old Linux distribution or a 64-bit one, you'll need this. There are several binary versions available along with the source code.
  3. gdcmac: Same as above, but built specifically for OS X.

Interfacing with C Libraries

Although D borrows heavily from C, it is not backward compatible with C source code. But it can use C libraries such as Allegro with the help of D import files (similar to a C header files). The import files simply define the structures and API, such as:

<HighlightSyntax language="d"> extern (C) {

 struct POINT {
   int x, y;
 void hello_world();

} </HighlightSyntax>

Under Windows, dmd is compatible with object files and libraries created with the Digital Mars C compiler. This means you must either build a static library using the Digital Mars' C compiler or create an import library from any DLL using the included implib utility. Under Linux and OS X, dmd and gdc are compatible with the standard ELF files, so you can link directly with any library.



Download DMD and DMC from You should install them in separate directories, such as c:\dm and c:\dmd. Add c:\dm\bin and c:\dmd\bin to the PATH environment variable.

Linux (32-bit)

Download DMD from (You'll also need gcc, but you probably already have that installed.) Just copy the files to a place like /usr/local/dmd and add /usr/local/dmd/bin to your PATH environment variable.

Linux (64-bit)

Download the x86_64 gdc binary from the gdc sourceforge site. Just extract the file to your system's root folder, and everything should go in the proper place. (Although you should probably extract the folder to a safe place first.)


Download the gdc binary from the gdc-mac sourceforge site. It comes with an installer that will set everything up.

Your First Program

D source files are given a d extension. Similar to Java, the name of the source file serves as a meaningful organization purpose. Because of this, D has no need (or support) for include files. But we are getting ahead of ourselves! Let's take a look at our first program.

<HighlightSyntax language="d"> module hello;

import std.stdio;

void main() {

 writefln("Hello, World!");

} </HighlightSyntax>

To compile it with dmd (Windows / Linux 32-bit), use:

dmd hello.d -ofhello.exe 

To compile it using gdc (OS X / Linux 64-bit), use:

gdc hello.d -o hello.exe

The program does exactly what you expect, so let's look at the D specific things. The first line of every file should start with module name. For example, your sprite engine might be module mycoolgame.sprite which would indicate that the file lives at mycoolgame/sprite.d. So for this example, the file will be hello.d.

The import command is similar to #include; the main difference is you don't need to create a header file! You just import the name of the module, as described in the previous paragraph. So in C terms, we are doing something like #include <std/stdio.d>. The import will, by default, make all the public functions in that module available to the local module. You can filter your import by doing things like:

<HighlightSyntax language="d"> import std.stdio : writefln; // only imports writefln import std.stdio : print = writefln; // imports writefln but aliases it to print. </HighlightSyntax>

Working with Larger Projects

The larger your project becomes, the more of a hassle compiling it by hand becomes. Fortunately there is a very nice package available, namely rebuild, to assist with compiling large projects. With rebuild, all you need to do is specify the main file, and it does the rest by analyzing the source and including any necessary file. (It's very much like an automatic makefile.)

To install it under Windows, just download the Windows binary and place rebuild in your dmd/bin folder. For Linux and OS X, download the source and run make. Then copy the rebuild binary and the rebuild.conf directory to /usr/bin.

Using rebuild is easy:

rebuild mygame/main.d -ofmygame.exe -O -oqobjdir=obj

This will look at mygame/main.d and automatically include any other source files that it references. The output file will be called mygame.exe, and all the object files will be placed in the obj directory. (It uses fully qualifed names for the object files, so you don't have to worry about naming collisions.)

Using it with C libraries is just as easy. For example, under Windows you could do this to link against the dynamic version of Allegro:

rebuild mygame/main.d alleg.lib -ofmygame.exe -O -oqobjdir=obj

Or under Linux, you might do this:

rebuild mygame/main.d -L-L/usr/local/lib -L-lalleg-4.2.2 -L-lalleg_unsharable -ofmygame.exe -O -oqobjdir=obj

The -L option passes the rest of the argument to the linker. (Please Note: These are not complete instructions for linking against Allegro! This is just an example.)

To see all of the available rebuild options, just run rebuild with no arguments.

Third Party Libraries

Rebuild is just one of many third party libraries available for D. You'll find many more at The following list of libraries are particularly useful if you plan on using DAllegro:

  • Tango: This is (more or less) a drop-in replacement for the standard library (Phobos) that comes with D.

TODO: Add More.