#include "pch.h" #include "App.h" #include using namespace $safeprojectname$; using namespace concurrency; using namespace Windows::ApplicationModel; using namespace Windows::ApplicationModel::Core; using namespace Windows::ApplicationModel::Activation; using namespace Windows::UI::Core; using namespace Windows::UI::Input; using namespace Windows::System; using namespace Windows::Foundation; using namespace Windows::Graphics::Display; // 主函数仅用于初始化我们的 IFrameworkView 类。 [Platform::MTAThread] int main(Platform::Array^) { auto direct3DApplicationSource = ref new Direct3DApplicationSource(); CoreApplication::Run(direct3DApplicationSource); return 0; } IFrameworkView^ Direct3DApplicationSource::CreateView() { return ref new App(); } App::App() : m_windowClosed(false), m_windowVisible(true) { } // 创建 IFrameworkView 时调用的第一个方法。 void App::Initialize(CoreApplicationView^ applicationView) { // 注册应用程序生命周期的事件处理程序。此示例包括 Activated,因此我们 // 可激活 CoreWindow 并开始在窗口上渲染。 applicationView->Activated += ref new TypedEventHandler(this, &App::OnActivated); CoreApplication::Suspending += ref new EventHandler(this, &App::OnSuspending); CoreApplication::Resuming += ref new EventHandler(this, &App::OnResuming); // 此时,我们具有访问设备的权限。 // 我们可创建与设备相关的资源。 m_deviceResources = std::make_shared(); } // 创建(或重新创建) CoreWindow 对象时调用。 void App::SetWindow(CoreWindow^ window) { window->SizeChanged += ref new TypedEventHandler(this, &App::OnWindowSizeChanged); window->VisibilityChanged += ref new TypedEventHandler(this, &App::OnVisibilityChanged); window->Closed += ref new TypedEventHandler(this, &App::OnWindowClosed); DisplayInformation^ currentDisplayInformation = DisplayInformation::GetForCurrentView(); currentDisplayInformation->DpiChanged += ref new TypedEventHandler(this, &App::OnDpiChanged); currentDisplayInformation->OrientationChanged += ref new TypedEventHandler(this, &App::OnOrientationChanged); DisplayInformation::DisplayContentsInvalidated += ref new TypedEventHandler(this, &App::OnDisplayContentsInvalidated); m_deviceResources->SetWindow(window); } // 初始化场景资源或加载之前保存的应用程序状态。 void App::Load(Platform::String^ entryPoint) { if (m_main == nullptr) { m_main = std::unique_ptr<$safeprojectname$Main>(new $safeprojectname$Main(m_deviceResources)); } } // 将在窗口处于活动状态后调用此方法。 void App::Run() { while (!m_windowClosed) { if (m_windowVisible) { CoreWindow::GetForCurrentThread()->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessAllIfPresent); m_main->Update(); if (m_main->Render()) { m_deviceResources->Present(); } } else { CoreWindow::GetForCurrentThread()->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessOneAndAllPending); } } } // IFrameworkView 所必需的。 // 终止事件不会导致调用 Uninitialize。如果在应用程序在前台运行时销毁 IFrameworkView // 类,则将调用该方法。 void App::Uninitialize() { } // 应用程序生命周期事件处理程序。 void App::OnActivated(CoreApplicationView^ applicationView, IActivatedEventArgs^ args) { // Run() 在 CoreWindow 激活前将不会开始。 CoreWindow::GetForCurrentThread()->Activate(); } void App::OnSuspending(Platform::Object^ sender, SuspendingEventArgs^ args) { // 在请求延期后异步保存应用程序状态。保留延期 // 表示应用程序正忙于执行挂起操作。 // 请注意,延期不是无限期的。在大约五秒后, // 将强制应用程序退出。 SuspendingDeferral^ deferral = args->SuspendingOperation->GetDeferral(); create_task([this, deferral]() { m_deviceResources->Trim(); // 在此处插入代码。 deferral->Complete(); }); } void App::OnResuming(Platform::Object^ sender, Platform::Object^ args) { // 还原在挂起时卸载的任何数据或状态。默认情况下, // 在从挂起中恢复时,数据和状态会持续保留。请注意, // 如果之前已终止应用程序,则不会发生此事件。 // 在此处插入代码。 } // 窗口事件处理程序。 void App::OnWindowSizeChanged(CoreWindow^ sender, WindowSizeChangedEventArgs^ args) { m_deviceResources->SetLogicalSize(Size(sender->Bounds.Width, sender->Bounds.Height)); m_main->CreateWindowSizeDependentResources(); } void App::OnVisibilityChanged(CoreWindow^ sender, VisibilityChangedEventArgs^ args) { m_windowVisible = args->Visible; } void App::OnWindowClosed(CoreWindow^ sender, CoreWindowEventArgs^ args) { m_windowClosed = true; } // DisplayInformation 事件处理程序。 void App::OnDpiChanged(DisplayInformation^ sender, Object^ args) { // 注意: 在此处检索到的 LogicalDpi 值可能与应用的有效 DPI 不匹配 // 如果正在针对高分辨率设备对它进行缩放。在 DeviceResources 上设置 DPI 后, // 应始终使用 GetDpi 方法进行检索。 // 有关详细信息,请参阅 DeviceResources.cpp。 m_deviceResources->SetDpi(sender->LogicalDpi); m_main->CreateWindowSizeDependentResources(); } void App::OnOrientationChanged(DisplayInformation^ sender, Object^ args) { m_deviceResources->SetCurrentOrientation(sender->CurrentOrientation); m_main->CreateWindowSizeDependentResources(); } void App::OnDisplayContentsInvalidated(DisplayInformation^ sender, Object^ args) { m_deviceResources->ValidateDevice(); }