Application.cpp 5.2 KB

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