//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// initTriangles
// Initialize the 3D mesh loaded from file
HRESULT initTriangles() {
HRESULT hr;
// build a vertex buffer that matches our vertex format
int num_elems = cVertex;
#ifdef DEBUG_ON
fprintf(pLOG,"NUMBER OF VERTICES = %d \n",num_elems);
fprintf(pLOG,"SIZE IN KB = %d\n\n",sizeof(vertex)*num_elems);
#endif
hr = pID3DDevice->CreateVertexBuffer(sizeof(vertex) *
num_elems,
D3DUSAGE_WRITEONLY,
D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1,
D3DPOOL_DEFAULT,
&pStreamData);
if(FAILED(hr)) {
#ifdef DEBUG_ON
fprintf(pLOG,"DX8 D3D CreateVertexBuffer HAS FAILED, ERROR: ");
errorLog(hr);
#endif
closeDINPUT();
shutdownD3D();
exit(0);
}
vertex *v;
hr = pStreamData->Lock(0, 0, (BYTE**)&v, 0);
if(FAILED(hr))
exit(0);
/**********************************
* INITIALIZE ALL THE VERTICIES
**********************************/
for(int ii = 0; ii < num_elems; ii++)
{
v[ii].x = vertices[ii].x;
v[ii].y = vertices[ii].y;
v[ii].z = vertices[ii].z;
v[ii].color = vertices[ii].color;
v[ii].tu = vertices[ii].tu;
v[ii].tv = vertices[ii].tv;
}
hr = pStreamData->Unlock();
if(FAILED(hr)) {
#ifdef DEBUG_ON
fprintf(pLOG,"DX8 D3D GetAdapterDisplayMode HAS FAILED, ERROR: ");
errorLog(hr);
#endif
closeDINPUT();
shutdownD3D();
exit(0);
}
hr = pID3DDevice->SetVertexShader(D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1);
if(FAILED(hr)) {
#ifdef DEBUG_ON
fprintf(pLOG,"DX8 D3D GetAdapterDisplayMode HAS FAILED, ERROR: ");
errorLog(hr);
#endif
closeDINPUT();
shutdownD3D();
exit(0);
}
hr = pID3DDevice->SetStreamSource(0, pStreamData, sizeof(vertex));
/***********************************
* INITIALIZE ALL THE INDICIES
***********************************/
num_elems = cIndex;
#ifdef DEBUG_ON
fprintf(pLOG,"NUMBER OF INDICES = %d \n",num_elems);
fprintf(pLOG,"SIZE IN KB = %d \n\n",sizeof(WORD)*num_elems);
#endif
hr = pID3DDevice->CreateIndexBuffer(sizeof(WORD) * num_elems, // in bytes, not indices
D3DUSAGE_WRITEONLY,
D3DFMT_INDEX16,
D3DPOOL_DEFAULT,
&pIndexBuffer);
WORD *pIndex;
hr = pIndexBuffer->Lock(0, 0, (BYTE **)&pIndex, 0);
if(FAILED(hr)) {
#ifdef DEBUG_ON
fprintf(pLOG,"DX8 D3D GetAdapterDisplayMode HAS FAILED, ERROR: ");
errorLog(hr);
#endif
closeDINPUT();
shutdownD3D();
exit(0);
}
for(ii = 0; ii < num_elems; ii++)
{
pIndex[ii] = indices[ii];
}
hr = pIndexBuffer->Unlock();
if(FAILED(hr)) {
#ifdef DEBUG_ON
fprintf(pLOG,"DX8 D3D GetAdapterDisplayMode HAS FAILED, ERROR: ");
errorLog(hr);
#endif
closeDINPUT();
shutdownD3D();
exit(0);
}
hr = pID3DDevice->SetIndices(pIndexBuffer, 0);
if(FAILED(hr)) {
#ifdef DEBUG_ON
fprintf(pLOG,"DX8 D3D GetAdapterDisplayMode HAS FAILED, ERROR: ");
errorLog(hr);
#endif
closeDINPUT();
shutdownD3D();
exit(0);
}
//LOAD THE GREEN TEXTURE
hr = D3DXCreateTextureFromFileA(pID3DDevice,
"1.bmp",
&pTexture);
if (FAILED(hr)) {
#ifdef DEBUG_ON
fprintf(pLOG,"DX8 D3D GetAdapterDisplayMode HAS FAILED, ERROR: ");
errorLog(hr);
#endif
closeDINPUT();
shutdownD3D();
exit(0);
}
// pID3DDevice->SetTexture(0, pTexture);
return hr;
}
The program crashes if I have the :
//LOAD THE GREEN TEXTURE
hr = D3DXCreateTextureFromFileA(pID3DDevice,
"1.bmp",
&pTexture);
line, but if I remove it, or my terrain is small enough everything works fine. This isin''t a DX error, there is a system error. Is there another way of loading textures into DX8? I''m assuming it''s a memory issue or something, but quite frankly I''ve little experience in DX programming, I''ve been using OpenGL until now. Any suggestions would be much appreciated.
Damn DX8 textures crashing on me
I''m writing a fractal terrain generator in DX8 and I''ve had my program crash every time I make my terrain larger than 21 x 21. I''ve isolated it to the texture code, when I remove texture loading it works fine. My D3D8 init looks like so (Sorry about the length):
maybe this works:
if( FAILED( D3DXCreateTextureFromFile( pID3DDevice, "1.bmp",&pTexture ) ) )
return E_FAIL;
Edited by - Jonus on January 27, 2001 1:29:17 PM
if( FAILED( D3DXCreateTextureFromFile( pID3DDevice, "1.bmp",&pTexture ) ) )
return E_FAIL;
Edited by - Jonus on January 27, 2001 1:29:17 PM
I fixed it, and it had nothing to do with what I sent. I was so sure my inexperience in DX8 was causing the problem that I didn''t realise I had a bug somewhere else in my code. I was not allocating enough memory for my terrain.
Gotta love C, since it worked with an array of certain size, I guess that I was accesing memory that was used for textures, causing the crash. Anyway, thanks for your help.
P.S. your site is very nice.
Gotta love C, since it worked with an array of certain size, I guess that I was accesing memory that was used for textures, causing the crash. Anyway, thanks for your help.
P.S. your site is very nice.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement