I am having a problem with memcpying my index buffer:
#define D3DFVF_Vertex (D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1)
LPDIRECT3DVERTEXBUFFER9 vbTerrain = NULL;
LPDIRECT3DINDEXBUFFER9 ibTerrain = NULL;
int nHeightArray[128][128];//these are the heights of each pixel on the heightmap
WORD sIndices[(6 * 127 * 127)];//these are the order in which to draw the vertices
struct Vertex
{
float x, y, z;
D3DCOLOR diffuse;
float u, v;
};
Vertex TerrainData[128 * 128];//these are the actual vertices
//skipping past windows and d3d intialization code
bool InitTerrain()
{
int nCount = 0;
ZeroMemory(TerrainData, sizeof(TerrainData));
for(int i = 0; i < 128; i++)
{
for(int j = 0; j < 128; j++)
{
nHeightArray[i][j] = 0;
Vertex vTemp = {(float)(-64 + i), 0, (float)(-64 + j), D3DCOLOR_XRGB(0, 255, 0), 0.0f, 0.0f};
TerrainData[nCount++] = vTemp;
}
}
nCount = 0;
ZeroMemory(sIndices, sizeof(sIndices));
for(int i = 0; i != 16256; i += 128)
{
for(int j = 0; j < 128; j++)
{
sIndices[nCount++] = i + j;
sIndices[nCount++] = i + j + 1;
sIndices[nCount++] = i + j + 128;
sIndices[nCount++] = i + j + 1;
sIndices[nCount++] = i + j + 128 + 1;
sIndices[nCount++] = i + j + 128;
}
}
//Create Vertex Buffer
if(FAILED(device->CreateVertexBuffer(16384 * sizeof(Vertex), 0, D3DFVF_Vertex, D3DPOOL_DEFAULT, &vbTerrain, NULL)))
return false;
Vertex *Vertices;
if(FAILED(vbTerrain->Lock(0, sizeof(TerrainData), (void**)&Vertices, 0)))
return false;
memcpy(Vertices, TerrainData, sizeof(TerrainData));
vbTerrain->Unlock();
//Create Index Buffer
int nIndexListSize = sizeof(WORD) * nCount;
if(FAILED(device->CreateIndexBuffer(nCount, 0, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &ibTerrain, NULL)))
return false;
VOID* indices;
if(FAILED(ibTerrain->Lock(0, 0, (VOID**)&indices, 0)))
return false;
memcpy(indices, sIndices, sizeof(sIndices));//throws error here.
ibTerrain->Unlock();
device->SetIndices(ibTerrain);
return true;
}
When it gets to the:
memcpy(indices, sIndices, sizeof(sIndices));
line, it throws an error in memcpy.asm file saying "Unhandled exception at 0x0041f753 in TerrainViewer.exe: 0xC0000005: Access violation writing location 0x00f06000." What could I do to fix this? [and any other errors that I can''t see at this time]