Application.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //
  2. // Copyright (c) 2008-2014 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "Precompiled.h"
  23. #include "Application.h"
  24. #include "Engine.h"
  25. #include "IOEvents.h"
  26. #include "Log.h"
  27. #include "ProcessUtils.h"
  28. #include <exception>
  29. #include "DebugNew.h"
  30. namespace Urho3D
  31. {
  32. Application::Application(Context* context) :
  33. Object(context),
  34. exitCode_(EXIT_SUCCESS)
  35. {
  36. engineParameters_ = Engine::ParseParameters(GetArguments());
  37. // Create the Engine, but do not initialize it yet. Subsystems except Graphics & Renderer are registered at this point
  38. engine_ = new Engine(context);
  39. // Subscribe to log messages so that can show errors if ErrorExit() is called with empty message
  40. SubscribeToEvent(E_LOGMESSAGE, HANDLER(Application, HandleLogMessage));
  41. }
  42. int Application::Run()
  43. {
  44. try
  45. {
  46. Setup();
  47. if (exitCode_)
  48. return exitCode_;
  49. if (!engine_->Initialize(engineParameters_))
  50. {
  51. ErrorExit();
  52. return exitCode_;
  53. }
  54. Start();
  55. if (exitCode_)
  56. return exitCode_;
  57. while (!engine_->IsExiting())
  58. engine_->RunFrame();
  59. Stop();
  60. return exitCode_;
  61. }
  62. catch (std::bad_alloc&)
  63. {
  64. ErrorDialog(GetTypeName(), "An out-of-memory error occurred. The application will now exit.");
  65. return EXIT_FAILURE;
  66. }
  67. }
  68. void Application::ErrorExit(const String& message)
  69. {
  70. engine_->Exit(); // Close the rendering window
  71. exitCode_ = EXIT_FAILURE;
  72. // Only for WIN32, otherwise the error messages would be double posted on Mac OS X and Linux platforms
  73. if (!message.Length())
  74. {
  75. #ifdef WIN32
  76. ErrorDialog(GetTypeName(), startupErrors_.Length() ? startupErrors_ :
  77. "Application has been terminated due to unexpected error.");
  78. #endif
  79. }
  80. else
  81. ErrorDialog(GetTypeName(), message);
  82. }
  83. void Application::HandleLogMessage(StringHash eventType, VariantMap& eventData)
  84. {
  85. using namespace LogMessage;
  86. if (eventData[P_LEVEL].GetInt() == LOG_ERROR)
  87. {
  88. // Strip the timestamp if necessary
  89. String error = eventData[P_MESSAGE].GetString();
  90. unsigned bracketPos = error.Find(']');
  91. if (bracketPos != String::NPOS)
  92. error = error.Substring(bracketPos + 2);
  93. startupErrors_ += error + "\n";
  94. }
  95. }
  96. }