void d3dLoadGraphics::gPlainTerrain(IDirect3DVertexBuffer9 *vBuff, IDirect3DIndexBuffer9 *iBuff, unsigned short int width, unsigned short int depth) { TDVertices *roadV; try { roadV = new TDVertices [width * depth]; } catch (std::exception& e) { MessageBox(NULL, e.what(), "Error", MB_ICONERROR); } unsigned int rIndex = 0; bool u = 0, v = 0; for(unsigned short int z = 0; z < depth; z++) { for(unsigned short int x = 0; x < width; x++) { roadV[rIndex].x = (float(x) * 15) - (float(width) * 15 / 2); roadV[rIndex].y = 0; roadV[rIndex].z = (float(z) * (-15)) + (float(width) * 15 / 2); roadV[rIndex].nx = 0; roadV[rIndex].ny = 1; roadV[rIndex].nz = 0; roadV[rIndex].u = u; roadV[rIndex].v = v; (u == 0) ? u = 1 : u = 0; rIndex++; } u = 0; (v == 0) ? v = 1 : v = 0; } void *pVData; vBuff->Lock(0, 0, &pVData, 0); memcpy(pVData, roadV, sizeof(roadV)); vBuff->Unlock(); delPointer<TDVertices*>(roadV); rIndex = 0; WORD *pIData; iBuff->Lock(0, 0, (void**)&pIData, 0); for(unsigned short int z = 0; z < depth - 1; z++) { for(unsigned short int x = 0; x < width - 1; x++) { pIData[rIndex] = (z * width) + x; pIData[rIndex + 1] = pIData[rIndex] + 1; pIData[rIndex + 2] = (z + 1) * width + x + 1; pIData[rIndex + 3] = pIData[rIndex + 2]; pIData[rIndex + 4] = pIData[rIndex + 2] - 1; pIData[rIndex + 5] = pIData[rIndex]; rIndex += 6; } } iBuff->Unlock();}
Problem is with dynamic allocation of TDVertices structure. If I give say,
TDVertices roadV[900];
then the code works perfectly. But I need to dynamically allocate the struct. It won't give any exception either.
Isn't 'int a[100]' same as 'int *roadV = new a[10 * 10]'? If yes, then why is my code not working?
Can anyone help me please? Thanks!!!