Question

I am learning D and have mostly experience in C#. Specifically I am trying to use the Derelict3 Binding to SDL2. I have been able to get some basic functionality working just fine but I have become stumped on how to create an array argument for a specific call.

The library contains a call

SDL_RenderDrawLines(SDL_Renderer*, const(SDL_Point)*, int)  //Derelict3 Binding

And I have been unable to correctly form the argument for

const(SDL_Point)*

The SDL Documentation for this function states that this argument is an array of SDL_Point, but I am unclear how to create an appropriate array to pass to this function.

Here is an example of what I have at the moment:

void DrawShape(SDL_Renderer* renderer)
{
    SDL_Point a = { x:10, y:10};
    SDL_Point b = { x:500, y:500};

    const(SDL_Point[2]) points = [a,b];

    Uint8 q = 255;
    SDL_SetRenderDrawColor(renderer,q,q,q,q);
    SDL_RenderDrawLines(renderer,points,1);
}

And the compiler complains that I am not passing the correct type of argument for const(SDL_Point)* in points.

Error: function pointer SDL_RenderDrawLines (SDL_Renderer*, const(SDL_Point)*, int)
is not callable using argument types (SDL_Renderer*, const(SDL_Point[2u]), int)

I suspect this is a fundamental misunderstanding on my part so any help would be appreciated.

Was it helpful?

Solution

Arrays aren't implicitly castable to pointers in D. Instead, each array (both static and dynamic) has an intrinsic .ptr property that is a pointer to its first element.

Change your code to:

SDL_RenderDrawLines(renderer,points.ptr,1);

OTHER TIPS

given that the call asks for a pointer and length, I feel it is safer to define you own wrapper:

SDL_RenderDrawLines(SDL_Renderer* rend, const SDL_Point[] points){
    SDL_RenderDrawLines(rend,points.ptr,points.length);
}

(why it isn't defined I don't know, any performance hit from the extra function call is just a -inline away from being resolved)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top