www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Instantiate C struct on heap

reply TencoDK <marisalovesusall gmail.com> writes:
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?
Aug 04 2016
next sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
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.
Aug 04 2016
parent TencoDK <marisalovesusall gmail.com> writes:
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.
Aug 04 2016
prev sibling next sibling parent TencoDK <marisalovesusall gmail.com> writes:
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 );
Aug 04 2016
prev sibling parent reply Mike Parker <aldacron gmail.com> writes:
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.
Aug 04 2016
parent Mike Parker <aldacron gmail.com> writes:
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.
Aug 04 2016