Skip to main content
GameDev.net gamedev.net
🔒 Locked

constructor functions too flexible?

Started by Metus Nov 29, 2005 at 3:09 AM 1 replies 1.1k views
Original Post
Metus
Metus
I really love to fiddle with engine core design and that includes some very interesting ways of creating flexibility. Yesterday a thing popped up in my head: what if my engine isn't too flexible to work in a large-scale project? 10 minutes lates the answer to that question was something I called "constructor functions". It means that any resource-based constructor only takes one argument and that is a function pointer. Example of my basic texture class:
[source lang = "cpp"]

Texture::Texture( boost::function < IDirect3DTexture9 () > constructor )
{
 IDirect3DTexure9* texture = constructor();
 // Error checking
 m_texture = boost::intrusive_ptr < IDirect3DTexure9 >( texture, false );
}


With this constructor and the boost::bind I can allow the user to pass in any function that returns an IDirect3DDevice9* to my texture constructor
[source lang = "cpp"]
IDirect3DTexture9* CreateFromFile( std::string const& source, IDirect3DDevice9 device )
{
 IDirect3DTexture9* texture = 0;
 RESULT result = D3DXCreateTextureFromFile( device, source.c_str(), &texture );
 // Error checking
 return texture;
}

IDirect3DDevice9* device = renderer->get_IDirect3DDevice9();
typedef boost::shared_ptr < Texture > TexturePtr;
TexturePtr texture( new Texture( boost::bind( CreateFromFile, "file.dds", device ) ) );

before I start to implement these constructor functions in the rest of my engine, is this a way that you'd like to work or is it just plain stupid? edit: perhaps this should be placed in the game programming section, but since the code contains directx code I though it might fit better here edit2: changed the subject name edit3+ tried to format the code
Ethereal
Sneftel
Sneftel
Just plain stupid. (I would have put it more politely, but you forced my hand. [grin]) Constructors don't have to be the only functions that create objects; if you need a function which loads a texture file and returns a Texture, go for it. Alternatively, just pass in the already-created IDirect3DTexture9, instead of making the constructor beg for it. Additionally, this exposes the innards of the creation unneccesarily; do you really want the client code to have to know about the CreateFromFile function?
Metus
Metus
Hehe.. no hard feelings about that :)
There's one reason that I'd want the constructor to create the object: I personally prefer that way instead of

[source lang = "cpp"]typedef boost::shared_ptr < Texture > TexturePtr;TexturePtr texture( new Texture )texture->load_from_file( "file.dds" );// or this wayIDirect3DTexture* d3d_texture = { ... };texure->assign( d3d_texture );


hmm... it seems pretty... weird anyway.. welll I think I have to get back to the design board then :)
Ethereal

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.