SFML community forums

Bindings - other languages => D => Topic started by: Jebbs on March 25, 2013, 05:16:35 pm

Title: Candidate for new official DSFML
Post by: Jebbs on March 25, 2013, 05:16:35 pm
Hey all!

A while back I became very interested in the D programming language and had already fallen in love with SFML, but when I looked into it there didn't seem to be much support. Unfortunately, deadalnix's repo for a DSFML seems to not have had much work done on it in about a year and I think we deserve a good D binding. I wrote this(https://github.com/Jebbs/DSFML (https://github.com/Jebbs/DSFML)) from scratch, and I think I did a pretty good job of making it work well and as if it was an actual D API. There are definitely some parts that could use improvement, but I plan on doing active development to make sure everything is up to date and that the binding continues to improve overall! Hopefully I didn't jump the gun on giving it the name DSFML!

It's very similar to krzat's binding in a lot of ways, so if you were able to get his working then the same process will apply to mine for the most part. Things my binding feature are lot's of code compatibility with the C++ API, overloaded operators for many things, and a general avoidance of pointers in favor of built in D types. I also wrote some code to make Drawable inheritance possible and only have the need to write the draw code once and still it work with a RenderTexture and a RenderWindow. More cool updates/improvements to follow!

In any case, let me know what you guys think! This is my first project I have ever had done that other people would use. The very next things I want to work on for the binding are getting it up to date and then writing some tutorials, which I will do soon, but I wanted to finally release something for others to see!
Title: Re: Candidate for new official DSFML
Post by: Nexus on March 25, 2013, 08:07:47 pm
Cool that you take the effort to write a D binding! Have you considered working together with other people that port SFML to the D language? If you have a similar philosophy, you might achieve more. On the other hand, it is more difficult to have a single code style.

Why don't you divide the classes to separate files? Especially if you plan to provide more than a plain wrapper, things get a real mess if you have only one file per module.
Title: Re: Candidate for new official DSFML
Post by: Jebbs on March 26, 2013, 12:36:46 am
I wanted to work with other people before I first started the project, and even had started a thread about it, but not a lot of people were active in the D section and it didn't go anywhere so I started writing one myself. It wasn't until I had a good chunk done that other people started posting stuff. I considered helping krzat, but he posted once and was done, so I kept working on my own binding. I really don't mind working on it by myself though, mostly because once I improve it and get everything fully functioning the way I want it I plan putting the project in my portfolio to help get me into a certain program at a university.

As for the way the modules are set up , I see what you are saying. I actually had it that way originally, but I have encountered reasons in which it makes more sense for it to be all in one file rather than in separate ones. Plus, I have some plans that will reduce the number of lines of code and make things look quite a bit cleaner.
Title: Re: Candidate for new official DSFML
Post by: esquinn on March 26, 2013, 12:18:23 pm
This might be a newbie mistake, but I'm trying to futz around with your code, but I haven't even quite gotten to the point of calling any of your functions. I've tried the latest DMD for both D1 and D2, and both of them error out with this snippet.

module main;

import dsfml.window;
import dsfml.audio;
import dsfml.graphics;
import dsfml.network;
import dsfml.system;

import std.stdio;


int main (string[] args)
{
        writefln ("Hi! I'm a bug.");
        return 0;
}
 

I'm running dmd -run test.d

d1 gives this string of errors:

dsfml\window.d(106): Error: Declaration expected, not '('
dsfml\window.d(112): Error: Declaration expected, not 'TOK216'
dsfml\window.d(117): Error: unrecognized declaration
dsfml\audio.d(26): Error: Declaration expected, not 'TOK216'
dsfml\audio.d(37): Error: unrecognized declaration
dsfml\graphics.d(65): Error: semicolon expected following function declaration
dsfml\graphics.d(67): Error: Declaration expected, not 'return'
dsfml\graphics.d(71): Error: Declaration expected, not 'TOK216'
dsfml\graphics.d(81): Error: unrecognized declaration
dsfml\network.d(130): Error: found '!' when expecting ';' following statement
dsfml\network.d(161): Error: Declaration expected, not '('
dsfml\network.d(175): Error: basic type expected, not const
dsfml\network.d(175): Error: default argument expected for int
dsfml\network.d(175): Error: found 'const' when expecting ')'
dsfml\network.d(175): Error: semicolon expected following function declaration
dsfml\network.d(175): Error: Declaration expected, not '('
dsfml\network.d(178): Error: no identifier for declarator Message
dsfml\network.d(178): Error: semicolon expected to close declaration, not '!'
dsfml\network.d(178): Error: Declaration expected, not '!'
dsfml\network.d(251): Error: unrecognized declaration
dsfml\system.d(44): Error: semicolon expected following function declaration
 


Compiling with D2, on the other hand, gets me this:

dsfml\window.d(70): Error: ContextSettings(0u, 0u, 0u, 2u, 0u) is not an lvalue
dsfml\window.d(76): Error: ContextSettings(0u, 0u, 0u, 2u, 0u) is not an lvalue
dsfml\graphics.d(1006): Error: RenderStates(cast(BlendMode)0, Transform(sfTransform([1F, 0F, 0F, 0F, 1F, 0F, 0F, 0F, 1F])), null, null, sfRenderStates(cast(BlendMode)0, sfTransform([nanF, nanF, nanF, nanF, nanF, nanF, nanF, nanF, nanF]), null, null)) is not an lvalue
dsfml\graphics.d(1008): Error: RenderStates(cast(BlendMode)0, Transform(sfTransform([1F, 0F, 0F, 0F, 1F, 0F, 0F, 0F, 1F])), null, null, sfRenderStates(cast(BlendMode)0, sfTransform([nanF, nanF, nanF, nanF, nanF, nanF, nanF, nanF, nanF]), null, null)) is not an lvalue
dsfml\graphics.d(1101): Error: RenderStates(cast(BlendMode)0, Transform(sfTransform([1F, 0F, 0F, 0F, 1F, 0F, 0F, 0F, 1F])), null, null, sfRenderStates(cast(BlendMode)0, sfTransform([nanF, nanF, nanF, nanF, nanF, nanF, nanF, nanF, nanF]), null, null)) is not an lvalue
dsfml\graphics.d(1100): Deprecation: overriding base class function without using override attribute is deprecated (dsfml.graphics.RenderTexture.draw overrides dsfml.graphics.RenderTarget.draw)
dsfml\graphics.d(1107): Error: RenderStates(cast(BlendMode)0, Transform(sfTransform([1F, 0F, 0F, 0F, 1F, 0F, 0F, 0F, 1F])), null, null, sfRenderStates(cast(BlendMode)0, sfTransform([nanF, nanF, nanF, nanF, nanF, nanF, nanF, nanF, nanF]), null, null)) is not an lvalue
dsfml\graphics.d(1106): Deprecation: overriding base class function without using override attribute is deprecated (dsfml.graphics.RenderTexture.draw overrides dsfml.graphics.RenderTarget.draw)
dsfml\graphics.d(1136): Error: ContextSettings(0u, 0u, 0u, 2u, 0u) is not an lvalue
dsfml\graphics.d(1142): Error: ContextSettings(0u, 0u, 0u, 2u, 0u) is not an lvalue
dsfml\graphics.d(1238): Error: RenderStates(cast(BlendMode)0, Transform(sfTransform([1F, 0F, 0F, 0F, 1F, 0F, 0F, 0F, 1F])), null, null, sfRenderStates(cast(BlendMode)0, sfTransform([nanF, nanF, nanF, nanF, nanF, nanF, nanF, nanF, nanF]), null, null)) is not an lvalue
dsfml\graphics.d(1237): Deprecation: overriding base class function without using override attribute is deprecated (dsfml.graphics.RenderWindow.draw overrides dsfml.graphics.RenderTarget.draw)
dsfml\graphics.d(1243): Error: RenderStates(cast(BlendMode)0, Transform(sfTransform([1F, 0F, 0F, 0F, 1F, 0F, 0F, 0F, 1F])), null, null, sfRenderStates(cast(BlendMode)0, sfTransform([nanF, nanF, nanF, nanF, nanF, nanF, nanF, nanF, nanF]), null, null)) is not an lvalue
dsfml\graphics.d(1242): Deprecation: overriding base class function without using override attribute is deprecated (dsfml.graphics.RenderWindow.draw overrides dsfml.graphics.RenderTarget.draw)
 

Title: Re: Candidate for new official DSFML
Post by: Jebbs on March 26, 2013, 03:58:17 pm
To be honest, I'm not even sure what you could be doing wrong. Just to test, I pulled the source from the git, compiled it with the code you posted, and it worked fine. The only reason I can see it throwing errors like that would be if lines of code were missing :/ Maybe you could try downloading the source files again?

And I don't think it will work for D1, but that isn't being updated anymore anyways so I had my focus on D2.


Edit:
Is there anyway you could send me your project stuff as well as any build scripts if you use them?
Title: Re: Candidate for new official DSFML
Post by: esquinn on March 26, 2013, 05:22:32 pm
Here's the "project" as such--At the moment, I'm compiling entirely on the command line. I have that in my Documents\projects\dsfmltest folder, cd into it with the command prompt, and manually run "dmd test.d".

https://dl.dropbox.com/u/187059/dsfmltest.zip

If it helps, I'm running on windows 7 x64, though to the best of my knowledge, my entire build chain is 32-bit. My DMD reads version 2.062
Title: Re: Candidate for new official DSFML
Post by: esquinn on March 26, 2013, 06:18:19 pm
Okay, I'm not actually all that familiar with the deeper intracacies of D, and  this might be some really bad hackery and might even segfault once I start actually calling DSFML code, but...

The compiler seems to be having difficulty with the format "ref StructName argument = StructName ()"



sooooo, playing with window.d, I put this at the top of the module:

const ContextSettings DefaultContextSettings = ContextSettings ();


then changed every instance of "ref ContextSettings settings = ContextSettings ()" to "ref ContextSettings settings = DefaultContextSettings"

for DefaultRenderstates, however, I had to leave the 'const' out of the declaration up top. I hope *that* doesn't bode ill.

and it compiles.
Title: Re: Candidate for new official DSFML
Post by: esquinn on March 26, 2013, 07:11:01 pm
Alrighty. Did a quick best-as-possible conversion of the SFML wiki's little sample program, and I've got something that not only compiles, but runs.

Caveats: You've got a typo in your Font class--the loadFromFile () method is listed as lodFromFile.

And you don't seem to have the openFromFile methods implemented for your Audio classes

But, with that said and done, this compiled and ran on my end, with the modifications I made.

module main;

import dsfml.window;
import dsfml.audio;
import dsfml.graphics;
import dsfml.network;
import dsfml.system;

import std.stdio;

int main (string[] args)
{
        auto window = new dsfml.graphics.RenderWindow (dsfml.graphics.VideoMode (800,600), "SFML window");

        auto texture = new dsfml.graphics.Texture ();
       
        writefln ("phase!");

        if (!texture.loadFromFile("cute_image.jpg"))
                return 1;
        writefln ("phase!");

        auto sprite = new dsfml.graphics.Sprite (texture);

        writefln ("phase!");

        auto font = new dsfml.graphics.Font();
        if (!font.loadFromFile("arial.ttf"))
                return 1;

        writefln ("phase!");

        auto text = new dsfml.graphics.Text ("Hello SFML", font, 50);

        writefln ("phase!");

        auto music = new dsfml.audio.Music ("nice_music.ogg");

        writefln ("phase!");

/*      if (!music.openFromFile("nice_music.ogg"))
                return 1;*/


        music.play ();

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

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

                window.clear (dsfml.graphics.Color.Black);
                window.draw (sprite);
                window.draw (text);
                window.display ();
        }


        writefln ("Hi! I'm not a bug.");

        return 0;
}
 
