Application.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. //
  2. // Copyright (c) 2008-2014 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 "../Container/DebugNew.h"
  34. namespace Urho3D
  35. {
  36. #ifdef IOS
  37. // Code for supporting SDL_iPhoneSetAnimationCallback
  38. void RunFrame(void* data)
  39. {
  40. Application* instance = reinterpret_cast<Application*>(data);
  41. instance->GetSubsystem<Engine>()->RunFrame();
  42. }
  43. #endif
  44. Application::Application(Context* context) :
  45. Object(context),
  46. exitCode_(EXIT_SUCCESS)
  47. {
  48. engineParameters_ = Engine::ParseParameters(GetArguments());
  49. // Create the Engine, but do not initialize it yet. Subsystems except Graphics & Renderer are registered at this point
  50. engine_ = new Engine(context);
  51. // Subscribe to log messages so that can show errors if ErrorExit() is called with empty message
  52. SubscribeToEvent(E_LOGMESSAGE, HANDLER(Application, HandleLogMessage));
  53. }
  54. int Application::Run()
  55. {
  56. try
  57. {
  58. Setup();
  59. if (exitCode_)
  60. return exitCode_;
  61. if (!engine_->Initialize(engineParameters_))
  62. {
  63. ErrorExit();
  64. return exitCode_;
  65. }
  66. Start();
  67. if (exitCode_)
  68. return exitCode_;
  69. // Platforms other than iOS run a blocking main loop
  70. #ifndef IOS
  71. while (!engine_->IsExiting())
  72. engine_->RunFrame();
  73. Stop();
  74. // iOS will setup a timer for running animation frames so eg. Game Center can run. In this case we do not
  75. // support calling the Stop() function, as the application will never stop manually
  76. #else
  77. SDL_iPhoneSetAnimationCallback(GetSubsystem<Graphics>()->GetImpl()->GetWindow(), 1, &RunFrame, this);
  78. #endif
  79. return exitCode_;
  80. }
  81. catch (std::bad_alloc&)
  82. {
  83. ErrorDialog(GetTypeName(), "An out-of-memory error occurred. The application will now exit.");
  84. return EXIT_FAILURE;
  85. }
  86. }
  87. void Application::ErrorExit(const String& message)
  88. {
  89. engine_->Exit(); // Close the rendering window
  90. exitCode_ = EXIT_FAILURE;
  91. // Only for WIN32, otherwise the error messages would be double posted on Mac OS X and Linux platforms
  92. if (!message.Length())
  93. {
  94. #ifdef WIN32
  95. ErrorDialog(GetTypeName(), startupErrors_.Length() ? startupErrors_ :
  96. "Application has been terminated due to unexpected error.");
  97. #endif
  98. }
  99. else
  100. ErrorDialog(GetTypeName(), message);
  101. }
  102. void Application::HandleLogMessage(StringHash eventType, VariantMap& eventData)
  103. {
  104. using namespace LogMessage;
  105. if (eventData[P_LEVEL].GetInt() == LOG_ERROR)
  106. {
  107. // Strip the timestamp if necessary
  108. String error = eventData[P_MESSAGE].GetString();
  109. unsigned bracketPos = error.Find(']');
  110. if (bracketPos != String::NPOS)
  111. error = error.Substring(bracketPos + 2);
  112. startupErrors_ += error + "\n";
  113. }
  114. }
  115. }