digitalmars.D.learn - Instantiate C struct on heap
- TencoDK (11/11) Aug 04 2016 Hey,
- Adam D. Ruppe (3/5) Aug 04 2016 That kind of struct isn't supposed to be created directly...
- TencoDK (2/8) Aug 04 2016 Thanks, by the way.
- TencoDK (12/23) Aug 04 2016 Looks like I found a solution. Too much C++ lately %)
- Mike Parker (8/19) Aug 04 2016 C structs can be created with new just like D structs as long as
- Mike Parker (6/7) Aug 04 2016 For clarity, in a D binding to a C library, a C struct *is* a D
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
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
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:Thanks, by the way.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
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
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
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 longFor 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