Engine.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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/Object.h"
  24. #include "../Core/Timer.h"
  25. namespace Atomic
  26. {
  27. class Console;
  28. class DebugHud;
  29. /// Urho3D engine. Creates the other subsystems.
  30. class ATOMIC_API Engine : public Object
  31. {
  32. ATOMIC_OBJECT(Engine, Object);
  33. public:
  34. /// Construct.
  35. Engine(Context* context);
  36. /// Destruct. Free all subsystems.
  37. virtual ~Engine();
  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. void SetMinFps(int fps);
  50. /// Set maximum frames per second. The engine will sleep if FPS is higher than this.
  51. void SetMaxFps(int fps);
  52. /// Set maximum frames per second when the application does not have input focus.
  53. void SetMaxInactiveFps(int fps);
  54. /// Set how many frames to average for timestep smoothing. Default is 2. 1 disables smoothing.
  55. void SetTimeStepSmoothing(int frames);
  56. /// Set whether to pause update events and audio when minimized.
  57. void SetPauseMinimized(bool enable);
  58. /// Set whether to exit automatically on exit request (window close button.)
  59. void SetAutoExit(bool enable);
  60. /// Override timestep of the next frame. Should be called in between RunFrame() calls.
  61. void SetNextTimeStep(float seconds);
  62. /// Close the graphics window and set the exit flag. No-op on iOS/tvOS, as an iOS/tvOS application can not legally exit.
  63. void Exit();
  64. /// Dump information of all resources to the log.
  65. void DumpResources(bool dumpFileName = false);
  66. /// Dump information of all memory allocations to the log. Supported in MSVC debug mode only.
  67. void DumpMemory();
  68. /// Get timestep of the next frame. Updated by ApplyFrameLimit().
  69. float GetNextTimeStep() const { return timeStep_; }
  70. /// Return the minimum frames per second.
  71. int GetMinFps() const { return minFps_; }
  72. /// Return the maximum frames per second.
  73. int GetMaxFps() const { return maxFps_; }
  74. /// Return the maximum frames per second when the application does not have input focus.
  75. int GetMaxInactiveFps() const { return maxInactiveFps_; }
  76. /// Return how many frames to average for timestep smoothing.
  77. int GetTimeStepSmoothing() const { return timeStepSmoothing_; }
  78. /// Return whether to pause update events and audio when minimized.
  79. bool GetPauseMinimized() const { return pauseMinimized_; }
  80. /// Return whether to exit automatically on exit request.
  81. bool GetAutoExit() const { return autoExit_; }
  82. /// Return whether engine has been initialized.
  83. bool IsInitialized() const { return initialized_; }
  84. /// Return whether exit has been requested.
  85. bool IsExiting() const { return exiting_; }
  86. /// Return whether the engine has been created in headless mode.
  87. bool IsHeadless() const { return headless_; }
  88. /// Send frame update events.
  89. void Update();
  90. /// Render after frame update.
  91. void Render();
  92. /// Get the timestep for the next frame and sleep for frame limiting if necessary.
  93. void ApplyFrameLimit();
  94. /// Parse the engine startup parameters map from command line arguments.
  95. static VariantMap ParseParameters(const Vector<String>& arguments);
  96. /// Return whether startup parameters contains a specific parameter.
  97. static bool HasParameter(const VariantMap& parameters, const String& parameter);
  98. /// Get an engine startup parameter, with default value if missing.
  99. static const Variant
  100. & GetParameter(const VariantMap& parameters, const String& parameter, const Variant& defaultValue = Variant::EMPTY);
  101. // ATOMIC BEGIN
  102. /// Set whether the engine is paused.
  103. void SetPaused(bool paused);
  104. /// Set whether to run the next frame even if paused (for stepping frame by frame)
  105. void SetRunNextPausedFrame(bool run);
  106. /// Return whether the engine is paused.
  107. bool IsPaused() const { return paused_; }
  108. /// Return whether to run the next frame even if paused (for stepping frame by frame)
  109. bool GetRunNextPausedFrame() const { return runNextPausedFrame_; }
  110. /// Return the engine's current framerate (updated at 1/2 second intervals)
  111. unsigned GetFps() const { return fps_; }
  112. bool GetDebugBuild() const;
  113. // ATOMIC END
  114. private:
  115. /// Handle exit requested event. Auto-exit if enabled.
  116. void HandleExitRequested(StringHash eventType, VariantMap& eventData);
  117. // ATOMIC BEGIN
  118. /// Handle Pause or Resume requested event.
  119. void HandlePauseResumeRequested(StringHash eventType, VariantMap& eventData);
  120. /// Handle Single Step requested event.
  121. void HandlePauseStepRequested(StringHash eventType, VariantMap& eventData);
  122. // ATOMIC END
  123. /// Actually perform the exit actions.
  124. void DoExit();
  125. /// Frame update timer.
  126. HiresTimer frameTimer_;
  127. /// Previous timesteps for smoothing.
  128. PODVector<float> lastTimeSteps_;
  129. /// Next frame timestep in seconds.
  130. float timeStep_;
  131. /// How many frames to average for the smoothed timestep.
  132. unsigned timeStepSmoothing_;
  133. /// Minimum frames per second.
  134. unsigned minFps_;
  135. /// Maximum frames per second.
  136. unsigned maxFps_;
  137. /// Maximum frames per second when the application does not have input focus.
  138. unsigned maxInactiveFps_;
  139. /// Pause when minimized flag.
  140. bool pauseMinimized_;
  141. #ifdef ATOMIC_TESTING
  142. /// Time out counter for testing.
  143. long long timeOut_;
  144. #endif
  145. /// Auto-exit flag.
  146. bool autoExit_;
  147. /// Initialized flag.
  148. bool initialized_;
  149. /// Exiting flag.
  150. bool exiting_;
  151. /// Headless mode flag.
  152. bool headless_;
  153. /// Audio paused flag.
  154. bool audioPaused_;
  155. // ATOMIC BEGIN
  156. /// Engine paused flag
  157. bool paused_;
  158. /// Whether to run the next frame even if paused (for stepping frame by frame)
  159. bool runNextPausedFrame_;
  160. /// Time since last fps display update
  161. float fpsTimeSinceUpdate_;
  162. /// Frames since last fps display update
  163. float fpsFramesSinceUpdate_;
  164. /// Calculated fps
  165. unsigned fps_;
  166. // ATOMIC END
  167. };
  168. }