Thread overview
including c++
Jun 27, 2002
ben
Jun 27, 2002
anderson
Jun 27, 2002
ben
Jun 27, 2002
anderson
Jun 27, 2002
ben
Jul 02, 2002
Walter
Jul 03, 2002
ben
Jun 27, 2002
Pavel Minayev
Jun 28, 2002
ben
Jul 02, 2002
Walter
June 27, 2002
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
June 27, 2002
"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


June 27, 2002
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

June 27, 2002
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:



June 27, 2002
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:

June 27, 2002
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.

June 28, 2002
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.

July 02, 2002
"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.


July 02, 2002
"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.



July 03, 2002
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