- #include <windows.h>
- #include <d3d11.h>
- #include <dxgi1_3.h>
- #include <tchar.h>
- #include "BaseWindow.h"
- #include "Logger.h"
- #include "VertexDescriptor.h"
- #include "PixelShader.h"
- #include "VertexShader.h"
- #include "InputLayout.h"
- #include "Geometry.h"
- #include "GeometryLoader.h"
- #pragma comment( lib, "d3d11.lib" )
- #pragma comment( lib, "dxgi.lib" )
- #pragma comment( lib, "D3DCompiler.lib" )
- #pragma comment( lib, "dxguid.lib" )
- class Engine : public BaseWindow
- {
- private:
- ID3D11Device* device;
- ID3D11DeviceContext* context;
- int width;
- int height;
- IDXGIFactory2* dxgiFactory;
- IDXGISwapChain1* swapChain;
- ID3D11Texture2D* backBuffer;
- ID3D11RenderTargetView* renderTargetView;
- private:
- void getBackBufferPointer()
- {
- swapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), reinterpret_cast<void**>(&backBuffer));
- }
- void createDeviceAndDeviceContext()
- {
- DWORD deviceFlags = D3D11_CREATE_DEVICE_SINGLETHREADED;
- #if defined(_DEBUG)
- deviceFlags |= D3D11_CREATE_DEVICE_DEBUG;
- #endif
- D3D_FEATURE_LEVEL chosenFeatureLevel;
- HRESULT deviceResult = D3D11CreateDevice(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, deviceFlags,
- nullptr, 0, D3D11_SDK_VERSION, &device, &chosenFeatureLevel, &context);
- if (FAILED(deviceResult))
- {
- std::cout << "ERROR device creation failed!" << std::endl;
- }
- }
- void createSwapChain()
- {
- HRESULT factoryResult = CreateDXGIFactory2(0, __uuidof(IDXGIFactory2),
- (void**)(&dxgiFactory));
- if (FAILED(factoryResult))
- {
- std::cout << "ERROR: DXGI Factory creation failed!" << std::endl;
- }
- DXGI_SWAP_CHAIN_DESC1 swapChainDescription;
- ZeroMemory(&swapChainDescription, sizeof(DXGI_SWAP_CHAIN_DESC1));
- swapChainDescription.Width = this->width;
- swapChainDescription.Height = this->height;
- swapChainDescription.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
- swapChainDescription.Stereo = FALSE;
- swapChainDescription.SampleDesc.Count = 1;
- swapChainDescription.SampleDesc.Quality = 0;
- swapChainDescription.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
- swapChainDescription.BufferCount = 1;
- swapChainDescription.Scaling = DXGI_SCALING_STRETCH;
- swapChainDescription.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
- swapChainDescription.AlphaMode = DXGI_ALPHA_MODE_UNSPECIFIED;
- swapChainDescription.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;
- DXGI_SWAP_CHAIN_FULLSCREEN_DESC fullscreenDescription;
- ZeroMemory(&fullscreenDescription, sizeof(DXGI_SWAP_CHAIN_FULLSCREEN_DESC));
- fullscreenDescription.RefreshRate.Numerator = 60;
- fullscreenDescription.RefreshRate.Denominator = 1;
- fullscreenDescription.Windowed = true;
- HRESULT swapChainResult = dxgiFactory->CreateSwapChainForHwnd(device,
- hwnd, &swapChainDescription, &fullscreenDescription, nullptr, &swapChain);
- if (FAILED(swapChainResult))
- {
- std::cout << "ERROR! Swap Chain creation failed!" << std::endl;
- }
- }
- void createRenderTarget()
- {
- getBackBufferPointer();
- HRESULT result = device->CreateRenderTargetView(backBuffer, NULL, &renderTargetView);
- if (FAILED(result))
- {
- std::cout << "ERROR! Render Target View creation failed!" << std::endl;
- }
- }
- void createViewport()
- {
- }
- void createTriangleBuffer(ID3D11Device* device)
- {
- }
- void createShadersAndInputLayout(ID3D11Device* device)
- {
- }
- void bindEngineResources()
- {
- }
- void clearRenderTarget(int r, int g, int b)
- {
- float color[4] = { r / 255.0f, g / 255.0f, b / 255.0f, 0.0f };
- context->ClearRenderTargetView(renderTargetView, color);
- }
- void present()
- {
- swapChain->Present(0, 0);
- }
- public:
- Engine() : BaseWindow("GK_001", 800, 600), width(800), height(600)
- {
- }
- void initialize() override
- {
- createDeviceAndDeviceContext();
- createSwapChain();
- createRenderTarget();
- createViewport();
- createTriangleBuffer(device);
- createShadersAndInputLayout(device);
- bindEngineResources();
- }
- void update() override
- {
- }
- void render() override
- {
- clearRenderTarget(120, 30, 30);
- present();
- }
- };
- int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmd, int show)
- {
- Engine* engine = new Engine;
- engine->start();
- return 0;
- }