Application.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //
  2. // Copyright (c) 2008-2017 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 "../Engine/Application.h"
  24. #include "../IO/IOEvents.h"
  25. #include "../IO/Log.h"
  26. #if defined(IOS) || defined(TVOS)
  27. #include "../Graphics/Graphics.h"
  28. #include <SDL/SDL.h>
  29. #endif
  30. #include "../DebugNew.h"
  31. namespace Urho3D
  32. {
  33. #if defined(IOS) || defined(TVOS) || defined(__EMSCRIPTEN__)
  34. // Code for supporting SDL_iPhoneSetAnimationCallback() and emscripten_set_main_loop_arg()
  35. #if defined(__EMSCRIPTEN__)
  36. #include <emscripten/emscripten.h>
  37. #endif
  38. void RunFrame(void* data)
  39. {
  40. static_cast<Engine*>(data)->RunFrame();
  41. }
  42. #endif
  43. Application::Application(Context* context) :
  44. Object(context),
  45. exitCode_(EXIT_SUCCESS)
  46. {
  47. engineParameters_ = Engine::ParseParameters(GetArguments());
  48. // Create the Engine, but do not initialize it yet. Subsystems except Graphics & Renderer are registered at this point
  49. engine_ = new Engine(context);
  50. // Subscribe to log messages so that can show errors if ErrorExit() is called with empty message
  51. SubscribeToEvent(E_LOGMESSAGE, URHO3D_HANDLER(Application, HandleLogMessage));
  52. }
  53. int Application::Run()
  54. {
  55. #if !defined(__GNUC__) || __EXCEPTIONS
  56. try
  57. {
  58. #endif
  59. Setup();
  60. if (exitCode_)
  61. return exitCode_;
  62. if (!engine_->Initialize(engineParameters_))
  63. {
  64. ErrorExit();
  65. return exitCode_;
  66. }
  67. Start();
  68. if (exitCode_)
  69. return exitCode_;
  70. // Platforms other than iOS/tvOS and Emscripten run a blocking main loop
  71. #if !defined(IOS) && !defined(TVOS) && !defined(__EMSCRIPTEN__)
  72. while (!engine_->IsExiting())
  73. engine_->RunFrame();
  74. Stop();
  75. // iOS/tvOS will setup a timer for running animation frames so eg. Game Center can run. In this case we do not
  76. // support calling the Stop() function, as the application will never stop manually
  77. #else
  78. #if defined(IOS) || defined(TVOS)
  79. SDL_iPhoneSetAnimationCallback(GetSubsystem<Graphics>()->GetWindow(), 1, &RunFrame, engine_);
  80. #elif defined(__EMSCRIPTEN__)
  81. emscripten_set_main_loop_arg(RunFrame, engine_, 0, 1);
  82. #endif
  83. #endif
  84. return exitCode_;
  85. #if !defined(__GNUC__) || __EXCEPTIONS
  86. }
  87. catch (std::bad_alloc&)
  88. {
  89. ErrorDialog(GetTypeName(), "An out-of-memory error occurred. The application will now exit.");
  90. return EXIT_FAILURE;
  91. }
  92. #endif
  93. }
  94. void Application::ErrorExit(const String& message)
  95. {
  96. engine_->Exit(); // Close the rendering window
  97. exitCode_ = EXIT_FAILURE;
  98. if (!message.Length())
  99. {
  100. ErrorDialog(GetTypeName(), startupErrors_.Length() ? startupErrors_ :
  101. "Application has been terminated due to unexpected error.");
  102. }
  103. else
  104. ErrorDialog(GetTypeName(), message);
  105. }
  106. void Application::HandleLogMessage(StringHash eventType, VariantMap& eventData)
  107. {
  108. using namespace LogMessage;
  109. if (eventData[P_LEVEL].GetInt() == LOG_ERROR)
  110. {
  111. // Strip the timestamp if necessary
  112. String error = eventData[P_MESSAGE].GetString();
  113. unsigned bracketPos = error.Find(']');
  114. if (bracketPos != String::NPOS)
  115. error = error.Substring(bracketPos + 2);
  116. startupErrors_ += error + "\n";
  117. }
  118. }
  119. }