Engine.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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, as an iOS application can not legally exit.
  63. void Exit();
  64. /// Dump profiling information to the log.
  65. void DumpProfiler();
  66. /// Dump information of all resources to the log.
  67. void DumpResources(bool dumpFileName = false);
  68. /// Dump information of all memory allocations to the log. Supported in MSVC debug mode only.
  69. void DumpMemory();
  70. /// Get timestep of the next frame. Updated by ApplyFrameLimit().
  71. float GetNextTimeStep() const { return timeStep_; }
  72. /// Return the minimum frames per second.
  73. int GetMinFps() const { return minFps_; }
  74. /// Return the maximum frames per second.
  75. int GetMaxFps() const { return maxFps_; }
  76. /// Return the maximum frames per second when the application does not have input focus.
  77. int GetMaxInactiveFps() const { return maxInactiveFps_; }
  78. /// Return how many frames to average for timestep smoothing.
  79. int GetTimeStepSmoothing() const { return timeStepSmoothing_; }
  80. /// Return whether to pause update events and audio when minimized.
  81. bool GetPauseMinimized() const { return pauseMinimized_; }
  82. /// Return whether to exit automatically on exit request.
  83. bool GetAutoExit() const { return autoExit_; }
  84. /// Return whether engine has been initialized.
  85. bool IsInitialized() const { return initialized_; }
  86. /// Return whether exit has been requested.
  87. bool IsExiting() const { return exiting_; }
  88. /// Return whether the engine has been created in headless mode.
  89. bool IsHeadless() const { return headless_; }
  90. /// Send frame update events.
  91. void Update();
  92. /// Render after frame update.
  93. void Render();
  94. /// Get the timestep for the next frame and sleep for frame limiting if necessary.
  95. void ApplyFrameLimit();
  96. /// Parse the engine startup parameters map from command line arguments.
  97. static VariantMap ParseParameters(const Vector<String>& arguments);
  98. /// Return whether startup parameters contains a specific parameter.
  99. static bool HasParameter(const VariantMap& parameters, const String& parameter);
  100. /// Get an engine startup parameter, with default value if missing.
  101. static const Variant
  102. & GetParameter(const VariantMap& parameters, const String& parameter, const Variant& defaultValue = Variant::EMPTY);
  103. // ATOMIC BEGIN
  104. /// Set whether the engine is paused.
  105. void SetPaused(bool paused);
  106. /// Set whether to run the next frame even if paused (for stepping frame by frame)
  107. void SetRunNextPausedFrame(bool run);
  108. /// Return whether the engine is paused.
  109. bool IsPaused() const { return paused_; }
  110. /// Return whether to run the next frame even if paused (for stepping frame by frame)
  111. bool GetRunNextPausedFrame() const { return runNextPausedFrame_; }
  112. /// Return the engine's current framerate (updated at 1/2 second intervals)
  113. unsigned GetFps() const { return fps_; }
  114. bool GetDebugBuild() const;
  115. // ATOMIC END
  116. private:
  117. /// Handle exit requested event. Auto-exit if enabled.
  118. void HandleExitRequested(StringHash eventType, VariantMap& eventData);
  119. // ATOMIC BEGIN
  120. /// Handle Pause or Resume requested event.
  121. void HandlePauseResumeRequested(StringHash eventType, VariantMap& eventData);
  122. /// Handle Single Step requested event.
  123. void HandlePauseStepRequested(StringHash eventType, VariantMap& eventData);
  124. // ATOMIC END
  125. /// Actually perform the exit actions.
  126. void DoExit();
  127. /// Frame update timer.
  128. HiresTimer frameTimer_;
  129. /// Previous timesteps for smoothing.
  130. PODVector<float> lastTimeSteps_;
  131. /// Next frame timestep in seconds.
  132. float timeStep_;
  133. /// How many frames to average for the smoothed timestep.
  134. unsigned timeStepSmoothing_;
  135. /// Minimum frames per second.
  136. unsigned minFps_;
  137. /// Maximum frames per second.
  138. unsigned maxFps_;
  139. /// Maximum frames per second when the application does not have input focus.
  140. unsigned maxInactiveFps_;
  141. /// Pause when minimized flag.
  142. bool pauseMinimized_;
  143. #ifdef ATOMIC_TESTING
  144. /// Time out counter for testing.
  145. long long timeOut_;
  146. #endif
  147. /// Auto-exit flag.
  148. bool autoExit_;
  149. /// Initialized flag.
  150. bool initialized_;
  151. /// Exiting flag.
  152. bool exiting_;
  153. /// Headless mode flag.
  154. bool headless_;
  155. /// Audio paused flag.
  156. bool audioPaused_;
  157. // ATOMIC BEGIN
  158. /// Engine paused flag
  159. bool paused_;
  160. /// Whether to run the next frame even if paused (for stepping frame by frame)
  161. bool runNextPausedFrame_;
  162. /// Time since last fps display update
  163. float fpsTimeSinceUpdate_;
  164. /// Frames since last fps display update
  165. float fpsFramesSinceUpdate_;
  166. /// Calculated fps
  167. unsigned fps_;
  168. // ATOMIC END
  169. };
  170. }