Application.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #include <Renderer/Renderer.h>
  5. #include <Renderer/Font.h>
  6. #include <Input/Keyboard.h>
  7. #include <Input/Mouse.h>
  8. #include <Jolt/Core/Reference.h>
  9. // STL includes
  10. JPH_SUPPRESS_WARNINGS_STD_BEGIN
  11. #include <chrono>
  12. JPH_SUPPRESS_WARNINGS_STD_END
  13. class UIManager;
  14. class DebugUI;
  15. namespace JPH {
  16. class DebugRenderer;
  17. }
  18. class Application
  19. {
  20. private:
  21. /// Camera state
  22. CameraState mLocalCamera;
  23. CameraState mWorldCamera;
  24. protected:
  25. /// Debug renderer module
  26. DebugRenderer * mDebugRenderer;
  27. /// Main window
  28. ApplicationWindow * mWindow;
  29. /// Render module
  30. Renderer * mRenderer;
  31. /// Default font
  32. RefConst<Font> mFont;
  33. /// Input
  34. Keyboard * mKeyboard;
  35. Mouse * mMouse;
  36. /// Menu
  37. UIManager * mUI;
  38. DebugUI * mDebugUI;
  39. /// A string that is shown on screen to indicate the status of the application
  40. String mStatusString;
  41. public:
  42. /// Constructor
  43. Application(const char *inApplicationName, const String &inCommandLine);
  44. virtual ~Application();
  45. /// Create a single string command line
  46. static String sCreateCommandLine(int inArgC, char **inArgV);
  47. /// Enter the main loop
  48. void Run();
  49. protected:
  50. /// Update the application
  51. virtual bool UpdateFrame(float inDeltaTime) { return false; }
  52. /// Pause / unpause the simulation
  53. void Pause(bool inPaused) { mIsPaused = inPaused; }
  54. /// Programmatically single step the simulation
  55. void SingleStep() { mIsPaused = true; mSingleStep = true; }
  56. /// Set the frequency at which we want to render frames
  57. void SetRenderFrequency(float inFrequency) { mRequestedDeltaTime = 1.0f / inFrequency; }
  58. /// Will restore camera position to that returned by GetInitialCamera
  59. void ResetCamera();
  60. /// Override to specify the initial camera state (local to GetCameraPivot)
  61. virtual void GetInitialCamera(CameraState &ioState) const { }
  62. /// Override to specify a camera pivot point and orientation (world space)
  63. virtual RMat44 GetCameraPivot(float inCameraHeading, float inCameraPitch) const { return RMat44::sIdentity(); }
  64. /// Get scale factor for this world, used to boost camera speed and to scale detail of the shadows
  65. virtual float GetWorldScale() const { return 1.0f; }
  66. /// Get current state of the camera (world space)
  67. const CameraState & GetCamera() const { return mWorldCamera; }
  68. /// Clear debug lines / triangles / texts that have been accumulated
  69. void ClearDebugRenderer();
  70. private:
  71. /// Render a frame
  72. bool RenderFrame();
  73. /// Extract heading and pitch from the local space (relative to the camera pivot) camera forward
  74. void GetCameraLocalHeadingAndPitch(float &outHeading, float &outPitch);
  75. /// Convert local space camera to world space camera
  76. void ConvertCameraLocalToWorld(float inCameraHeading, float inCameraPitch);
  77. /// Update the local and world space camera transform
  78. void UpdateCamera(float inDeltaTime);
  79. /// Draw the frame rate counter
  80. void DrawFPS(float inDeltaTime);
  81. chrono::high_resolution_clock::time_point mLastUpdateTime;
  82. bool mIsPaused = false;
  83. bool mSingleStep = false;
  84. bool mDebugRendererCleared = true;
  85. bool mLeftMousePressed = false;
  86. float mFPS = 0.0f;
  87. float mRequestedDeltaTime = 0.0f;
  88. float mResidualDeltaTime = 0.0f;
  89. float mTotalDeltaTime = 0.0f;
  90. int mNumFrames = 0;
  91. };