Application.h 2.8 KB

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