www.digitalmars.com         C & C++   DMDScript  

D - including c++

reply ben <zander echotech.ca> writes:
Hello Everybody

I am one of the developers of a cross platform library called plib 
(plib.sourceforge.net), used for developing 3d games.. I know there has 
been alot of work done porting gl, and glu over to d, has glut been ported 
yet. 
On to my real question, is there any way to use c++ libraries. If so what do 
i have to do. Compile my c++ libraries(static or shared), convert c++ 
headers to d and compile my new d program using the d headers.. Can live be 
that easy or am i missing something.. 

Thanks Ben
Jun 27 2002
next sibling parent reply "anderson" <anderson firestar.com.au> writes:
"ben" <zander echotech.ca> wrote in message
news:aff549$1aiv$2 digitaldaemon.com...
 Hello Everybody

 I am one of the developers of a cross platform library called plib
 (plib.sourceforge.net), used for developing 3d games.. I know there has
 been alot of work done porting gl, and glu over to d, has glut been ported
 yet.
To my knowledge, glut has not been ported yet.
 On to my real question, is there any way to use c++ libraries. If so what
do
 i have to do. Compile my c++ libraries(static or shared), convert c++
 headers to d and compile my new d program using the d headers.. Can live
be
 that easy or am i missing something..
No, you can't link to c++ libs in D. You'd have to port the entire library right up to the C stuff which is sometimes impossible. What C++ libaries did you want to use?
 Thanks Ben
Jun 27 2002
parent reply ben <zander echotech.ca> writes:
plib is the library.. have a look its a great library for working with 3d 
scenes, flightgear was created using it.. its at plib.sourceforge.net

Later, Ben

anderson wrote:

 
 "ben" <zander echotech.ca> wrote in message
 news:aff549$1aiv$2 digitaldaemon.com...
 Hello Everybody

 I am one of the developers of a cross platform library called plib
 (plib.sourceforge.net), used for developing 3d games.. I know there has
 been alot of work done porting gl, and glu over to d, has glut been
 ported yet.
To my knowledge, glut has not been ported yet.
 On to my real question, is there any way to use c++ libraries. If so what
do
 i have to do. Compile my c++ libraries(static or shared), convert c++
 headers to d and compile my new d program using the d headers.. Can live
be
 that easy or am i missing something..
No, you can't link to c++ libs in D. You'd have to port the entire library right up to the C stuff which is sometimes impossible. What C++ libaries did you want to use?
 Thanks Ben
Jun 27 2002
parent reply "anderson" <anderson firestar.com.au> writes:
Sorry I over read that. The simple answer is you can't. I read some where
that Walter looked into adding c++ support but found it much to complex to
interface to D. There may be other free libraries C out there that build on
openGL, otherwise your stuck with openGL, directX and SDL in D.

Incidently have you tried using SDL (Simple DirectMedia Layer import module)
with openGL, it doesn't read any 3d file formats but it does sound, textures
and lots of other things.


 "ben" <zander echotech.ca> wrote in message
news:affa3i$1q85$1 digitaldaemon.com...
 plib is the library.. have a look its a great library for working with 3d
 scenes, flightgear was created using it.. its at plib.sourceforge.net

 Later, Ben

 anderson wrote:
Jun 27 2002
parent reply ben <zander echotech.ca> writes:
Ok, there are some major changes for c++ to d, if it ever remotely possible 
to re-write everything in d... 

Later, Ben

anderson wrote:

 Sorry I over read that. The simple answer is you can't. I read some where
 that Walter looked into adding c++ support but found it much to complex to
 interface to D. There may be other free libraries C out there that build
 on openGL, otherwise your stuck with openGL, directX and SDL in D.
 
 Incidently have you tried using SDL (Simple DirectMedia Layer import
 module) with openGL, it doesn't read any 3d file formats but it does
 sound, textures and lots of other things.
 
 
 "ben" <zander echotech.ca> wrote in message