Title: Re: Candidate for new official DSFML
Post by: Jebbs on March 26, 2013, 08:03:43 pm
I'm glad you got it working! I never played much with the the command line  so it took me a few minutes and you had already posted before I had a chance to reply!

Thanks for the info on the other stuff.  I'll fix the typo shortly! As for the audio stuff, I used to have everything initialized in the constructors instead of having load from methods. I decided it was better to be consistent with SFML so I added them in, but it appears I missed the audio module. I'll fix that too!

Feel free to let me know if you find anything else. I also built the most up to date CSFML libs last night so I will update everything soon as well.
Title: Re: Candidate for new official DSFML
Post by: Jebbs on March 26, 2013, 09:52:11 pm
All of the issues you described have been fixed in the repo! Thanks again for finding those!

Also, I just wanted to mention a couple of things. The modules are set up to auto-import any other modules they depend upon. So just like in SFML, importing dsfml.window will auto-import dsfml.system, dsfml.graphics will auto-import dsfml.window which auto-imports dsfml.system, etc.

Your import section for using everything you can just be:
import dsfml.audio;
import dsfml.graphics;
import dsfml.network;

Secondly, you don't need to include the type location when referring to things. For example:

auto window = new dsfml.graphics.RenderWindow (dsfml.graphics.VideoMode (800,600), "SFML window");

