Problem to create a window and redirect the rendering to it with win32 api

Started by
5 comments, last by rafaelsantana 4 years, 5 months ago

Hi !

I use win32 to display a window with directx rendering inside it, but the window never appears.

I saw that the rendering is done offline and is not redirected to a window.

int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance,
				   LPSTR lpCmdLine, int nCmdShow)
{
	
	HWND hwnd;
	MSG msg;
	WNDCLASS wc;
	//CMFCMenuResolutionDlg resoltionChoice;
	/*LPPOINT pixelCoordinates;*/
	/*PhysX physX;*/

	RECT rc;
	GetWindowRect(GetDesktopWindow(), &rc);

	HANDLE processHandle = GetCurrentProcess();

	BOOL returnedValue = SetPriorityClass(processHandle, THREAD_PRIORITY_TIME_CRITICAL);

	DirectInput::getInstance()->createDirectInputInstance(hinstance);


	D3D11Rendering::width = 3840.0f; //(float)rc.right;
	D3D11Rendering::height = 2160.0f; // (float)rc.bottom;

	wc.style = 0;
	wc.lpfnWndProc = MainWndProc;
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hInstance = hinstance;
	wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground = (HBRUSH)(1 + COLOR_BTNFACE);
	wc.lpszMenuName =  NULL;
	wc.lpszClassName = (LPCSTR)"WinClass";

	if(!RegisterClass(&wc)) 
		return FALSE;

	hwnd = CreateWindow((LPCSTR)"WinClass", (LPCSTR)"DirectX engine", WS_POPUP,
		/*CW_USEDEFAULT*/0, 0, rc.right, rc.bottom,
		NULL, NULL, hinstance, NULL);
	if (!hwnd) 
		return FALSE;

	//LRESULT lresult = SendMessage(hwnd, WM_LBUTTONDOWN, 0, 0);
	ShowCursor(false);
	ShowWindow(hwnd, nCmdShow);
	UpdateWindow(hwnd);

	while (GetMessage(&msg, NULL, 0, 0))
	{
		//GetCursorPos(&D3D9Rendering::pixelCoordinates);
// 		D3D9Rendering::lookAt->x = pixelCoordinates->x ;
// 		D3D9Rendering::lookAt->y = pixelCoordinates->y ;
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	return 0;// msg.wParam;
}
/******************************************************************************/


LRESULT CALLBACK MainWndProc(HWND hwnd, UINT uMsg, WPARAM keyPressed, LPARAM lParam)
{

	switch (uMsg)
	{
	case WM_CREATE:
	{	
		//_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
		D3D11Rendering::hwnd = hwnd ;
		SetFocus(hwnd);
		SetActiveWindow(hwnd);
		DirectInput::getInstance()->setUpController(hwnd);
		DirectInput::getInstance()->gamePadStates();
		//Inputs::getInstance();
		D3D11Rendering::getInstance();
		//Gameplay::d3D11Rendering = new D3D11Rendering();//tester avec un unique_ptr
		Sounds::getInstance();
		Sounds::getInstance()->randomMusic();
		
	
		return 0;
	}

And from DirectX :

	ZeroMemory( &swapChainDesc, sizeof( swapChainDesc ) );
	swapChainDesc.BufferCount = 1;
	swapChainDesc.BufferDesc.Width = (int) D3D11Rendering::width ;
	swapChainDesc.BufferDesc.Height = (int) D3D11Rendering::height ;
	swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
	swapChainDesc.BufferDesc.RefreshRate.Numerator = 0;
	swapChainDesc.BufferDesc.RefreshRate.Denominator = 1;
	swapChainDesc.BufferUsage =  DXGI_USAGE_RENDER_TARGET_OUTPUT; // DXGI_USAGE_BACK_BUFFER;
	swapChainDesc.OutputWindow = D3D11Rendering::hwnd;
	swapChainDesc.SampleDesc.Count = 1;
	swapChainDesc.SampleDesc.Quality = 0;
	swapChainDesc.Windowed = TRUE;
	swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD ;
	swapChainDesc.Flags = 0 ;
	D3D11Rendering::aspectRatio = D3D11Rendering::width / D3D11Rendering::height ;

Can you help me ?

Advertisement

Present() function has a window handle parameter, that defines where you will observe color render buffer output.

https://docs.microsoft.com/en-us/windows/win32/api/dxgi/nf-dxgi-idxgiswapchain-present

Are you sure ? Or there is another Present() ?

I have found the solution, I added it :

swapChain->SetFullscreenState(TRUE, NULL);

In fact, it works if I run my program from visual studio, but not if I click on the executable directly.

Would you have a better solution than mine ?

It's always a good idea to understand the solution so that you know what's going on. Cheers!

This topic is closed to new replies.

Advertisement