news:affa3i$1q85$1 digitaldaemon.com...
 plib is the library.. have a look its a great library for working with 3d
 scenes, flightgear was created using it.. its at plib.sourceforge.net

 Later, Ben

 anderson wrote:
Jun 27 2002
parent reply "Walter" <walter digitalmars.com> writes:
"ben" <zander echotech.ca> wrote in message
news:afff3d$27li$1 digitaldaemon.com...
 Ok, there are some major changes for c++ to d, if it ever remotely
possible
 to re-write everything in d...
The only way at the moment to interface D to C++ is to write a C interface to the C++ library, and then access the C interface via D. The trouble with interfacing to C++ is the C++ object model is so complex it would essentially require building most of a C++ compiler into the D compiler.
Jul 02 2002
parent ben <zander echotech.ca> writes:
Walter wrote:

 
 "ben" <zander echotech.ca> wrote in message
 news:afff3d$27li$1 digitaldaemon.com...
 Ok, there are some major changes for c++ to d, if it ever remotely
possible
 to re-write everything in d...
The only way at the moment to interface D to C++ is to write a C interface to the C++ library, and then access the C interface via D. The trouble with interfacing to C++ is the C++ object model is so complex it would essentially require building most of a C++ compiler into the D compiler.
Thanks for the update.. Ben
Jul 03 2002
prev sibling parent reply Pavel Minayev <evilone omen.ru> writes:
On Thu=2C 27 Jun 2002 10=3A54=3A23 -0300 ben =3Czander=40echotech=2Eca=3E
wrote=3A

=3E Hello Everybody
=3E 
=3E I am one of the developers of a cross platform library called plib 
=3E =28plib=2Esourceforge=2Enet=29=2C used for developing 3d games=2E=2E I know
there has 
=3E been alot of work done porting gl=2C and glu over to d=2C has glut been
ported 
=3E yet=2E 

Nope=2E However=2C SDL is far better than GLUT for most purposes=2C and it
is ported=2E

=3E On to my real question=2C is there any way to use c++ libraries=2E If so
what do 
=3E i have to do=2E Compile my c++ libraries=28static or shared=29=2C convert
c++ 
=3E headers to d and compile my new d program using the d headers=2E=2E Can
live be 
=3E that easy or am i missing something=2E=2E 

Not that easy=2E You cannot just import C++ classes from D modules=2E
There are two ways to do this=2E One is to provide a C wrapper function
over each C++ class method=2C e=2Eg=2E=3A

=09=2F=2F C++ file
=09class Surface
=09{
=09public=3A
=09=09Surface=28=29=3B
=09=09void blit=28Surface& target=29=3B
=09=09virtual PIXEL* lock=28=29=3B
=09=09virtual void unlock=28=29=3B
=09=09=2E=2E=2E
=09}=3B

=09=2F=2F C wrappers
=09extern =22C=22
=09{
=09Surface* Surface=5FSurface=28=29 { return new Surface=3B }
=09void Surface=5Fblit=28Surface* =5Fthis=2C Surface* target=29 {
=5Fthis-=3Eblit=28target=29=3B }
=09PIXEL* Surface=5Flock=28Surface* =5Fthis=29 { return
=5Fthis-=3Elock=28=29=3B }
=09void Surface=5Funlock=28Surface* =5Fthis=29 { =5Fthis-=3Eunlock=28=29=3B }
=09=2E=2E=2E
=09}

=09
=09=2F+++ D module +++=2F
=09=2F=2F import C wrappers
=09extern=28C=29
=09{
=09typedef void* Surface=3B
=09Surface* Surface=5FSurface=28=29=3B
=09void Surface=5Fblit=28Surface* =5Fthis=2C Surface* target=29=3B
=09PIXEL* Surface=5Flock=28Surface* =5Fthis=29=3B
=09void Surface=5Funlock=28Surface* =5Fthis=29=3B
=09}=09

