Passing d3d device by reference

Started by
12 comments, last by cozzie 10 years, 9 months ago
IDirect3DDevice9 has no const member functions. You basically can't do anything useful with a pointer to const IDirect3DDevice9. However, a const LPDIRECT3DDEVICE9 is a const pointer to a non-const IDirect3DDevice9. When you add const to a typedef of a pointer you get a const pointer type, not a pointer to a const object. It's equivalent to IDirect3DDevice9 * const not const IDirect3DDevice9 *.
Advertisement
OK, so basically there's no reason to do it otherwise then just giving the LPDIRECT3DDEVICE9 as a parameter to the function, which is already a pointer to the idirect3ddevice9,

void LoadTextures(LPDIRECT3DDEVICE9 myDev)
{
D3dxfunction(....., myDev or &myDev)
myDev->setstreamsource etc.
}

This also means that this function is allowed to do everything with the device where myDev points too.
I was hoping there was a way to distinct what and what not a function is allowed to do with the device, but since the idirect3ddevice9 has no const member functions, I think this is a no go.

Would there be a possibility to distinct using the device just as a parameter/ reference when calling other functions who just need to know the device, versus functions who need to execute functions on the device itself:

1. D3dxcreatetexturefromfile(...., myDev);

Versus

2. myDev->avalidfunction (i.e. Reset, release, setpixelshader, setstreamsouce etc.)

That way I might be able to protect misusage of the device, in functions where I just need to pass it as a reference to a d3d(x) function.

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

New question;

I've been doing some reading and looking at examples.
What I see often to 'solve' this is:

- the main d3d wrapper class has a ccomptr to the d3d device
- other classes who need access to the device have a private or protected d3d device ccomptr
Which is set when initializing an object of that class
- this 'copy' of the ccomptr to the device is not accesible from outside these classes

This seems like a good solution to me (for my d3dscene, mesh and skybox classes).
Things to think about:

- what if the device that's pointed too changes? (not that I expect this though)
- this doesn't prevent other classes then the main d3d wrapper class, to manipulate things on the device and run member functions on it

What do you think?

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

I now implemented it as above, works fine.
I now pass the device pointer only once to an object of my classes, if more then one functions (mostly create or init) need the device pointer, I've created a private member containing a ccomptr where I save the pointer in the init/ create function for that object (which receives the pointer as a function parameter.

Finally I set the device ccomptr to NULL in both the constructor and destructor for these classes (mesh, d3dscene, skybox).
No more ->Release() calls now, with using ccomptr/ smart pointers (for idirect3ddevice9, idirect3dtexture9 and and id3dxmesh objects).

If you have any suggestions or remarks, please reply

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

This topic is closed to new replies.

Advertisement