Skip to content
24 changes: 23 additions & 1 deletion Source/Core/Engine/Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include "Window/Window.h"
#include "frameData.h"
#include "Util/jsonUtil.h"
#include "Graphics/DX11.h"
#include "Graphics/DX12.h"

#include <nlohmann/json.hpp>

Expand All @@ -21,11 +23,31 @@ RF::Engine::Engine(const RF::EngineCreationParams& params) {

mWindow = std::make_unique<RF::Window>();
mWindow->Init(windowParams);

switch (mGraphicsAPI) {
case GraphicsAPI::DirectX11:
{
DX11CreationParams dx11Params;
dx11Params.hwnd = mWindow->GetHWND();
dx11Params.width = windowParams.width;
dx11Params.height = windowParams.height;
//dx11Params.clearColour = { 0.2f, 0.4f, 0.6f, 1.0f };
mRenderer = std::make_unique<RF::DX11>(dx11Params);
break;
}
case GraphicsAPI::DirectX12:
{
mRenderer = std::make_unique<RF::DX12>(mWindow->GetHWND(), windowParams.width, windowParams.height);
break;
}
}
Comment on lines +27 to +43
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Det här borde vara en preprocessor check i och med att vi aldrig kommer byta api i runtime

Suggested change
switch (mGraphicsAPI) {
case GraphicsAPI::DirectX11:
mRenderer = std::make_unique<RF::DX11>(mWindow->GetHWND(), windowParams.width, windowParams.height);
break;
case GraphicsAPI::DirectX12:
mRenderer = std::make_unique<RF::DX12>(mWindow->GetHWND(), windowParams.width, windowParams.height);
break;
}
#ifdef DX11
mRenderer = std::make_unique<RF::DX11>(mWindow->GetHWND(), windowParams.width, windowParams.height);
#elif DX12
mRenderer = std::make_unique<RF::DX12>(mWindow->GetHWND(), windowParams.width, windowParams.height);
#endif

}

void RF::Engine::Update(const FrameData& frameData) { frameData; }

void RF::Engine::Render(const FrameData& frameData) { frameData; }
void RF::Engine::Render(const FrameData& frameData) {
mRenderer->Render(frameData);
}

void RF::Engine::Shutdown() {}

Expand Down
11 changes: 10 additions & 1 deletion Source/Core/Engine/Engine.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#pragma once
#include "Graphics/IRenderer.h"