=09=2F=2F use it
=09Surface logo =3D Surface=5FSurface=28=29=3B
=09PIXEL* data =3D Surface=5Flock=28logo=29=3B
=09=2E=2E=2E
=09Surface=5Funlock=28logo=29=3B
=09Surface=5Fblit=28screen=29=3B
=09
Then=2C you can wrap this all into classes=2C reconstructing C++ class
hierarchy=2E
This is the most portable way=2C it'll work on any platform which has C++
and D compilers=2E But how about virtual methods=3F Well=2C you can then define
a
C++ class as a struct in D=3A

=09struct Surface
=09{
=09=09struct =5FVMT=09 =2F=2F virtual method table
=09=09{
=09=09=09PIXEL* =28*lock=29=28Surface*=29=3B
=09=09=09void =28*unlock=29=28Surface*=29=3B=09=09=09
=09=09}
=09=09=5FVMT* =5Fvmt=3B
=09}

Now you can use late binding as well=3A

=09data =3D logo-=3E=5Fvmt-=3Elock=28logo=29=3B
=09=2E=2E=2E
=09logo-=3E=5Fvmt-=3Eunlock=28logo=29=3B

Not sure if this is portable=2C though=2E I guess it works with DMC=2FDMD=2C
MSVC=2C
BCC=2C probably GCC as well=2C but it's better to check=2E
Jun 27 2002
next sibling parent ben <zander echotech.ca> writes:
Ok, thanks for your help. 

Pavel Minayev wrote:

 On Thu, 27 Jun 2002 10:54:23 -0300 ben <zander echotech.ca> wrote:
 
 Hello Everybody
 
 I am one of the developers of a cross platform library called plib
 (plib.sourceforge.net), used for developing 3d games.. I know there has
 been alot of work done porting gl, and glu over to d, has glut been
 ported yet.
Nope. However, SDL is far better than GLUT for most purposes, and it is ported.
 On to my real question, is there any way to use c++ libraries. If so what
 do i have to do. Compile my c++ libraries(static or shared), convert c++
 headers to d and compile my new d program using the d headers.. Can live
 be that easy or am i missing something..
Not that easy. You cannot just import C++ classes from D modules. There are two ways to do this. One is to provide a C wrapper function over each C++ class method, e.g.: // C++ file class Surface { public: Surface(); void blit(Surface& target); virtual PIXEL* lock(); virtual void unlock(); ... }; // C wrappers extern "C" { Surface* Surface_Surface() { return new Surface; } void Surface_blit(Surface* _this, Surface* target) { _this->blit(target); } PIXEL* Surface_lock(Surface* _this) { return _this->lock(); } void Surface_unlock(Surface* _this) { _this->unlock(); } ... } /+++ D module +++/ // import C wrappers extern(C) { typedef void* Surface; Surface* Surface_Surface(); void Surface_blit(Surface* _this, Surface* target); PIXEL* Surface_lock(Surface* _this); void Surface_unlock(Surface* _this); } // use it Surface logo = Surface_Surface(); PIXEL* data = Surface_lock(logo); ... Surface_unlock(logo); Surface_blit(screen); Then, you can wrap this all into classes, reconstructing C++ class hierarchy. This is the most portable way, it'll work on any platform which has C++ and D compilers. But how about virtual methods? Well, you can then define a C++ class as a struct in D: struct Surface { struct _VMT // virtual method table { PIXEL* (*lock)(Surface*); void (*unlock)(Surface*); } _VMT* _vmt; } Now you can use late binding as well: data = logo->_vmt->lock(logo); ... logo->_vmt->unlock(logo); Not sure if this is portable, though. I guess it works with DMC/DMD, MSVC, BCC, probably GCC as well, but it's better to check.
Jun 28 2002
prev sibling parent "Walter" <walter digitalmars.com> writes:
"Pavel Minayev" <evilone omen.ru> wrote in message
news:CFN374351043202778 news.digitalmars.com...
Not sure if this is portable, though. I guess it works with DMC/DMD, MSVC,
BCC, probably GCC as well, but it's better to check.
DMC's object model (i.e. the location of the vptr) is compatible with MSVC. It's different for GCC, so some care needs to be taken there.
Jul 02 2002