Pointer to D3D Device

Started by
6 comments, last by RVOswald 13 years, 6 months ago
Hello all..

I have recently begun programming in C++ With the DirectX libraries. The examples I have been following are working well for me, but they were all coded in one main cpp file, so I've started moving them out into classes.

Specifically I am working on a particle & particle emitter class, but I have a question about passing around the LPDIRECT3DDEVICE9. This is a pointer to the device and I declare one in my main cpp file
LPDIRECT3DDEVICE9 d3dDevice;


In my Particle class that I have written, I have to at points use the device. For example, I must load the texture
D3DXCreateTextureFromFile(device,textureFileName,&texture);


I digress... I have created a second LPDIRECT3DDEVICE9 in the scope of my Particle class. Then in the constructor I do this:
Particle(LPDIRECT3DDEVICE9* device, ...){    d3dDevice = device;    ...}


The method call being
Particle particle[PARTICLECOUNT];for(int i = 0; i < PARTICLECOUNT; i++){    particle = Particle(&d3dDevice, ...);}

In effect I have a pointer to a pointer. Is this necessary or am I going a step too far with this? FYI it works with this method, but I am worried that I am creating unessential overhead?
Advertisement
Why do each of your particles need to store a copy of the device pointer?
LPDIRECT3DDEVICE9 is already a pointer. No need to add another pointer to it making it **.
I personally find it more confusing to use the LP defines, especially for beginners getting their heads round pointers.

I prefer:

IDirect3DDevice9 *Device;


LPDIRECT3DDEVICE9 is just a typedef in d3dtypes.h:

typedef IDirect3DDevice9* LPDIRECT3DDEVICE9;


I think it has something to do with the bad old days of 16-bit addressing, when it was possible to have two different sized pointer types.
Quote:Original post by no such user
Why do each of your particles need to store a copy of the device pointer?


As I said, I need to use the device to create the texture for the particles, which is done in the constructor of the Particle class. It seems that anything over 10,000 particles causes the program to stop working, less than that and it takes a while to load but it does run smoothly.
I tried switching over to IDirect3DDevice9* but it's still just as slow. Is there another way to accomplish this, without each Particle having a device pointer?
Sounds like you are rendering each particle one at a time. This will cripple your speed.

If any of your particles share a texture and render states, you really need to batch them into one vertex buffer and draw them in one go with one call to DrawPrimitive or whatever.
Quote:As I said, I need to use the device to create the texture for the particles, which is done in the constructor of the Particle class


Even if every particle has a different texture, I'd rather store the texture than the device. If you really allocate that a lot of textures, I bet your "program stops working" issue is a run-out of video memory. Some debugging, checking the DX API result values or even enabling the DX debug runtimes should show you what it is exactly.

Another approach than storing the texture per particle is to draw groups of particles which share the same texture, if your rendering logic allows that. In that case the particle does not need to store the texture at all.

Edit: Ninja-ed by Aardvajk. Correct: The approach you are looking for is called batching.
Okay, that makes sense. I am still learning here so my "logic" is hardly well defined. I'll look into ways of setting a single texture, it is apparent that I have a copy of the texture for each particle.

Thank you for the help.

This topic is closed to new replies.

Advertisement