Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma warning (disable:4996)
- #pragma comment (lib, "d3d11.lib")
- #include <Windows.h>
- #include <d3d11.h>
- #include <gdiplus.h>
- #include <string>
- using namespace Gdiplus;
- using namespace std;
- namespace D3DProject1
- {
- UINT windowStyle = WS_OVERLAPPEDWINDOW;
- HWND hMainWindow;
- HINSTANCE hCurrentInstance;
- IDXGISwapChain* swapchain;
- ID3D11RenderTargetView *backbuffer;
- ID3D11Device* dev;
- ID3D11DeviceContext* devCon;
- wchar_t* W(wstring str)
- {
- wchar_t* result = new wchar_t[str.size()];
- wcscpy(result, str.c_str());
- return result;
- }
- void CreateWC(wstring name, WNDPROC OnMessage)
- {
- WNDCLASSEX wc;
- ZeroMemory(&wc, sizeof(wc));
- wc.cbSize = sizeof(WNDCLASSEX);
- wc.style = CS_HREDRAW | CS_VREDRAW;
- wc.lpfnWndProc = OnMessage;
- wc.hInstance = hCurrentInstance;
- wc.hCursor = LoadCursor(NULL, IDC_ARROW);
- wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
- wc.lpszClassName = W(name);
- RegisterClassEx(&wc);
- }
- Size AdjustWndSzToClientAreaSz(int clientAreaWidth, int clientAreaHeight, UINT wndStyle = windowStyle)
- {
- RECT wr = { 0, 0, clientAreaWidth, clientAreaHeight };
- AdjustWindowRect(&wr, wndStyle, FALSE);
- return Size(wr.right - wr.left, wr.bottom - wr.top);
- }
- DXGI_SWAP_CHAIN_DESC CreateSCD(HWND hWnd)
- {
- DXGI_SWAP_CHAIN_DESC scd;
- ZeroMemory(&scd, sizeof(DXGI_SWAP_CHAIN_DESC));
- scd.BufferCount = 1;
- scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
- scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
- scd.OutputWindow = hWnd;
- scd.SampleDesc.Count = 4;
- scd.Windowed = TRUE;
- return scd;
- }
- void InitD3DDeviceAndSwapChain(DXGI_SWAP_CHAIN_DESC& scd, IDXGISwapChain* sc, ID3D11Device* dev, ID3D11DeviceContext* devCon)
- {
- D3D11CreateDeviceAndSwapChain
- (
- NULL,
- D3D_DRIVER_TYPE_HARDWARE,
- NULL,
- NULL,
- NULL,
- NULL,
- D3D11_SDK_VERSION,
- &scd,
- &sc,
- &dev,
- NULL,
- &devCon
- );
- }
- D3D11_VIEWPORT CreateViewport(int width, int height)
- {
- D3D11_VIEWPORT viewport;
- ZeroMemory(&viewport, sizeof(D3D11_VIEWPORT));
- viewport.TopLeftX = 0;
- viewport.TopLeftY = 0;
- viewport.Width = width;
- viewport.Height = height;
- return viewport;
- }
- void SetupRenderTarget(ID3D11RenderTargetView* backbuffer)
- {
- ID3D11Texture2D *pBackBuffer;
- swapchain->GetBuffer(0, __uuidof(ID3D11Texture2D), (void**)&pBackBuffer);
- dev->CreateRenderTargetView(pBackBuffer, NULL, &backbuffer);
- devCon->OMSetRenderTargets(1, &backbuffer, NULL);
- pBackBuffer->Release();
- }
- void InitD3D(HWND hWnd = hMainWindow)
- {
- auto scd = CreateSCD(hWnd);
- InitD3DDeviceAndSwapChain(scd, swapchain, dev, devCon);
- SetupRenderTarget(backbuffer);
- auto viewport = CreateViewport(800, 600);
- devCon->RSSetViewports(1, &viewport);
- }
- void RenderFrame()
- {
- }
- void ReleaseD3D()
- {
- swapchain->Release();
- dev->Release();
- devCon->Release();
- }
- LRESULT CALLBACK OnMessage(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- switch (message)
- {
- case WM_DESTROY:
- ReleaseD3D();
- ExitProcess(0);
- break;
- }
- return DefWindowProc(hWnd, message, wParam, lParam);
- }
- HWND CreateWnd(int width, int height, wstring title, UINT wndStyle = windowStyle)
- {
- CreateWC(title, OnMessage);
- HWND hWnd = CreateWindow
- (
- W(title),
- W(title),
- wndStyle,
- 300,
- 300,
- width,
- height,
- NULL,
- NULL,
- hCurrentInstance,
- NULL
- );
- ShowWindow(hWnd, SW_RESTORE);
- return hWnd;
- }
- void InitMessageLoop()
- {
- MSG msg;
- while (true)
- {
- if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- else
- {
- RenderFrame();
- }
- }
- }
- int WinMain(HINSTANCE hInstance, string cmdLine, int showCmd)
- {
- hCurrentInstance = hInstance;
- Size sz = AdjustWndSzToClientAreaSz(800, 600);
- hMainWindow = CreateWnd(sz.Width, sz.Height, L"D3DProject1 Main Window");
- InitD3D(hMainWindow);
- InitMessageLoop();
- return 0;
- }
- }
- int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR cmdLine, int showCmd)
- {
- return D3DProject1::WinMain(hInstance, cmdLine, showCmd);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement