Sign in to follow this  
acidleaf

Dynamic array as static data member

Recommended Posts

Hello GD! This is my first post here, nice to meet you all! Currently I'm taking baby steps into DirectX, and I have a question that I'm not sure if it fits into this forum...so admins please move it to the correct one if needed! Thanks!

I am making a game where a spaceship is flying through space avoiding asteroids. There are four kinds of asteroids, each differing in shape, and they are loaded in as meshes. I find that loading the mesh each time an asteroid is created is a waste of CPU, so I've decided to make a manager class for it. The manager class will be responsible for loading each mesh at runtime and passing out pointers to these meshes for each instance of the asteroid to be drawn. My first question is, is this approach appropriate?

And my second question, here's my code:

AsteroidManager.h:
class AsteroidManager {
const static int NUM_MESHES = 4;

static LPD3DXMESH _mesh[NUM_MESHES];
static D3DMATERIAL9 **_materials;
static LPDIRECT3DTEXTURE9 **_textures;
static DWORD *_numMat;

public:
static bool loadMeshes();
};


AsteroidManager.cpp:
#inclulde "AsteroidManager.h"

LPD3DXMESH AsteroidManager::_mesh[NUM_MESHES];
D3DMATERIAL9 AsteroidManager::**_materials;
LPDIRECT3DTEXTURE9 AsteroidManager::**_textures;
DWORD AsteroidManager::*_numMat;

bool AsteroidManager::loadMeshes() {
_materials = new D3DMATERIAL9*[NUM_MESHES];
_textures = new LPDIRECT3DTEXTURE9*[NUM_MESHES];
_numMat = new DWORD[NUM_MESHES];

// The rest goes here
// ...

return true;
}


loadMeshes is called during initialization of my game. When I compile my program, it gives:

error LNK2001: unresolved external symbol "private: static unsigned long * AsteroidManager::_numMat" (?_numMat@AsteroidManager@@0PAKA)
error LNK2001: unresolved external symbol "private: static struct IDirect3DTexture9 * * * AsteroidManager::_textures" (?_textures@AsteroidManager@@0PAPAPAUIDirect3DTexture9@@A)
error LNK2001: unresolved external symbol "private: static struct _D3DMATERIAL9 * * AsteroidManager::_materials" (?_materials@MeteorManager@@0PAPAU_D3DMATERIAL9@@A)


Am I declaring my variables correctly? Please guide me :(

Thanks alot for your time!

Share this post


Link to post
Share on other sites
The pointer asterisks go before the "AsteroidManager::" part. You are declaring pointers to members with your current setup.

LPD3DXMESH AsteroidManager::_mesh[NUM_MESHES];

D3DMATERIAL9 **AsteroidManager::_materials;

LPDIRECT3DTEXTURE9 **AsteroidManager::_textures;

DWORD *AsteroidManager::_numMat;



Also if all the arrays are going to share the same size consider grouping the data into a structure and having an array of that. Also, consider using std::vector<> rather than maintaining a pointer,count pair.

Finally such static data is usually a bad idea, but one step at a time for the moment, get it working and if you're interested about improving your design come back to us.

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