Direct3DApp1.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #include "pch.h"
  2. #include "BasicTimer.h"
  3. #include "Direct3DApp1.h"
  4. using namespace Windows::ApplicationModel;
  5. using namespace Windows::ApplicationModel::Core;
  6. using namespace Windows::ApplicationModel::Activation;
  7. using namespace Windows::UI::Core;
  8. using namespace Windows::System;
  9. using namespace Windows::Foundation;
  10. using namespace Windows::Graphics::Display;
  11. using namespace concurrency;
  12. Direct3DApp1::Direct3DApp1()
  13. : m_windowClosed(false)
  14. , m_windowVisible(true)
  15. {
  16. }
  17. void Direct3DApp1::Initialize(CoreApplicationView ^ applicationView)
  18. {
  19. applicationView->Activated +=
  20. ref new TypedEventHandler<CoreApplicationView ^, IActivatedEventArgs ^>(
  21. this, &Direct3DApp1::OnActivated);
  22. CoreApplication::Suspending += ref new EventHandler<SuspendingEventArgs ^>(
  23. this, &Direct3DApp1::OnSuspending);
  24. CoreApplication::Resuming +=
  25. ref new EventHandler<Platform::Object ^>(this, &Direct3DApp1::OnResuming);
  26. m_renderer = ref new CubeRenderer();
  27. }
  28. void Direct3DApp1::SetWindow(CoreWindow ^ window)
  29. {
  30. window->SizeChanged +=
  31. ref new TypedEventHandler<CoreWindow ^, WindowSizeChangedEventArgs ^>(
  32. this, &Direct3DApp1::OnWindowSizeChanged);
  33. window->VisibilityChanged +=
  34. ref new TypedEventHandler<CoreWindow ^, VisibilityChangedEventArgs ^>(
  35. this, &Direct3DApp1::OnVisibilityChanged);
  36. window->Closed +=
  37. ref new TypedEventHandler<CoreWindow ^, CoreWindowEventArgs ^>(
  38. this, &Direct3DApp1::OnWindowClosed);
  39. #ifndef PHONE
  40. window->PointerCursor = ref new CoreCursor(CoreCursorType::Arrow, 0);
  41. #endif
  42. window->PointerPressed +=
  43. ref new TypedEventHandler<CoreWindow ^, PointerEventArgs ^>(
  44. this, &Direct3DApp1::OnPointerPressed);
  45. window->PointerMoved +=
  46. ref new TypedEventHandler<CoreWindow ^, PointerEventArgs ^>(
  47. this, &Direct3DApp1::OnPointerMoved);
  48. m_renderer->Initialize(CoreWindow::GetForCurrentThread());
  49. }
  50. void Direct3DApp1::Load(Platform::String ^ entryPoint)
  51. {
  52. }
  53. void Direct3DApp1::Run()
  54. {
  55. BasicTimer ^ timer = ref new BasicTimer();
  56. while (!m_windowClosed) {
  57. if (m_windowVisible) {
  58. timer->Update();
  59. CoreWindow::GetForCurrentThread()->Dispatcher->ProcessEvents(
  60. CoreProcessEventsOption::ProcessAllIfPresent);
  61. m_renderer->Update(timer->Total, timer->Delta);
  62. m_renderer->Render();
  63. m_renderer
  64. ->Present(); // This call is synchronized to the display frame rate.
  65. } else {
  66. CoreWindow::GetForCurrentThread()->Dispatcher->ProcessEvents(
  67. CoreProcessEventsOption::ProcessOneAndAllPending);
  68. }
  69. }
  70. }
  71. void Direct3DApp1::Uninitialize()
  72. {
  73. }
  74. void Direct3DApp1::OnWindowSizeChanged(CoreWindow ^ sender,
  75. WindowSizeChangedEventArgs ^ args)
  76. {
  77. m_renderer->UpdateForWindowSizeChange();
  78. }
  79. void Direct3DApp1::OnVisibilityChanged(CoreWindow ^ sender,
  80. VisibilityChangedEventArgs ^ args)
  81. {
  82. m_windowVisible = args->Visible;
  83. }
  84. void Direct3DApp1::OnWindowClosed(CoreWindow ^ sender,
  85. CoreWindowEventArgs ^ args)
  86. {
  87. m_windowClosed = true;
  88. }
  89. void Direct3DApp1::OnPointerPressed(CoreWindow ^ sender,
  90. PointerEventArgs ^ args)
  91. {
  92. // Insert your code here.
  93. }
  94. void Direct3DApp1::OnPointerMoved(CoreWindow ^ sender, PointerEventArgs ^ args)
  95. {
  96. // Insert your code here.
  97. }
  98. void Direct3DApp1::OnActivated(CoreApplicationView ^ applicationView,
  99. IActivatedEventArgs ^ args)
  100. {
  101. CoreWindow::GetForCurrentThread()->Activate();
  102. }
  103. void Direct3DApp1::OnSuspending(Platform::Object ^ sender,
  104. SuspendingEventArgs ^ args)
  105. {
  106. // Save app state asynchronously after requesting a deferral. Holding a
  107. // deferral
  108. // indicates that the application is busy performing suspending operations.
  109. // Be
  110. // aware that a deferral may not be held indefinitely. After about five
  111. // seconds,
  112. // the app will be forced to exit.
  113. SuspendingDeferral ^ deferral = args->SuspendingOperation->GetDeferral();
  114. m_renderer->ReleaseResourcesForSuspending();
  115. create_task([this, deferral]() {
  116. // Insert your code here.
  117. deferral->Complete();
  118. });
  119. }
  120. void Direct3DApp1::OnResuming(Platform::Object ^ sender,
  121. Platform::Object ^ args)
  122. {
  123. // Restore any data or state that was unloaded on suspend. By default, data
  124. // and state are persisted when resuming from suspend. Note that this event
  125. // does not occur if the app was previously terminated.
  126. m_renderer->CreateWindowSizeDependentResources();
  127. }
  128. IFrameworkView ^ Direct3DApplicationSource::CreateView()
  129. {
  130. return ref new Direct3DApp1();
  131. }
  132. [Platform::MTAThread] int main(Platform::Array<Platform::String ^> ^)
  133. {
  134. auto direct3DApplicationSource = ref new Direct3DApplicationSource();
  135. CoreApplication::Run(direct3DApplicationSource);
  136. return 0;
  137. }