1
0
mirror of https://github.com/borgesdan/xn65 synced 2024-12-29 21:54:47 +01:00
xn65/framework/platform-dx/rendertarget.cpp
2024-08-05 15:42:56 -03:00

66 lines
1.7 KiB
C++

#include "xna/xna-dx.hpp"
namespace xna {
RenderTarget2D::RenderTarget2D() : Texture2D(nullptr) {
impl2 = unew<PlatformImplementation>();
}
RenderTarget2D::RenderTarget2D(sptr<GraphicsDevice> const& device) : Texture2D(device) {
impl2 = unew<PlatformImplementation>();
}
P_RenderTarget2D RenderTarget2D::FromBackBuffer(P_GraphicsDevice const& device) {
auto& swapChain = device->impl->_swapChain;
auto rt = snew<RenderTarget2D>(device);
auto& implementation = rt->impl;
auto& implementation2 = rt->impl2;
if (!swapChain->impl->GetBackBuffer(implementation->dxTexture2D))
{
Exception::Throw(Exception::FAILED_TO_CREATE);
}
rt->Initialize();
return rt;
}
void RenderTarget2D::Initialize() {
if (!impl || !m_device || !m_device->impl->_device) {
Exception::Throw(Exception::UNABLE_TO_INITIALIZE);
}
impl->dxDescription.Width = width;
impl->dxDescription.Height = height;
impl->dxDescription.BindFlags = D3D11_BIND_FLAG::D3D11_BIND_RENDER_TARGET;
Texture2D::Initialize();
auto& dxdevice = m_device->impl->_device;
const auto hr = dxdevice->CreateRenderTargetView(
impl->dxTexture2D.Get(),
NULL,
impl2->_renderTargetView.ReleaseAndGetAddressOf());
if (FAILED(hr)) {
Exception::Throw(Exception::FAILED_TO_CREATE);
}
impl2->_renderTargetView->GetDesc(&impl2->_renderTargetDesc);
}
void RenderTarget2D::Apply() {
if (!m_device || !m_device->impl->_context) {
Exception::Throw(Exception::FAILED_TO_APPLY);
}
if (!impl2->_renderTargetView)
{
Initialize();
}
auto& context = m_device->impl->_context;
context->OMSetRenderTargets(1, impl2->_renderTargetView.GetAddressOf(), nullptr);
}
}