mirror of
https://github.com/borgesdan/xn65
synced 2024-12-29 21:54:47 +01:00
commit
01f45a7dfc
@ -22,47 +22,61 @@ namespace xna {
|
||||
}
|
||||
|
||||
void GraphicsDevice::Initialize() {
|
||||
auto _this = shared_from_this();
|
||||
|
||||
reset(*impl);
|
||||
createDevice(*impl, *adapter);
|
||||
|
||||
auto hr = CreateDXGIFactory1(__uuidof(IDXGIFactory1), (void**)&impl->_factory);
|
||||
reset(*impl);
|
||||
|
||||
if FAILED(hr)
|
||||
Exception::Throw(Exception::FAILED_TO_CREATE);
|
||||
|
||||
viewport = xna::Viewport(0.0F, 0.0F,
|
||||
presentationParameters->BackBufferWidth,
|
||||
presentationParameters->BackBufferHeight,
|
||||
0.0F, 1.F);
|
||||
auto _this = shared_from_this();
|
||||
|
||||
createDevice(*impl, *adapter);
|
||||
|
||||
//
|
||||
// Background
|
||||
//
|
||||
|
||||
const auto backColor = Colors::CornflowerBlue;
|
||||
const auto backColorV3 = backColor.ToVector3();
|
||||
|
||||
impl->_backgroundColor[0] = backColorV3.X;
|
||||
impl->_backgroundColor[1] = backColorV3.Y;
|
||||
impl->_backgroundColor[2] = backColorV3.Z;
|
||||
impl->_backgroundColor[3] = 1.0f;
|
||||
impl->_backgroundColor[3] = 1.0f;
|
||||
|
||||
impl->_swapChain = snew<xna::SwapChain>(_this);
|
||||
impl->_swapChain->Initialize();
|
||||
//
|
||||
// Window Association
|
||||
//
|
||||
|
||||
auto hr = CreateDXGIFactory1(__uuidof(IDXGIFactory1), (void**)&impl->_factory);
|
||||
|
||||
if FAILED(hr)
|
||||
Exception::Throw(Exception::FAILED_TO_CREATE);
|
||||
|
||||
auto hwnd = reinterpret_cast<HWND>(presentationParameters->DeviceWindowHandle);
|
||||
hr = impl->_factory->MakeWindowAssociation(hwnd, DXGI_MWA_NO_ALT_ENTER);
|
||||
|
||||
if (FAILED(hr))
|
||||
Exception::Throw(Exception::FAILED_TO_MAKE_WINDOW_ASSOCIATION);
|
||||
|
||||
impl->_renderTarget2D = snew<RenderTarget2D>(_this);
|
||||
impl->_renderTarget2D->Initialize();
|
||||
impl->_renderTarget2D->Apply();
|
||||
|
||||
//
|
||||
// Viewport
|
||||
//
|
||||
|
||||
viewport = xna::Viewport(0.0F, 0.0F,
|
||||
presentationParameters->BackBufferWidth,
|
||||
presentationParameters->BackBufferHeight,
|
||||
0.0F, 1.F);
|
||||
|
||||
D3D11_VIEWPORT view = DxHelpers::ViewportToDx(viewport);
|
||||
impl->_context->RSSetViewports(1, &view);
|
||||
|
||||
//
|
||||
// States
|
||||
//
|
||||
|
||||
initAndApplyState(blendState, rasterizerState, depthStencilState, samplerStateCollection, _this);
|
||||
|
||||
//
|
||||
// Presentation
|
||||
//
|
||||
|
||||
const auto currentPresenInterval = presentationParameters->PresentationInterval;
|
||||
|
||||
switch (currentPresenInterval)
|
||||
@ -79,10 +93,27 @@ namespace xna {
|
||||
impl->vSyncValue = 1;
|
||||
break;
|
||||
}
|
||||
|
||||
impl->_swapChain = snew<xna::SwapChain>(_this);
|
||||
impl->_swapChain->Initialize();
|
||||
|
||||
//
|
||||
//Render Target
|
||||
//
|
||||
|
||||
if (renderTarget) {
|
||||
renderTarget->Initialize();
|
||||
impl->_renderTarget2D = renderTarget;
|
||||
}
|
||||
else {
|
||||
impl->_renderTarget2D = RenderTarget2D::FromBackBuffer(_this);
|
||||
}
|
||||
|
||||
const auto& renderView = impl->_renderTarget2D->impl2->_renderTargetView;
|
||||
impl->_context->OMSetRenderTargets(1, renderView.GetAddressOf(), nullptr);
|
||||
}
|
||||
|
||||
bool GraphicsDevice::Present() const {
|
||||
const auto currentPresenInterval = presentationParameters->PresentationInterval;
|
||||
bool result = impl->_swapChain->Present(impl->vSyncValue != 0);
|
||||
|
||||
impl->_context->OMSetRenderTargets(
|
||||
|
@ -1,49 +1,55 @@
|
||||
#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>();
|
||||
}
|
||||
|
||||
void RenderTarget2D::Initialize() {
|
||||
if (!impl || !m_device || !m_device->impl->_device) {
|
||||
Exception::Throw(Exception::UNABLE_TO_INITIALIZE);
|
||||
}
|
||||
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;
|
||||
|
||||
auto& swapChain = m_device->impl->_swapChain;
|
||||
|
||||
if (!swapChain->impl->GetBackBuffer(impl->dxTexture2D))
|
||||
if (!swapChain->impl->GetBackBuffer(implementation->dxTexture2D))
|
||||
{
|
||||
Exception::Throw(Exception::FAILED_TO_CREATE);
|
||||
}
|
||||
|
||||
impl->dxTexture2D->GetDesc(&impl->dxDescription);
|
||||
rt->Initialize();
|
||||
|
||||
return rt;
|
||||
}
|
||||
|
||||
void RenderTarget2D::Initialize() {
|
||||
if (!impl || !m_device || !m_device->impl->_device) {
|
||||
Exception::Throw(Exception::UNABLE_TO_INITIALIZE);
|
||||
}
|
||||
|
||||
if (impl2->_renderTargetView)
|
||||
return;
|
||||
|
||||
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());
|
||||
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);
|
||||
|
||||
//depthStencilFormat = DepthFormat::None;
|
||||
multiSampleCount = impl->dxDescription.SampleDesc.Count;
|
||||
//targetUsage = RenderTargetUsage::DiscardContent;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
@ -4,47 +4,16 @@ namespace xna {
|
||||
static void setDefaultTexture2DDesc(Texture2D::PlatformImplementation& impl);
|
||||
static HRESULT internalTexture2DSetData(Texture2D::PlatformImplementation& impl, GraphicsDevice& device, UINT const* data);
|
||||
|
||||
void Texture2D::Initialize()
|
||||
{
|
||||
if (!m_device || !m_device->impl->_device) {
|
||||
Exception::Throw(Exception::UNABLE_TO_INITIALIZE);
|
||||
}
|
||||
|
||||
auto hr = m_device->impl->_device->CreateTexture2D(&impl->dxDescription, nullptr, impl->dxTexture2D.ReleaseAndGetAddressOf());
|
||||
|
||||
if (FAILED(hr)) {
|
||||
Exception::Throw(Exception::FAILED_TO_CREATE);
|
||||
}
|
||||
|
||||
comptr<ID3D11Resource> resource = nullptr;
|
||||
hr = impl->dxTexture2D->QueryInterface(IID_ID3D11Resource, (void**)resource.GetAddressOf());
|
||||
|
||||
if (FAILED(hr)) {
|
||||
Exception::Throw(Exception::INVALID_OPERATION);
|
||||
}
|
||||
|
||||
hr = m_device->impl->_device->CreateShaderResourceView(resource.Get(), &impl->dxShaderDescription, &impl->dxShaderResource);
|
||||
|
||||
if (FAILED(hr)) {
|
||||
Exception::Throw(Exception::FAILED_TO_CREATE);
|
||||
}
|
||||
|
||||
surfaceFormat = DxHelpers::SurfaceFormatToXna(impl->dxDescription.Format);
|
||||
levelCount = static_cast<Int>(impl->dxShaderDescription.Texture2D.MipLevels);
|
||||
width = static_cast<Int>(impl->dxDescription.Width);
|
||||
height = static_cast<Int>(impl->dxDescription.Height);
|
||||
}
|
||||
|
||||
Texture2D::Texture2D() : Texture(nullptr) {
|
||||
impl = unew<PlatformImplementation>();
|
||||
setDefaultTexture2DDesc(*impl);
|
||||
}
|
||||
|
||||
Texture2D::Texture2D(sptr<GraphicsDevice> const& device, size_t width, size_t height) : Texture(device) {
|
||||
Texture2D::Texture2D(sptr<GraphicsDevice> const& device, size_t width, size_t height) : Texture(device), width(width), height(height) {
|
||||
impl = unew<PlatformImplementation>();
|
||||
setDefaultTexture2DDesc(*impl);
|
||||
impl->dxDescription.Width = static_cast<UINT>(width);
|
||||
impl->dxDescription.Height = static_cast<UINT>(height);
|
||||
impl->dxDescription.Width = static_cast<UINT>(this->width);
|
||||
impl->dxDescription.Height = static_cast<UINT>(this->height);
|
||||
}
|
||||
|
||||
Texture2D::Texture2D(sptr<GraphicsDevice> const& device) : Texture(device) {
|
||||
@ -52,15 +21,63 @@ namespace xna {
|
||||
setDefaultTexture2DDesc(*impl);
|
||||
}
|
||||
|
||||
Texture2D::Texture2D(sptr<GraphicsDevice> const& device, size_t width, size_t height, size_t mipMap, SurfaceFormat format) : Texture(device)
|
||||
{
|
||||
Texture2D::Texture2D(sptr<GraphicsDevice> const& device, size_t width, size_t height, size_t mipMap, SurfaceFormat format)
|
||||
: Texture(device), width(width), height(height), levelCount(mipMap) {
|
||||
impl = unew<PlatformImplementation>();
|
||||
setDefaultTexture2DDesc(*impl);
|
||||
impl->dxDescription.Width = static_cast<UINT>(width);
|
||||
impl->dxDescription.Height = static_cast<UINT>(height);
|
||||
impl->dxDescription.MipLevels = static_cast<UINT>(mipMap);
|
||||
impl->dxDescription.Width = static_cast<UINT>(this->width);
|
||||
impl->dxDescription.Height = static_cast<UINT>(this->height);
|
||||
impl->dxDescription.MipLevels = static_cast<UINT>(this->levelCount);
|
||||
impl->dxDescription.Format = DxHelpers::SurfaceFormatToDx(format);
|
||||
}
|
||||
}
|
||||
|
||||
void Texture2D::Initialize()
|
||||
{
|
||||
if (!m_device || !m_device->impl->_device) {
|
||||
Exception::Throw(Exception::UNABLE_TO_INITIALIZE);
|
||||
}
|
||||
|
||||
auto& deviceImpl = m_device->impl;
|
||||
|
||||
HRESULT hr = 0;
|
||||
|
||||
if (!impl->dxTexture2D) {
|
||||
hr = deviceImpl->_device->CreateTexture2D(
|
||||
&impl->dxDescription,
|
||||
nullptr,
|
||||
impl->dxTexture2D.ReleaseAndGetAddressOf());
|
||||
|
||||
if FAILED(hr)
|
||||
Exception::Throw(Exception::FAILED_TO_CREATE);
|
||||
}
|
||||
else {
|
||||
//Updates description if texture is not null
|
||||
impl->dxTexture2D->GetDesc(&impl->dxDescription);
|
||||
}
|
||||
|
||||
comptr<ID3D11Resource> resource = nullptr;
|
||||
hr = impl->dxTexture2D->QueryInterface(IID_ID3D11Resource, (void**)resource.GetAddressOf());
|
||||
|
||||
if FAILED(hr)
|
||||
Exception::Throw(Exception::INVALID_OPERATION);
|
||||
|
||||
//Only initializes if it is a ShaderResource
|
||||
if (impl->dxDescription.BindFlags & D3D11_BIND_SHADER_RESOURCE) {
|
||||
hr = deviceImpl->_device->CreateShaderResourceView(
|
||||
resource.Get(),
|
||||
&impl->dxShaderDescription,
|
||||
impl->dxShaderResource.ReleaseAndGetAddressOf());
|
||||
|
||||
if FAILED(hr)
|
||||
Exception::Throw(Exception::FAILED_TO_CREATE);
|
||||
|
||||
}
|
||||
|
||||
surfaceFormat = DxHelpers::SurfaceFormatToXna(impl->dxDescription.Format);
|
||||
levelCount = static_cast<Int>(impl->dxShaderDescription.Texture2D.MipLevels);
|
||||
width = static_cast<Int>(impl->dxDescription.Width);
|
||||
height = static_cast<Int>(impl->dxDescription.Height);
|
||||
}
|
||||
|
||||
void Texture2D::SetData(std::vector<Uint> const& data, size_t startIndex, size_t elementCount)
|
||||
{
|
||||
|
@ -207,6 +207,7 @@ namespace xna {
|
||||
using P_FileStream = sptr<FileStream>;
|
||||
using P_Texture = sptr<Texture>;
|
||||
using P_Texture2D = sptr<Texture2D>;
|
||||
using P_RenderTarget2D = sptr<RenderTarget2D>;
|
||||
}
|
||||
|
||||
|
||||
|
@ -45,7 +45,9 @@ namespace xna {
|
||||
constexpr xna::Viewport Viewport() const { return viewport; }
|
||||
//Gets or sets a viewport identifying the portion of the render target to receive draw calls.
|
||||
void Viewport(xna::Viewport const& viewport);
|
||||
|
||||
//Sets a new render target for this GraphicsDevice.
|
||||
void SetRenderTarget(P_RenderTarget2D const& renderTarget) { this->renderTarget = renderTarget; }
|
||||
|
||||
void Initialize();
|
||||
|
||||
private:
|
||||
@ -55,6 +57,7 @@ namespace xna {
|
||||
P_RasterizerState rasterizerState{ nullptr };
|
||||
P_SamplerStateCollection samplerStateCollection{ nullptr };
|
||||
P_PresentationParameters presentationParameters{ nullptr };
|
||||
P_RenderTarget2D renderTarget{ nullptr };
|
||||
GraphicsProfile graphicsProfile{ GraphicsProfile::HiDef };
|
||||
xna::Viewport viewport{};
|
||||
|
||||
|
@ -4,17 +4,24 @@
|
||||
#include "../default.hpp"
|
||||
|
||||
namespace xna {
|
||||
//Contains presentation parameters.
|
||||
struct PresentationParameters {
|
||||
constexpr PresentationParameters() = default;
|
||||
|
||||
//Gets or sets a value indicating the width of the new swap chain's back buffer.
|
||||
Int BackBufferWidth{ 0 };
|
||||
//Gets or sets a value indicating the height of the new swap chain's back buffer.
|
||||
Int BackBufferHeight{ 0 };
|
||||
//Gets or sets the format of the back buffer.
|
||||
SurfaceFormat BackBufferFormat{ SurfaceFormat::Color };
|
||||
SwapEffect PresentationSwapEffect{ SwapEffect::FlipDiscard };
|
||||
//Gets or sets the handle to the device window.
|
||||
intptr_t DeviceWindowHandle{ 0 };
|
||||
//Gets or sets a value indicating whether the application is in full screen mode.
|
||||
bool IsFullscreen{ false };
|
||||
//Gets or sets a value indicating the number of sample locations during multisampling.
|
||||
Int MultiSampleCount{ 0 };
|
||||
//Gets or sets the maximum rate at which the swap chain's back buffers can be presented to the front buffer.
|
||||
PresentInterval PresentationInterval{ PresentInterval::Default };
|
||||
//Gets or sets the depth stencil data format.
|
||||
DepthFormat DepthStencilFormat{ DepthFormat::None };
|
||||
};
|
||||
}
|
||||
|
@ -5,25 +5,15 @@
|
||||
#include "texture.hpp"
|
||||
|
||||
namespace xna {
|
||||
//Contains a 2D texture that can be used as a render target.
|
||||
class RenderTarget2D : public Texture2D {
|
||||
public:
|
||||
RenderTarget2D();
|
||||
RenderTarget2D(sptr<GraphicsDevice> const& device);
|
||||
|
||||
//Gets the data format for the depth stencil data.
|
||||
constexpr DepthFormat DepthStencilFormat() const { return depthStencilFormat; }
|
||||
//Gets the number of sample locations during multisampling.
|
||||
constexpr Int MultiSampleCount() const { return multiSampleCount; }
|
||||
//Gets or sets the render target usage.
|
||||
constexpr RenderTargetUsage TargetUsage() const { return targetUsage; }
|
||||
RenderTarget2D(sptr<GraphicsDevice> const& device);
|
||||
|
||||
static P_RenderTarget2D FromBackBuffer(P_GraphicsDevice const& device);
|
||||
|
||||
void Initialize();
|
||||
void Apply();
|
||||
private:
|
||||
DepthFormat depthStencilFormat{ DepthFormat::None };
|
||||
Int multiSampleCount{ 0 };
|
||||
RenderTargetUsage targetUsage{ RenderTargetUsage::DiscardContents };
|
||||
|
||||
public:
|
||||
struct PlatformImplementation;
|
||||
uptr<PlatformImplementation> impl2 = nullptr;
|
||||
|
@ -6,70 +6,74 @@
|
||||
#include "../default.hpp"
|
||||
|
||||
namespace xna {
|
||||
//Describes a custom vertex format structure that contains position and color information.
|
||||
struct VertexPositionColor {
|
||||
Vector3 position{};
|
||||
Color color{};
|
||||
Vector3 Position{};
|
||||
xna::Color Color{};
|
||||
|
||||
constexpr VertexPositionColor() = default;
|
||||
constexpr VertexPositionColor(Vector3 const& position, Color const& color):
|
||||
position(position), color(color){}
|
||||
constexpr VertexPositionColor(Vector3 const& position, xna::Color const& color):
|
||||
Position(position), Color(color){}
|
||||
|
||||
constexpr bool operator==(const VertexPositionColor& other) const {
|
||||
return position == other.position && color == other.color;
|
||||
return Position == other.Position && Color == other.Color;
|
||||
}
|
||||
};
|
||||
|
||||
//Describes a custom vertex format structure that contains position and one set of texture coordinates.
|
||||
struct VertexPositionTexture {
|
||||
Vector3 position{};
|
||||
Vector2 textureCoordinate{};
|
||||
Vector3 Position{};
|
||||
Vector2 TextureCoordinate{};
|
||||
|
||||
constexpr VertexPositionTexture() = default;
|
||||
|
||||
constexpr bool operator==(const VertexPositionTexture& other) const {
|
||||
return position == other.position
|
||||
&& textureCoordinate == other.textureCoordinate;
|
||||
return Position == other.Position
|
||||
&& TextureCoordinate == other.TextureCoordinate;
|
||||
}
|
||||
|
||||
constexpr VertexPositionTexture(const Vector3& position, const Vector2& textureCoordinate)
|
||||
: position(position), textureCoordinate(textureCoordinate)
|
||||
: Position(position), TextureCoordinate(textureCoordinate)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
//Describes a custom vertex format structure that contains position, color, and one set of texture coordinates.
|
||||
struct VertexPositionColorTexture {
|
||||
Vector3 position{};
|
||||
Vector2 textureCoodinate{};
|
||||
Color color{};
|
||||
Vector3 Position{};
|
||||
Vector2 TextureCoodinate{};
|
||||
xna::Color Color{};
|
||||
|
||||
constexpr VertexPositionColorTexture() = default;
|
||||
|
||||
constexpr bool operator==(const VertexPositionColorTexture& other) const {
|
||||
return position == other.position
|
||||
&& textureCoodinate == other.textureCoodinate
|
||||
&& color == other.color;
|
||||
return Position == other.Position
|
||||
&& TextureCoodinate == other.TextureCoodinate
|
||||
&& Color == other.Color;
|
||||
}
|
||||
|
||||
constexpr VertexPositionColorTexture(const Vector3& position, const Vector2& textureCoodinate, const Color& color)
|
||||
: position(position), textureCoodinate(textureCoodinate), color(color)
|
||||
constexpr VertexPositionColorTexture(const Vector3& position, const Vector2& textureCoodinate, const xna::Color& color)
|
||||
: Position(position), TextureCoodinate(textureCoodinate), Color(color)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
//Describes a custom vertex format structure that contains position, normal data, and one set of texture coordinates.
|
||||
struct VertexPositionNormalTexture {
|
||||
Vector3 position{};
|
||||
Vector3 normal{};
|
||||
Vector2 textureCoodinate{};
|
||||
Vector3 Position{};
|
||||
Vector3 Normal{};
|
||||
Vector2 TextureCoodinate{};
|
||||
|
||||
constexpr VertexPositionNormalTexture() = default;
|
||||
|
||||
bool operator==(const VertexPositionNormalTexture& other) const {
|
||||
return position == other.position
|
||||
&& normal == other.normal
|
||||
&& textureCoodinate == other.textureCoodinate;
|
||||
return Position == other.Position
|
||||
&& Normal == other.Normal
|
||||
&& TextureCoodinate == other.TextureCoodinate;
|
||||
}
|
||||
|
||||
constexpr VertexPositionNormalTexture(const Vector3& position, const Vector3& normal, const Vector2& textureCoodinate)
|
||||
: position(position), normal(normal), textureCoodinate(textureCoodinate)
|
||||
: Position(position), Normal(normal), TextureCoodinate(textureCoodinate)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
@ -4,4 +4,4 @@
|
||||
|
||||
# Add source to this project's executable.
|
||||
add_subdirectory ("01_blank")
|
||||
#add_subdirectory ("02_PlatfformerStarterKit")
|
||||
add_subdirectory ("02_PlatfformerStarterKit")
|
||||
|
Loading…
x
Reference in New Issue
Block a user