can be rewritten as

auto window = new RenderWindow (VideoMode (800,600), "SFML window");

Hopefully that makes thing a little better for you!

Title: Re: Candidate for new official DSFML
Post by: JayArby on April 13, 2013, 05:55:25 am
Looks great! It's good to have someone working on this again.

It really fits into the D style, yet remains faithful to the C++ API, which is great to see.
Title: Re: Candidate for new official DSFML
Post by: Jebbs on April 30, 2013, 07:42:47 am
Thanks for the kind words!


I figured I'd also give a little bit of an update since I just uploaded some code: The binding is about as complete as I am willing to make it while still being based off of CSFML.

There's a lot of stuff I still want to do to improve things though, and so I have started rewriting the C interface this binding is based off of. This will eliminate the need for a lot of the CSFML intermediate types as well as give me better control over how the D code interacts with the C code. Plus the libraries will be named DSFML instead of CSFML, which is pretty sweet. :P

It'll be a while before any of these major changes happen, so for now enjoy the binding as is. I'll still be maintaining this version until the new version get's up off the ground.  I'll also be active on this board, so feel free to ask any questions here!
Title: Re: Candidate for new official DSFML
Post by: con on June 05, 2013, 03:55:04 am
Hello there.

Nice wrapper, I think that this project rocks.

