Application.cpp 5.2 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 "Precompiled.h"
  23. #include "../Engine/Application.h"
  24. #include "../Engine/Engine.h"
  25. #ifdef IOS
  26. #include "../Graphics/Graphics.h"
  27. #include "../Graphics/GraphicsImpl.h"
  28. #endif
  29. #include "../IO/IOEvents.h"
  30. #include "../IO/Log.h"
  31. #include "../Core/ProcessUtils.h"
  32. #include <exception>
  33. #include "../DebugNew.h"
  34. namespace Atomic
  35. {
  36. #if defined(IOS) || defined(EMSCRIPTEN)
  37. // Code for supporting SDL_iPhoneSetAnimationCallback() and emscripten_set_main_loop_arg()
  38. #if defined(EMSCRIPTEN)
  39. #include <emscripten.h>
  40. #endif
  41. // SDL2 needs a main loop to be running during initialization
  42. // otherwise EGL will error, so this is the main loop used during init
  43. // and is canceled once everything is initialized
  44. void InitializationMainLoop()
  45. {
  46. }
  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. // Create the Engine, but do not initialize it yet. Subsystems except Graphics & Renderer are registered at this point
  58. engine_ = new Engine(context);
  59. // Subscribe to log messages so that can show errors if ErrorExit() is called with empty message
  60. SubscribeToEvent(E_LOGMESSAGE, HANDLER(Application, HandleLogMessage));
  61. }
  62. int Application::Run()
  63. {
  64. // Emscripten-specific: C++ exceptions are turned off by default in -O1 (and above), unless '-s DISABLE_EXCEPTION_CATCHING=0' flag is set
  65. // Urho3D build configuration uses -O3 (Release), -O2 (RelWithDebInfo), and -O0 (Debug)
  66. // Thus, the try-catch block below should be optimised out except in Debug build configuration
  67. try
  68. {
  69. #if defined(EMSCRIPTEN)
  70. emscripten_set_main_loop(InitializationMainLoop, 0, 0);
  71. #endif
  72. Setup();
  73. if (exitCode_)
  74. return exitCode_;
  75. if (!engine_->Initialize(engineParameters_))
  76. {
  77. ErrorExit();
  78. return exitCode_;
  79. }
  80. Start();
  81. if (exitCode_)
  82. return exitCode_;
  83. // Platforms other than iOS and EMSCRIPTEN run a blocking main loop
  84. #if !defined(IOS) && !defined(EMSCRIPTEN)
  85. while (!engine_->IsExiting())
  86. engine_->RunFrame();
  87. Stop();
  88. // iOS will setup a timer for running animation frames so eg. Game Center can run. In this case we do not
  89. // support calling the Stop() function, as the application will never stop manually
  90. #else
  91. #if defined(IOS)
  92. SDL_iPhoneSetAnimationCallback(GetSubsystem<Graphics>()->GetImpl()->GetWindow(), 1, &RunFrame, engine_);
  93. #elif defined(EMSCRIPTEN)
  94. // cancel the initialization loop
  95. emscripten_cancel_main_loop();
  96. // and run the engine loop
  97. emscripten_set_main_loop_arg(RunFrame, engine_, 0, 1);
  98. #endif
  99. #endif
  100. return exitCode_;
  101. }
  102. catch (std::bad_alloc&)
  103. {
  104. ErrorDialog(GetTypeName(), "An out-of-memory error occurred. The application will now exit.");
  105. return EXIT_FAILURE;
  106. }
  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. }