Author Topic: First experiences with D programming langage  (Read 2376 times)

0 Members and 1 Guest are viewing this topic.

Offline LittleWhite

  • Senior Member
  • Amiga 1200
  • ********
  • Posts: 418
  • Karma: 31
  • It's me!
    • View Profile
You can breath calmly :) this is not a question.

(Since I didn't know where to post it (not section for D) I will post here at the moment. Anyway, maybe it will be the only message on D in the whole forum.

So ... you already know that I will talk on D.
This is not a so new programming language like Go (the google's programming language).
D has been created by Digital Mars (sounds delicious  :whack:), released as first stable version in 1.0
Now we are looking to the version 2.0 including new features (of course  ::)

Some random documentation:
http://www.digitalmars.com/d/
http://en.wikipedia.org/wiki/D_%28programming_language%29

Now ... going in the deepness of the thing.
The first good new:

It has a Garbage Collector :D Yeah \ o /
It wants to remove pointers and copy by reference by defaults ... but allow to use pointers (if you want to crash everything :D )
Documentation is shipped in the language (no need of additional doxygen)
Asserts too
Unit test too \ o /
C libraries can be used in D  :updance:

Now I will stop to say what you could know by reading the wikipedia. Really, I am not good at this language yet, but it's easy to write it. It looks like an improved C++ No headers, you have array with extended functionalities (and this without extended std lib) (but there is some if needed (Tango)). Some kind of hash map (called for the case associative array) are also shipped :).

Some problems also exists. Actually I am under GNU/Linux. Fine, there are compilers :) Great there is a GCC port for D. Even if this one is not completely supporting it it works pretty well (since I am doing basic stuff). I still have some problem to debug it with gdb :(
Otherwise they are proposiing their own compiler. Also working on Windows ;)

I have started a silly D OpenGL program. We can do demo in D and I know already one -> http://pouet.net/prod.php?which=53942

The OpenGL wrapper is called Derelict (also SDL / OpenAL / GLU / and many others) -> http://svn.dsource.org/projects/derelict/trunk/docs/index.html

So what to say about my experience with it ... hum ... nothing. I am still continuing to learn it. It looks really simpler that C++ (if you know C++ and the problems). Templates and operator overloading are allowed too and not too hard ;) (better syntax that in C++)

THere is one major problem ... the documentation. Since the language is not massively used ... tutorials are few. And try to type just 'D' in google ... not giving you good answers (Better with 'D programming language' ... I think.

ANd that's all for now. Don't hesitate to ask.

Oh ... really great news ... OpenGL is really nicely supported in the wrapper and extensions easier to use than in C with GLEW
The demoscene will never die, never!

Offline Hotshot

  • DBF Aficionado
  • ******
  • Posts: 2114
  • Karma: 91
    • View Profile
Re: First experiences with D programming langage
« Reply #1 on: March 19, 2011 »
Quote
It looks really simpler that C++ (if you know C++ and the problems)

You mean It looks really simpler than C++?

Offline LittleWhite

  • Senior Member
  • Amiga 1200
  • ********
  • Posts: 418
  • Karma: 31
  • It's me!
    • View Profile
Re: First experiences with D programming langage
« Reply #2 on: March 19, 2011 »
Hum ... I heard some not liking the headers ... I heard some saying things ...
Personnaly ... I really like the debug function ... which is a replacement for the
Code: [Select]
ifdef _DEBUG
// Stuff
#endif

Here some D code ->
Code: [Select]
private import derelict.sdl.sdl;
private import derelict.opengl.gl;

class WindowCreationException : Error
{
    this()
    {
        super("Fail to open the window!");
    }
}


class Window
{
    private SDL_Surface* pWindow;
   
    public this(uint width, uint height, string windowName, uint sdlFlags)
    {
        pWindow = SDL_SetVideoMode(width,height,32,sdlFlags);
        if ( pWindow == null )
        {
            throw new WindowCreationException();
        }
       
        SDL_WM_SetCaption(cast(char*)windowName, null);

    }
   
    // No destructor since the window surface is destructed in SDL_QUIT
    // Yes ... SDL has only one window :(
   
    public uint getWidth() {return pWindow.w; }
    public uint getHeight() { return pWindow.h; }
    public SDL_Surface* getSurface() { return pWindow; }
    public void clear() { SDL_FillRect(pWindow,null,SDL_MapRGB(pWindow.format,0,0,0)); }
   
    public void refresh() { SDL_UpdateRect(pWindow,0,0,0,0); }
}

// Right, it should be in a different file
class GLWindow : Window
{
    this(uint width, uint height, string windowName, bool isFullscreen)
    {
        SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
       
        uint sdlFlags = SDL_SWSURFACE | SDL_OPENGL;
        if ( isFullscreen )
        {
            sdlFlags |= SDL_FULLSCREEN;
        }
       
        super(width, height, windowName ~ " (OpenGL)", sdlFlags);
    }
   
    public void clear() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); }
    public void refresh() { SDL_GL_SwapBuffers(); }
}

