Camera-Renderer/Camera-Renderer_cpp/CameraDirect_cppMain.cpp

142 lines
4.6 KiB
C++
Raw Permalink Normal View History

2021-10-05 00:17:24 +13:00
#include "pch.h"
#include "CameraDirect_cppMain.h"
#include "Common\DirectXHelper.h"
2021-10-05 00:27:45 +13:00
using namespace CameraRenderer_cpp;
2021-10-05 00:17:24 +13:00
using namespace Windows::Foundation;
using namespace Windows::System::Threading;
using namespace Concurrency;
// Loads and initializes application assets when the application is loaded.
2021-10-05 00:27:45 +13:00
CameraRenderer_cppMain::CameraRenderer_cppMain(const std::shared_ptr<DX::DeviceResources>& deviceResources) :
2021-10-05 00:17:24 +13:00
m_deviceResources(deviceResources), m_pointerLocationX(0.0f)
{
// Register to be notified if the Device is lost or recreated
m_deviceResources->RegisterDeviceNotify(this);
// TODO: Replace this with your app's content initialization.
m_sceneRenderer = std::unique_ptr<Sample3DSceneRenderer>(new Sample3DSceneRenderer(m_deviceResources));
m_fpsTextRenderer = std::unique_ptr<SampleFpsTextRenderer>(new SampleFpsTextRenderer(m_deviceResources));
// TODO: Change the timer settings if you want something other than the default variable timestep mode.
// e.g. for 60 FPS fixed timestep update logic, call:
/*
m_timer.SetFixedTimeStep(true);
m_timer.SetTargetElapsedSeconds(1.0 / 60);
*/
}
2021-10-05 00:27:45 +13:00
CameraRenderer_cppMain::~CameraRenderer_cppMain()
2021-10-05 00:17:24 +13:00
{
// Deregister device notification
m_deviceResources->RegisterDeviceNotify(nullptr);
}
// Updates application state when the window size changes (e.g. device orientation change)
2021-10-05 00:27:45 +13:00
void CameraRenderer_cppMain::CreateWindowSizeDependentResources()
2021-10-05 00:17:24 +13:00
{
// TODO: Replace this with the size-dependent initialization of your app's content.
m_sceneRenderer->CreateWindowSizeDependentResources();
}
2021-10-05 00:27:45 +13:00
void CameraRenderer_cppMain::StartRenderLoop()
2021-10-05 00:17:24 +13:00
{
// If the animation render loop is already running then do not start another thread.
if (m_renderLoopWorker != nullptr && m_renderLoopWorker->Status == AsyncStatus::Started)
{
return;
}
// Create a task that will be run on a background thread.
auto workItemHandler = ref new WorkItemHandler([this](IAsyncAction ^ action)
{
// Calculate the updated frame and render once per vertical blanking interval.
while (action->Status == AsyncStatus::Started)
{
critical_section::scoped_lock lock(m_criticalSection);
Update();
if (Render())
{
m_deviceResources->Present();
}
}
});
// Run task on a dedicated high priority background thread.
m_renderLoopWorker = ThreadPool::RunAsync(workItemHandler, WorkItemPriority::High, WorkItemOptions::TimeSliced);
}
2021-10-05 00:27:45 +13:00
void CameraRenderer_cppMain::StopRenderLoop()
2021-10-05 00:17:24 +13:00
{
m_renderLoopWorker->Cancel();
}
// Updates the application state once per frame.
2021-10-05 00:27:45 +13:00
void CameraRenderer_cppMain::Update()
2021-10-05 00:17:24 +13:00
{
ProcessInput();
// Update scene objects.
m_timer.Tick([&]()
{
// TODO: Replace this with your app's content update functions.
m_sceneRenderer->Update(m_timer);
m_fpsTextRenderer->Update(m_timer);
});
}
// Process all input from the user before updating game state
2021-10-05 00:27:45 +13:00
void CameraRenderer_cppMain::ProcessInput()
2021-10-05 00:17:24 +13:00
{
// TODO: Add per frame input handling here.
m_sceneRenderer->TrackingUpdate(m_pointerLocationX);
}
// Renders the current frame according to the current application state.
// Returns true if the frame was rendered and is ready to be displayed.
2021-10-05 00:27:45 +13:00
bool CameraRenderer_cppMain::Render()
2021-10-05 00:17:24 +13:00
{
// Don't try to render anything before the first Update.
if (m_timer.GetFrameCount() == 0)
{
return false;
}
auto context = m_deviceResources->GetD3DDeviceContext();
// Reset the viewport to target the whole screen.
auto viewport = m_deviceResources->GetScreenViewport();
context->RSSetViewports(1, &viewport);
// Reset render targets to the screen.
ID3D11RenderTargetView *const targets[1] = { m_deviceResources->GetBackBufferRenderTargetView() };
context->OMSetRenderTargets(1, targets, m_deviceResources->GetDepthStencilView());
// Clear the back buffer and depth stencil view.
context->ClearRenderTargetView(m_deviceResources->GetBackBufferRenderTargetView(), DirectX::Colors::CornflowerBlue);
context->ClearDepthStencilView(m_deviceResources->GetDepthStencilView(), D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0f, 0);
// Render the scene objects.
// TODO: Replace this with your app's content rendering functions.
m_sceneRenderer->Render();
m_fpsTextRenderer->Render();
return true;
}
// Notifies renderers that device resources need to be released.
2021-10-05 00:27:45 +13:00
void CameraRenderer_cppMain::OnDeviceLost()
2021-10-05 00:17:24 +13:00
{
m_sceneRenderer->ReleaseDeviceDependentResources();
m_fpsTextRenderer->ReleaseDeviceDependentResources();
}
// Notifies renderers that device resources may now be recreated.
2021-10-05 00:27:45 +13:00
void CameraRenderer_cppMain::OnDeviceRestored()
2021-10-05 00:17:24 +13:00
{
m_sceneRenderer->CreateDeviceDependentResources();
m_fpsTextRenderer->CreateDeviceDependentResources();
CreateWindowSizeDependentResources();
}