Sign in to follow this  

DirectX Forward Declarations

This topic is 1856 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi,

I am trying to forward declare stucts that I am using from directX so that I don't have to #include <D3DX9math.h> every time I want to use one in a header.

I am having some trouble forward declaring some classes (structs) from DirectX. My Vertex and index buffer(s) declarations work and I can use the members in my .cpp file but the Vertex declaration and shader don't work in my .cpp.

Is there a convention for using DirectX objects in custom classes or is there another way I can do this.

Below is what I current have.

[source lang="cpp"]#ifndef SPRITEMANAGER_H
#define SPRITEMANAGER_H

#include <vector>
//Custom (sprite) vertex for our Vertex Buffer
struct SpriteVertex;//forward declaration
#define D3DFVF_SPRITEVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1)

//-----------------------------------------------------------------------------------------------------------------
//Forward Declarations:
//-----------------------------------------------------------------------------------------------------------------
typedef struct IDirect3DVertexBuffer9 *LPDIRECT3DVERTEXBUFFER9, *PDIRECT3DVERTEXBUFFER9;
typedef struct IDirect3DIndexBuffer9 *LPDIRECT3DINDEXBUFFER9, *PDIRECT3DINDEXBUFFER9;
typedef struct IDirect3DVertexDeclaration9 *LPDIRECT3DVERTEXDECLARATION9, *PDIRECT3DVERTEXDECLARATION9;
typedef interface ID3DXEffect LPD3DXEFFECT;

class Sprite;
class SpriteManager
{
public:
SpriteManager(void);
~SpriteManager(void);

private:


//A Vector of all sprites created
std::vector<Sprite*> m_sprites;

//A dynamic Vertex Buffer that changes with each fram to make is hold the co-ordinates of each sprite
LPDIRECT3DVERTEXBUFFER9 m_pVB; // Buffer to hold vertices

//A Index buffer to go with the Vertex buffer
LPDIRECT3DINDEXBUFFER9 m_pIB; // Buffer to hold Indicies

//Vertex Declaration
IDirect3DVertexDeclaration9 m_pVertexDecl;

//Our shader
LPD3DXEFFECT g_pSpriteShader; //=== OUR SHADER EFFECT:
};[/source]

Thanks Edited by AussieSpoon

Share this post


Link to post
Share on other sites
Why dont you want to have to #include <D3DX9math.h> every time I want to use one in a header.
thats what its for.

Share this post


Link to post
Share on other sites
[quote name='ankhd' timestamp='1355730084' post='5011577']
Why dont you want to have to #include <D3DX9math.h> every time I want to use one in a header.
thats what its for.
[/quote]
From what I understood #including headers should only be done in .cpp files

Because if it is done a lot of times in multiple .h files it is being used unnecessarily and is inefficient? Correct me if I'm wrong.

Share this post


Link to post
Share on other sites
This is more general C++ rather than DirectX-specific, but my personal take is that you probably don't want to include headers in other headers if you don't really have to. This is because doing so can...

A. Promote leaky abstractions where the implementation details of a class or group of functions can "leak out" into the code that uses it

and

B. Cause long compile times when you change something in a header, since that header might be included in other headers which spreads the changes into many .cpp files

In your particular case B probably isn't too much of an issue, since you won't be modifying the DirectX headers. A depends on the your particular class and how it fits into your project.

If you do want to go with forward-declarations and avoid including the DX headers, I would suggest that you make it easy on yourself and only forward-declare the interface classes and skip the typedef's for pointer types. That will simplify things for you, and also I think it looks less ugly than a big mess of capital letters. Either way, you'll want to make your member "m_pVertexDecl" have type "IDirect3DVertexDeclaration9*" rather than "IDirect3DVertexDeclaration9", since interface types need to be pointers. Otherwise the compiler will complain that you're trying to instantiate an abstract class.

If you have any other compiler issues, I would suggest posting them here.

Share this post


Link to post
Share on other sites
[quote name='MJP' timestamp='1355731474' post='5011583']
I would suggest that you make it easy on yourself and only forward-declare the interface classes and skip the typedef's for pointer types.
[/quote]

So instead of:
[source lang="cpp"]typedef struct IDirect3DVertexDeclaration9 *LPDIRECT3DVERTEXDECLARATION9, *PDIRECT3DVERTEXDECLARATION9;[/source]
I use :
[source lang="cpp"]typedef interface IDirect3DVertexDeclaration9 IDirect3DVertexDeclaration9;[/source]
That doesn't seem to work, if that is what you meant

