//  // // # # ### # # -= Nuclex Project =-  // // ## # # # ## ## NCXDirect3D9.cpp - Nuclex Direct3D9 extension  // // ### # # ###  // // # ### # ### Registers the extension module  // // # ## # # ## ##  // // # # ### # # R8 (C)2002 Markus Ewald -> License.txt  // //  // #include "Direct3D9Plugin/Direct3D9Plugin.h" #include "Direct3D9Plugin/Video/Direct3D9VideoDriver.h" #include "Direct3D9Plugin/Scene/XModelCodec.h" #include "Nuclex/Video/VideoServer.h" #include "Nuclex/Scene/SceneServer.h" #include "Nuclex/Kernel.h" using namespace Nuclex; using namespace Nuclex::Video; using namespace Nuclex::Scene; static IDirect3D9 *g_pDirect3D9 = NULL; //  // //  Direct3D9InitTerm  // //  // /// Direct3D9 global initialization class /** Initializes global DLL data. Currently includes loading D3D8.dll (which is required only once per process) and searching for the function pointer to create the main Direct3D9 interface. */ static class Direct3D9InitTerm { public: /// Constructor /** Initializes global data when the dll enters a process */ Direct3D9InitTerm() : m_hD3D9Dll(NULL) { typedef IDirect3D9 *WINAPI fnDirect3DCreate9(UINT SDKVersion); // Try to load D3D9.dll to get access to the creation method m_hD3D9Dll = ::LoadLibraryA("D3D9"); if(!m_hD3D9Dll) { Kernel::logMessage( Kernel::MT_WARNING, "D3D9.dll could not be loaded. Direct3D9 support disabled." ); return; } { Loki::ScopeGuard Free_hD3D9Dll = Loki::MakeGuard(FreeLibrary, m_hD3D9Dll); // Get the function pointer to the Direct3DCreate9() procedure fnDirect3DCreate9 *pfnDirect3DCreate9 = reinterpret_cast( ::GetProcAddress(m_hD3D9Dll, "Direct3DCreate9") ); if(!pfnDirect3DCreate9) { Kernel::logMessage( Kernel::MT_WARNING, "Direct3DCreate9() procedure not found in D3D9.dll. Direct3D9 support disabled." ); return; } g_pDirect3D9 = pfnDirect3DCreate9(D3D_SDK_VERSION); if(!g_pDirect3D9) { Kernel::logMessage( Kernel::MT_WARNING, "Direct3DCreate9() failed. Direct3D9 support disabled." ); return; } Free_hD3D9Dll.Dismiss(); } Kernel::logMessage( Kernel::MT_INFO, "Direct3D9 available and successfully initialized." ); } /// Destructor /** Destroys global data when the dll leaves a process */ ~Direct3D9InitTerm() { if(m_hD3D9Dll) { if(g_pDirect3D9) { // TODO: Releasing D3D here causes an access violation. Probably the destruction time // of the initterm class is too late to do this, construct a dynamic initializer. //g_pDirect3D9->Release(); g_pDirect3D9 = NULL; } ::FreeLibrary(m_hD3D9Dll); m_hD3D9Dll = NULL; } } private: HMODULE m_hD3D9Dll; ///< The d3d9.dll handle } _Direct3D9InitTerm; // ####################################################################### // // # Nuclex::Video::getDirect3D9() # // // ####################################################################### // DIRECT3D9PLUGIN_API IDirect3D9 *Nuclex::Video::getDirect3D9() { return g_pDirect3D9; } // ############################################################################################# // // # NuclexVersion() # // // ############################################################################################# // /** Retrieve the version number of the nuclex framework the plugin was compiled against @return The framework version against which the plugin was compiled */ extern "C" DIRECT3D9PLUGIN_API int nuclexPluginVersion() { return NUCLEX_VERSION; } // ############################################################################################# // // # NuclexRegister() # // // ############################################################################################# // /** Register the plugin to a nuclex kernel instance @param pKernel Kernel to register to */ extern "C" DIRECT3D9PLUGIN_API void nuclexPluginRegister(Kernel *pKernel) { const shared_ptr &spVideoServer = pKernel->getVideoServer(); IDirect3D9 *pDirect3D9 = getDirect3D9(); if(pDirect3D9) { unsigned long nAdapters = pDirect3D9->GetAdapterCount(); for(unsigned long nAdapter = 0; nAdapter < nAdapters; nAdapter++) { shared_ptr spD3D9VD(new Direct3D9VideoDriver(pKernel, nAdapter)); spVideoServer->addDriver(spD3D9VD->getName(), spD3D9VD); } } pKernel->getSceneServer()->addModelCodec( "X", shared_ptr(new XModelCodec(pKernel->getVideoServer())) ); } // ############################################################################################# // // # NuclexUnregister() # // // ############################################################################################# // /** Unregister the plugin from a nuclex kernel instance @param pKernel Kernel to unregister from */ extern "C" DIRECT3D9PLUGIN_API void nuclexPluginUnregister(Kernel *pKernel) { //pKernel->getSceneServer()->removeModelCodec("X"); }