| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- #include "pch.h"
- #include "App.h"
- #include <ppltasks.h>
- using namespace TemplateApp;
- 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;
- // The DirectX 12 Application template is documented at http://go.microsoft.com/fwlink/?LinkID=613670&clcid=0x409
- // The main function is only used to initialize our IFrameworkView class.
- [Platform::MTAThread]
- int main(Platform::Array<Platform::String^>^)
- {
- 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)
- {
- }
- // The first method called when the IFrameworkView is being created.
- void App::Initialize(CoreApplicationView^ applicationView)
- {
- // Register event handlers for app lifecycle. This example includes Activated, so that we
- // can make the CoreWindow active and start rendering on the window.
- applicationView->Activated +=
- ref new TypedEventHandler<CoreApplicationView^, IActivatedEventArgs^>(this, &App::OnActivated);
- CoreApplication::Suspending +=
- ref new EventHandler<SuspendingEventArgs^>(this, &App::OnSuspending);
- CoreApplication::Resuming +=
- ref new EventHandler<Platform::Object^>(this, &App::OnResuming);
- }
- // Called when the CoreWindow object is created (or re-created).
- void App::SetWindow(CoreWindow^ window)
- {
- window->SizeChanged +=
- ref new TypedEventHandler<CoreWindow^, WindowSizeChangedEventArgs^>(this, &App::OnWindowSizeChanged);
- window->VisibilityChanged +=
- ref new TypedEventHandler<CoreWindow^, VisibilityChangedEventArgs^>(this, &App::OnVisibilityChanged);
- window->Closed +=
- ref new TypedEventHandler<CoreWindow^, CoreWindowEventArgs^>(this, &App::OnWindowClosed);
- DisplayInformation^ currentDisplayInformation = DisplayInformation::GetForCurrentView();
- currentDisplayInformation->DpiChanged +=
- ref new TypedEventHandler<DisplayInformation^, Object^>(this, &App::OnDpiChanged);
- currentDisplayInformation->OrientationChanged +=
- ref new TypedEventHandler<DisplayInformation^, Object^>(this, &App::OnOrientationChanged);
- DisplayInformation::DisplayContentsInvalidated +=
- ref new TypedEventHandler<DisplayInformation^, Object^>(this, &App::OnDisplayContentsInvalidated);
- }
- // Initializes scene resources, or loads a previously saved app state.
- void App::Load(Platform::String^ entryPoint)
- {
- if (m_main == nullptr)
- {
- m_main = std::unique_ptr<TemplateAppMain>(new TemplateAppMain());
- }
- }
- // This method is called after the window becomes active.
- void App::Run()
- {
- while (!m_windowClosed)
- {
- if (m_windowVisible)
- {
- CoreWindow::GetForCurrentThread()->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessAllIfPresent);
- auto commandQueue = GetDeviceResources()->GetCommandQueue();
- PIXBeginEvent(commandQueue, 0, L"Update");
- {
- m_main->Update();
- }
- PIXEndEvent(commandQueue);
- PIXBeginEvent(commandQueue, 0, L"Render");
- {
- if (m_main->Render())
- {
- GetDeviceResources()->Present();
- }
- }
- PIXEndEvent(commandQueue);
- }
- else
- {
- CoreWindow::GetForCurrentThread()->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessOneAndAllPending);
- }
- }
- }
- // Required for IFrameworkView.
- // Terminate events do not cause Uninitialize to be called. It will be called if your IFrameworkView
- // class is torn down while the app is in the foreground.
- void App::Uninitialize()
- {
- }
- // Application lifecycle event handlers.
- void App::OnActivated(CoreApplicationView^ applicationView, IActivatedEventArgs^ args)
- {
- // Run() won't start until the CoreWindow is activated.
- CoreWindow::GetForCurrentThread()->Activate();
- }
- void App::OnSuspending(Platform::Object^ sender, SuspendingEventArgs^ args)
- {
- // Save app state asynchronously after requesting a deferral. Holding a deferral
- // indicates that the application is busy performing suspending operations. Be
- // aware that a deferral may not be held indefinitely. After about five seconds,
- // the app will be forced to exit.
- SuspendingDeferral^ deferral = args->SuspendingOperation->GetDeferral();
- create_task([this, deferral]()
- {
- // TODO: Insert your code here.
- m_main->OnSuspending();
- deferral->Complete();
- });
- }
- void App::OnResuming(Platform::Object^ sender, Platform::Object^ args)
- {
- // Restore any data or state that was unloaded on suspend. By default, data
- // and state are persisted when resuming from suspend. Note that this event
- // does not occur if the app was previously terminated.
- // TODO: Insert your code here.
- m_main->OnResuming();
- }
- // Window event handlers.
- void App::OnWindowSizeChanged(CoreWindow^ sender, WindowSizeChangedEventArgs^ args)
- {
- GetDeviceResources()->SetLogicalSize(Size(sender->Bounds.Width, sender->Bounds.Height));
- m_main->OnWindowSizeChanged();
- }
- void App::OnVisibilityChanged(CoreWindow^ sender, VisibilityChangedEventArgs^ args)
- {
- m_windowVisible = args->Visible;
- }
- void App::OnWindowClosed(CoreWindow^ sender, CoreWindowEventArgs^ args)
- {
- m_windowClosed = true;
- }
- // DisplayInformation event handlers.
- void App::OnDpiChanged(DisplayInformation^ sender, Object^ args)
- {
- GetDeviceResources()->SetDpi(sender->LogicalDpi);
- m_main->OnWindowSizeChanged();
- }
- void App::OnOrientationChanged(DisplayInformation^ sender, Object^ args)
- {
- GetDeviceResources()->SetCurrentOrientation(sender->CurrentOrientation);
- m_main->OnWindowSizeChanged();
- }
- void App::OnDisplayContentsInvalidated(DisplayInformation^ sender, Object^ args)
- {
- GetDeviceResources()->ValidateDevice();
- }
- std::shared_ptr<DX::DeviceResources> App::GetDeviceResources()
- {
- if (m_deviceResources != nullptr && m_deviceResources->IsDeviceRemoved())
- {
- // All references to the existing D3D device must be released before a new device
- // can be created.
- m_deviceResources = nullptr;
- m_main->OnDeviceRemoved();
- }
- if (m_deviceResources == nullptr)
- {
- m_deviceResources = std::make_shared<DX::DeviceResources>();
- m_deviceResources->SetWindow(CoreWindow::GetForCurrentThread());
- m_main->CreateRenderers(m_deviceResources);
- }
- return m_deviceResources;
- }
|