Archived

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

Okay, drawprimitve gone mad

This topic is 5618 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

When I try to run this program the screen only starts to flicker and after a while it hangs and show half my windowdesktop on the screen. I´m to tired to check it myself for the moment , so if you got time you might have a better chance of understanding the problem.
#include
#include

#define D3DFVF_CUSTOMVERTEX (D3DFVF_DIFFUSE|D3DFVF_XYZRHW)

//Globals////////////////////
const char *WINDOW_CLASS_NAME = "Window Class Name";
const char *WINDOW_NAME = "My Window";
const short WINDOW_HEIGHT = 480;
const short WINDOW_WIDTH = 640;

HWND hWnd;
MSG msg;



struct CUSTOMVERTEX
{
    FLOAT x, y, z, rhw; // The transformed position for the vertex.
    DWORD color;        // The vertex color.
};


CUSTOMVERTEX sVertex[] =
{
    { 150.0f,  50.0f, 0.5f, 1.0f, 0xffff0000, }, // x, y, z, rhw, color
    { 250.0f, 250.0f, 0.5f, 1.0f, 0xff00ff00, },
    {  50.0f, 250.0f, 0.5f, 1.0f, 0xff00ffff, },
};



IDirect3D8 *iD3D8 = NULL;
IDirect3DDevice8 *iD3DDevice8 = NULL;
IDirect3DVertexBuffer8 *iD3DVertBuf8 = NULL;

D3DDISPLAYMODE d3ddm;
D3DPRESENT_PARAMETERS d3dpp;




//Functions/////////////////

bool D3DInit(void)
{
	iD3D8 = Direct3DCreate8(D3D_SDK_VERSION);
	if(iD3D8 == NULL)
		return false;

	ZeroMemory(&d3ddm, sizeof(D3DDISPLAYMODE));
	d3ddm.Width = 640;
	d3ddm.Height = 480;
	d3ddm.RefreshRate = 0; //Default
	d3ddm.Format = D3DFMT_R5G6B5;

	//Put some mode check here
	
	ZeroMemory(&d3dpp, sizeof(D3DPRESENT_PARAMETERS));

	d3dpp.Windowed = false;
	d3dpp.SwapEffect = D3DSWAPEFFECT_FLIP;
	d3dpp.BackBufferFormat = d3ddm.Format;
	d3dpp.BackBufferWidth = WINDOW_WIDTH;
	d3dpp.BackBufferHeight = WINDOW_HEIGHT;

	if(FAILED(iD3D8->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,
		hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &iD3DDevice8)))
		return false;

	return true;

}

HRESULT D3DTriangle(void)
{

	if( FAILED( iD3DDevice8->CreateVertexBuffer( 3*sizeof(CUSTOMVERTEX),
                                              0 /* Usage */, D3DFVF_CUSTOMVERTEX,
                                              D3DPOOL_DEFAULT, &iD3DVertBuf8) ) )
    return E_FAIL;

	VOID* pVertices;
	if( FAILED( iD3DVertBuf8->Lock( 0, sizeof(sVertex), (BYTE**)&pVertices, 0 ) ) )
		return E_FAIL;
	
	memcpy( pVertices, sVertex, sizeof(sVertex) );
	iD3DVertBuf8->Unlock();

	return S_OK;
}


void D3DRender(void)
{
	iD3DDevice8->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_RGBA(0,0,255,0), 1.0f, 0);

	iD3DDevice8->BeginScene();

	iD3DDevice8->SetStreamSource(0, iD3DVertBuf8, sizeof(sVertex));
	iD3DDevice8->SetVertexShader(FVF_CUSTOMVERTEX);
	iD3DDevice8->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);

	iD3DDevice8->EndScene();

	iD3DDevice8->Present(NULL, NULL, NULL,NULL);

}

LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch(msg)
	{
	case WM_DESTROY:
		{
			PostQuitMessage(0);
		}break;
	case WM_PAINT:
		{
			D3DRender();
		}break;
	}

	return DefWindowProc(hWnd, msg, wParam, lParam);
}


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	WNDCLASSEX wClass;

	wClass.cbClsExtra = 0;
	wClass.cbSize = sizeof(WNDCLASSEX);
	wClass.cbWndExtra = 0;
	wClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
	wClass.hCursor = LoadCursor(NULL, IDI_APPLICATION);
	wClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
	wClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
	wClass.hInstance = hInstance;
	wClass.lpfnWndProc = WndProc;
	wClass.lpszMenuName = NULL;
	wClass.lpszClassName = WINDOW_CLASS_NAME;
	wClass.style = CS_VREDRAW|CS_HREDRAW;

	RegisterClassEx(&wClass);

	hWnd = CreateWindowEx(NULL,
		WINDOW_CLASS_NAME,WINDOW_NAME,
		WS_OVERLAPPEDWINDOW|WS_VISIBLE,
		100,100, WINDOW_WIDTH,WINDOW_HEIGHT,NULL,NULL,hInstance,NULL);

	if(hWnd == NULL)
		return 0;


	D3DInit();
	D3DRender();


	while(1)
	{
		PeekMessage(&msg, NULL, 0,0,PM_REMOVE);
		if(msg.message == WM_QUIT)
			break;
		if(GetAsyncKeyState(VK_ESCAPE))
			break;

		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	return 0;

}

 

Any help would be appreciated.    

sHaKaZeD

http://www.shakazed.tk [edited by - shakazed on July 26, 2002 6:22:13 AM]
[edited by - shakazed on July 26, 2002 6:30:17 AM]

Share this post


Link to post
Share on other sites
1. Use the debug runtime, see if it''s telling you anything. It probably is!. If the screen is flashing red & green, then it definately is.

2. From a quick look at the code:
SetStreamSource(0, iD3DVertBuf8, sizeof(sVertex));
The last parameter should be the size of a single vertex . sizeof(sVertex) gets you the size of the whole array! That should be sizeof(CUSTOMVERTEX)...

3. There may be other problems - such as nothing ever actually shutting down D3D or destroying the window. Check the "Tutorial" files in the SDK samples - they do almost the same as you''ve done, except they work properly and should be a good example.



--
Simon O''Connor
Creative Asylum Ltd
www.creative-asylum.com

Share this post


Link to post
Share on other sites