• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
AussieSpoon

DirectX Forward Declarations

5 posts in this topic

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
0

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.
0

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.
0

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.
2

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
0

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
1

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  
Followers 0