Archived

This topic is now archived and is closed to further replies.

shakazed

Error at setstreamsource

Recommended Posts

I´ve made a class to handle my graphics and it works fine until I get to setstreamsource, there the application halts and I get a win xp error. Thought I fixed it earlier but then I wanted to make a "grid" of points and it started to act up on me again. Simply put I want to do a 256*256 grid of points. I´ll post the class here and if you have the time please check it out.
//cGraphics header file


#include<windows.h>
#include<d3d9.h>
#include<d3dx9.h>
#include<stdio.h>

#define CUSTOMVERTEX_FVF (D3DFVF_XYZ|D3DFVF_DIFFUSE)

struct CUSTOMVERTEX 
{
	float x,y,z;
	D3DCOLOR d3dColor;
};

class CGraphics
{
public:

	bool Initialize(HWND hWnd); //Set it up

	void Present(void);   //Presents the scene

	bool LoadHeightMap(void);
	void Cleanup(void); 
	void SetupMatrices(void);
	CGraphics(void); //Constructor


private:

	IDirect3D9 *p_iD3D9;
	IDirect3DDevice9 *p_iD3DDevice9;
	IDirect3DIndexBuffer9 *p_iD3DIndexBuffer9;
	IDirect3DVertexBuffer9 *p_iD3DVertexBuffer9;
	int TERRAIN_X; //Width

	int TERRAIN_Z; //Height;


};


bool CGraphics::Initialize(HWND hWnd)
{
	p_iD3D9 = Direct3DCreate9(D3D_SDK_VERSION);
	
	D3DPRESENT_PARAMETERS d3dpp;
	ZeroMemory(&d3dpp, sizeof(d3dpp));
	d3dpp.Windowed = TRUE;
	d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
	d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
	d3dpp.EnableAutoDepthStencil = TRUE;
	d3dpp.AutoDepthStencilFormat = D3DFMT_D16;

	if(FAILED(p_iD3D9->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING,
	&d3dpp, &p_iD3DDevice9)))
	{
		MessageBox(NULL, "Failed to create device", "Error", MB_OK);
		return false;
	}


	p_iD3DDevice9->SetRenderState(D3DRS_CULLMODE,D3DCULL_NONE);
	p_iD3DDevice9->SetRenderState(D3DRS_ZENABLE, TRUE);

	LoadHeightMap(); //Load the .raw file and setup vertices/indices


	return true;
}

void CGraphics::Present(void)
{
	
	p_iD3DDevice9->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,0,0),1.0f,0);

	p_iD3DDevice9->BeginScene();
	SetupMatrices(); //Setup camera 

	
	// Set the location source of our vertex input stream

	p_iD3DDevice9->SetStreamSource( 0, p_iD3DVertexBuffer9,0, sizeof(CUSTOMVERTEX));

	// Set the default vertex shader to our #defined vertex

	p_iD3DDevice9->SetFVF(CUSTOMVERTEX_FVF);
	// Set the location of our index information to our Index Buffer

	//p_iD3DDevice9->SetIndices( p_iD3DIndexBuffer9 );


	p_iD3DDevice9->DrawPrimitive(D3DPT_POINTLIST,0,256*256);
	/*p_iD3DDevice9->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,
					   0,
					   256*6*256,
					   256*256,
					   0,
					   512*256);*/



	p_iD3DDevice9->EndScene();

	RECT rRect;
	rRect.left = 0;
	rRect.top  = 0;
	rRect.right = 480;
	rRect.bottom = 480;

	//Render only from and to the given rect

	p_iD3DDevice9->Present(&rRect,&rRect,NULL,NULL);
}

