뭐가 잘못된거냐


<WinMain.h>

#pragma once
#include <Windows.h>
#include <iostream>
#include "D3DDevice.h"

#define WIDTH 1280
#define HEIGHT 960

extern HWND hwnd;


<WinMain.cpp>

#include "WinMain.h"

HRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);

HWND hwnd;

int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR CmdLine, int nCmdShow)
{
 MSG msg;
 WNDCLASSEXW wc;

 wc.cbClsExtra = 0;
 wc.cbSize = sizeof(WNDCLASSEXW);
 wc.cbWndExtra = 0;
 wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
 wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
 wc.hIconSm = NULL;
 wc.hInstance = hInstance;
 wc.lpfnWndProc = WndProc;
 wc.lpszClassName = L"아아";
 wc.lpszMenuName = NULL;
 wc.style = CS_VREDRAW | CS_HREDRAW;

 RegisterClassExW(&wc);

 DWORD style = WS_OVERLAPPEDWINDOW;
 RECT screen = { 0, 0, WIDTH, HEIGHT };

 AdjustWindowRectEx(&screen, style, FALSE, NULL);

 hwnd = CreateWindowW(wc.lpszClassName, L"아", style, CW_USEDEFAULT, CW_USEDEFAULT, (screen.right - screen.left), (screen.bottom - screen.top), NULL, NULL, hInstance, NULL);

 ShowWindow(hwnd, nCmdShow);

 CD3DDevice::Init();

 while (GetMessage(&msg, hwnd, 0, 0) == TRUE)
 {
  TranslateMessage(&msg);
  DispatchMessageW(&msg);

  CD3DDevice::Clear(255, 255, 255);
  CD3DDevice::Present();
 }

 CD3DDevice::Exit();

 return 0;
}

HRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
 switch (msg)
 {
 case WM_CREATE:
  break;

 case WM_DESTROY:
  CD3DDevice::Exit();
  PostQuitMessage(0);
  break;
 }

 return DefWindowProc(hwnd, msg, wParam, lParam);
}


<D3DDevice.h>

#pragma once
#include <d3d11.h>
#include <Windows.h>
#include "WinMain.h"

#pragma comment ( lib, "d3d11.lib")
#pragma comment ( lib, "D3DCompiler.lib")

#define ReleaseCOM(x){if(x){x->Release();x=NULL;}}

class CD3DDevice
{
private:
 static ID3D11Device *pID3D11Device;
 static ID3D11DeviceContext *pID3D11DeviceContext;
 static UINT numQualityLevels;
 static IDXGISwapChain *pIDXGISwapChain;

 static ID3D11RenderTargetView *pID3D11RenderTargetView;
 static ID3D11Texture2D *pID3DDepthStencilBuffer;
 static ID3D11DepthStencilView *pID3D11DepthStencilView;

 static HRESULT CreateDevice();
 static HRESULT CreateSwapChain();
 static HRESULT CreateSRView();

public:
 CD3DDevice();
 virtual ~CD3DDevice();

 static HRESULT Init();
 static HRESULT Exit();

 static ID3D11Device *GetDevice();
 static ID3D11DeviceContext *GetDeviceContext();

 static HRESULT Clear(float r, float g, float b);
 static HRESULT Present();
};


<D3DDevice.cpp>

#include "D3DDevice.h"

////////////////////////////////////////////////private

ID3D11Device *CD3DDevice::pID3D11Device = NULL;
ID3D11DeviceContext *CD3DDevice::pID3D11DeviceContext = NULL;
UINT CD3DDevice::numQualityLevels = NULL;
IDXGISwapChain *CD3DDevice::pIDXGISwapChain = NULL;

ID3D11RenderTargetView *CD3DDevice::pID3D11RenderTargetView = NULL;
ID3D11Texture2D *CD3DDevice::pID3DDepthStencilBuffer = NULL;
ID3D11DepthStencilView *CD3DDevice::pID3D11DepthStencilView = NULL;

HRESULT CD3DDevice::CreateDevice()
{
 HRESULT hr;
 D3D_FEATURE_LEVEL featureLevel = D3D_FEATURE_LEVEL_11_1;

 hr = D3D11CreateDevice
 (
  NULL,
  D3D_DRIVER_TYPE_HARDWARE,
  NULL,
  0,
  NULL,
  0,
  D3D11_SDK_VERSION,
  &pID3D11Device,
  &featureLevel,
  &pID3D11DeviceContext
 );

 pID3D11Device->CheckMultisampleQualityLevels(DXGI_FORMAT_R8G8B8A8_UNORM, 4, &numQualityLevels);

 return S_OK;
}

