Archived

This topic is now archived and is closed to further replies.

Why does my app crash

This topic is 5617 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

My code is failing inside of this function in the first DrawPrimitive call from what debugger tells me and waht my own personal debugging has been through.
  

void CTerrain::Draw()
{
	
	g_pDevice->SetStreamSource(0,pTerrainVertexBuffer,sizeof(D3DVERTEX));
	
	g_pDevice->DrawPrimitive(D3DPT_TRIANGLELIST,0,16129);

	g_pDevice->DrawPrimitive(D3DPT_TRIANGLELIST,48387,16129);
	
}
  
I have no idea why it''s crashing though I kind of believe it could be dealing with my vertexbuffer code which is dealt with in this section.
   
void CTerrain::CreateVertexBuffer(void)
{
	static D3DVERTEX aTerrain[(TERRAIN_X - 1) * (TERRAIN_Y - 1) * 6];
	unsigned char aHeightMap[TERRAIN_X][TERRAIN_Y];
	int VertexCount = 0;
	ifstream File;
	BYTE *pData;

	File.open("heightmap.raw",ios::binary);

	for(int j = 0; j < TERRAIN_Y; j++)
	{
		for(int i = 0; i < TERRAIN_X; i++)
		{
			File.get(aHeightMap[i][j]);
		}
	}

	File.close();

	for(int y = 0; y < TERRAIN_Y - 1; y++)
	{
		for(int x = 0; x < TERRAIN_X - 1; x++)
		{
			aTerrain[VertexCount].fX = (float)x;
			aTerrain[VertexCount].fY = (float)y;
			aTerrain[VertexCount].fZ = (float)aHeightMap[x][y] / 10;

			aTerrain[VertexCount+ 1].fX = (float)x+1;
			aTerrain[VertexCount+ 1].fY = (float)y;
			aTerrain[VertexCount+ 1].fZ = (float)aHeightMap[x+1][y] / 10;

			aTerrain[VertexCount+ 2].fX = (float)x+1;
			aTerrain[VertexCount+ 2].fY = (float)y+1;
			aTerrain[VertexCount+ 2].fZ = (float)aHeightMap[x+1][y+1] / 10;
		
			aTerrain[VertexCount + 3].fX = (float)x;
			aTerrain[VertexCount + 3].fY = (float)y;
			aTerrain[VertexCount + 3].fZ = (float)aHeightMap[x][y] / 10;
			
			aTerrain[VertexCount + 4].fX = (float)x + 1;
			aTerrain[VertexCount + 4].fZ = (float)aHeightMap[x + 1][y + 1] / 10;
			aTerrain[VertexCount + 4].fY = (float)y + 1;

			aTerrain[VertexCount + 5].fX = (float)x;
			aTerrain[VertexCount + 5].fY = (float)y + 1;
			aTerrain[VertexCount + 5].fZ = (float)aHeightMap[x][y + 1] / 10;
			
			VertexCount +=6;
		}
	}
	
	g_pDevice->CreateVertexBuffer(sizeof(aTerrain)*VertexCount,NULL,
								  D3DFVF_CUSTOMVERTEX,
								  D3DPOOL_DEFAULT,
								  &pTerrainVertexBuffer);
	
	if(FAILED(pTerrainVertexBuffer->Lock(0,sizeof(pData),(BYTE **)&pData,0)))
	{
		MessageBox(NULL,"Locked","Locked",MB_OK);
	}
	CopyMemory(pData,&aTerrain,sizeof(aTerrain));
	pTerrainVertexBuffer->Unlock();
}

  
could any of you provide me some help to why my app isnt displaying and running when i have those 2 draw primitive calls runnin, when i comment them out the app runs fine?? Eric Wright o0Programmer0o

Share this post


Link to post
Share on other sites
How could you make a so basic mistake, I think it due to a lack of cofee :D

You do this to lock the vertex buffer
if(FAILED(pTerrainVertexBuffer->Lock(0,sizeof(pData),(BYTE **)&pData,0)))


But it lock only 4 byte, because pData is a pointer you can''t use sizeof (you can on constant array).

You must do:

// Since the array is a D3DVERTEX, the sizeof will return the corret size in byte (don''t multiply by VertexCount)
g_pDevice->CreateVertexBuffer(sizeof(aTerrain),NULL, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &pTerrainVertexBuffer);

if(FAILED(pTerrainVertexBuffer->Lock(0,sizeof(aTerrain),(BYTE **)&pData,0)))



Good Luck

Share this post


Link to post
Share on other sites
I don''t know how work your CopyMemory function but it''s strange to pass "&aTerrain" (this is a unsigned char**) is it intend ?

Is your D3DFVF_CUSTOMVERTEX define like this:
#define D3DFVF_CUSTOMVERTEX D3DFVF_XYZ

If not, this may be the cause of app crash

Have you any other things drawn ?

If it still crash, you can send me you code via email.
It''s a little hard to know what gone wrong only with this code..

Good Luck :D

Share this post


Link to post
Share on other sites
CopyMemory is an Windows API function (unless it''s not the same CopyMemory here). Either way, taking the address of aTerrain won''t yield the address of something "useful," and I wouldn''t be a bit surprised if that were the/a problem right there. All you need is to pass aTerrain because that expression yields the address of the array, which is exactly what you want.

Share this post


Link to post
Share on other sites
quote:
Original post by Viquel Lukhar
I don''t know how work your CopyMemory function but it''s strange to pass "&aTerrain" (this is a unsigned char**) is it intend ?


Because aTerrain is a statically allocated array the following is true:

(aTerrain == &aTerrain)

In other words: &aTerrain is not a char** (aTerrain isn''t even a char*).

Share this post


Link to post
Share on other sites
Hmm. I just tried it out and you''re exactly right, both about the equivalence and the stipulation that it only works with a stack-allocated object. Anyway, if you ask me, it''s safest not to take the address of an array since you won''t get a "useful" address if the array is dynamically-allocated.

Share this post


Link to post
Share on other sites