Explorar o código

try not to crash if no data - for now it just happens that font is the first thing that fails, so we throw an exception and use that to bail out of main

Paul %!s(int64=11) %!d(string=hai) anos
pai
achega
b56690bd25
Modificáronse 2 ficheiros con 74 adicións e 66 borrados
  1. 67 60
      QGearsMain/src/Main.cpp
  2. 7 6
      QGearsMain/src/core/DebugDraw.cpp

+ 67 - 60
QGearsMain/src/Main.cpp

@@ -36,104 +36,111 @@
 int
 main(int argc, char *argv[])
 {
-    QGears::Application app( argc, argv );
-    if( !app.initOgre() ) return 0;
+    try
+    {
+        QGears::Application app(argc, argv);
+        if (!app.initOgre()) return 0;
 
-    Ogre::Root         *root( app.getRoot() );
-    Ogre::RenderWindow *window( app.getRenderWindow() );
-    Ogre::SceneManager *scene_manager( NULL );
+        Ogre::Root         *root(app.getRoot());
+        Ogre::RenderWindow *window(app.getRenderWindow());
+        Ogre::SceneManager *scene_manager(NULL);
 
-    auto timer = std::make_unique<Timer>();
-    auto particle_system_manager = std::make_unique<ParticleSystemManager>();
+        auto timer = std::make_unique<Timer>();
+        auto particle_system_manager = std::make_unique<ParticleSystemManager>();
 
-    // set scene camera and viewport for other moduls
-    // create this before initialize particle because some of them use scene to create themself
-    scene_manager = root->createSceneManager( Ogre::ST_GENERIC, "Scene" );
-    scene_manager->setAmbientLight( Ogre::ColourValue( 1, 1, 1 ) );
-    scene_manager->setAmbientLight( Ogre::ColourValue( 0.5, 0.5, 0.5 ) );
+        // set scene camera and viewport for other moduls
+        // create this before initialize particle because some of them use scene to create themself
+        scene_manager = root->createSceneManager(Ogre::ST_GENERIC, "Scene");
+        scene_manager->setAmbientLight(Ogre::ColourValue(1, 1, 1));
+        scene_manager->setAmbientLight(Ogre::ColourValue(0.5, 0.5, 0.5));
 
-    Ogre::Light *directionalLight( scene_manager->createLight("directionalLight") );
-    directionalLight->setType( Ogre::Light::LT_DIRECTIONAL );
-    directionalLight->setDiffuseColour( Ogre::ColourValue( 0.5, 0.5, 0.5 ) );
-    directionalLight->setSpecularColour( Ogre::ColourValue( 0.0, 0.0, 0.0 ) );
-    directionalLight->setDirection( Ogre::Vector3( 0, 1, 0 ) );
+        Ogre::Light *directionalLight(scene_manager->createLight("directionalLight"));
+        directionalLight->setType(Ogre::Light::LT_DIRECTIONAL);
+        directionalLight->setDiffuseColour(Ogre::ColourValue(0.5, 0.5, 0.5));
+        directionalLight->setSpecularColour(Ogre::ColourValue(0.0, 0.0, 0.0));
+        directionalLight->setDirection(Ogre::Vector3(0, 1, 0));
 
-   // auto fontManager = std::make_unique<Ogre::FontManager>();
+        // auto fontManager = std::make_unique<Ogre::FontManager>();
 
-    QGears::MapFileManager* worldManager = new QGears::MapFileManager();
+        QGears::MapFileManager* worldManager = new QGears::MapFileManager();
 
-    Ogre::Root::getSingleton().addResourceLocation(".", "FileSystem");
-    Ogre::Root::getSingleton().addResourceLocation("./data/wm", "FileSystem", "TEST");
-    Ogre::Root::getSingleton().addResourceLocation("./data/wm/world_us.lgp", QGears::LGPArchiveFactory::ARCHIVE_TYPE, "TEST");
+        Ogre::Root::getSingleton().addResourceLocation(".", "FileSystem");
+        Ogre::Root::getSingleton().addResourceLocation("./data/wm", "FileSystem", "TEST");
+        Ogre::Root::getSingleton().addResourceLocation("./data/wm/world_us.lgp", QGears::LGPArchiveFactory::ARCHIVE_TYPE, "TEST");
 
 
-    Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
+        Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
 
-    // init it before console because it may use it
-    auto config_var_manager = std::make_unique<ConfigVarManager>();
-    auto config_cmd_manager = std::make_unique<ConfigCmdManager>();
+        // init it before console because it may use it
+        auto config_var_manager = std::make_unique<ConfigVarManager>();
+        auto config_cmd_manager = std::make_unique<ConfigCmdManager>();
 
-    auto debug_draw = std::make_unique<DebugDraw>();
+        auto debug_draw = std::make_unique<DebugDraw>();
 
 
-    // init before GameFrameListener, but after ConfigCmdManager
-    auto input_manager = std::make_unique<InputManager>();
+        // init before GameFrameListener, but after ConfigCmdManager
+        auto input_manager = std::make_unique<InputManager>();
 
 
 #ifdef QGears_SOUND
-    //auto audio_manager = std::make_unique<AudioManager>();
-    //audio_manager->MusicPlay( "loop1" );
+        //auto audio_manager = std::make_unique<AudioManager>();
+        //audio_manager->MusicPlay( "loop1" );
 #endif
 
-    // create This earlier than DisplayFrameListener cause it can fire event there
-    auto camera_manager = std::make_unique<CameraManager>();
+        // create This earlier than DisplayFrameListener cause it can fire event there
+        auto camera_manager = std::make_unique<CameraManager>();
 
-    auto console = std::make_unique<Console>();
+        auto console = std::make_unique<Console>();
 
 
-    auto entity_manager = std::make_unique<EntityManager>();
-    auto ui_manager = std::make_unique<UiManager>();
+        auto entity_manager = std::make_unique<EntityManager>();
+        auto ui_manager = std::make_unique<UiManager>();
 
-    auto worldMapModule = std::make_unique<QGears::WorldMapModule>();
+        auto worldMapModule = std::make_unique<QGears::WorldMapModule>();
 
-    ui_manager->SetLanguage( "English" );
+        ui_manager->SetLanguage("English");
 
-    // init after game managers because it attach them to script
-    auto script_manager = std::make_unique<ScriptManager>();
+        // init after game managers because it attach them to script
+        auto script_manager = std::make_unique<ScriptManager>();
 
-    // set base listner for usual game moduls
-    auto frame_listener = std::make_unique<GameFrameListener>( window );
-    root->addFrameListener( frame_listener.get() );
+        // set base listner for usual game moduls
+        auto frame_listener = std::make_unique<GameFrameListener>(window);
+        root->addFrameListener(frame_listener.get());
 
 
 
 
-    // execute config
-    {
-        ConfigFile config;
-        config.Execute( "./data/config.cfg" );
-    }
+        // execute config
+        {
+            ConfigFile config;
+            config.Execute("./data/config.cfg");
+        }
 
-    // init ui and run it scripts
-    ui_manager->Initialise();
+        // init ui and run it scripts
+        ui_manager->Initialise();
 
 
-    // run application cycle
-    QGears::g_ApplicationState = QGears::G_GAME;
-    root->startRendering();
+        // run application cycle
+        QGears::g_ApplicationState = QGears::G_GAME;
+        root->startRendering();
 
 
 
-    // system moduls
-    // we must remove this first cause this can fire event to console
-    root->removeFrameListener( frame_listener.get() );
+        // system moduls
+        // we must remove this first cause this can fire event to console
+        root->removeFrameListener(frame_listener.get());
 
 
-    // Must destruct before the script manager
-    entity_manager.reset();
-    ui_manager.reset();
+        // Must destruct before the script manager
+        entity_manager.reset();
+        ui_manager.reset();
 
-    script_manager.reset();
+        script_manager.reset();
+    }
+    catch (const std::runtime_error& ex)
+    {
+        std::cout << "std::runtime_error thrown: " << ex.what() << std::endl;
+    }
 
     return 0;
 }

