Engine.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #pragma once
  4. #include "../Core/Object.h"
  5. #include "../Core/Timer.h"
  6. namespace Urho3D
  7. {
  8. class Console;
  9. class DebugHud;
  10. /// Urho3D engine. Creates the other subsystems.
  11. class URHO3D_API Engine : public Object
  12. {
  13. URHO3D_OBJECT(Engine, Object);
  14. public:
  15. /// Construct.
  16. explicit Engine(Context* context);
  17. /// Destruct. Free all subsystems.
  18. ~Engine() override;
  19. /// Initialize engine using parameters given and show the application window. Return true if successful.
  20. bool Initialize(const VariantMap& parameters);
  21. /// Reinitialize resource cache subsystem using parameters given. Implicitly called by Initialize. Return true if successful.
  22. bool InitializeResourceCache(const VariantMap& parameters, bool removeOld = true);
  23. /// Run one frame.
  24. void RunFrame();
  25. /// Create the console and return it. May return null if engine configuration does not allow creation (headless mode).
  26. Console* CreateConsole();
  27. /// Create the debug hud.
  28. DebugHud* CreateDebugHud();
  29. /// Set minimum frames per second. If FPS goes lower than this, time will appear to slow down.
  30. /// @property
  31. void SetMinFps(int fps);
  32. /// Set maximum frames per second. The engine will sleep if FPS is higher than this.
  33. /// @property
  34. void SetMaxFps(int fps);
  35. /// Set maximum frames per second when the application does not have input focus.
  36. /// @property
  37. void SetMaxInactiveFps(int fps);
  38. /// Set how many frames to average for timestep smoothing. Default is 2. 1 disables smoothing.
  39. /// @property
  40. void SetTimeStepSmoothing(int frames);
  41. /// Set whether to pause update events and audio when minimized.
  42. /// @property
  43. void SetPauseMinimized(bool enable);
  44. /// Set whether to exit automatically on exit request (window close button).
  45. /// @property
  46. void SetAutoExit(bool enable);
  47. /// Override timestep of the next frame. Should be called in between RunFrame() calls.
  48. void SetNextTimeStep(float seconds);
  49. /// Close the graphics window and set the exit flag. No-op on iOS/tvOS, as an iOS/tvOS application can not legally exit.
  50. void Exit();
  51. /// Dump profiling information to the log.
  52. void DumpProfiler();
  53. /// Dump information of all resources to the log.
  54. void DumpResources(bool dumpFileName = false);
  55. /// Dump information of all memory allocations to the log. Supported in MSVC debug mode only.
  56. void DumpMemory();
  57. /// Get timestep of the next frame. Updated by ApplyFrameLimit().
  58. float GetNextTimeStep() const { return timeStep_; }
  59. /// Return the minimum frames per second.
  60. /// @property
  61. int GetMinFps() const { return minFps_; }
  62. /// Return the maximum frames per second.
  63. /// @property
  64. int GetMaxFps() const { return maxFps_; }
  65. /// Return the maximum frames per second when the application does not have input focus.
  66. /// @property
  67. int GetMaxInactiveFps() const { return maxInactiveFps_; }
  68. /// Return how many frames to average for timestep smoothing.
  69. /// @property
  70. int GetTimeStepSmoothing() const { return timeStepSmoothing_; }
  71. /// Return whether to pause update events and audio when minimized.
  72. /// @property
  73. bool GetPauseMinimized() const { return pauseMinimized_; }
  74. /// Return whether to exit automatically on exit request.
  75. /// @property
  76. bool GetAutoExit() const { return autoExit_; }
  77. /// Return whether engine has been initialized.
  78. /// @property
  79. bool IsInitialized() const { return initialized_; }
  80. /// Return whether exit has been requested.
  81. /// @property
  82. bool IsExiting() const { return exiting_; }
  83. /// Return whether the engine has been created in headless mode.
  84. /// @property
  85. bool IsHeadless() const { return headless_; }
  86. /// Send frame update events.
  87. void Update();
  88. /// Render after frame update.
  89. void Render();
  90. /// Get the timestep for the next frame and sleep for frame limiting if necessary.
  91. void ApplyFrameLimit();
  92. /// Parse the engine startup parameters map from command line arguments.
  93. static VariantMap ParseParameters(const Vector<String>& arguments);
  94. /// Return whether startup parameters contains a specific parameter.
  95. static bool HasParameter(const VariantMap& parameters, const String& parameter);
  96. /// Get an engine startup parameter, with default value if missing.
  97. static const Variant
  98. & GetParameter(const VariantMap& parameters, const String& parameter, const Variant& defaultValue = Variant::EMPTY);
  99. private:
  100. /// Handle exit requested event. Auto-exit if enabled.
  101. void HandleExitRequested(StringHash eventType, VariantMap& eventData);
  102. /// Actually perform the exit actions.
  103. void DoExit();
  104. /// Frame update timer.
  105. HiresTimer frameTimer_;
  106. /// Previous timesteps for smoothing.
  107. Vector<float> lastTimeSteps_;
  108. /// Next frame timestep in seconds.
  109. float timeStep_;
  110. /// How many frames to average for the smoothed timestep.
  111. unsigned timeStepSmoothing_;
  112. /// Minimum frames per second.
  113. unsigned minFps_;
  114. /// Maximum frames per second.
  115. unsigned maxFps_;
  116. /// Maximum frames per second when the application does not have input focus.
  117. unsigned maxInactiveFps_;
  118. /// Pause when minimized flag.
  119. bool pauseMinimized_;
  120. #ifdef URHO3D_TESTING
  121. /// Time out counter for testing.
  122. long long timeOut_;
  123. #endif
  124. /// Auto-exit flag.
  125. bool autoExit_;
  126. /// Initialized flag.
  127. bool initialized_;
  128. /// Exiting flag.
  129. bool exiting_;
  130. /// Headless mode flag.
  131. bool headless_;
  132. /// Audio paused flag.
  133. bool audioPaused_;
  134. };
  135. }