namespace RF {
struct FrameData;
struct WindowCreationParams;
Expand All @@ -10,6 +12,11 @@ namespace RF {
HINSTANCE hInstance = nullptr;
};

enum class GraphicsAPI {
DirectX11,
DirectX12,
};

class Engine {
public:
Engine() = delete;
Expand All @@ -29,7 +36,9 @@ namespace RF {
void LoadConfigFile(RF::WindowCreationParams& windowParams);

std::unique_ptr<Window> mWindow;
std::unique_ptr<IRenderer> mRenderer;
GraphicsAPI mGraphicsAPI = GraphicsAPI::DirectX11;

std::wstring mAssetsPath;
};
}
}
1 change: 1 addition & 0 deletions Source/Core/Engine/Window/Window.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace RF {
std::wstring title = L"RuneForge";
bool isFullScreen = false;
bool isResizable = true;
//Colour clearColour = { 0.2f, 0.4f, 0.6f, 1.0f };

HINSTANCE hInstance = nullptr;
int cmdShow = 0;
Expand Down
89 changes: 89 additions & 0 deletions Source/Core/Graphics/DX11.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#include "stdafx.h"
#include "DX11.h"
#include <stdexcept>

using namespace Microsoft::WRL;

RF::DX11::DX11(const DX11CreationParams& params) :
mWidth(params.width),
mHeight(params.height) {
//mClearColour(params.clearColour) {
if (FAILED(CreateDeviceAndSwapChain(params.hwnd, params.width, params.height)))
throw std::runtime_error("Failed to create Direct3D 11 device and swap chain.");

if (FAILED(CreateRenderTargetView()))
throw std::runtime_error("Failed to create Direct3D 11 render target view.");

SetViewport(mWidth, mHeight);
}

RF::DX11::~DX11() {
}

HRESULT RF::DX11::CreateDeviceAndSwapChain(const HWND hwnd, const uint32_t width, const uint32_t height) {
DXGI_SWAP_CHAIN_DESC scd = {};
scd.BufferDesc.Width = width;
scd.BufferDesc.Height = height;
scd.BufferDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
scd.BufferDesc.RefreshRate.Numerator = 0u;
scd.BufferDesc.RefreshRate.Denominator = 0u;
scd.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
scd.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
scd.SampleDesc.Count = 1u; // Anti-aliasing
scd.SampleDesc.Quality = 0u; // Anti-aliasing
scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
scd.BufferCount = 1u; // 1 back buffer and 1 front buffer
scd.OutputWindow = hwnd;
scd.Windowed = true;
scd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
scd.Flags = 0u;
Comment thread
OlleKReutercrona marked this conversation as resolved.

UINT swapCreateFlags = 0u;
#ifndef NDEBUG
swapCreateFlags |= D3D11_CREATE_DEVICE_DEBUG;
#endif

return D3D11CreateDeviceAndSwapChain(
nullptr,
D3D_DRIVER_TYPE_HARDWARE,
nullptr,
swapCreateFlags,
nullptr,
0,
D3D11_SDK_VERSION,
&scd,
&mSwap,
&mDevice,
nullptr,
&mContext
);
}

HRESULT RF::DX11::CreateRenderTargetView() {
HRESULT hr = S_OK;

Microsoft::WRL::ComPtr<ID3D11Resource> backBuffer;
hr = mSwap->GetBuffer(0u, __uuidof(ID3D11Resource), &backBuffer);
if (FAILED(hr))
return hr;

hr = mDevice->CreateRenderTargetView(backBuffer.Get(), nullptr, &mDefaultTarget);
return hr;
}

void RF::DX11::SetViewport(const uint32_t width, const uint32_t height) {
D3D11_VIEWPORT vp = {};
vp.Width = static_cast<FLOAT>(width);
vp.Height = static_cast<FLOAT>(height);
vp.MinDepth = 0.0f;
vp.MaxDepth = 1.0f;
vp.TopLeftX = 0.0f;
vp.TopLeftY = 0.0f;
mContext->RSSetViewports(1u, &vp);
}

void RF::DX11::Render(const FrameData&) {
const FLOAT clearColor[] = { 0.2f, 0.4f, 0.6f, 1.0f };
Comment thread
OlleKReutercrona marked this conversation as resolved.
mContext->ClearRenderTargetView(mDefaultTarget.Get(), clearColor);
mSwap->Present(1u, 0u);
}
40 changes: 40 additions & 0 deletions Source/Core/Graphics/DX11.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#pragma once
#include "IRenderer.h"

#include <wrl.h>
#include <d3d11.h>

namespace RF {
struct DX11CreationParams {
HWND hwnd;
uint32_t width;
uint32_t height;
//Colour clearColour = { 0.2f, 0.4f, 0.6f, 1.0f };
bool startInFullScreen = false;
};

class DX11 : public IRenderer {
public:
DX11(const DX11CreationParams& params);
~DX11();

DX11() = delete;
DX11(const DX11&) = delete;
void operator=(const DX11&) = delete;

virtual void Render(const FrameData& frameData);
private:
HRESULT CreateDeviceAndSwapChain(const HWND hwnd, const uint32_t width, const uint32_t height);
HRESULT CreateRenderTargetView();
void SetViewport(const uint32_t width, const uint32_t height);

uint32_t mWidth;
uint32_t mHeight;
//Colour mClearColour;

Microsoft::WRL::ComPtr<ID3D11Device> mDevice;
Microsoft::WRL::ComPtr<IDXGISwapChain> mSwap;
Microsoft::WRL::ComPtr<ID3D11DeviceContext> mContext;
Microsoft::WRL::ComPtr<ID3D11RenderTargetView> mDefaultTarget;
};
}
Loading
Loading