Sign in to follow this  
Muzzy A

A Global D3D Device problem

Recommended Posts

Muzzy A    737
Hey guys a little new to these forums =).

I've written my own wrapper class for DirectX 9 and now I'm going back to change a few things to make it absolutely perfect =D.

[CODE]
// These classes and structs are in the same header file
static IDirect3DDevice9 *d3dDevice

class Direct3D
{
IDirect3D9 *d3dObject;

public:
// In here I Initiate everything for direct x
Direct3D9();
// I get a valid pointer to the device like it's supposed to
};

struct Sprite
{
IDirect3DTexture9 *texture;
// In here i try to load the texture, BUT d3dDevice is NULL
Sprite();
};

int main()
{
Direct3D game;
// As soon as i leave the constructor... d3dDevice is set to NULL

// So i crash here
Sprite player;

return 0;
}
[/CODE]

I used to have the Device Inside the Direct3D class, but that meant i had to pass Direct3D to the constructor of every Sprite I make, which is what i'm trying to avoid

Share this post


Link to post
Share on other sites
Aardvajk    13207
Well you have two choices. You either pass the instance to the sprite constructor or you'll have to have some kind of global access to the device. Simple as that really, however you dress it up.

The real question is why you would object to passing your device to the sprite constructor? Seems perfectly sensible to me.

Generally people worrying about passing around parameters end up tying themselves in worse knots in my experience.

Share this post


Link to post
Share on other sites
Muzzy A    737
I'm objecting to it because that means when i create a game class, i have to call the constructor for every sprite in the constructor of the game class like so

[code]

// The more non-dynamic objecs i make on that class the nastier this constructor is going to look
class Game
{
IDirect3DDevice9 *d3dDevice;

Sprite player;
Sprite anotherPlayer;
Sprite anObject;
};
Game::Game() : player(d3dDevice), anotherPlayer(d3dDevice), anObject(d3dDevice)
{

}

[/code]

i hate having to call the constructor in the Game constructor like this for several items. So i want a global Device so i can create Sprites without calling the contructor in the Game classes constructor.

My goal is to completly hide ALL the Direct X code.

Share this post


Link to post
Share on other sites
yckx    1298
Maybe have a SpriteFactory class that takes a pointer to the D3Ddevice in its constructor and holds it. To create a sprite call its CreateSprite method, which compartmentalized all that objectionable DirectX code from the code you really care about.

Edit: I thought maybe I should be as explicit as I could be, just in case. Your Game class will own an instance of SpriteFactory, and pass the device's pointer along to SpriteFactory's constructor in the initializer list of Game's constructor. SpriteFactory remembers it. Call SpriteFactory::CreateSprite() wherever you damn well please within Game's methods, or even pass a reference to the SpriteFactory to other classes if needed, depending on your program design. It encapsulates the DirectX sprite creation so you don't have to deal with API code you don't like. And it obviates your stated reason for using the global.

That's how I'd approach it, anyway.

Share this post


Link to post
Share on other sites
Muzzy A    737
[quote name='yckx' timestamp='1333343051' post='4927375']
Maybe have a SpriteFactory class that takes a pointer to the D3Ddevice in its constructor and holds it. To create a sprite call its CreateSprite method, which compartmentalized all that objectionable DirectX code from the code you really care about.

Edit: I thought maybe I should be as explicit as I could be, just in case. Your Game class will own an instance of SpriteFactory, and pass the device's pointer along to SpriteFactory's constructor in the initializer list of Game's constructor. SpriteFactory remembers it. Call SpriteFactory::CreateSprite() wherever you damn well please within Game's methods, or even pass a reference to the SpriteFactory to other classes if needed, depending on your program design. It encapsulates the DirectX sprite creation so you don't have to deal with API code you don't like. And it obviates your stated reason for using the global.

That's how I'd approach it, anyway.
[/quote]

Ya good idea, thanks. I don't know why i did't think of that myself =\.

if you wanna take me home baby there's a price to pay, I'm a genie in a bottle baby, gotta rub me the right way.

thanks for the help

,Muzzy A

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