Engine.h 6.8 KB

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