But my main problem is still trying to forward declare my shader (LPD3DXEFFECT)

I have:
[source lang="cpp"]#ifndef SPRITEMANAGER_H
#define SPRITEMANAGER_H

#include <vector>

//Custom (sprite) vertex for our Vertex Buffer
struct SpriteVertex;//forward declaration

#define D3DFVF_SPRITEVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1)

//-----------------------------------------------------------------------------------------------------------------
//Forward Declarations:
//-----------------------------------------------------------------------------------------------------------------
typedef struct IDirect3DVertexBuffer9 *LPDIRECT3DVERTEXBUFFER9, *PDIRECT3DVERTEXBUFFER9;
typedef struct IDirect3DIndexBuffer9 *LPDIRECT3DINDEXBUFFER9, *PDIRECT3DINDEXBUFFER9;
typedef struct IDirect3DVertexDeclaration9 *LPDIRECT3DVERTEXDECLARATION9, *PDIRECT3DVERTEXDECLARATION9;
//Shader Declaration
typedef interface ID3DXEffect *LPD3DXEFFECT;
#undef INTERFACE
#define INTERFACE ID3DXEffect

class Sprite;

class SpriteManager
{

public:
SpriteManager(void);
~SpriteManager(void);

private:

//A Vector of all sprites created
std::vector<Sprite*> m_sprites;

//A dynamic Vertex Buffer that changes with each fram to make is hold the co-ordinates of each sprite
LPDIRECT3DVERTEXBUFFER9 m_pVB; // Buffer to hold vertices

//A Index buffer to go with the Vertex buffer
LPDIRECT3DINDEXBUFFER9 m_pIB; // Buffer to hold Indicies

//Vertex Declaration
IDirect3DVertexDeclaration9* m_pVertexDecl;

//Shader pointer
LPD3DXEFFECT* g_pSpriteShader;
};


#endif//SPRITEMANAGER_H[/source]
The:
[source lang="cpp"]//Shader Declaration
typedef interface ID3DXEffect *LPD3DXEFFECT;
#undef INTERFACE
#define INTERFACE ID3DXEffect[/source]
Is a complete guess because it's the only relevant thing I could find in d3dx9effect.h

When I try to use it's member (g_pSpriteShader) it says 'No members avaliable'. So how do I forward declae the shader (LPD3DXEFFECT) "class"?

Thanks

Share this post


Link to post
Share on other sites
What I meant was that I would do it like this:

[source]
#ifndef SPRITEMANAGER_H
#define SPRITEMANAGER_H

#include <vector>

//Custom (sprite) vertex for our Vertex Buffer
struct SpriteVertex;//forward declaration

#define D3DFVF_SPRITEVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1)

//-----------------------------------------------------------------------------------------------------------------
//Forward Declarations:
//-----------------------------------------------------------------------------------------------------------------
struct IDirect3DVertexBuffer9;
struct IDirect3DIndexBuffer9;
struct IDirect3DVertexDeclaration9;
struct ID3DXEffect;

class Sprite;

class SpriteManager
{

public:
SpriteManager(void);
~SpriteManager(void);

private:

//A Vector of all sprites created
std::vector<Sprite*> m_sprites;

//A dynamic Vertex Buffer that changes with each fram to make is hold the co-ordinates of each sprite
IDirect3DVertexBuffer9* m_pVB; // Buffer to hold vertices

//A Index buffer to go with the Vertex buffer
IDirect3DIndexBuffer9* m_pIB; // Buffer to hold Indicies

//Vertex Declaration
IDirect3DVertexDeclaration9* m_pVertexDecl;

//Shader pointer
ID3DXEffect* g_pSpriteShader;
};

#endif//SPRITEMANAGER_H
[/source]

Your problem with the effect appears to be that you've declared a pointer to a pointer to an ID3DXEffect. You have this:

[code]
LPD3DXEFFECT* g_pSpriteShader;
[/code]

which is equivalent to this:

[code]
ID3DXEffect** g_pSpriteShader;
[/code]

This is why I recommended not using those typedefs: they can be confusing. Edited by MJP

Share this post


Link to post
Share on other sites

This topic is 1856 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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