Application.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. //
  2. // Copyright (c) 2008-2015 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 "../Engine/Application.h"
  23. #include "../Engine/Engine.h"
  24. #ifdef IOS
  25. #include "../Graphics/Graphics.h"
  26. #include "../Graphics/GraphicsImpl.h"
  27. #endif
  28. #include "../IO/IOEvents.h"
  29. #include "../IO/Log.h"
  30. #include "../Core/ProcessUtils.h"
  31. #include <exception>
  32. #include "../DebugNew.h"
  33. namespace Urho3D
  34. {
  35. #ifdef IOS
  36. // Code for supporting SDL_iPhoneSetAnimationCallback
  37. void RunFrame(void* data)
  38. {
  39. Application* instance = reinterpret_cast<Application*>(data);
  40. instance->GetSubsystem<Engine>()->RunFrame();
  41. }
  42. #endif
  43. #if defined(EMSCRIPTEN)
  44. #include <emscripten.h>
  45. // REVISIT: it seems there should only be one instance of Application
  46. // and it's needed here as there's no way to pass it along with a callback
  47. // for emscripten_set_main_loop() so just store a static pointer here for now
  48. static Application *emInstance = 0;
  49. void RunFrame()
  50. {
  51. if (emInstance)
  52. emInstance->GetSubsystem<Engine>()->RunFrame();
  53. }
  54. #endif
  55. Application::Application(Context* context) :
  56. Object(context),
  57. exitCode_(EXIT_SUCCESS)
  58. {
  59. engineParameters_ = Engine::ParseParameters(GetArguments());
  60. // Create the Engine, but do not initialize it yet. Subsystems except Graphics & Renderer are registered at this point
  61. engine_ = new Engine(context);
  62. // Subscribe to log messages so that can show errors if ErrorExit() is called with empty message
  63. SubscribeToEvent(E_LOGMESSAGE, HANDLER(Application, HandleLogMessage));
  64. }
  65. int Application::Run()
  66. {
  67. #if !defined(EMSCRIPTEN) // TODO: do we really need to do this?
  68. try
  69. {
  70. #endif
  71. Setup();
  72. if (exitCode_)
  73. return exitCode_;
  74. if (!engine_->Initialize(engineParameters_))
  75. {
  76. ErrorExit();
  77. return exitCode_;
  78. }
  79. Start();
  80. if (exitCode_)
  81. return exitCode_;
  82. // Platforms other than iOS and EMSCRIPTEN run a blocking main loop
  83. #if !defined(IOS) && !defined(EMSCRIPTEN)
  84. while (!engine_->IsExiting())
  85. engine_->RunFrame();
  86. Stop();
  87. // iOS will setup a timer for running animation frames so eg. Game Center can run. In this case we do not
  88. // support calling the Stop() function, as the application will never stop manually
  89. #else
  90. #if defined(IOS)
  91. SDL_iPhoneSetAnimationCallback(GetSubsystem<Graphics>()->GetImpl()->GetWindow(), 1, &RunFrame, this);
  92. #else
  93. // EMSCRIPTEN
  94. emInstance = this;
  95. emscripten_set_main_loop(RunFrame, 0, 1);
  96. #endif
  97. #endif
  98. return exitCode_;
  99. #if !defined(EMSCRIPTEN)
  100. }
  101. catch (std::bad_alloc&)
  102. {
  103. ErrorDialog(GetTypeName(), "An out-of-memory error occurred. The application will now exit.");
  104. return EXIT_FAILURE;
  105. }
  106. #endif
  107. }
  108. void Application::ErrorExit(const String& message)
  109. {
  110. engine_->Exit(); // Close the rendering window
  111. exitCode_ = EXIT_FAILURE;
  112. // Only for WIN32, otherwise the error messages would be double posted on Mac OS X and Linux platforms
  113. if (!message.Length())
  114. {
  115. #ifdef WIN32
  116. ErrorDialog(GetTypeName(), startupErrors_.Length() ? startupErrors_ :
  117. "Application has been terminated due to unexpected error.");
  118. #endif
  119. }
  120. else
  121. ErrorDialog(GetTypeName(), message);
  122. }
  123. void Application::HandleLogMessage(StringHash eventType, VariantMap& eventData)
  124. {
  125. using namespace LogMessage;
  126. if (eventData[P_LEVEL].GetInt() == LOG_ERROR)
  127. {
  128. // Strip the timestamp if necessary
  129. String error = eventData[P_MESSAGE].GetString();
  130. unsigned bracketPos = error.Find(']');
  131. if (bracketPos != String::NPOS)
  132. error = error.Substring(bracketPos + 2);
  133. startupErrors_ += error + "\n";
  134. }
  135. }
  136. }