Jump to content
  • Advertisement
Sign in to follow this  
Deus Ex Otium

It's not rendering a primitive and I don't know why... [vc++]

This topic is 4751 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

I just want to draw a quad. That's all I want... Here is my winmain code:
HINSTANCE _hInstance;
HWND _hWnd; 

int WINAPI WinMain (HINSTANCE hInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR lpszArgument,
                    int nFunsterStil)
{
	CoInitialize(NULL);
    MSG Msg;
              /* This is the handle for our window */
    WNDCLASSEX wcexWnd = { sizeof(WNDCLASSEX), CS_CLASSDC,
            WindowProc , 0L, 0L, _hInstance, 
            NULL, 
            LoadCursor(NULL, IDC_ARROW),
            /*(HBRUSH)GetStockObject(BLACK_BRUSH)*/NULL,
            NULL,
            "GameClass", NULL };

    /* Register the window class, and if it fails quit the program */
    if (!RegisterClassEx (&wcexWnd))
        return FALSE;

    /* The class is registered, let's create the program*/
    _hWnd = CreateWindow (
           "GameClass",         /* Classname */
           "Chaos Realm",       /* Title Text */
		   WS_OVERLAPPEDWINDOW, /* CHANGE TO WS_POPUPWINDOW LATER! */
           0,                   /* (0,0) is the position */
           0,                   /* where the window ends up on the screen */
           800,                 /* The programs width */
           600,                 /* and height in pixels */
           GetDesktopWindow(),  /* Parent window */
           NULL,                /* No menu */
           _hInstance,          /* Program Instance handler */
           NULL                 /* No Window Creation data */
           );

    // Return on error creating the Window
    if (_hWnd == NULL)
        return FALSE;

	InitializeGraphics(_hWnd);
	BlitD3D(0L, 0L, _hWnd);

    /* Make the window visible on the screen */
	ShowWindow (_hWnd, SW_SHOWNORMAL);
    UpdateWindow (_hWnd);
	
    ZeroMemory(&Msg, sizeof(MSG));
    // Enter the message pump
    while (Msg.message != WM_QUIT){
        // Peek into the queue and see if there's a message waiting
        if(PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE)) {
            // There's a message!  Handle it normally.
            TranslateMessage(&Msg);
            DispatchMessage(&Msg);
        } else {
            // No messages waiting.  Go ahead and do time-crucial
            // stuff here, such as rendering the game's graphics.			

        }
    }
	close(_pDPClient, g_pD3D, g_pd3dDevice, _hInstance, vertexBuffer, _hWnd);
    /* The program return-value is 0 - The value that PostQuitMessage() gave */
    return Msg.wParam;
}
Here is my initialization of Direct3D:
HRESULT InitializeGraphics(HWND hWnd){
	HRESULT ErrorResult;

	if(!(g_pD3D=Direct3DCreate9(D3D_SDK_VERSION)))
		return E_FAIL;
	D3DPRESENT_PARAMETERS d3dpp; 
    ZeroMemory( &d3dpp, sizeof(d3dpp) );
    d3dpp.Windowed = TRUE;
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
	d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;

	ErrorResult = g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
			D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_pd3dDevice );
	if FAILED(ErrorResult)
    {
        MessageBox(hWnd, "Creating Device", "ERROR", MB_OK);
		MessageBox(hWnd, DXGetErrorDescription9(ErrorResult),
				DXGetErrorString9(ErrorResult), MB_OK);
    }

	//Create vertex buffer
	ErrorResult = g_pd3dDevice->CreateVertexBuffer(4*sizeof(TLVERTEX),
			0, D3DFVF_TLVERTEX, D3DPOOL_DEFAULT, &vertexBuffer, NULL);
	if(FAILED(ErrorResult))
	{
	MessageBox(hWnd,"Creating Vertex Buffer ", "ERROR", MB_OK);
	MessageBox(hWnd, DXGetErrorDescription9(ErrorResult),
			DXGetErrorString9(ErrorResult), MB_OK);
	}


	return S_OK;
}
And here is my render function:
void BlitD3D (LONG x, LONG y, HWND hWnd)
{
	RECT rDest;
	rDest.left=x;
	rDest.top=y;
	rDest.bottom=y+600;
	rDest.right=x+800;
	TLVERTEX* pVertices;
	HRESULT ErrorResult;

	//Lock the vertex buffer
	ErrorResult = vertexBuffer->Lock(0, 4*sizeof(TLVERTEX), (void**)&pVertices,
			NULL);
	if(FAILED(ErrorResult)){
        MessageBox(hWnd, "Locking the Vertex Buffer", "ERROR", MB_OK);
		MessageBox(hWnd, DXGetErrorDescription9(ErrorResult),
				DXGetErrorString9(ErrorResult), MB_OK);
    }

	//Setup vertices
	pVertices[0].color = 0xffff0000;
	pVertices[0].x = (float)rDest.left;
	pVertices[0].y = (float)rDest.top;
	pVertices[0].z = 0.0f;
	pVertices[0].rhw = 1.0f;

	pVertices[1].color = 0xff00ff00;
	pVertices[1].x = (float)rDest.right;
	pVertices[1].y = (float)rDest.top;
	pVertices[1].z = 0.0f;
	pVertices[1].rhw = 1.0f;

	pVertices[2].color = 0xffff0000;
	pVertices[2].x = (float)rDest.right;
	pVertices[2].y = (float)rDest.bottom;
	pVertices[2].z = 0.0f;
	pVertices[2].rhw = 1.0f;

	pVertices[3].color = 0xff00ffff;
	pVertices[3].x = (float)rDest.left;
	pVertices[3].y = (float)rDest.bottom;
	pVertices[3].z = 0.0f;
	pVertices[3].rhw = 1.0f;

	//Unlock the vertex buffer
	ErrorResult = vertexBuffer->Unlock();
	if(FAILED(ErrorResult)){
		MessageBox(hWnd, "Unlocking Vetex Buffer", "ERROR", MB_OK);
		MessageBox(hWnd, DXGetErrorDescription9(ErrorResult),
				DXGetErrorString9(ErrorResult), MB_OK);
	}

	ErrorResult = g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET,
			D3DCOLOR_XRGB(0,0,255), 1.0f, 0L);
	if(FAILED(ErrorResult)){
        MessageBox(hWnd, "Clearing backbuffer", "ERROR", MB_OK);
		MessageBox(hWnd, DXGetErrorDescription9(ErrorResult),
				DXGetErrorString9(ErrorResult), MB_OK);
    }
	
	ErrorResult = g_pd3dDevice->BeginScene();
	if(FAILED(ErrorResult)){
        MessageBox(hWnd, "Beginning Scene", "ERROR", MB_OK);
		MessageBox(hWnd, DXGetErrorDescription9(ErrorResult),
				DXGetErrorString9(ErrorResult), MB_OK);
    }

	ErrorResult = g_pd3dDevice->SetStreamSource(0, vertexBuffer, 0, 
			sizeof(TLVERTEX));
	if(FAILED(ErrorResult))
	{
		MessageBox(hWnd,"Setting Stream Source", "ERROR", MB_OK);
		MessageBox(hWnd, DXGetErrorDescription9(ErrorResult),
				DXGetErrorString9(ErrorResult), MB_OK);
	}

	ErrorResult = g_pd3dDevice->SetFVF(D3DFVF_TLVERTEX);
	if(FAILED(ErrorResult))
	{
		MessageBox(hWnd, "Setting FVF", "ERROR", MB_OK);
		MessageBox(hWnd, DXGetErrorDescription9(ErrorResult),
				DXGetErrorString9(ErrorResult), MB_OK);
	}

	//Draw Image
	ErrorResult = g_pd3dDevice->DrawPrimitive (D3DPT_TRIANGLEFAN, 0, 2);
	if(FAILED(ErrorResult)){
        MessageBox(hWnd, "Drawing Primitive", "ERROR", MB_OK);
		MessageBox(hWnd, DXGetErrorDescription9(ErrorResult),
				DXGetErrorString9(ErrorResult), MB_OK);
    }

	ErrorResult = g_pd3dDevice->EndScene();
	if(FAILED(ErrorResult)){
        MessageBox(hWnd, "Ending Scene", "ERROR", MB_OK);
		MessageBox(hWnd, DXGetErrorDescription9(ErrorResult),
				DXGetErrorString9(ErrorResult), MB_OK);
    }

	ErrorResult = g_pd3dDevice->Present(NULL, NULL, NULL, NULL);
	if(FAILED(ErrorResult)){
        MessageBox(hWnd, "Presenting", "ERROR", MB_OK);
		MessageBox(hWnd, DXGetErrorDescription9(ErrorResult),
				DXGetErrorString9(ErrorResult), MB_OK);
    }

	ZeroMemory(&ErrorResult,sizeof(HRESULT));
	
}
What's wrong? [bawling]

