myd8.h:
Code: Select all
class CD3DManager
{
public:
CD3DManager(IDirect3DDevice8 *pD3Ddev, myIDirect3DDevice8 *pParent)
{
m_pParent = pParent;
m_pD3Ddev = pD3Ddev;
}
~CD3DManager() {}
HRESULT Initialize();
HRESULT PreReset();
HRESULT PostReset();
HRESULT Release();
myIDirect3DDevice8* GetD3DDevice() { return m_pParent; };
private:
myIDirect3DDevice8 *m_pParent;
IDirect3DDevice8 *m_pD3Ddev;
};
interface myIDirect3DDevice8 : public IDirect3DDevice8
{
public:
myIDirect3DDevice8(IDirect3DDevice8 **ppReturnedDeviceInterface, D3DPRESENT_PARAMETERS *pPresentParam, IDirect3D8 *pIDirect3D8)
{
m_pD3Ddev = *ppReturnedDeviceInterface;
*ppReturnedDeviceInterface = this;
m_PresentParam = *pPresentParam;
m_pD3Dint = pIDirect3D8;
m_pManager = new CD3DManager(m_pD3Ddev, this);
m_pManager->Initialize();
m_refCount = 1;
}
IDirect3DDevice8 *m_pD3Ddev;
LPDIRECT3DDEVICE8 m_device;
IDirect3D8 *m_pD3Dint;
UINT m_refCount;
D3DPRESENT_PARAMETERS m_PresentParam;
CD3DManager *m_pManager;
STDMETHOD(QueryInterface)(THIS_ REFIID riid, void** ppvObj);
STDMETHOD_(ULONG,AddRef)(THIS);
STDMETHOD_(ULONG,Release)(THIS);
STDMETHOD(TestCooperativeLevel)(THIS);
STDMETHOD_(UINT, GetAvailableTextureMem)(THIS);
STDMETHOD(ResourceManagerDiscardBytes)(THIS_ DWORD Bytes);
STDMETHOD(GetDirect3D)(THIS_ IDirect3D8** ppD3D8);
STDMETHOD(GetDeviceCaps)(THIS_ D3DCAPS8* pCaps);
STDMETHOD(GetDisplayMode)(THIS_ D3DDISPLAYMODE* pMode);
……………………
……………………
……………………
STDMETHOD(DeletePatch)(THIS_ UINT Handle);
};
myd8.cpp:
Code: Select all
using namespace myUI;
myUI::myCEGUI *g_pCEGUIRender = NULL;
HRESULT CD3DManager::Initialize()
{
g_pCEGUIRender = new myUI(m_pParent);
return S_OK;
}
HRESULT CD3DManager::PreReset()
{
CEGUI::Renderer *pRenderer = CEGUI::System::getSingleton().getRenderer();
CEGUI::DirectX81Renderer* d3d_renderer = static_cast<CEGUI::DirectX81Renderer*>(pRenderer);
if (pRenderer != NULL && d3d_renderer != NULL)
{
d3d_renderer->preD3DReset();
}
return S_OK;
}
HRESULT CD3DManager::PostReset()
{
CEGUI::Renderer *pRenderer = CEGUI::System::getSingleton().getRenderer();
CEGUI::DirectX81Renderer* d3d_renderer = static_cast<CEGUI::DirectX81Renderer*>(pRenderer);
if (pRenderer != NULL && d3d_renderer != NULL)
{
d3d_renderer->postD3DReset();
}
return S_OK;
}
HRESULT CD3DManager::Release()
{
return S_OK;
}
//-----------------------------------------------------------------------------
HRESULT APIENTRY myIDirect3DDevice8::QueryInterface(REFIID riid, void** ppvObj)
{
return m_pD3Ddev->QueryInterface(riid, ppvObj);
}
ULONG APIENTRY myIDirect3DDevice8::AddRef(void)
{
m_refCount++;
return m_pD3Ddev->AddRef();
}
……………………
……………………
HRESULT APIENTRY myIDirect3DDevice8::Reset(D3DPRESENT_PARAMETERS* pPresentationParameters)
{
m_pManager->PreReset();
HRESULT hRet = m_pD3Ddev->Reset(pPresentationParameters);
if( SUCCEEDED(hRet) )
{
m_PresentParam = *pPresentationParameters;
m_pManager->PostReset();
}
return hRet;
}
……………………
……………………
HRESULT APIENTRY myIDirect3DDevice8::BeginScene(void)
{
return m_pD3Ddev->BeginScene();
}
HRESULT APIENTRY myIDirect3DDevice8::EndScene(void)
{
g_pCEGUIRender->mygui_Render();
return m_pD3Ddev->EndScene();
}
mycegui.h :
Code: Select all
#include "CEGUI.h"
#include "renderer.h" // D3D8 CEGUIRenderer
#include <fstream>
using namespace std;
using namespace CEGUI;
namespace myUI
{
class myUI_EXPORT myCEGUI
{
public:
myCEGUI(IDirect3DDevice8 *pd3dDevice);
virtual ~myCEGUI();
public:
virtual void mygui_Render(void);
LRESULT my_WndProc(HWND hWnd, UINT nMessage, WPARAM wParam, LPARAM lParam);
public:
static myCEGUI* my_GetMe(void) { return m_pSMe; };
protected:
Renderer *m_pGUIRenderer;
CEGUI::Window *m_pLayout;
}
These are just some of my project code,My intention is to want to CEGUI embedded inside the game.
The results have been successful.
but,if I change the EndScene() function,like this:
Code: Select all
HRESULT APIENTRY myIDirect3DDevice8::EndScene(void)
{
LPDIRECT3DVERTEXBUFFER8 d_buffer;
UINT stride;
DWORD VERTEX_FVF;
m_pD3Ddev->GetStreamSource(0, &d_buffer, &stride);
m_pD3Ddev->GetVertexShader(&VERTEX_FVF);
// set device states
DWORD zenable, fillmode, alphatestenable, zwriteenable, fogenable, cullmode;
m_pD3Ddev->GetRenderState(D3DRS_ZENABLE, &zenable);
m_pD3Ddev->GetRenderState(D3DRS_FILLMODE, &fillmode);
m_pD3Ddev->GetRenderState(D3DRS_ALPHATESTENABLE, &alphatestenable);
m_pD3Ddev->GetRenderState(D3DRS_ZWRITEENABLE, &zwriteenable);
m_pD3Ddev->GetRenderState(D3DRS_FOGENABLE, &fogenable);
m_pD3Ddev->GetRenderState(D3DRS_CULLMODE, &cullmode);
// setup texture addressing settings
DWORD addressu, addressv;
m_pD3Ddev->GetTextureStageState(0, D3DTSS_ADDRESSU, &addressu);
m_pD3Ddev->GetTextureStageState(0, D3DTSS_ADDRESSV, &addressv);
// setup colour calculations
DWORD colorarg1, colorarg2, colorop;
m_pD3Ddev->GetTextureStageState(0, D3DTSS_COLORARG1, &colorarg1);
m_pD3Ddev->GetTextureStageState(0, D3DTSS_COLORARG2, &colorarg2);
m_pD3Ddev->GetTextureStageState(0, D3DTSS_COLOROP, &colorop);
// setup alpha calculations
DWORD alphaarg1, alphaarg2, alphaop;
m_pD3Ddev->GetTextureStageState(0, D3DTSS_ALPHAARG1, &alphaarg1);
m_pD3Ddev->GetTextureStageState(0, D3DTSS_ALPHAARG2, &alphaarg2);
m_pD3Ddev->GetTextureStageState(0, D3DTSS_ALPHAOP, &alphaop);
// setup filtering
DWORD minfilter, magfilter;
m_pD3Ddev->GetTextureStageState(0, D3DTSS_MINFILTER, &minfilter);
m_pD3Ddev->GetTextureStageState(0, D3DTSS_MAGFILTER, &magfilter);
// disable texture stages we do not need.
DWORD colorop1;
m_pD3Ddev->GetTextureStageState(1, D3DTSS_COLOROP, &colorop1);
// setup scene alpha blending
DWORD alphablendenable, srcblend, destblend;
m_pD3Ddev->GetRenderState(D3DRS_ALPHABLENDENABLE, &alphablendenable);
m_pD3Ddev->GetRenderState(D3DRS_SRCBLEND, &srcblend);
m_pD3Ddev->GetRenderState(D3DRS_DESTBLEND, &destblend);
g_pCEGUIRender->mygui_Render();
m_pD3Ddev->SetStreamSource(0, d_buffer, stride);
m_pD3Ddev->SetVertexShader(VERTEX_FVF);
// set device states
m_pD3Ddev->SetRenderState(D3DRS_ZENABLE, zenable);
m_pD3Ddev->SetRenderState(D3DRS_FILLMODE, fillmode);
m_pD3Ddev->SetRenderState(D3DRS_ALPHATESTENABLE, alphatestenable);
m_pD3Ddev->SetRenderState(D3DRS_ZWRITEENABLE, zwriteenable);
m_pD3Ddev->SetRenderState(D3DRS_FOGENABLE, fogenable);
m_pD3Ddev->SetRenderState(D3DRS_CULLMODE, cullmode);
// setup texture addressing settings
m_pD3Ddev->SetTextureStageState(0, D3DTSS_ADDRESSU, addressu);
m_pD3Ddev->SetTextureStageState(0, D3DTSS_ADDRESSV, addressv);
// setup colour calculations
m_pD3Ddev->SetTextureStageState(0, D3DTSS_COLORARG1, colorarg1);
m_pD3Ddev->SetTextureStageState(0, D3DTSS_COLORARG2, colorarg2);
m_pD3Ddev->SetTextureStageState(0, D3DTSS_COLOROP, colorop);
// setup alpha calculations
m_pD3Ddev->SetTextureStageState(0, D3DTSS_ALPHAARG1, alphaarg1);
m_pD3Ddev->SetTextureStageState(0, D3DTSS_ALPHAARG2, alphaarg2);
m_pD3Ddev->SetTextureStageState(0, D3DTSS_ALPHAOP, alphaop);
// setup filtering
m_pD3Ddev->SetTextureStageState(0, D3DTSS_MINFILTER, minfilter);
m_pD3Ddev->SetTextureStageState(0, D3DTSS_MAGFILTER, magfilter);
// disable texture stages we do not need.
m_pD3Ddev->SetTextureStageState(1, D3DTSS_COLOROP, colorop1);
// setup scene alpha blending
m_pD3Ddev->SetRenderState(D3DRS_ALPHABLENDENABLE, alphablendenable);
m_pD3Ddev->SetRenderState(D3DRS_SRCBLEND, srcblend);
m_pD3Ddev->SetRenderState(D3DRS_DESTBLEND, destblend);
return m_pD3Ddev->EndScene();
}
when I (Alt + Tab) switch the window. the Game.exe is Crash!
I think it should be lostDevice!
I tried to solve ,but still dont OK , I need help!!