+ 7 - 6
QGearsMain/src/core/DebugDraw.cpp

@@ -56,6 +56,7 @@ DebugDraw::DebugDraw():
     if( m_Font.isNull() )
     {
         LOG_ERROR( "Could not find font \"CourierNew\" for debug draw." );
+        throw std::runtime_error("Font load failure");
     }
 
     m_Font->load();
@@ -139,8 +140,8 @@ DebugDraw::Line( const float x1, const float y1, const float x2, const float y2
     }
 
     Ogre::Viewport *viewport( CameraManager::getSingleton().getViewport() );
-    float width = viewport->getActualWidth();
-    float height = viewport->getActualHeight();
+    float width = static_cast<float>(viewport->getActualWidth());
+    float height = static_cast<float>(viewport->getActualHeight());
 
     float new_x1 = ( m_ScreenSpace == true ) ? ( ( int ) x1 / width ) * 2 - 1 : x1;
     float new_y1 = ( m_ScreenSpace == true ) ? -( ( ( int ) y1 / height ) * 2 - 1 ) : y1;
@@ -261,8 +262,8 @@ DebugDraw::Quad( const float x1, const float y1, const float x2, const float y2,
     }
 
     Ogre::Viewport *viewport( CameraManager::getSingleton().getViewport() );
-    float width = viewport->getActualWidth();
-    float height = viewport->getActualHeight();
+    float width = static_cast<float>(viewport->getActualWidth());
+    float height = static_cast<float>(viewport->getActualHeight());
 
     float new_x1 = ( m_ScreenSpace == true ) ? ( ( int ) x1 / width ) * 2 - 1 : x1;
     float new_y1 = ( m_ScreenSpace == true ) ? -( ( ( int ) y1 / height ) * 2 - 1 ) : y1;
@@ -344,8 +345,8 @@ DebugDraw::Text( const float x, const float y, const Ogre::String& text )
     writeIterator += m_TextRenderOp.vertexData->vertexCount * 9;
 
     Ogre::Viewport *viewport( CameraManager::getSingleton().getViewport() );
-    float width = viewport->getActualWidth();
-    float height = viewport->getActualHeight();
+    float width = static_cast<float>(viewport->getActualWidth());
+    float height = static_cast<float>(viewport->getActualHeight());
 
     float length = 0;
     if( m_TextAlignment != LEFT )