View on GitHub

DSFML

Introduction

Before we begin, make sure you have the following:

We'll use DMD for this tutorial, so read up on how to use your compiler of choice.

Installing DSFML/DSFMLC

There are two libraries that are needed in order to get DSFML working properly. The backend known as DSFMLC is a set of shared libraries that wraps SFML with a C ABI. DSFML itself is a D wrapper around DSFMLC, so both are necessary.

The easiest way to get these is from the downloads page. You can put them in your preferred location such as on your system's PATH or in a personal directory.

You can also build from source if you need to.

Used Code

In order to test out DSFML, we're going to need some code.

This is the code we'll be using for this tutorial, and we'll name it app.d:

module main;

import dsfml.graphics;

void main(string[] args)
{
    auto window = new RenderWindow(VideoMode(800,600),"Hello DSFML!");

    auto head = new CircleShape(100);
    head.fillColor = Color.Green;
    head.position = Vector2f(300,100);

    auto leftEye = new CircleShape(10);
    leftEye.fillColor = Color.Blue;
    leftEye.position = Vector2f(350,150);

    auto rightEye = new CircleShape(10);
    rightEye.fillColor = Color.Blue;
    rightEye.position = Vector2f(430,150);

    auto smile = new CircleShape(30);
    smile.fillColor = Color.Red;
    smile.position = Vector2f(368,200);

    auto smileCover = new RectangleShape(Vector2f(60,30));
    smileCover.fillColor = Color.Green;
    smileCover.position = Vector2f(368,200);

    while (window.isOpen())
    {
        Event event;

        while(window.pollEvent(event))
        {
            if(event.type == event.EventType.Closed)
            {
                window.close();
            }
        }

        window.clear();

        window.draw(head);
        window.draw(leftEye);
        window.draw(rightEye);
        window.draw(smile);
        window.draw(smileCover);

        window.display();
    }
}

Compiling a DSFML program (Command Line)

While we don't cover IDE's here, the set up should be mostly the same. You'll need to know the path to the source/import files, the path to the DSFML static libraries, and the path to the DSFMLC shared libraries.

In this section we'll refer to these as DSFML/src, DSFML/lib, and DSFMLC/lib respectively.

On Windows

For 32 bit:

dmd app.d -IDSFML\src\ -L+DSFMLC\lib\ -L+DSFML\lib\ dsfml-graphics.lib dsfml-window.lib dsfml-system.lib dsfmlc-graphics.lib dsfmlc-window.lib dsfmlc-system.lib

For 64 bit:

dmd app.d -m64 -IDSFML\src\ -L/LIBPATH:DSFMLC\lib\ -L/LIBPATH:DSFML\lib\ dsfml-graphics.lib dsfml-window.lib dsfml-system.lib dsfmlc-graphics.lib dsfmlc-window.lib dsfmlc-system.lib

On OS X and Linux

dmd app.d -IDSFML/scr -L-LDSFMLC/lib -L-LDSFML/lib -L-ldsfml-graphics -L-ldsfml-window -L-ldsfml-system -L-ldsfmlc-graphics -L-ldsfmlc-window -L-ldsfmlc-system

Just make sure that the DSFMLC shared libraries are able to be found by your application when you try to run it.

(Check out the bottom of the page for the expected output)

Compiling a DSFML program (DUB)

Using DUB to build your simplifies a lot of things. All you need to know is location of the DSFMLC shared libraries. For this section, we'll call it DSFMLC/lib.

In your generated dub.json, you'll want to add the lflags section:

Windows For 32 bit:

"lflags" : ["+DSFMLC\\lib\\"],

For 64 bit:

"lflags" : ["/LIBPATH:DSFMLC\\lib\\"],

OS X and Linux

"lflags" : ["-LDSFMLC/lib"],

Next, add DSFML to the dependencies:

"dependencies": 
    {
        "dsfml:graphics": "~>2.1"
    }

Note: Dependency packages will pull in what they need. For example, the graphics package also uses the system and windows packages.

Just make sure that the DSFMLC shared libraries are able to be found by your application when you try to run it.

Expected Output

If you have done everything correctly, you will see a weird green smiling face.

FACE!

FACE!

That should take care of the basics of setting up your DSFML project.

Check out the other tutorials for how to use DSFML.