Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

D language function call with argument

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.

like image 323
MrJolo Avatar asked Mar 14 '26 16:03

MrJolo


1 Answers

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);
like image 67
Vladimir Panteleev Avatar answered Mar 16 '26 06:03

Vladimir Panteleev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!