Skip to main content
gamedev.net
🔒 Locked

Initialization Question

Started by junder Mar 1, 2009 at 11:31 PM 6 replies 1k views
Original Post
junder
junder
I have been doing the initialization of my shape classes a certain way for a while and I now want to move on to a better way of doing it. I currently have five vertex types. I was creating a new vertex declaration every time the classes were created. Now I want to just make them each one time, then assign them to a class variable. My first thought of this was the fact that they are globals, and even though they are used only once, would this still be bad? Right now in my vertex types header I have the following code:

enum eVertexType { VertexBase, VertexLN, VertexLTEX, VertexLNTEX, VertexLNTEXTAN, _SIZE };

static IDirect3DVertexDeclaration9 *gcVertexDefinitions[_SIZE];
static size_t gcVertexSizes[_SIZE];

void InitializeVertexTypes(LPDIRECT3DDEVICE9 lpD3Device);

Inside of InitializeVertexTypes I will initialize all of the vertex declarations and set the sizes for gcVertexSizes. Then inside of my shape class I will have the following

	IDirect3DVertexDeclaration9 *m_VD;
	tVertexBase			m_vtVertexType;	
	size_t m_nSizeOfType;

My question is, is there a better way to do this? I am currently doing this and I call InitializeVertexTypes, all the data becomes valid but then later in my code all of the variables become erased back to (0x00000) like they were reset.
Nanoha
Nanoha
Quote:
My question is, is there a better way to do this? I am currently doing this and I call InitializeVertexTypes, all the data becomes valid but then later in my code all of the variables become erased back to (0x00000) like they were reset.


That sounds like you have two different instances of your objects. Are you missing inclusion guards (#pragma once or #ifndef...).
Interested in Fractals? Check out my App, Fractal Scout, free on the Google Play store.
junder
junder
I do have #ifndef on the header. Any other suggestions?
Leafteaner
Leafteaner
Quote:
That sounds like you have two different instances of your objects. Are you missing inclusion guards (#pragma once or #ifndef...).

This would give a linker error, at least in VS.


Quote:
My question is, is there a better way to do this? I am currently doing this and I call InitializeVertexTypes, all the data becomes valid but then later in my code all of the variables become erased back to (0x00000) like they were reset.

If I'm understanding you correctly, that first block of code are the globals that get setup once at load time. And then m_VD points to one of those global IDirect3DVertexDeclaration9's in gcVertexDefinitions? If you are doing this once at load time, and then release them when you exit, there should not be a problem. I would have to guess that you are releasing them somewhere in your code, perhaps your shape class releases m_VD?
junder
junder
I do not think my shape class releases them because by the time I get into the shape class they are already zeros.
Leafteaner
Leafteaner
hmmm. Are you checking the HRESULT values when you try to create them? Either the create functions are failing or somewhere they are getting released and set to NULL before they get to the shape class.
junder
junder
I keep getting the HRESULT of S_OK. I am starting to think it is a problem with it being a static global. Is there a better way to do this?

When I initialize it looks like this

	D3DVERTEXELEMENT9 dvVertexBaseElements[] =	{		{0,  0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},		D3DDECL_END()	};	HRESULT hr = lpD3Device->CreateVertexDeclaration(dvVertexBaseElements, &gcVertexDefinitions[VertexBase]);	// Vertex Base (VertexLN)	D3DVERTEXELEMENT9 dvVertexLNElements[] =	{		{0,  0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},		{0, 12, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL,   0},		D3DDECL_END()	};	hr = lpD3Device->CreateVertexDeclaration(dvVertexLNElements, &gcVertexDefinitions[VertexLN]);	// Vertex Base (VertexLNTEX)	D3DVERTEXELEMENT9 dvVertexLNTEXElements[] =	{		{0,  0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},		{0, 12, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL,   0},		{0, 24, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},		D3DDECL_END()	};	hr = lpD3Device->CreateVertexDeclaration(dvVertexLNTEXElements, &gcVertexDefinitions[VertexLNTEX]);	// Vertex Base (VertexLNTEXTAN)	D3DVERTEXELEMENT9 dvVertexLNTEXTANElements[] =	{		{0,  0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},		{0, 12, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL,   0},		{0, 24, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},		{0, 32, D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TANGENT,  0},		D3DDECL_END()	};	hr = lpD3Device->CreateVertexDeclaration(dvVertexLNTEXTANElements, &gcVertexDefinitions[VertexLNTEXTAN]);
junder
junder
Alright, so I have tracked it down to a scope problem. It seems as if as soon as I leave that function that they will get set back to 0x000000. Does anyone see why?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.