Site under construction!
Building Your First Program
Introduction
This tutorial outlines how to build a DSFML program.
Prerequisites:
- A D compiler of your choice
- DSFML source files
- SFML 2.4
- DSFML static libraries (if you don't use DUB)
For the sake of brevity, this tutorial will use DMD.
Installing SFML
As DSFML is a binding to SFML, we will need this library to make our programs compile and work. If you don't already have this library installed, please visit https://www.sfml-dev.org/ for more information.
While it isn't a requirement to do so, this tutorial assumes that you'll be linking to the shared library version of SFML.
Building DSFML (Optional)
If you don't plan on using DUB to build your projects, you will need to have DSFML static libraries to link with. If you haven't already, see the tutorial on building from source. If you do plan on using DUB, you can skip that tutorial as DUB will handle building DSFML for you.
Example Code
If we're going to build a program, we're going to need some code.
The following code segement is what we'll use, 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 evt;
while(window.pollEvent(evt))
{
if(evt.type == evt.EventType.Closed)
{
window.close();
}
}
window.clear();
window.draw(head);
window.draw(leftEye);
window.draw(rightEye);
window.draw(smile);
window.draw(smileCover);
window.display();
}
}
Compiling - Command Line
To compile the test program, you'll need to know the path to three locations:
- the DSFML source files
- the DSFML static libraries
- the SFML shared libraries
In this section we'll refer to these as DSFML/src
,
DSFML/lib
, and SFML/lib
respectively.
Windows - 32 bit
dmd app.d -m32mscoff -IDSFML\src\ -L/LIBPATH:DSFMLC\lib\ -L/LIBPATH:DSFML\lib\ dsfml-graphics.lib dsfml-window.lib dsfml-system.lib sfml-graphics.lib sfml-window.lib sfml-system.lib
Windows - 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 sfml-graphics.lib sfml-window.lib sfml-system.lib
Linux/macOS
dmd app.d -IDSFML\src\ -L-LDSFMLC\lib\ -L-LDSFML\lib\ -L-ldsfml-graphics -L-ldsfml-window -L-ldsfml-system -L-lsfml-graphics -L-lsfml-window -L-lsfml-system
Compiling - DUB
Using DUB
to build your project simplifies a lot of things. All you need to
know is location of the SFML shared libraries. For this section,
we'll refer to to this location as SFML/lib
.
Follow the instruction for setting up your project here.
Once you have a package.json
or package.sdl
file created, add the SFML/lib
directory to
the lflags
setting using the correct linker
flag for your toolchain. For example, on Windows you will probably
add /LIBPATH:SFML\\lib\\
, while on Linux
or macOS you'll probably add -LSFML/lib
.
Next, add DSFML as a dependency by adding "dsfml:graphics": "~>2.4"
to your DUB
dependency
list.
Finally, envoke DUB on the command line to build your project.
dub build
Expected Output
If everything was done correctly, you will see a weird green smiling face.
That should take care of the basics of setting up a DSFML project. Check out the other tutorials on the specifics of using DSFML.