TemplateAppMain.cpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #include "pch.h"
  2. #include "TemplateAppMain.h"
  3. #include "Common\DirectXHelper.h"
  4. using namespace TemplateApp;
  5. using namespace Windows::Foundation;
  6. using namespace Windows::System::Threading;
  7. using namespace Concurrency;
  8. // The DirectX 12 Application template is documented at http://go.microsoft.com/fwlink/?LinkID=613670&clcid=0x409
  9. // Loads and initializes application assets when the application is loaded.
  10. TemplateAppMain::TemplateAppMain()
  11. {
  12. // TODO: Change the timer settings if you want something other than the default variable timestep mode.
  13. // e.g. for 60 FPS fixed timestep update logic, call:
  14. /*
  15. m_timer.SetFixedTimeStep(true);
  16. m_timer.SetTargetElapsedSeconds(1.0 / 60);
  17. */
  18. }
  19. // Creates and initializes the renderers.
  20. void TemplateAppMain::CreateRenderers(const std::shared_ptr<DX::DeviceResources>& deviceResources)
  21. {
  22. // TODO: Replace this with your app's content initialization.
  23. m_sceneRenderer = std::unique_ptr<Sample3DSceneRenderer>(new Sample3DSceneRenderer(deviceResources));
  24. OnWindowSizeChanged();
  25. }
  26. // Updates the application state once per frame.
  27. void TemplateAppMain::Update()
  28. {
  29. // Update scene objects.
  30. m_timer.Tick([&]()
  31. {
  32. // TODO: Replace this with your app's content update functions.
  33. m_sceneRenderer->Update(m_timer);
  34. });
  35. }
  36. // Renders the current frame according to the current application state.
  37. // Returns true if the frame was rendered and is ready to be displayed.
  38. bool TemplateAppMain::Render()
  39. {
  40. // Don't try to render anything before the first Update.
  41. if (m_timer.GetFrameCount() == 0)
  42. {
  43. return false;
  44. }
  45. // Render the scene objects.
  46. // TODO: Replace this with your app's content rendering functions.
  47. return m_sceneRenderer->Render();
  48. }
  49. // Updates application state when the window's size changes (e.g. device orientation change)
  50. void TemplateAppMain::OnWindowSizeChanged()
  51. {
  52. // TODO: Replace this with the size-dependent initialization of your app's content.
  53. m_sceneRenderer->CreateWindowSizeDependentResources();
  54. }
  55. // Notifies the app that it is being suspended.
  56. void TemplateAppMain::OnSuspending()
  57. {
  58. // TODO: Replace this with your app's suspending logic.
  59. // Process lifetime management may terminate suspended apps at any time, so it is
  60. // good practice to save any state that will allow the app to restart where it left off.
  61. m_sceneRenderer->SaveState();
  62. // If your application uses video memory allocations that are easy to re-create,
  63. // consider releasing that memory to make it available to other applications.
  64. }
  65. // Notifes the app that it is no longer suspended.
  66. void TemplateAppMain::OnResuming()
  67. {
  68. // TODO: Replace this with your app's resuming logic.
  69. }
  70. // Notifies renderers that device resources need to be released.
  71. void TemplateAppMain::OnDeviceRemoved()
  72. {
  73. // TODO: Save any necessary application or renderer state and release the renderer
  74. // and its resources which are no longer valid.
  75. m_sceneRenderer->SaveState();
  76. m_sceneRenderer = nullptr;
  77. }