Thread overview
Instantiate C struct on heap
Aug 04, 2016
TencoDK
Aug 04, 2016
Adam D. Ruppe
Aug 04, 2016
TencoDK
Aug 04, 2016
TencoDK
Aug 04, 2016
Mike Parker
Aug 04, 2016
Mike Parker
August 04, 2016
Hey,

I'm using DerelictSFML2 + CSFML, I have stuck on instantiating window.

derelict/window.d (binding from C):
struct sfWindow;

my_file.d:
sfWindow* sfmlWindow = null;
sfmlWindow = new sfWindow; // ???

This code snippet gives me:
Error: struct derelict.sfml2.window.sfWindow unknown size

How must I create stuctures from C bindings?
August 04, 2016
On Thursday, 4 August 2016 at 21:02:59 UTC, TencoDK wrote:
> derelict/window.d (binding from C):
> struct sfWindow;

That kind of struct isn't supposed to be created directly... there should be a create window function in the library somewhere.
August 04, 2016
On Thursday, 4 August 2016 at 21:02:59 UTC, TencoDK wrote:
> Hey,
>
> I'm using DerelictSFML2 + CSFML, I have stuck on instantiating window.
>
> derelict/window.d (binding from C):
> struct sfWindow;
>
> my_file.d:
> sfWindow* sfmlWindow = null;
> sfmlWindow = new sfWindow; // ???
>
> This code snippet gives me:
> Error: struct derelict.sfml2.window.sfWindow unknown size
>
> How must I create stuctures from C bindings?

Looks like I found a solution. Too much C++ lately %)

sfmlWindow = sfWindow_create(
	sfVideoMode(800, 600),
        "Engine demo",
	sfTitlebar | sfClose,
	new sfContextSettings(	0, // depth bits
				0, // stencil bits
				0, // antialiasing
				4, // major
				3) // minor
);
August 04, 2016
On Thursday, 4 August 2016 at 21:21:14 UTC, Adam D. Ruppe wrote:
> On Thursday, 4 August 2016 at 21:02:59 UTC, TencoDK wrote:
>> derelict/window.d (binding from C):
>> struct sfWindow;
>
> That kind of struct isn't supposed to be created directly... there should be a create window function in the library somewhere.

Thanks, by the way.
August 04, 2016
On Thursday, 4 August 2016 at 21:02:59 UTC, TencoDK wrote:
> Hey,
>
> I'm using DerelictSFML2 + CSFML, I have stuck on instantiating window.
>
> derelict/window.d (binding from C):
> struct sfWindow;
>
> my_file.d:
> sfWindow* sfmlWindow = null;
> sfmlWindow = new sfWindow; // ???
>
> This code snippet gives me:
> Error: struct derelict.sfml2.window.sfWindow unknown size
>
> How must I create stuctures from C bindings?

C structs can be created with new just like D structs as long as they have a definition. sfWindow is an 'opaque type', which, just like a forward reference in C or C++, can not be instantiated directly because they have no definition. The error gives you a good hint with 'unknown size'. Many C libraries work this way, hiding the implementation internally and exposing an opaque type in the public headers.
August 04, 2016
On Thursday, 4 August 2016 at 23:49:26 UTC, Mike Parker wrote:

>
> C structs can be created with new just like D structs as long

For clarity, in a D binding to a C library, a C struct *is* a D struct. If they are declared as extern(C), that affects the name of the symbol and how it exists in the namespace (e.g. foo.bar.mystruct becomes simply mystruct), but nothing more. None of the Derelict packages do that, though.