2
0

Application.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. /// Render module
  28. Renderer * mRenderer;
  29. /// Default font
  30. RefConst<Font> mFont;
  31. /// Input
  32. Keyboard * mKeyboard;
  33. Mouse * mMouse;
  34. /// Menu
  35. UIManager * mUI;
  36. DebugUI * mDebugUI;
  37. /// A string that is shown on screen to indicate the status of the application
  38. String mStatusString;
  39. public:
  40. /// Constructor
  41. Application();
  42. virtual ~Application();
  43. /// Enter the main loop
  44. void Run();
  45. protected:
  46. /// Callback to render a frame
  47. virtual bool RenderFrame(float inDeltaTime) { return false; }
  48. /// Pause / unpause the simulation
  49. void Pause(bool inPaused) { mIsPaused = inPaused; }
  50. /// Programmatically single step the simulation
  51. void SingleStep() { mIsPaused = true; mSingleStep = true; }
  52. /// Will restore camera position to that returned by GetInitialCamera
  53. void ResetCamera();
  54. /// Override to specify the initial camera state (local to GetCameraPivot)
  55. virtual void GetInitialCamera(CameraState &ioState) const { }
  56. /// Override to specify a camera pivot point and orientation (world space)
  57. virtual RMat44 GetCameraPivot(float inCameraHeading, float inCameraPitch) const { return RMat44::sIdentity(); }
  58. /// Get scale factor for this world, used to boost camera speed and to scale detail of the shadows
  59. virtual float GetWorldScale() const { return 1.0f; }
  60. /// Get current state of the camera (world space)
  61. const CameraState & GetCamera() const { return mWorldCamera; }
  62. /// Clear debug lines / triangles / texts that have been accumulated
  63. void ClearDebugRenderer();
  64. private:
  65. /// Extract heading and pitch from the local space (relative to the camera pivot) camera forward
  66. void GetCameraLocalHeadingAndPitch(float &outHeading, float &outPitch);
  67. /// Convert local space camera to world space camera
  68. void ConvertCameraLocalToWorld(float inCameraHeading, float inCameraPitch);
  69. /// Update the local and world space camera transform
  70. void UpdateCamera(float inDeltaTime);
  71. /// Draw the frame rate counter
  72. void DrawFPS(float inDeltaTime);
  73. chrono::high_resolution_clock::time_point mLastUpdateTime;
  74. bool mIsPaused = false;
  75. bool mSingleStep = false;
  76. bool mDebugRendererCleared = true;
  77. bool mLeftMousePressed = false;
  78. float mFPS = 0.0f;
  79. float mTotalDeltaTime = 0.0f;
  80. int mNumFrames = 0;
  81. };