This is of course useful when programming tools : editor for levels, special effects, animations, etc...
virtual u32 CreateSwapChain (const HWND hWnd,const u32 u32Flags=_WINDOWED_|_ZBUFFER_, const u32 u32Width=0,const u32 u32Height=0) = 0; virtual void ReleaseSwapChain (const u32 u32Chain) = 0; virtual bool SelectSwapChain (const u32 u32Chain) = 0; virtual bool AddSwapChain (CSwapChain* pSwapChain) = 0; virtual bool RemoveSwapChain (CSwapChain* pSwapChain) = 0; virtual CSwapChain* GetSwapChain (const u32 u32Chain) = 0; virtual CSwapChain* GetCurrentChain (void) const = 0; |
virtual bool AddSwapChain (CSwapChain* pSwapChain); virtual bool RemoveSwapChain (CSwapChain* pSwapChain); virtual CSwapChain* GetSwapChain (const u32 u32Chain); virtual CSwapChain* GetCurrentChain (void) const; |
// typedefs public: typedef std::list< CSwapChain* > listChain; typedef listChain::iterator iterChain; // protected data protected: listChain m_SwapChains; u32 m_u32CurrentChain; CSwapChain* m_pCurrentChain; |
//--------------------------------------------------------------------------------------------------------------------// // SWAP CHAIN CLASS // // (MULTIPLE VIEWS) // // 22/05/02, Mythos // //--------------------------------------------------------------------------------------------------------------------// #ifndef _MYTHOS_SWAPCHAIN_H_ #define _MYTHOS_SWAPCHAIN_H_ #pragma once //----------------------------------------------- INCLUDES -----------------------------------------------------------// #include "Maths/VectMat.h" #include "Global/Defines.h" #include "Memory/SmartPtr.h" //----------------------------------------------- CLASSES ------------------------------------------------------------// //--------------------------------------------------------------------------------------------------------------------// // CSwapChain // //--------------------------------------------------------------------------------------------------------------------// namespace Mythos { class CSwapChain { // public methods public: // constructors & destructor CSwapChain (u32 u32Handle,bool boWindowed,bool boZBuffer,HWND hWnd,u32 u32Width,u32 u32Height); virtual ~CSwapChain (void); // get/set static u32 GetFreeHandle (void); u32 GetHandle (void) const; bool IsWindowed (void) const; bool HasZBuffer (void) const; HWND GetHwnd (void) const; u32 GetWidth (void) const; u32 GetHeight (void) const; CVect3D GetClearColor (void) const; void SetClearColor (const CVect3D& v3Color); // protected data protected: static u32 m_u32NextHandle; u32 m_u32Handle; bool m_boWindowed; bool m_boZBuffer; HWND m_hWnd; u32 m_u32Width,m_u32Height; CVect3D m_v3ClearColor; }; // smart ptr _SMART_POINTER_(CSwapChain) } //----------------------------------------------- INLINES ------------------------------------------------------------// #ifndef _DEBUG #include "SwapChain.inl" #endif //--------------------------------------------------------------------------------------------------------------------// #endif // _MYTHOS_SWAPCHAIN_H_ |
// get/set IDirect3DSwapChain8* GetInterface (void) const; void SetInterface (IDirect3DSwapChain8* pInterface); IDirect3DSurface8* GetBackBuffer (void) const; void SetBackBuffer (IDirect3DSurface8* pBackBuffer); IDirect3DSurface8* GetZStencil (void) const; void SetZStencil (IDirect3DSurface8* pZStencil); // protected data protected: IDirect3DSwapChain8* m_pInterface; IDirect3DSurface8* m_pBackBuffer; IDirect3DSurface8* m_pZStencil; |
//----------------------------------------------- CreateSwapChain ----------------------------------------------------// // create additional swap chain // 21/05/02, Mythos // in : window handle,flags(=_WINDOWED_|_ZBUFFER_),width(=0) & height(=0) // out: chain handle, 0==error // rem: if width==height==0 the client area of the window is used //--------------------------------------------------------------------------------------------------------------------// u32 Mythos::CRendererDX8::CreateSwapChain(const HWND hWnd,const u32 u32Flags,const u32 u32Width,const u32 u32Height) { bool boWindowed =(u32Flags & _WINDOWED_) != 0; bool boZBuffer =(u32Flags & _ZBUFFER_ ) != 0; u32 u32Width2 = u32Width; u32 u32Height2 = u32Height; if(boWindowed) { RECT rect; if(!GetClientRect(hWnd,&rect)) return 0; if(!u32Width) u32Width2 = rect.right; if(!u32Height) u32Height2 = rect.bottom; } if(!u32Width2 || !u32Height2) return 0; // back buffer if(!m_pD3D | !m_pDevice) return 0; D3DDISPLAYMODE d3ddm; if(FAILED(m_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,&d3ddm))) return 0; D3DPRESENT_PARAMETERS d3dpp; ZeroMemory(&d3dpp,sizeof(d3dpp)); d3dpp.BackBufferWidth = u32Width2; d3dpp.BackBufferHeight = u32Height2; d3dpp.BackBufferFormat = d3ddm.Format; d3dpp.BackBufferCount = 1; d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.hDeviceWindow = hWnd; d3dpp.Windowed = boWindowed; d3dpp.EnableAutoDepthStencil = false; d3dpp.Flags = D3DPRESENTFLAG_LOCKABLE_BACKBUFFER; IDirect3DSwapChain8* pInterface; HRESULT hrErr = m_pDevice->CreateAdditionalSwapChain(&d3dpp,&pInterface); if(FAILED(hrErr)) { TCHAR szError[1024]; D3DXGetErrorString(hrErr,szError,1024); _ASSERT_(false,"CreateAdditionalSwapChain failed"); _ASSERT_(false,szError); return 0; } // Z & stencil IDirect3DSurface8* pZStencil = NULL; if(boZBuffer) { hrErr = m_pDevice->CreateDepthStencilSurface(u32Width2,u32Height2,D3DFMT_D16,D3DMULTISAMPLE_NONE,&pZStencil); if(FAILED(hrErr)) { TCHAR szError[1024]; D3DXGetErrorString(hrErr,szError,1024); _ASSERT_(false,"CreateDepthStencilSurface failed"); _ASSERT_(false,szError); pInterface->Release(); return 0; } } // CSwapChainDX8* pSwapChain = new CSwapChainDX8(CSwapChain::GetFreeHandle(),boWindowed,boZBuffer,hWnd,u32Width2,u32Height2); if(!pSwapChain) { pInterface->Release(); if(pZStencil) pZStencil->Release(); return 0; } IDirect3DSurface8* pBackBuffer; pInterface->GetBackBuffer(0,D3DBACKBUFFER_TYPE_MONO,&pBackBuffer); AddSwapChain(pSwapChain); pSwapChain->SetInterface (pInterface); pSwapChain->SetBackBuffer(pBackBuffer); pSwapChain->SetZStencil (pZStencil); return pSwapChain->GetHandle(); } |
//----------------------------------------------- ReleaseSwapChain ---------------------------------------------------// // release additional swap chain // 21/05/02, Mythos // in : chain handle // out: //--------------------------------------------------------------------------------------------------------------------// void Mythos::CRendererDX8::ReleaseSwapChain(const u32 u32Chain) { CSwapChainDX8* pSwapChain = reinterpret_cast<CSwapChainDX8*>(GetSwapChain(u32Chain)); if(!pSwapChain) return; RemoveSwapChain(pSwapChain); delete pSwapChain; } |
//----------------------------------------------- SelectSwapChain ----------------------------------------------------// // select as rendering target // 21/05/02, Mythos // in : chain handle // out: OK? //--------------------------------------------------------------------------------------------------------------------// bool Mythos::CRendererDX8::SelectSwapChain(const u32 u32Chain) { if(!m_pDevice) return false; if((u32Chain == m_u32CurrentChain) && m_pCurrentChain) return true; CSwapChainDX8* pSwapChain = reinterpret_cast<CSwapChainDX8*>(GetSwapChain(u32Chain)); if(!pSwapChain) return false; IDirect3DSurface8* pBackBuffer = pSwapChain->GetBackBuffer(); IDirect3DSurface8* pZStencil = pSwapChain->GetZStencil(); if(!pBackBuffer) return false; if(FAILED(m_pDevice->SetRenderTarget(pBackBuffer,pZStencil))) return false; m_pCurrentChain = pSwapChain; m_u32CurrentChain = u32Chain; return true; } |
// swap chain 0 CSwapChainDX8* pSwapChain = new CSwapChainDX8(0,boWindowed,boZBuffer,hWnd,u32Width2,u32Height2); if(!pSwapChain) return false; AddSwapChain(pSwapChain); IDirect3DSurface8* pBackBuffer; IDirect3DSurface8* pZStencil; m_pDevice->GetBackBuffer(0,D3DBACKBUFFER_TYPE_MONO,&pBackBuffer); m_pDevice->GetDepthStencilSurface(&pZStencil); pSwapChain->SetInterface (NULL); pSwapChain->SetBackBuffer(pBackBuffer); pSwapChain->SetZStencil (pZStencil); SelectSwapChain(0); |
//----------------------------------------------- Swap ---------------------------------------------------------------// // swap front & back buffers // 20/05/02, Mythos // in : source(=NULL) & dest(=NULL) rects,nb vbls(=0) to wait (TBI) // out: OK? //--------------------------------------------------------------------------------------------------------------------// bool Mythos::CRendererDX8::Swap(const RECT* pSrcRect,const RECT* pDestRect,const u32 u32Vbls) { if(!m_pDevice) return false; if(!m_u32CurrentChain) return SUCCEEDED(m_pDevice->Present(pSrcRect,pDestRect,NULL,NULL)); CSwapChainDX8* pSwapChain = reinterpret_cast<CSwapChainDX8*>(m_pCurrentChain); if(!pSwapChain) return false; IDirect3DSwapChain8* pInterface = pSwapChain->GetInterface(); if(!pInterface) return false; return SUCCEEDED(pInterface->Present(pSrcRect,pDestRect,NULL,NULL)); UNREFERENCED_PARAMETER(u32Vbls); } |
void CMultiDxView::DrawFrame() { if(!m_u32Renderer || !theApp.m_pRenderer) return; Mythos::IRenderer* pRenderer = theApp.m_pRenderer; if(!pRenderer->SelectSwapChain(m_u32Renderer)) { _ASSERT_(false,"DrawFrame : SelectSwapChain failed"); return; } // rotate mesh Mythos::CMat4x4 m4Y; m4Y.RotationY(GetDocument()->m_fAngleY); pRenderer->SetModel2World(m4Y); // camera Mythos::CMat4x4 m4W2C; Mythos::CMat4x4 m4Rot = Mythos::CMat4x4::Identity; m4W2C.Translation(Mythos::CVect3D(0.f,0.f,-5.f)); switch(m_u32Renderer) { case 1 : break; case 2 : m4Rot.RotationY(-_PI_/2.f); break; case 3 : m4Rot.RotationX(-_PI_/2.f); break; case 4 : m4Rot.RotationXYZ(Mythos::CVect3D(-_PI_/4.f,_PI_/6.f,_PI_/4.f)); break; } m4W2C *= m4Rot; pRenderer->SetWorld2View(m4W2C); // draw _ASSERT_(pRenderer->Clear(), "DrawFrame : Clear failed"); _ASSERT_(pRenderer->BeginScene(),"DrawFrame : BeginScene failed"); pRenderer->DrawTeapot(); _ASSERT_(pRenderer->EndScene(), "DrawFrame : EndScene failed"); _ASSERT_(pRenderer->Swap(), "DrawFrame : Swap failed"); } // bool CMultiDxView::CreateRenderer() { Mythos::IRenderer* pRenderer = theApp.m_pRenderer; if(!pRenderer) return false; CFrameWnd* pFrame = GetParentFrame(); // CChildFrame if(!pFrame) return false; RECT Rect; pFrame->GetClientRect(&Rect); Rect.right /= 2; Rect.bottom /= 2, m_u32Renderer = pRenderer->CreateSwapChain(m_hWnd,Mythos::IRenderer::_WINDOWED_ | Mythos::IRenderer::_ZBUFFER_,Rect.right,Rect.bottom); if(!m_u32Renderer) return false; if(!pRenderer->SelectSwapChain(m_u32Renderer)) return false; Mythos::CVect3D v3Red (.5f,0.f,0.f); Mythos::CVect3D v3Green (0.f,.5f,0.f); Mythos::CVect3D v3Blue (0.f,0.f,.5f); Mythos::CVect3D v3Yellow(.5f,.5f,0.f); Mythos::CVect3D v3Color; static DWORD dwColor = 0; switch(dwColor) { case 0 : v3Color = v3Red; break; case 1 : v3Color = v3Green; break; case 2 : v3Color = v3Blue; break; case 3 : v3Color = v3Yellow; break; default : v3Color.Set(0.f); break; } dwColor++; dwColor &= 3; pRenderer->SetClearColor(v3Color); return true; } |
RECT rect; GetClientRect(&rect); pRenderer->SetPerspective(_DEG2RAD_(60.f),1.f,1000.f,(float)rect.right/rect.bottom); |