d3d8 swapchains deving me crazy!

Started by
-1 comments, last by Laroche 20 years, 5 months ago
I have multiple views setup in my mfc app. Each of the views has its own "CDirect3D" pointer, which it "news" and then creates the objects, etc. This works fine with just one view. I have a static member called instances that increases every time a view is created, and after 1 it tries to create an aditional swap chain, lika so. sorry for all the code btw, but i cant figure this out and ive got a massive headache from thinking about it.

void CDirect3D::InitWindow(int Width, int Height, HWND hWnd)
{
	ClearScreen = D3DCOLOR_XRGB( 185, 185, 200 );

	if (Instances > 0)
	{
		D3DPRESENT_PARAMETERS d3dpp; 
		ZeroMemory( &d3dpp, sizeof(d3dpp) );
		d3dpp.Windowed = TRUE;
		d3dpp.SwapEffect = D3DSWAPEFFECT_COPY;
		
		// Use the current display mode.

		D3DDISPLAYMODE mode;
		pd3d->GetAdapterDisplayMode(D3DADAPTER_DEFAULT , &mode);
		d3dpp.BackBufferFormat = mode.Format;
		
		// m_hWnd contains child window handle

		d3dpp.hDeviceWindow = hWnd;
		// m_pSwapChain is IDirect3DSwapChain * 

		pd3ddev->CreateAdditionalSwapChain(&d3dpp, &pSwapChain);

		Child = true;
		Instances++;
		return;
	}

	Instances++;
	Child = false;

	// Create the D3D object.

	if(NULL == (pd3d = Direct3DCreate8(D3D_SDK_VERSION)))
	{
		return;
	}

	// Get the current desktop display mode, so we can set up a back

	// buffer of the same format

	D3DDISPLAYMODE d3ddm;
	if(FAILED( pd3d->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm)))
	{
		return;
	}

	// Set up the structure used to create the D3DDevice

	D3DPRESENT_PARAMETERS d3dpp;
	ZeroMemory(&d3dpp, sizeof(d3dpp));
	d3dpp.Windowed = TRUE;
	d3dpp.BackBufferCount = 2;
	d3dpp.EnableAutoDepthStencil = TRUE;
	d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
	d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
	d3dpp.BackBufferWidth = Width;
	d3dpp.BackBufferHeight = Height;
	d3dpp.BackBufferFormat = d3ddm.Format;

	// Create the D3DDevice

	if(FAILED(pd3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
		D3DCREATE_SOFTWARE_VERTEXPROCESSING,
		&d3dpp, &pd3ddev)))
		{	
			return;
		}
}

// RESIZE



   
void CDirect3D::Resize(int NewWidth, int NewHeight, HWND hWnd)
{
	if (Child)
	{
		pSwapChain->Release();

		D3DPRESENT_PARAMETERS d3dpp; 
		ZeroMemory( &d3dpp, sizeof(d3dpp) );
		d3dpp.Windowed = TRUE;
		d3dpp.SwapEffect = D3DSWAPEFFECT_COPY;
		
		// Use the current display mode.

		D3DDISPLAYMODE mode;
		pd3d->GetAdapterDisplayMode(D3DADAPTER_DEFAULT , &mode);
		d3dpp.BackBufferWidth = NewWidth;
		d3dpp.BackBufferHeight = NewHeight;
		d3dpp.BackBufferFormat = mode.Format;
		
		// m_hWnd contains child window handle

		d3dpp.hDeviceWindow = hWnd;
		// m_pSwapChain is IDirect3DSwapChain * 

		pd3ddev->CreateAdditionalSwapChain(&d3dpp, &pSwapChain);

		ScreenX = NewWidth;
		ScreenY = NewHeight;
		return;
	}

	D3DDISPLAYMODE d3ddm;
	if(FAILED( pd3d->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm)))
	{
		return;
	}

	D3DPRESENT_PARAMETERS d3dpp;
	ZeroMemory(&d3dpp, sizeof(d3dpp));
	d3dpp.Windowed = TRUE;
	d3dpp.BackBufferCount = 1;
	d3dpp.EnableAutoDepthStencil = FALSE;
	d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
	d3dpp.BackBufferWidth = NewWidth;
	d3dpp.BackBufferHeight = NewHeight;
	d3dpp.BackBufferFormat = d3ddm.Format;

	pd3ddev->Reset(&d3dpp);

	if(FAILED(pd3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
	D3DCREATE_SOFTWARE_VERTEXPROCESSING,
	&d3dpp, &pd3ddev)))
	{
			return;
	}

	ScreenX = NewWidth;
	ScreenY = NewHeight;
	return;
}

// ON DRAW - RENDER METHOD


void CBoneView::OnDraw(CDC* pDC)
{
	CHackanimationDoc* pDoc = GetDocument();

	if (pd3d->IsChild())
	{
		LPDIRECT3DSURFACE8 pBack=NULL,pStencil=NULL;
		pd3d->GetSwapChain()->GetBackBuffer(0,D3DBACKBUFFER_TYPE_MONO,&pBack);
		pd3d->GetDevice()->GetDepthStencilSurface(&pStencil);
		pd3d->GetDevice()->SetRenderTarget(pBack,pStencil);
		pBack->Release();
		pStencil->Release();
	}

	pd3d->GetDevice()->BeginScene();
	pd3d->Clear();
	pRender->GetVB()->Begin();

	ManagerPtr BM = pDoc->RequestManager("BONE");

	for (int i = 0; i < BM->GetElements(); i++)
	{
		CBone * pBone = static_cast<CBone*>(BM->RequestAsset(i));
		pRender->PushObject(pBone->GetObject());
	}

	pRender->Render();

	pRender->GetVB()->End();
	pd3d->GetDevice()->EndScene();

	if (pd3d->IsChild())
	{
		pd3d->GetSwapChain()->Present(NULL,NULL,NULL,NULL);
		return;
	}

	pd3d->PresentScene();
	// TODO: add draw code here

}

Most of the code is taken from http://www.mvps.org/directx/articles/rendering_to_multiple_windows.htm. Now the first view almost clears the screen correctly, but seems to have quite a few artifacts. No objects get drawn. Bear in mind im doing 2d stuff and dont use the depth buffer nor the stencil buffer normally. The second view is hopeless, and does nothing . If ANYONE knows ANYTHING and could help, id be VERY grateful!! [edited by - laroche on November 11, 2003 12:37:55 AM]
Check out my music at: http://zed.cbc.ca/go.ZeD?user_id=41947&user=Laroche&page=content

This topic is closed to new replies.

Advertisement