HRESULT CD3DDevice::CreateSwapChain()
{
 DXGI_SWAP_CHAIN_DESC sd;

 ZeroMemory(&sd, sizeof(DXGI_SWAP_CHAIN_DESC));

 sd.BufferDesc.Width = WIDTH;
 sd.BufferDesc.Height = HEIGHT;
 sd.BufferDesc.RefreshRate.Numerator = 60;
 sd.BufferDesc.RefreshRate.Denominator = 1;
 sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
 sd.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
 sd.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
 sd.SampleDesc.Count = 4;
 sd.SampleDesc.Quality = numQualityLevels -1;
 sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
 sd.BufferCount = 1;
 sd.OutputWindow = hwnd;
 sd.Windowed = TRUE;
 sd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
 sd.Flags = DXGI_SWAP_CHAIN_FLAG_DISPLAY_ONLY;

 HRESULT hr;

 IDXGIDevice *dxgiDevice = NULL;
 IDXGIAdapter *dxgiAdapter = NULL;
 IDXGIFactory *dxgiFactory = NULL;

 pID3D11Device->QueryInterface(__uuidof(IDXGIDevice), (void **)&dxgiDevice);
 dxgiDevice->GetParent(__uuidof(IDXGIAdapter), (void **)&dxgiAdapter);
 dxgiAdapter->GetParent(__uuidof(IDXGIFactory), (void **)&dxgiFactory);
 dxgiFactory->CreateSwapChain(pID3D11Device, &sd, &pIDXGISwapChain);

 ReleaseCOM(dxgiDevice);
 ReleaseCOM(dxgiAdapter);
 ReleaseCOM(dxgiFactory);

 return S_OK;
}

HRESULT CD3DDevice::CreateSRView()
{
 HRESULT hr;

 ID3D11Texture2D *backBuffer;
 D3D11_VIEWPORT ScreenViewport;

 pIDXGISwapChain->ResizeBuffers(1, WIDTH, HEIGHT, DXGI_FORMAT_R8G8B8A8_UNORM, 0);
 pIDXGISwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (void **)(&backBuffer));
 pID3D11Device->CreateRenderTargetView(backBuffer, 0, &pID3D11RenderTargetView);

 D3D11_TEXTURE2D_DESC depthStencilDesc;

 depthStencilDesc.Width = WIDTH;
 depthStencilDesc.Height = HEIGHT;
 depthStencilDesc.MipLevels = 1;
 depthStencilDesc.ArraySize = 1;
 depthStencilDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
 depthStencilDesc.SampleDesc.Count = 4;
 depthStencilDesc.SampleDesc.Quality = numQualityLevels - 1;
 depthStencilDesc.Usage = D3D11_USAGE_DEFAULT;
 depthStencilDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
 depthStencilDesc.CPUAccessFlags = 0;
 depthStencilDesc.MiscFlags = 0;
 
 pID3D11Device->CreateTexture2D(&depthStencilDesc, 0, &pID3DDepthStencilBuffer);
 pID3D11Device->CreateDepthStencilView(pID3DDepthStencilBuffer, 0, &pID3D11DepthStencilView);

 ScreenViewport.TopLeftX = 0;
 ScreenViewport.TopLeftY = 0;
 ScreenViewport.Width = (float)WIDTH;
 ScreenViewport.Height = (float)HEIGHT;
 ScreenViewport.MinDepth = 0.0f;
 ScreenViewport.MaxDepth = 1.0f;

 pID3D11DeviceContext->RSSetViewports(1, &ScreenViewport);

 ReleaseCOM(backBuffer);

 return S_OK;
}

////////////////////////////////////////////////public

CD3DDevice::CD3DDevice()
{

}

CD3DDevice::~CD3DDevice()
{

}

HRESULT CD3DDevice::Init()
{
 HRESULT hr;

 CreateDevice();
 CreateSwapChain();
 CreateSRView();

 return S_OK;
}

HRESULT CD3DDevice::Exit()
{
 ReleaseCOM(pID3D11RenderTargetView);
 ReleaseCOM(pID3D11DepthStencilView);
 ReleaseCOM(pIDXGISwapChain);
 ReleaseCOM(pID3DDepthStencilBuffer);
 // Restore all default settings.
 if (pID3D11DeviceContext != NULL)
 {
  pID3D11DeviceContext->ClearState();
 }

 ReleaseCOM(pID3D11DeviceContext);
 ReleaseCOM(pID3D11Device);

 return S_OK;
}

ID3D11Device *CD3DDevice::GetDevice()
{
 return pID3D11Device;
}

ID3D11DeviceContext *CD3DDevice::GetDeviceContext()
{
 return pID3D11DeviceContext;
}

HRESULT CD3DDevice::Clear(float r, float g, float b)
{
 float BGColor[4];
 BGColor[0] = r;
 BGColor[1] = g;
 BGColor[2] = b;
 BGColor[3] = 1.0f;

 pID3D11DeviceContext->ClearRenderTargetView(pID3D11RenderTargetView, BGColor);
 pID3D11DeviceContext->ClearDepthStencilView(pID3D11DepthStencilView, D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0f, 0);

 return S_OK;
}

HRESULT CD3DDevice::Present()
{
 pIDXGISwapChain->Present(0, 0);

 return S_OK;
}