SafelyLoadingRenderSystems         How to safely load rendersystems without a crash (as in, fail gracefully on systems that lack DirectX or GL)

Having your application crash on loading is pretty sad. It's slightly unprofessional and is tedious to debug with an user. One thing that can fail in a great number of ways is loading the rendersystems, especially the DX variants.

This is the solution I use in ShortHike. I have disable the plugins.cfg file and load the plugins manually instead. I use this snippet of code to check for the DirectX version and to load the rendersystem. The GetDXVersion is included with the DirectX SDK.

This snippet guards against two common situations:

  • The computer is running an older version of DirectX (5, 6, 7 etc). Loading the DX9 plugin would crash your application with a missing DLL call (NB. you really crash. No exceptions or anything. Just plain crash.)
  • You have compiled OGRE on 9.0c and the user has 9.0b. The versions aren't compatible so the we need an inner try/catch


Take care! Kai

void 
 Main::loadRenderSystems() 
 { 
   Root* ogreRoot = Root::getSingletonPtr(); 
 
 #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32 
   HRESULT hr; 
   DWORD dwDirectXVersion = 0; 
   TCHAR strDirectXVersion[10]; 
   
   hr = GetDXVersion( &dwDirectXVersion, strDirectXVersion, 10 ); 
   if( SUCCEEDED(hr) ) { 
     ostringstream dxinfoStream; 
     dxinfoStream << "DirectX version: " << strDirectXVersion; 
     LogManager::getSingleton().logMessage(dxinfoStream.str()); 
     
     if(dwDirectXVersion >= 0x00090000) { 
       try { 
         ogreRoot->loadPlugin("RenderSystem_Direct3D9"); 
       } 
       catch(Exception& e) { 
         LogManager::getSingleton().logMessage(String("Unable to create D3D9 RenderSystem: ") + e.getFullDescription()); 
        } 
     } 
   } 
 #endif
   try {  
     ogreRoot->loadPlugin("RenderSystem_GL");  
   } 
   catch(Exception& e) { 
     LogManager::getSingleton().logMessage(String("Unable to create OpenGL RenderSystem: ") + e.getFullDescription()); 
   } 
 
   try { 
     ogreRoot->loadPlugin("Plugin_CgProgramManager");  
   } 
   catch(Exception& e) { 
     LogManager::getSingleton().logMessage(String("Unable to create CG Program manager RenderSystem: ") + e.getFullDescription()); 
   } 
 }

--Kai Backman 15:26, 4 Apr 2005 (CDT)