Application.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. /// \file
  4. /// @nobindfile
  5. #pragma once
  6. #include "../Core/Context.h"
  7. #include "../Core/Main.h"
  8. #include "../Engine/Engine.h"
  9. namespace Urho3D
  10. {
  11. class Engine;
  12. /// Base class for creating applications which initialize the Urho3D engine and run a main loop until exited.
  13. class URHO3D_API Application : public Object
  14. {
  15. URHO3D_OBJECT(Application, Object);
  16. public:
  17. /// Construct. Parse default engine parameters from the command line, and create the engine in an uninitialized state.
  18. explicit Application(Context* context);
  19. /// 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.
  20. virtual void Setup() { }
  21. /// Setup after engine initialization and before running the main loop. Call ErrorExit() to terminate without running the main loop. Called by Application.
  22. virtual void Start() { }
  23. /// Cleanup after the main loop. Called by Application.
  24. virtual void Stop() { }
  25. /// Initialize the engine and run the main loop, then return the application exit code. Catch out-of-memory exceptions while running.
  26. int Run();
  27. /// Show an error message (last log message if empty), terminate the main loop, and set failure exit code.
  28. void ErrorExit(const String& message = String::EMPTY);
  29. protected:
  30. /// Handle log message.
  31. void HandleLogMessage(StringHash eventType, VariantMap& eventData);
  32. /// Urho3D engine.
  33. SharedPtr<Engine> engine_;
  34. /// Engine parameters map.
  35. VariantMap engineParameters_;
  36. /// Collected startup error log messages.
  37. String startupErrors_;
  38. /// Application exit code.
  39. int exitCode_;
  40. };
  41. // Macro for defining a main function which creates a Context and the application, then runs it
  42. #if !defined(IOS) && !defined(TVOS)
  43. #define URHO3D_DEFINE_APPLICATION_MAIN(className) \
  44. int RunApplication() \
  45. { \
  46. Urho3D::SharedPtr<Urho3D::Context> context(new Urho3D::Context()); \
  47. Urho3D::SharedPtr<className> application(new className(context)); \
  48. return application->Run(); \
  49. } \
  50. URHO3D_DEFINE_MAIN(RunApplication())
  51. #else
  52. // On iOS/tvOS we will let this function exit, so do not hold the context and application in SharedPtr's
  53. #define URHO3D_DEFINE_APPLICATION_MAIN(className) \
  54. int RunApplication() \
  55. { \
  56. Urho3D::Context* context = new Urho3D::Context(); \
  57. className* application = new className(context); \
  58. return application->Run(); \
  59. } \
  60. URHO3D_DEFINE_MAIN(RunApplication());
  61. #endif
  62. }