Files
LCUI/src/platform/windows/uwp_main.cpp
T

125 lines
4.3 KiB
C++

/* ***************************************************************************
* uwp_main.h -- UWP application main implementation
*
* Copyright (C) 2017 by Liu Chao <lc-soft@live.cn>
*
* This file is part of the LCUI project, and may only be used, modified,
* and distributed under the terms of the GPLv2.
*
* By continuing to use, modify, or distribute this file you indicate that you
* have read the license and understand and accept it fully.
*
* The LCUI project is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GPL v2 for more details.
*
* You should have received a copy of the GPLv2 along with this file. It is
* usually in the LICENSE.TXT file, If not, see <http://www.gnu.org/licenses/>.
* ****************************************************************************/
/* ****************************************************************************
* uwp_renderer.h -- UWP 应用的主要功能实现
*
* 版权所有 (C) 2017 归属于 刘超 <lc-soft@live.cn>
*
* 这个文件是 LCUI 项目的一部分,并且只可以根据GPLv2许可协议来使用、更改和
* 发布。
*
* 继续使用、修改或发布本文件,表明您已经阅读并完全理解和接受这个许可协议。
*
* LCUI 项目是基于使用目的而加以散布的,但不负任何担保责任,甚至没有适销
* 性或特定用途的隐含担保,详情请参照GPLv2许可协议。
*
* 您应已收到附随于本文件的GPLv2许可协议的副本,它通常在 LICENSE 文件中,如果
* 没有,请查看:<http://www.gnu.org/licenses/>.
* ****************************************************************************/
#include "pch.h"
#include "uwp_main.h"
#include "Common/DirectXHelper.h"
using namespace LCUICore;
using namespace Windows::Foundation;
using namespace Windows::System::Threading;
using namespace Concurrency;
// 加载应用程序时加载并初始化应用程序资产。
Main::Main(const std::shared_ptr<DX::DeviceResources>& deviceResources) :
m_deviceResources(deviceResources)
{
// 注册以在设备丢失或重新创建时收到通知
m_deviceResources->RegisterDeviceNotify(this);
m_renderer = std::unique_ptr<Renderer>(new Renderer(m_deviceResources));
// TODO: 如果需要默认的可变时间步长模式之外的其他模式,请更改计时器设置。
// 例如,对于 60 FPS 固定时间步长更新逻辑,请调用:
/*
m_timer.SetFixedTimeStep(true);
m_timer.SetTargetElapsedSeconds(1.0 / 60);
*/
}
Main::~Main()
{
// 取消注册设备通知
m_deviceResources->RegisterDeviceNotify(nullptr);
}
// 在窗口大小更改(例如,设备方向更改)时更新应用程序状态
void Main::CreateWindowSizeDependentResources()
{
m_renderer->CreateWindowSizeDependentResources();
}
// 每帧更新一次应用程序状态。
void Main::Update()
{
// 更新场景对象。
m_timer.Tick( [&]() {
m_renderer->Update( m_timer );
} );
}
// 根据当前应用程序状态呈现当前帧。
// 如果帧已呈现并且已准备好显示,则返回 true。
bool Main::Render()
{
// 在首次更新前,请勿尝试呈现任何内容。
if (m_timer.GetFrameCount() == 0)
{
return false;
}
auto context = m_deviceResources->GetD3DDeviceContext();
// 将视区重置为针对整个屏幕。
auto viewport = m_deviceResources->GetScreenViewport();
context->RSSetViewports(1, &viewport);
// 将呈现目标重置为屏幕。
ID3D11RenderTargetView *const targets[1] = { m_deviceResources->GetBackBufferRenderTargetView() };
context->OMSetRenderTargets(1, targets, m_deviceResources->GetDepthStencilView());
// 清除后台缓冲区和深度模具视图。
context->ClearRenderTargetView(m_deviceResources->GetBackBufferRenderTargetView(), DirectX::Colors::CornflowerBlue);
context->ClearDepthStencilView(m_deviceResources->GetDepthStencilView(), D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0f, 0);
// 呈现场景对象。
m_renderer->Render();
return true;
}
// 通知呈现器,需要释放设备资源。
void Main::OnDeviceLost()
{
m_renderer->ReleaseDeviceDependentResources();
}
// 通知呈现器,现在可重新创建设备资源。
void Main::OnDeviceRestored()
{
m_renderer->CreateDeviceDependentResources();
CreateWindowSizeDependentResources();
}