bool CGraphics::LoadHeightMap(void)
{

	/*FILE *pFile = NULL;

	int iHeightMapArray[256*256]; //All the y values

	pFile = fopen("hmap.raw", "rb" );
	fread(&iHeightMapArray[256*256], 1, 256*256, pFile );
	fclose(pFile);
	/////////////////////////////////////////////////////////////////

	p_iD3DDevice9->CreateIndexBuffer(256*6*256*sizeof(int), D3DUSAGE_WRITEONLY,
		D3DFMT_INDEX16, D3DPOOL_DEFAULT, &p_iD3DIndexBuffer9,NULL);

	int iIndexData[256*6*256];
		

	for(int y = 0;y < TERRAIN_Z;++y)
	{
		for(int x = 0;x < TERRAIN_X;++x)
		{
			iIndexData[y*TERRAIN_X] = x + y * (TERRAIN_X + 1);					//v1
			iIndexData[y*TERRAIN_X + 1] = x + 1 + y * (TERRAIN_X + 1);					//v2
			iIndexData[y*TERRAIN_X + 2] = x + 1 + (y + 1) * (TERRAIN_X + 1);				//v4

			iIndexData[y*TERRAIN_X + 3] = x + y * (TERRAIN_X + 1);					//v1
			iIndexData[y*TERRAIN_X + 4] = x + 1 + (y + 1) * (TERRAIN_X + 1);				//v4
			iIndexData[y*TERRAIN_X + 5] = x + (y + 1) * (TERRAIN_X + 1);					//v3
		}
	}

	VOID *p_Indices;
	if( FAILED( p_iD3DIndexBuffer9->Lock( 0,                 //Fill from start of the buffer.
                      		     sizeof(iIndexData), // Size of the data to load.
                      		     (VOID**)&p_Indices, // Returned index data.
                      		     0 ) ) )            // Send default flags to the lock.
    return false;

	//Copy the indice data to the indexbuffer
	p_iD3DIndexBuffer9->Lock(0,sizeof(iIndexData),(void**)&p_Indices,0);
	memcpy( p_Indices, iIndexData, sizeof(iIndexData) );
	p_iD3DIndexBuffer9->Unlock();*/

	/////////////////////////////////////////////////////////////////






	CUSTOMVERTEX vertices[256*256]; 

	for(int i = 0; i<256; i++)
	{
		for(int j = 0; j<256; j++)
		{
	
			vertices[256*i+j].x = (float)j;
			vertices[256*i+j].y = 0.0f;//iHeightMapArray[i*TERRAIN_Z+j];

			vertices[256*i+j].z = (float)i;
			vertices[256*i+j].d3dColor = D3DCOLOR_XRGB(255,255,255);
		}
	}


	if(FAILED(p_iD3DDevice9->CreateVertexBuffer((256*256)*sizeof(CUSTOMVERTEX),
		0, CUSTOMVERTEX_FVF, D3DPOOL_DEFAULT,&p_iD3DVertexBuffer9,NULL)))
	{
		MessageBox(NULL, "Can´t create vertexbuffer","Error", MB_OK);
		return false;
	}
	
	
	VOID* pVertices;
	if( FAILED(p_iD3DVertexBuffer9->Lock( 0, sizeof(vertices), (void**)&pVertices, 0 ) ) )
		return false;

	memcpy( pVertices, vertices, sizeof(vertices) );
	p_iD3DVertexBuffer9->Unlock();

	return true;
}

CGraphics::CGraphics(void)
{
	TERRAIN_X = 256;
	TERRAIN_Z = 256;
	p_iD3D9 = NULL;
	p_iD3DDevice9= NULL;
	p_iD3DIndexBuffer9= NULL;
	p_iD3DVertexBuffer9= NULL;

}

void CGraphics::Cleanup(void)
{
	if(p_iD3DIndexBuffer9 != NULL)
	{
		p_iD3DIndexBuffer9->Release();
		p_iD3DIndexBuffer9 = NULL;
	}

	if(p_iD3DVertexBuffer9 != NULL)
	{
		p_iD3DVertexBuffer9->Release();
		p_iD3DVertexBuffer9 = NULL;
	}

	if(p_iD3DDevice9 != NULL)
	{
		p_iD3DDevice9->Release();
		p_iD3DDevice9 = NULL;
	}

	if(p_iD3D9 != NULL)
	{
		p_iD3D9->Release();
		p_iD3D9 = NULL;
	}
		
}





void CGraphics::SetupMatrices(void)
{
    D3DXMATRIX matWorld;
    D3DXMatrixIdentity( &matWorld );	
	p_iD3DDevice9->SetTransform( D3DTS_WORLD, &matWorld );

	//Set up the view. First struct is eye point, next is lookat point

	//third is the direction of up, which here is set to the y axis.

	D3DXMATRIX matView;
   	D3DXMatrixLookAtLH( &matView,
			    &D3DXVECTOR3( 0.0f, 0.0f, -50.0f ),
			    &D3DXVECTOR3( 0.0f, 0.0f, 0.0f ),
			    &D3DXVECTOR3( 0.0f, 1.0f, 0.0f ) );

	p_iD3DDevice9->SetTransform( D3DTS_VIEW, &matView );


	//Set up the projection so that I get a far clip at 100 and near

	//at 1. 

	D3DXMATRIX matProj;
   	D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, 1.0f, 1.0f, 100.0f );
   	p_iD3DDevice9->SetTransform( D3DTS_PROJECTION, &matProj );
	
}

Bad Monkey Productions

Share this post


Link to post
Share on other sites