Application.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. //
  2. // Copyright (c) 2008-2017 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. #pragma once
  23. #include "../Core/Context.h"
  24. #include "../Core/Main.h"
  25. #include "../Engine/Engine.h"
  26. namespace Atomic
  27. {
  28. class Engine;
  29. /// Base class for creating applications which initialize the Urho3D engine and run a main loop until exited.
  30. class ATOMIC_API Application : public Object
  31. {
  32. ATOMIC_OBJECT(Application, Object);
  33. public:
  34. /// Construct. Parse default engine parameters from the command line, and create the engine in an uninitialized state.
  35. Application(Context* context);
  36. /// Setup before engine initialization. This is a chance to eg. modify the engine parameters. Call ErrorExit() to terminate without initializing the engine. Called by Application.
  37. virtual void Setup() { }
  38. /// Setup after engine initialization and before running the main loop. Call ErrorExit() to terminate without running the main loop. Called by Application.
  39. virtual void Start() { }
  40. /// Cleanup after the main loop. Called by Application.
  41. virtual void Stop() { }
  42. /// Initialize the engine and run the main loop, then return the application exit code. Catch out-of-memory exceptions while running.
  43. int Run();
  44. /// Show an error message (last log message if empty), terminate the main loop, and set failure exit code.
  45. void ErrorExit(const String& message = String::EMPTY);
  46. // ATOMIC BEGIN
  47. static bool GetAutoMetrics() { return autoMetrics_; }
  48. static void SetAutoMetrics(bool value) { autoMetrics_ = value; }
  49. // ATOMIC END
  50. protected:
  51. /// Handle log message.
  52. void HandleLogMessage(StringHash eventType, VariantMap& eventData);
  53. /// Urho3D engine.
  54. SharedPtr<Engine> engine_;
  55. /// Engine parameters map.
  56. VariantMap engineParameters_;
  57. /// Collected startup error log messages.
  58. String startupErrors_;
  59. /// Application exit code.
  60. int exitCode_;
  61. // ATOMIC BEGIN
  62. static bool autoMetrics_;
  63. // ATOMIC END
  64. };
  65. // Macro for defining a main function which creates a Context and the application, then runs it
  66. #ifndef IOS
  67. #define ATOMIC_DEFINE_APPLICATION_MAIN(className) \
  68. int RunApplication() \
  69. { \
  70. Atomic::SharedPtr<Atomic::Context> context(new Atomic::Context()); \
  71. Atomic::SharedPtr<className> application(new className(context)); \
  72. return application->Run(); \
  73. } \
  74. ATOMIC_DEFINE_MAIN(RunApplication());
  75. #else
  76. // On iOS we will let this function exit, so do not hold the context and application in SharedPtr's
  77. #define ATOMIC_DEFINE_APPLICATION_MAIN(className) \
  78. int RunApplication() \
  79. { \
  80. Atomic::Context* context = new Atomic::Context(); \
  81. className* application = new className(context); \
  82. return application->Run(); \
  83. } \
  84. ATOMIC_DEFINE_MAIN(RunApplication());
  85. #endif
  86. }