The Game Developer's Guidepost

The Game Developer's Guidepost

Your guide to learning game development

Programming Simple 2D Games in D

Setup

Before you can start writing any game code you'll want to download and install a compiler to turn all that code into executable programs your computer can actually run. There are three major D compilers as of this writing, so it may be a little confusing which one to choose. The DMD compiler is actively developed by the creator of the D programming language and members of the D community, so it's typically the most up-to-date in terms of supported language features. Additionally, compilation tends to be much faster when using DMD over the other two. This is the compiler I suggest using when following along with this series as it's the compiler all the example code has been tested against and all the build scripts have been written for. But which one you choose is really up to you.

The latest version of the major three D compilers can be downloaded from here. The method of installing one of these D compilers will vary based on the operating system you're currently using. Fortunately, there are instructions on how to install DMD for Windows, Mac OS X, and Linux. Just follow the instructions for the OS of your choice and you should be ready to start compiling D applications.

The next step is to install the SDL2 development libraries. The main SDL2 library can be downloaded from here and instructions on how to install it can be found here. After you install SDL2, you'll also want to download and install some additional libraries in the SDL2 family. For image loading you'll want to install SDL_image and for font rendering you'll want to install SDL_ttf. Installing both of these should be very similar to how you installed the base SDL2 library.

Next you'll probably want to install dub, the official D package manager. This utility will make it easy to compile and run each project in this series. You can certainly use an IDE such as Visual Studio to handle this for you, but given the large number of IDEs and the frequent changes to their interfaces we won't bother discussing how to configure those here. If you'd prefer, you can skip installing dub and simply invoke the compiler directly from the terminal.

The final step you'll want to take is to choose the text editor or IDE you'll use to write all those lines of D source code. Personally, I'm quite fond of Textadept as it's both lightweight and powerful, but a list of alternatives can be found here.

Now comes the moment of truth. If you want to determine if you've installed and configured everything correctly you can download this zip file containing an SDL2 test program. Once you've downloaded it, extract the contents somewhere on your computer. Open a terminal and navigate to where you just extracted the contents of the zip file. If you've opted to use dub, all you have to do now is type "dub" in the terminal and it will automatically read the included "dub.json" file and then proceed to build and run the project. If you've instead chosen to invoke the compiler directly from the terminal, issue the following command:

dmd -of=test *.d -L-lSDL2

If this is your first time doing this sort of thing, this command may seem quite alien to you. dmd tells the terminal that you want to run the DMD compiler. -of=test tells the compiler that you want to build an application with the name "test." *.d tells the terminal to list all the files ending in .d in the current directory (try looking up "terminal wildcards" online for more information). The -L option tells DMD to pass the text just after it to the linker, in this case -lSDL2. -lSDL2 tells the linker that the application we're compiling will need to be linked with the SDL2 library.

Now try to run the compiled application (dub should have done this part automatically). You should see a window open filled with a light salmon background color. If so, you're now ready to move on to the series proper! If not, you'll need to start tracking down what went wrong. If the problem occurs when you try to compile the application, then make sure DMD is installed and that it's directory is in the terminal's PATH environment variable. Also check to ensure you installed SDL2 properly. If the issue is at runtime, try running the application in a terminal and look up any errors printed.

Once you have the test application running successfully you're ready to begin working on the first game project in this series.