How to avoid global LPDIRECT3DDEVICE9?

Started by
4 comments, last by reinhold 17 years ago
Hello I have a problem with my little DirectX project and could use some help. I'm using c++, VS-2005 and the feb 2007 directx 9 SDK. I'm trying to keep everything devided into classes and with the device pointers as private or protected members of the relevent class, here is an example of dxsprite.h: //HEADER FILES #include <d3dx9.h> #include "dxdevice.h" class DxSprite : public DxDevice { public: void InitDxSprite(); void ReleaseDxSprite(); private: ID3DXSprite* DXSPRITE; }; #endif This works fine generally but many of my classes need LPDIRECT3DDEVICE9 or IDirect3DDevice9 and I don't know how to get functions in my other classes to accept the pointer except resorting to have it as a global pointer and using extern to declare it. I have tired by setting friend classes and/or friend functions and I have tired having the class where LPDIRECT3DDEVICE9 is defined as a base class to the other classes that need access to it but it does not work. It compiles but will crach upon running with everything I'v tried except having LPDIRECT3DDEVICE9 as a global pointer and using extern. And in case I was unclear I don't care if I have to use the extern keyword, but I don't want the pointer as a global. Any help would be great, hours and hours of google was no use. Regards.
Advertisement
Quote:Original post by reinhold
class DxSprite : public DxDevice


Why is a DirectX Sprite also a DirectX Device? It seems pretty difficult to use a sprite as a device...

Quote:
void InitDxSprite();
void ReleaseDxSprite();


You might want to use RAII (turn these two into a constructor and destructor, respectively).

Quote:This works fine generally but many of my classes need LPDIRECT3DDEVICE9 or IDirect3DDevice9


This is bad news. You should group usage of this interface to a handful of classes (a renderer, a sprite and a sprite factory should be enough) and not let other classes use it. Other classes should use the renderer, sprite and sprite factory classes instead.

Pass the pointer around to everything that needs the device. For a while, this will be tremendously painful. The goal is to force you to identify unnecessary coupling between components. As you naturally seek to reduce the amount of poitners being pushed all over the place, it will help your design evolve to one with less dependencies spanning across the codebase.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
I usually have a static instance of the device in the main application class where the "loop" is and then pass that to the constructors of the classes that need it, then they also have a pointer to it.

Singletons and static classes have a place, sometimes you really do only need one of something. Of cousre having everything accessing one class has its own problems like thread safety.
Thank you all for helping.

"Why is a DirectX Sprite also a DirectX Device? It seems pretty difficult to use a sprite as a device..."

Yes sorry, I forgot to remove the base declaration after I tired to solve my
pointer problem by having LPDIRECT3DDEVICE9 in a base class to get access.

"You might want to use RAII (turn these two into a constructor and destructor, respectively)."

Will do :)

"This is bad news. You should group usage of this interface to a handful of classes (a renderer, a sprite and a sprite factory should be enough) and not let other classes use it. Other classes should use the renderer, sprite and sprite factory classes instead."

Ok but right now I don't think I know enough directx to make that work,
but will try later.

"Pass the pointer around to everything that needs the device. For a while, this will be tremendously painful. The goal is to force you to identify unnecessary coupling between components. As you naturally seek to reduce the amount of poitners being pushed all over the place, it will help your design evolve to one with less dependencies spanning across the codebase."

Do you mean passing it like this?
void DxSprite::InitDxSprite(LPDIRECT3DDEVICE9 device9)
{
code...
D3DXCreateSprite (device9, &DXSPRITE);
code...
}
I tried this but it did not work I don't know what I'm missing.

"I usually have a static instance of the device in the main application class where the "loop" is and then pass that to the constructors of the classes that need it, then they also have a pointer to it.
Singletons and static classes have a place, sometimes you really do only need one of something. Of cousre having everything accessing one class has its own problems like thread safety."

Thanks I will experiment some with that to.
I got the passing to work now, no clue how i managed to not get it to work before, thanks again.

This topic is closed to new replies.

Advertisement