How do you compile this example application?
Title: Re: Candidate for new official DSFML
Post by: Jebbs on June 05, 2013, 05:37:54 am
Thanks for the kind words!


How do you compile this example application?


Which example application? I'd be more than happy to help though.
Title: Re: Candidate for new official DSFML
Post by: con on June 05, 2013, 02:01:27 pm
This example made by esquinn.

The only thing that bothers me is the syntax of the command line.
dmd ...

Thanks for the help.

Title: Re: Candidate for new official DSFML
Post by: Jebbs on June 06, 2013, 07:59:55 am
Sorry I wasn't able to get to this today. Work's been very hectic. Tomorrow I will try to write some tutorials on some different ways to build and I'll let you know once I get them done.
Title: Re: Candidate for new official DSFML
Post by: con on June 06, 2013, 05:21:54 pm
Bad news is that there are not lots of tutorials for D, because this language just started getting popular and people currently jumping in. So any wiki or text will be helpful and always welcomed.


Title: Re: Candidate for new official DSFML
Post by: Jebbs on June 09, 2013, 05:52:18 am
Finally had some time to help you out. And maybe others as well!

There are at least two ways that I can think of to build using DSFML. One is to include the source files directly when building your program. Currently, this isn't a big deal as all of the library is included in the 5 modules, but with the new version of DSFML in production which breaks up the code in to smaller source files this will cause the full command line to be quite ridiculous if you build this way. The way I prefer to build the library is by first compiling the DSFML source as a static library, and then using that when building your actual application.

You'll have to forgive me as I am assuming you are using Windows(though it appears that SFML has pretty large Linux and Mac communities as well though) so if you happen to be using a different OS then you'll have to change the extensions to those you actually use.

To build DSFML as a static library, I have used the following command line.
dmd -lib "dsfml\audio.d" "dsfml\graphics.d" "dsfml\network.d" "dsfml\window.d" "dsfml\system.d" "-ofdsfml.lib"

This produces the dsfml.lib file.

I whipped up a little Hello World of sorts for you to test if you'd like. I named it main.d.
module main;

import std.stdio;

import dsfml.graphics;

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

        CircleShape awesomeCircleShape = new CircleShape(10);

        awesomeCircleShape.fillColor = Color.Red;

        awesomeCircleShape.position = Vector2f(300,100);


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

                while(testWindow.pollEvent(event))
                {

                        if(event.type == Event.Closed)
                        {
                                testWindow.close();

                        }
                }



                testWindow.clear(Color.Black);
                testWindow.draw(awesomeCircleShape);
                testWindow.display();

        }


}


I have put the dsfml, csfml-system, csfml-window, and csfml-graphics lib files in the same directory as main.d and then I use this command line to build.

