void CGameRenderer::Release(){ //release D3D textures and stuff, then D3D itself if(m_pBackgroundTexture)m_pBackgroundTexture->Release(); if(m_pFloorTexture)m_pFloorTexture->Release(); if(m_pBackgroundVB)m_pBackgroundVB->Release(); //release background vertex buffer g_cSpriteManager.Release(); ReleaseD3D(); //release Direct3D } //Release
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int) { WNDCLASSEX wc={sizeof(WNDCLASSEX), CS_CLASSDC | CS_DBLCLKS, MsgProc, 0L, 0L, GetModuleHandle(NULL), LoadIcon(hInstance, (LPCSTR)IDI_3DFX), LoadCursor(NULL, IDC_ARROW), (HBRUSH)GetStockObject(BLACK_BRUSH), NULL, "My D3D Application", NULL}; RegisterClassEx(&wc); hWnd=CreateWindow("My D3D Application", "My Effect", WS_OVERLAPPEDWINDOW, WindowX, WindowY, WindowWidth, WindowHeight, GetDesktopWindow(), NULL, wc.hInstance, NULL); //SetWindowLong( hWnd, GWL_STYLE, WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_THICKFRAME|WS_MINIMIZEBOX); char pInfo[500]; UINT InfoID = 0; if(SUCCEEDED(InitD3D(hInstance))) { InfoID = InitMyScene(); ShowWindow(hWnd,SW_SHOWDEFAULT); UpdateWindow(hWnd); MSG msg; while(GetMessage(&msg,NULL,0,0)) { TranslateMessage(&msg); DispatchMessage(&msg); #ifndef USE_DEBUG //非DEBUG的时候有错误不能直接弹出对话框,要根据函数返回值退出后再提示,另外要用RELEASE版编译后运行才不会在退出显示MESSAGEBOX时出错 if(InfoID != 0) { SendMessage(hWnd, WM_CLOSE, 0, 0); continue; } #endif if(msg.message==WM_CLOSE) break; ControlProc(); //显示出错,可能是重置设备或切换分辨率的问题 InfoID = DisplayMyScene(); #ifndef USE_DEBUG //非DEBUG的时候有错误不能直接弹出对话框,要根据函数返回值退出后再提示,另外要用RELEASE版编译后运行才不会在退出显示MESSAGEBOX时出错 if(InfoID != 0) { SendMessage(hWnd, WM_CLOSE, 0, 0); continue; } #endif } } ReleaseD3D(); UnregisterClass("My D3D Application", wc.hInstance); LoadString(hInstance, InfoID, pInfo, 500); // 如果直接返回E_FAIL,就不要提示了(表示该错误类型内部字串中是没有的),直接退出即可 if(InfoID != 0 && InfoID != E_FAIL) MessageBox(NULL, pInfo, "Fatal Error", MB_OK); return(0); }
// Creates a Direct3D9 object and initializes adapters. // If succeeded, returns S_OK, otherwise returns the error code. HRESULT D3DPipelineManager::InitD3D(void) { typedef IDirect3D9 * WINAPI FnDirect3DCreate9(UINT SDKVersion); hLibD3D9 = ::LoadLibrary(TEXT("d3d9.dll")); if (hLibD3D9 == NULL) { J2dRlsTraceLn(J2D_TRACE_ERROR, "InitD3D: no d3d9.dll"); return E_FAIL; } FnDirect3DCreate9 *d3dcreate9 = NULL; d3dcreate9 = (FnDirect3DCreate9*) ::GetProcAddress(hLibD3D9, "Direct3DCreate9"); if (d3dcreate9 == NULL) { J2dRlsTraceLn(J2D_TRACE_ERROR, "InitD3D: no Direct3DCreate9"); ::FreeLibrary(hLibD3D9); return E_FAIL; } pd3d9 = d3dcreate9(D3D_SDK_VERSION); if (pd3d9 == NULL) { J2dRlsTraceLn(J2D_TRACE_ERROR, "InitD3D: unable to create IDirect3D9 object"); ::FreeLibrary(hLibD3D9); return E_FAIL; } HRESULT res; if (FAILED(res = InitAdapters())) { J2dRlsTraceLn(J2D_TRACE_ERROR, "InitD3D: failed to init adapters"); ReleaseD3D(); return res; } return S_OK; }
D3DPipelineManager::~D3DPipelineManager(void) { J2dTraceLn(J2D_TRACE_INFO, "D3DPPLM::~D3DPipelineManager()"); ReleaseD3D(); }