Application.h 2.8 KB

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