dmd main.d csfml-system.lib csfml-window.lib csfml-graphics.lib dsfml.lib "-Isrc" "-ofoutput\helloworld.exe"


"-Isrc" uses the -I compiler switch, which will have the compiler search through the directory named src(which you can replace with the location of the dsfml folder that contains the source files) when it encounters an import statement. I also use the -of switch that stands for output file. The full switch "-ofoutput\helloworld.exe" creates the folder "output" and placed the compiled program helloworld.exe in it. As long as the necessary .dll files are with the .exe your program should be able to run! If you don't want to place the .lib files in the same directory as main.d, or any other source file for that matter, you can put the full location of the lib file in quotes. For example: "C:\Users\Jebbs\Downloads\DSFML\DSFML Libs\csfml-system.lib"

You should be able use this as a starting point to(hopefully) build anything else you need using DSFML. If you'd like any other explanations or more info feel free to ask! I check these forums several times a day,unless work is very busy, and I idle in the irc.
Title: Re: Candidate for new official DSFML
Post by: con on June 10, 2013, 07:36:46 pm
Thanks a lot, this made things clear. I have made some tests myself to understand this concept better, I will post my results here.

This is the sample program I used (a cleaner and minified version of previous example):
import dsfml.graphics;

void main()
{
        auto window = new RenderWindow(VideoMode(800,600), "Hello DSFML!");
        auto circle = new CircleShape(10);
        circle.fillColor = Color.Red;
        circle.position = Vector2f(300,100);

        Event event;
       
        while(window.isOpen())
        {
                while(window.pollEvent(event))
                {
                        if(event.type == Event.Closed)
                        {
                                window.close();
                        }
                }
                window.clear(Color.Black);
                window.draw(circle);
                window.display();
        }
}
 

Before you begin to read you must know that:

Now for the compilation commands.

First Option: Is to link directly and straight to the DSFML library path.

Sample: ::)
dmd main.d D:\Development\Libraries\DSFML\dsfml\graphics.d D:\Development\Libraries\DSFML\dsfml\system.d D:\Development\Libraries\DSFML\dsfml\window.d  -ID:\Development\Libraries\DSFML\ -L+D:\Development\Libraries\DSFML\DSFMLLibs\ csfml-graphics.lib csfml-system.lib csfml-window.lib
 

Explanation:
dmd The compiler.

main.d The source code of your application.

D:\Development\Libraries\DSFML\dsfml\graphics.d
D:\Development\Libraries\DSFML\dsfml\system.d
D:\Development\Libraries\DSFML\dsfml\window.d
The source code of the DSFML library, this allows the code to be compiled, same as our application.

-ID:\Development\Libraries\DSFML The include path of the DSFML library, this allows import statements to work.


-L+D:\Development\Libraries\DSFML\DSFMLLibs\
This is the Optlink method, so we can set the linker path once and then link to libraries alone. Alternatively it may not be added, but then .lib files will need to use relative or absolute paths to be found.
I have removed space from this folder just to be sure dmd won't break paths and avoid using double quotes.

csfml-graphics.lib
csfml-system.lib
csfml-window.lib
No need to use paths here.

Second Option: Is to compile a static version of DSFML and link directly to the library path. Same as previous but now there is already a compiled version of DSFML.
Example:
dmd main.d -ID:\Development\Libraries\DSFML -L+D:\Development\Libraries\DSFML\DSFMLLibs\ csfml-system.lib csfml-window.lib csfml-graphics.lib dsfml.lib

Explanation:
dmd Compiler
main.d Source
-ID:\Development\Libraries\DSFML Include path to let import statements work
-L+D:\Development\Libraries\DSFML\DSFMLLibs\ Use Optlink to set linker path. It's very important to use slash/backslash in the end to indicate that this is a folder, do not forget it.

csfml-system.lib csfml-window.lib csfml-graphics.lib dsfml.lib
The libraries, note that I have placed dsfml.lib inside the DSFMLLibs folder.

Use any preffered method of compilation according to your needs.  :)
Title: Re: Candidate for new official DSFML
Post by: Jebbs on June 14, 2013, 06:05:58 pm
This is great!

Would you mind if I used parts of this for the wiki? You'll definitely get credit for it.
Title: Re: Candidate for new official DSFML
Post by: con on June 15, 2013, 12:26:09 am
Sure, glad to help.  :D