Sign in to follow this  
Trillian

DirectX naming conventions

Recommended Posts

Trillian    410
I'm creating my own D3D library and I want to know somthing. Why does all the D3D things are interfaces? for exemple: -ID3DXMesh -IDirect3DVertexBuffer9 -etc I was reading a C# book recently (But i program in C++) and I felt on a page saying that interfaces were classes with only virtual member functions. If that's the case then we shouldn't be able to use these functions. example: IDirect3DVertexBuffer9::Unlock(); Because they were made to be defined in another class that inherits this interface... Could you please tell me what this is all about cause I want to have a similar naming convention in my library. My guesses: - __interface (which is how the IDirect3DVertexBuffer9 and others are declared) is not a true interface. - C++ interfaces have a different meaning than C# ones

Share this post


Link to post
Share on other sites
Arild Fines    968
If you have a pointer of an interface type, this pointer will always point to an instance of a class implementing said interface. IE:


class IBlah
{
public:
virtual void Bleh() = 0;
};

class ConcreteBlah : public IBlah
{
virtual void Bleh()
{
cout << "Bleh world" << endl;
}
};

// ...

IBLah* myBlah;

// ...


// you do this:
myBlah = new ConcreteBlah();


// but this is not allowed, since you cannot instantiate a class with
// one or more pure virtuals (the =0 part):
myBlah = new IBlah() ; //


The reason why DirectX uses interfaces for everything comes from the fact that it is based on COM. In COM, all functionality is accessed through interfaces. A pointer to concrete implementations of those interfaces is usually obtained through a class factory (traditional COM), through the CoCreateInstance() function or through various *Create() functions (very common in DX). Methods on COM objects can also return new instances of coclasses (a class implementing one or more COM interaces).

Share this post


Link to post
Share on other sites
MrJul    152
They are interface, same as C# or Java and every object model. That's why you cannot instance a IDirect3D object for exemple. DirectX must contains code with classes implenting those interfaces. However, you never have direct access to them, and it is a good thing, since you shouldn't be able to construct or destroy yourself DirectX classes. DirectX creates them for you through its functions, and return them as a pointer to an interface. Odd but nice way to do things, imho.

Share this post


Link to post
Share on other sites
Trillian    410
...And that explains why they made the LP things (like LPDIRECT3DTEXTURE9), beacause as it can only be used as a pointer which will be filled with an unknown dx class, you can't instanciate as a IDirect3DTexture9 alone.

Writing this:
IDirect3DTexture9 bob;

Would get you the error:
error C2259: 'IDirect3DTexture9' : cannot instantiate abstract class

Ok now thats really clear!

PS: I'm writing this more for myself than to you but anyways

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this