Application.cpp 3.2 KB

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