Original Post
I have searched for nearly two days on this topic and can't dig up anything. I have Beginning DirectX 10 Game Programming by Wendy Jones, and in the end of chapter 6 she creates a predefined terrain mesh. What I am trying to do is create a dynamic terrain piece by having the user input the number of rows, the number of columns and cell width and height. I have tried many many different options. I have tried pVertStruct.resize(tRows*tColumns) and everything turns up memory errors. I have very limited knowledge of vectors and am learning DirectX in the process of making this. I really dont know what I am doing wrong. Every vector tutorial says what I have is correct, but memory errors always pop up. With this build it shows up when the windows API calls up the New Terrain dialog box, which has not turned up memory errors in the past. Any information or help is greatly appreciated.
//Terrain Structs
struct VertexStruct
{
D3DXVECTOR3 pos;
};
/* Terrain Class */
class Terrain
{
public:
Terrain();
~Terrain();
void loadTerrainFile(LPCSTR filename);
void saveTerrainFile(LPCSTR filename);
HRESULT createNewTerrain();
private:
vector<VertexStruct*> pVertStruct;
ID3D10Buffer* pVerts;
ID3D10Buffer* pIndeces;
};
Terrain::Terrain()
{
pVerts = NULL;
if(FAILED(createNewTerrain()))
MessageBox(wndHandle, "Failed to create terrain!", "Error", MB_ICONERROR);
return;
}
HRESULT Terrain::createNewTerrain()
{
VertexStruct temp;
for(UINT z = 0; z < tRows; ++z)
{
for(UINT x = 0; x < tColumns; ++x)
{
temp.pos.x = (float)x * tCellWidth;
temp.pos.z = (float)z * tCellHeight;
temp.pos.y = 1.0f;
pVertStruct.push_back(&temp);
}
}
D3D10_BUFFER_DESC bufDesc;
bufDesc.Usage = D3D10_USAGE_DEFAULT;
bufDesc.ByteWidth = sizeof(VertexStruct) * (tRows * tColumns);
bufDesc.BindFlags = D3D10_BIND_VERTEX_BUFFER;
bufDesc.CPUAccessFlags = 0;
bufDesc.MiscFlags = 0;
D3D10_SUBRESOURCE_DATA initData;
initData.pSysMem = &pVertStruct[0];
HRESULT hr = pd3dDevice->CreateBuffer(&bufDesc, &initData, &pVerts);
if(FAILED(hr))
return hr;
return hr;
}