App.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. #include "pch.h"
  2. #include "App.h"
  3. #include <ppltasks.h>
  4. using namespace TemplateApp;
  5. using namespace concurrency;
  6. using namespace Windows::ApplicationModel;
  7. using namespace Windows::ApplicationModel::Core;
  8. using namespace Windows::ApplicationModel::Activation;
  9. using namespace Windows::UI::Core;
  10. using namespace Windows::UI::Input;
  11. using namespace Windows::System;
  12. using namespace Windows::Foundation;
  13. using namespace Windows::Graphics::Display;
  14. // The DirectX 12 Application template is documented at http://go.microsoft.com/fwlink/?LinkID=613670&clcid=0x409
  15. // The main function is only used to initialize our IFrameworkView class.
  16. [Platform::MTAThread]
  17. int main(Platform::Array<Platform::String^>^)
  18. {
  19. auto direct3DApplicationSource = ref new Direct3DApplicationSource();
  20. CoreApplication::Run(direct3DApplicationSource);
  21. return 0;
  22. }
  23. IFrameworkView^ Direct3DApplicationSource::CreateView()
  24. {
  25. return ref new App();
  26. }
  27. App::App() :
  28. m_windowClosed(false),
  29. m_windowVisible(true)
  30. {
  31. }
  32. // The first method called when the IFrameworkView is being created.
  33. void App::Initialize(CoreApplicationView^ applicationView)
  34. {
  35. // Register event handlers for app lifecycle. This example includes Activated, so that we
  36. // can make the CoreWindow active and start rendering on the window.
  37. applicationView->Activated +=
  38. ref new TypedEventHandler<CoreApplicationView^, IActivatedEventArgs^>(this, &App::OnActivated);
  39. CoreApplication::Suspending +=
  40. ref new EventHandler<SuspendingEventArgs^>(this, &App::OnSuspending);
  41. CoreApplication::Resuming +=
  42. ref new EventHandler<Platform::Object^>(this, &App::OnResuming);
  43. }
  44. // Called when the CoreWindow object is created (or re-created).
  45. void App::SetWindow(CoreWindow^ window)
  46. {
  47. window->SizeChanged +=
  48. ref new TypedEventHandler<CoreWindow^, WindowSizeChangedEventArgs^>(this, &App::OnWindowSizeChanged);
  49. window->VisibilityChanged +=
  50. ref new TypedEventHandler<CoreWindow^, VisibilityChangedEventArgs^>(this, &App::OnVisibilityChanged);
  51. window->Closed +=
  52. ref new TypedEventHandler<CoreWindow^, CoreWindowEventArgs^>(this, &App::OnWindowClosed);
  53. DisplayInformation^ currentDisplayInformation = DisplayInformation::GetForCurrentView();
  54. currentDisplayInformation->DpiChanged +=
  55. ref new TypedEventHandler<DisplayInformation^, Object^>(this, &App::OnDpiChanged);
  56. currentDisplayInformation->OrientationChanged +=
  57. ref new TypedEventHandler<DisplayInformation^, Object^>(this, &App::OnOrientationChanged);
  58. DisplayInformation::DisplayContentsInvalidated +=
  59. ref new TypedEventHandler<DisplayInformation^, Object^>(this, &App::OnDisplayContentsInvalidated);
  60. }
  61. // Initializes scene resources, or loads a previously saved app state.
  62. void App::Load(Platform::String^ entryPoint)
  63. {
  64. if (m_main == nullptr)
  65. {
  66. m_main = std::unique_ptr<TemplateAppMain>(new TemplateAppMain());
  67. }
  68. }
  69. // This method is called after the window becomes active.
  70. void App::Run()
  71. {
  72. while (!m_windowClosed)
  73. {
  74. if (m_windowVisible)
  75. {
  76. CoreWindow::GetForCurrentThread()->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessAllIfPresent);
  77. auto commandQueue = GetDeviceResources()->GetCommandQueue();
  78. PIXBeginEvent(commandQueue, 0, L"Update");
  79. {
  80. m_main->Update();
  81. }
  82. PIXEndEvent(commandQueue);
  83. PIXBeginEvent(commandQueue, 0, L"Render");
  84. {
  85. if (m_main->Render())
  86. {
  87. GetDeviceResources()->Present();
  88. }
  89. }
  90. PIXEndEvent(commandQueue);
  91. }
  92. else
  93. {
  94. CoreWindow::GetForCurrentThread()->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessOneAndAllPending);
  95. }
  96. }
  97. }
  98. // Required for IFrameworkView.
  99. // Terminate events do not cause Uninitialize to be called. It will be called if your IFrameworkView
  100. // class is torn down while the app is in the foreground.
  101. void App::Uninitialize()
  102. {
  103. }
  104. // Application lifecycle event handlers.
  105. void App::OnActivated(CoreApplicationView^ applicationView, IActivatedEventArgs^ args)
  106. {
  107. // Run() won't start until the CoreWindow is activated.
  108. CoreWindow::GetForCurrentThread()->Activate();
  109. }
  110. void App::OnSuspending(Platform::Object^ sender, SuspendingEventArgs^ args)
  111. {
  112. // Save app state asynchronously after requesting a deferral. Holding a deferral
  113. // indicates that the application is busy performing suspending operations. Be
  114. // aware that a deferral may not be held indefinitely. After about five seconds,
  115. // the app will be forced to exit.
  116. SuspendingDeferral^ deferral = args->SuspendingOperation->GetDeferral();
  117. create_task([this, deferral]()
  118. {
  119. // TODO: Insert your code here.
  120. m_main->OnSuspending();
  121. deferral->Complete();
  122. });
  123. }
  124. void App::OnResuming(Platform::Object^ sender, Platform::Object^ args)
  125. {
  126. // Restore any data or state that was unloaded on suspend. By default, data
  127. // and state are persisted when resuming from suspend. Note that this event
  128. // does not occur if the app was previously terminated.
  129. // TODO: Insert your code here.
  130. m_main->OnResuming();
  131. }
  132. // Window event handlers.
  133. void App::OnWindowSizeChanged(CoreWindow^ sender, WindowSizeChangedEventArgs^ args)
  134. {
  135. GetDeviceResources()->SetLogicalSize(Size(sender->Bounds.Width, sender->Bounds.Height));
  136. m_main->OnWindowSizeChanged();
  137. }
  138. void App::OnVisibilityChanged(CoreWindow^ sender, VisibilityChangedEventArgs^ args)
  139. {
  140. m_windowVisible = args->Visible;
  141. }
  142. void App::OnWindowClosed(CoreWindow^ sender, CoreWindowEventArgs^ args)
  143. {
  144. m_windowClosed = true;
  145. }
  146. // DisplayInformation event handlers.
  147. void App::OnDpiChanged(DisplayInformation^ sender, Object^ args)
  148. {
  149. GetDeviceResources()->SetDpi(sender->LogicalDpi);
  150. m_main->OnWindowSizeChanged();
  151. }
  152. void App::OnOrientationChanged(DisplayInformation^ sender, Object^ args)
  153. {
  154. GetDeviceResources()->SetCurrentOrientation(sender->CurrentOrientation);
  155. m_main->OnWindowSizeChanged();
  156. }
  157. void App::OnDisplayContentsInvalidated(DisplayInformation^ sender, Object^ args)
  158. {
  159. GetDeviceResources()->ValidateDevice();
  160. }
  161. std::shared_ptr<DX::DeviceResources> App::GetDeviceResources()
  162. {
  163. if (m_deviceResources != nullptr && m_deviceResources->IsDeviceRemoved())
  164. {
  165. // All references to the existing D3D device must be released before a new device
  166. // can be created.
  167. m_deviceResources = nullptr;
  168. m_main->OnDeviceRemoved();
  169. }
  170. if (m_deviceResources == nullptr)
  171. {
  172. m_deviceResources = std::make_shared<DX::DeviceResources>();
  173. m_deviceResources->SetWindow(CoreWindow::GetForCurrentThread());
  174. m_main->CreateRenderers(m_deviceResources);
  175. }
  176. return m_deviceResources;
  177. }