Some more (template + operator overloading)
Code: [Select]
import std.math;

template Vertex3(T)
{
    class Vertex3
    {
        public T x;
        public T y;
        public T z;

        public this()
        {
            x = 0;
            y = 0;
            z = 0;
        }

        public this(T x, T y, T z)
        {
            this.x = x;
            this.y = y;
            this.z = z;
        }

        public void normalise()
        {
            // calculate the norme
            float norme = sqrt(cast(float)(this.x * this.x + this.y * this.y + this.z * this.z));

            if ( norme != 0 )
            {
                this.x /= norme;
                this.y /= norme;
                this.z /= norme;
            }
        }

        public Vertex3 cross(T)(Vertex3 v)
        {
            return new Vertex3!(T)(this.y * v.z - this.z * v.y,
                                   this.z * v.x - this.x * v.z,
                                   this.x * v.y - this.y * v.x);
        }

        public float dot(T)(Vertex3 v)
        {
            return this.x * v.x + this.y * v.y + this.z * v.z;
        }

        public Vertex3 opAdd(T)(Vertex3!(T) v)
        {
            return new Vertex3!(T)(this.x + v.x,
                                   this.y + v.y,
                                   this.z + v.z);
        }

        public Vertex3 opSub(T)(Vertex3!(T) v)
        {
            return new Vertex3!(T)(this.x - v.x,
                                   this.y - v.y,
                                   this.z - v.z);
        }

        public Vertex3 opMul(T)(T value)
        {
            return new Vertex3!(T)(this.x * value,
                                   this.y * value,
                                   this.z * value);
        }
    }
}

Vertex3!(T) cross(T)(Vertex3!(T) v1, Vertex3!(T) v2)
{
    return new Vertex3!(T)(v1.y * v2.z - v1.z * v2.y,
                           v1.z * v2.x - v1.x * v2.z,
                           v1.x * v2.y - v1.y * v2.x);
}

float dot(T)(Vertex3!(T) v1, Vertex3!(T) v2)
{
    return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
}

Vertex3!(T) opAdd_r(T)(Vertex3!(T) v1, Vertex3!(T) v2)
{
    return new Vertex3!(T) (v1.x + v2.x,
                            v1.y + v2.y,
                            v1.z + v2.z);
}
/*
Vertex3!(T) opSub(T)(Vertex3!(T) v1, Vertex3!(T) v2)
{
    return new Vertex3!(T) (v1.x - v2.x,
                            v1.y - v2.y,
                            v1.z - v2.z);
}

Vertex3!(T) opMul(T)(Vertex3!(T) v1, T value)
{
    return new Vertex3!(T)(v1.x * value,
                           v1.y * value,
                           v1.z * value);
}*/

alias Vertex3!(float) Vertex3f;
alias Vertex3!(int) Vertex3i;
(Alias is like typedef ;))
The demoscene will never die, never!