Jump to content

  • Log In with Google      Sign In   
  • Create Account

DirectX Forward Declarations


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 AussieSpoon   Members   -  Reputation: 184

Like
0Likes
Like

Posted 17 December 2012 - 12:45 AM

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 Bufferstruct 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 createdstd::vector<Sprite*> m_sprites;//A dynamic Vertex Buffer that changes with each fram to make is hold the co-ordinates of each spriteLPDIRECT3DVERTEXBUFFER9 m_pVB; // Buffer to hold vertices//A Index buffer to go with the Vertex bufferLPDIRECT3DINDEXBUFFER9 m_pIB; // Buffer to hold Indicies//Vertex DeclarationIDirect3DVertexDeclaration9 m_pVertexDecl;//Our shaderLPD3DXEFFECT g_pSpriteShader; //=== OUR SHADER EFFECT:};[/source]

Thanks

Edited by AussieSpoon, 17 December 2012 - 12:46 AM.


Sponsor:

#2 ankhd   Members   -  Reputation: 1298

Like
0Likes
Like

Posted 17 December 2012 - 01:41 AM

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

#3 AussieSpoon   Members   -  Reputation: 184

Like
0Likes
Like

Posted 17 December 2012 - 01:50 AM

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

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.

#4 MJP   Moderators   -  Reputation: 11455

Like
2Likes
Like

Posted 17 December 2012 - 02:04 AM

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.

#5 AussieSpoon   Members   -  Reputation: 184

Like
0Likes
Like

Posted 17 December 2012 - 02:58 AM

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.


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 Bufferstruct 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 Declarationtypedef interface ID3DXEffect *LPD3DXEFFECT;#undef INTERFACE#define INTERFACE ID3DXEffectclass 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 Declarationtypedef 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

#6 MJP   Moderators   -  Reputation: 11455

Like
1Likes
Like

Posted 17 December 2012 - 12:55 PM

What I meant was that I would do it like this:

#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


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

LPD3DXEFFECT* g_pSpriteShader;

which is equivalent to this:

ID3DXEffect** g_pSpriteShader;

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

Edited by MJP, 17 December 2012 - 01:04 PM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS