i''ve got this code:
HRESULT InitTerrain()
{
// Initialize four vertices for rendering a square { -1.0f, -1.0f, 0.0f, 0xffffffff, 0, 1 }, // Bottom Left
LoadHeightmap();
CUSTOMVERTEX m_Vertices[40000];
char buffer[255];
for(int x=0;x<200;x++)
{
for(int z=0;z<200;z++)
{
m_Vertices[(unsigned)(x*200+z)].x = x;
m_Vertices[(unsigned)(x*200+z)].y = Heightmap[x][z];
m_Vertices[(unsigned)(x*200+z)].z = z;
m_Vertices[(unsigned)(x*200+z)].color = 0xffffffff;
m_Vertices[(unsigned)(x*200+z)].tu = 1;
m_Vertices[(unsigned)(x*200+z)].tv = 1;
sprintf(buffer,"%i\n",Heightmap[x][z]);
OutputDebugString(buffer);
}
}
// Initialize six indicesfor redering a square
short g_Indices[] = { 0, 1, 3, 3, 1, 2 };
// Create the vertex buffer.
if( FAILED( g_pd3dDevice->CreateVertexBuffer( 40000*sizeof(CUSTOMVERTEX),
0, D3DFVF_CUSTOMVERTEX,
D3DPOOL_DEFAULT, &g_pTerrainVB, NULL ) ) )
{
return E_FAIL;
}
.....
It gives me an access violation when CreateVertexBuffer, but when I uncomment the first line of that function (LoadHeightmap()
I don''t get the access error and everything runs fine.
Incase you need to know, here''s the source for LoadHeightmap
VOID LoadHeightmap()
{
// MessageBox(NULL,"Loading Heightmap","D3D",MB_OK);
//Load the heightmap
HANDLE HeightmapImage = LoadImage(0,"C:\\heightmap.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE|LR_CREATEDIBSECTION);
HDC HeightmapDC = CreateCompatibleDC(GetDC(GetActiveWindow()));
SelectObject(HeightmapDC,HeightmapImage);
for(int x=0;x<201;x++)
{
for(int y=0;y<201;y++)
{
//Add values into Heightmap array
Heightmap[x][y] = GetRValue(GetPixel(HeightmapDC,x,y));
}
}
}
any help would be greatly appriciated. Thanks