1
0
mirror of https://github.com/borgesdan/xn65 synced 2024-12-29 21:54:47 +01:00
xn65/framework/platform/device-dx.cpp
2024-04-23 16:11:17 -03:00

158 lines
4.6 KiB
C++
Raw Blame History

#include "device-dx.hpp"
#include "window-dx.hpp"
#include "swapchain-dx.hpp"
#include "rendertarget-dx.hpp"
#include "adapter-dx.hpp"
#include "blendstate-dx.hpp"
#include "gdeviceinfo-dx.hpp"
#include "../common/color.hpp"
#include "gdevicemanager-dx.hpp"
namespace xna {
GraphicsDevice::GraphicsDevice() {
_blendState = BlendState::NonPremultiplied();
_adapter = GraphicsAdapter::DefaultAdapter();
_adapter->CurrentDisplayMode(SurfaceFormat::Color, GraphicsDeviceManager::DefaultBackBufferWidth, GraphicsDeviceManager::DefaultBackBufferHeight);
}
GraphicsDevice::GraphicsDevice(GraphicsDeviceInformation const& info) {
_adapter = info.Adapter();
_presentParameters = info.PresentationParameters();
_adapter->CurrentDisplayMode(_presentParameters.BackBufferFormat, _presentParameters.BackBufferWidth, _presentParameters.BackBufferHeight);
}
bool GraphicsDevice::Initialize(GameWindow& gameWindow) {
if (_factory) {
_factory->Release();
_factory = nullptr;
}
auto hr = CreateDXGIFactory1(__uuidof(IDXGIFactory1), (void**)&_factory);
if (FAILED(hr)) {
return false;
}
if (_blendState == nullptr)
_blendState = BlendState::NonPremultiplied();
_createDeviceFlags |= D3D11_CREATE_DEVICE_DEBUG;
if (_device) {
_device->Release();
_device = nullptr;
}
if (_context) {
_context->Release();
_context = nullptr;
}
gameWindow.Size(_presentParameters.BackBufferWidth, _presentParameters.BackBufferHeight);
const auto bounds = gameWindow.ClientBounds();
_viewport = xna::Viewport(0.0F, 0.0F,
static_cast<float>(bounds.Width),
static_cast<float>(bounds.Height),
0.0F, 1.F);
if (!createDevice())
return false;
COLORREF color = gameWindow.Color();
_backgroundColor[0] = GetRValue(color) / 255.0f;
_backgroundColor[1] = GetGValue(color) / 255.0f;
_backgroundColor[2] = GetBValue(color) / 255.0f;
_backgroundColor[3] = 1.0f;
if (!_swapChain)
_swapChain = New<xna::SwapChain>();
_swapChain->Initialize(*this, gameWindow);
if FAILED(_factory->MakeWindowAssociation(gameWindow.WindowHandle(), DXGI_MWA_NO_ALT_ENTER))
return false;
if (!_renderTarget2D) {
_renderTarget2D = New<RenderTarget2D>();
}
if (!_renderTarget2D->Initialize(*this))
return false;
_renderTarget2D->Apply(*this);
D3D11_VIEWPORT view{};
view.TopLeftX = _viewport.X;
view.TopLeftY = _viewport.Y;
view.Width = _viewport.Width;
view.Height = _viewport.Height;
view.MinDepth = _viewport.MinDetph;
view.MaxDepth = _viewport.MaxDepth;
_context->RSSetViewports(1, &view);
_blendState->Apply(*this);
return true;
}
bool GraphicsDevice::Present() {
const auto result = _swapChain->Present(_usevsync);
_context->OMSetRenderTargets(1, &_renderTarget2D->_renderTargetView, nullptr);
return result;
}
bool GraphicsDevice::createDevice() {
#if _DEBUG
_createDeviceFlags |= D3D11_CREATE_DEVICE_FLAG::D3D11_CREATE_DEVICE_DEBUG;
#endif
if FAILED(
D3D11CreateDevice(
_adapter->dxadapter,
D3D_DRIVER_TYPE_UNKNOWN,
NULL,
_createDeviceFlags,
NULL,
0,
D3D11_SDK_VERSION,
&_device,
&_featureLevel,
&_context)) {
if FAILED(D3D11CreateDevice(
NULL,
D3D_DRIVER_TYPE_WARP,
NULL,
_createDeviceFlags,
NULL,
0,
D3D11_SDK_VERSION,
&_device,
&_featureLevel,
&_context))
return false;
OutputDebugString("---> Usando Adaptador WARP: n<>o h<> suporte ao D3D11\n");
}
return true;
}
void GraphicsDevice::Clear() {
_context->ClearRenderTargetView(_renderTarget2D->_renderTargetView, _backgroundColor);
}
void GraphicsDevice::Clear(Color const& color) {
const auto v4 = color.ToVector4();
_backgroundColor[0] = v4.X;
_backgroundColor[1] = v4.Y;
_backgroundColor[2] = v4.Z;
_backgroundColor[3] = v4.W;
_context->ClearRenderTargetView(_renderTarget2D->_renderTargetView, _backgroundColor);
}
}