Engine.h 7.3 KB

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