Share this post


Link to post
Share on other sites
Advertisement
Okay, so you're not seeing anything...

Are you seeing a blue screen? From the looks of your Clear() code, you should at least be seeing this.

If you're not seeing a blue screen, then you've got some sort of device/target error... at which point - any of your error traps reporting anything? any error messages? Have you run through the DirectX Debug runtimes to get maximum information?

In your WinMain you only ever draw once, so it is possible that its displaying but then getting lost if the window gets thrown a WM_PAINT (or related) message. Try rendering on each loop... you've even got a comment in the right place for this [smile]

Other than those things, I can't really see anything obviously wrong with what you're doing.
hth
Jack

Share this post


Link to post
Share on other sites
Your code
A few things you need to correct in your code.
1) Call the Rendering function inside the mainloop, not before it.
2) Since you aren't updating the content of the VertexBuffer, you should just put the Locking, Copying, Unlocking-procedure inside the Initializing function.
3) Over-errorchecking can be a bad thing; viewing your code is made very hard.

How to debug
If the above things do not fix your problem, you can use the D3D Debug Output to trace the bug. The Forum FAQ can teach you how.

Tip
Seeing the codingstyle you use, I think it's better to advice you to use C# to develope with DirectX instead. That is, if you have access to Visual Studio .NET.
1) Less(er) code.
2) Easier to manage/read, because of #1.
3) Easier to learn, because of #1 & #2.

Share this post


Link to post
Share on other sites
Quote:
Original post by load_bitmap_file
I don't see it in your snippets, so are you using transformed vertices or untransformed vertices? If you're using the latter, have you setup your camera?

It's transformed vertices:

ErrorResult = g_pd3dDevice->SetFVF(D3DFVF_TLVERTEX);

[smile]

Jack

Share this post


Link to post
Share on other sites
From a quick skim of the code posted, a couple of things do stand out:

1) Try putting the call to Blit3D() *after* the ShowWindow(). Otherwise the "Present" call doesn't have anything to "present" to so might not actually do anything.


2) You aren't setting any render states or texture stage states. For example the default state for texture stage 0 is to modulate the current texture with the current diffuse colour - if you haven't set a texture, then the result you'll see is undefined.

Trusting states to be at their default isn't always a good idea anyway - in an ideal world, they would be; in the real world buggy drivers sometimes get things wrong.


There are a few other things that could be causing nothing to be rendered, but I'll wait for you to see if fixing either of the above things helps fix things.

Share this post


Link to post
Share on other sites
Okay, now I see a blue screen. Thanks to S1CA's nuber 1 suggestion. Now, that's alll I see. Is there anything else I should be doing? Thanks for the help!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!