Application.cpp 4.9 KB

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