Direct3DApp1.cpp 4.4 KB

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