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