Archived

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

Annoying D3D problem. [SOLVED]

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

Greetings, I was beginning work on a game when I came across this really annoying problem. I have made several small games with D3D before so I was extremely surprised by this problem. It is probably just some obvious retardedness in my code that I am missing but any help would be hugely appreciated. Anyway, here's the problem. I have this [extremely simple] program. All it should do is open a generic window, initialize D3D and clear, beginscene, endscene and present forever. The window initializes fine. D3D initializes fine. The problems occurs in the next section. Sometimes it doesn't clear the screne at all. Sometimes it just stays black [the window background color] and sometimes it renders for a few seconds and then stops. The program doesn't stall or cut out, it just stops rendering. Heres the source
#include <windows.h>
#include <d3d9.h>

#define GAME_NAME "D3DTest2"

LRESULT CALLBACK MessageHandler(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam){
	switch (Msg){
		case WM_DESTROY:
			PostQuitMessage(0);
			break;
		default:
			return DefWindowProc(hWnd,Msg,wParam,lParam);
			break;
	}
	return 0l;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int){
	WNDCLASS wc;
	HWND hWnd = 0;

	/*Register window class*/
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
	wc.hCursor = (HCURSOR)LoadCursor(NULL,IDC_ARROW);
	wc.hIcon = (HICON)LoadIcon(NULL,IDI_APPLICATION);
	wc.hInstance = hInstance;
	wc.lpfnWndProc = MessageHandler;
	wc.lpszClassName = GAME_NAME;
	wc.lpszMenuName = NULL;
	wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;

	if (!RegisterClass(&wc)){
		return 0;
	}

	/*Create actual window*/
	hWnd = CreateWindow(GAME_NAME,GAME_NAME,WS_POPUPWINDOW | WS_CAPTION,	
						100,100,300,400,NULL,NULL,hInstance,NULL);			

	LPDIRECT3D9 D3D = 0;
	LPDIRECT3DDEVICE9 Device = 0;

	D3D = Direct3DCreate9(D3D_SDK_VERSION);
	if (D3D == 0){
		return false;
	}

	D3DPRESENT_PARAMETERS Params;
	ZeroMemory(&Params,sizeof(Params));
	Params.BackBufferCount = 1;
	Params.BackBufferHeight = 400;
	Params.BackBufferWidth = 300;
	Params.BackBufferFormat = D3DFMT_A8R8G8B8;
	Params.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
	Params.Windowed = true;
	Params.hDeviceWindow = hWnd;
	Params.SwapEffect = D3DSWAPEFFECT_DISCARD;

	HRESULT hRet = D3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,
		hWnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING,&Params,&Device);
    
	if (FAILED(hRet)){
		return false;
	}

	ShowWindow(hWnd, SW_SHOWDEFAULT);
	UpdateWindow(hWnd);

	MSG msg;
	msg.message = 0;
	unsigned int Color = 0xFFFFFF00;

	while (1){
		if (PeekMessage(&msg,NULL,0,0,PM_NOREMOVE)){
			while(GetMessage(&msg,NULL,0,0)){
				TranslateMessage(&msg);
				DispatchMessage(&msg);
			}
			if (msg.message == WM_QUIT){
				break;
			}
		}

		if (FAILED(Device->TestCooperativeLevel())){
			return false;
		}
		Device->Clear(0,NULL,D3DCLEAR_TARGET,Color,1.0f,0L);
		Device->BeginScene();
		Device->EndScene();
		Device->Present(NULL,NULL,NULL,NULL);
	}

	if (D3D != 0){
		D3D->Release();
		D3D = 0;
	}

	if (Device != 0){
		Device->Release();
		Device = 0;
	}
	return 0;
}
I tested this out with many different settings. This is the original version. Again any help would be greatly appreciated. Thanks. EDIT: Solved. I had a retarded MessageLoop. [edited by - stro on March 19, 2004 2:06:22 PM]

Share this post


Link to post
Share on other sites