Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // cl.exe MyProgram.cpp kernel32.lib user32.lib dwmapi.lib d3d10.lib
- //
- #define UNICODE
- #define _UNICODE
- #define INITGUID
- #include <windows.h>
- #include <dwmapi.h>
- #include <dxgi.h>
- #include <d3d10.h>
- void InitWindow();
- void InitDevice();
- void ResizeBuffers();
- void Render();
- void Cleanup();
- LRESULT CALLBACK MyWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
- HWND hwnd = NULL;
- HINSTANCE instance = NULL;
- ID3D10Device * device = NULL;
- IDXGISwapChain * swapChain = NULL;
- ID3D10RenderTargetView * backBufferRTV = NULL;
- UINT width = 640;
- UINT height = 480;
- int WinMain(HINSTANCE _instance, HINSTANCE prevInstance, LPSTR cmdLine, int cmdShow)
- {
- instance = _instance;
- InitWindow();
- InitDevice();
- MSG msg = {0};
- while(WM_QUIT != msg.message)
- {
- if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- else
- {
- Render();
- }
- }
- Cleanup();
- return 0;
- }
- void InitWindow()
- {
- WNDCLASSEX wc;
- SecureZeroMemory(&wc, sizeof(wc));
- wc.cbSize = sizeof(wc);
- wc.hbrBackground = (HBRUSH)NULL;
- wc.hCursor = LoadCursor(NULL, IDC_ARROW);
- wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
- wc.hInstance = instance;
- wc.lpfnWndProc = MyWindowProc;
- wc.lpszClassName = TEXT("Window");
- wc.style = CS_VREDRAW | CS_HREDRAW;
- if(!(RegisterClassEx(&wc)))
- FatalAppExit(0, TEXT("Unable to register class"));
- RECT r;
- GetWindowRect(GetDesktopWindow(), &r);
- if(!(hwnd = CreateWindowEx(
- WS_EX_COMPOSITED,
- TEXT("Window"),
- TEXT("Direct3D 10"),
- WS_POPUP,
- (r.right - r.left) / 2 - width / 2,
- (r.bottom - r.top) / 2 - height / 2,
- width,
- height,
- NULL,
- NULL,
- instance,
- NULL)))
- FatalAppExit(0, TEXT("Unable to create window"));
- MARGINS m = {-1};
- DwmExtendFrameIntoClientArea(hwnd, &m);
- ShowWindow(hwnd, SW_SHOWNORMAL);
- UpdateWindow(hwnd);
- }
- void InitDevice()
- {
- HRESULT hr;
- DXGI_SWAP_CHAIN_DESC sd;
- SecureZeroMemory(&sd, sizeof(sd));
- sd.BufferCount = 1;
- sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
- sd.Windowed = TRUE;
- sd.OutputWindow = hwnd;
- sd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
- sd.SampleDesc.Count = 1;
- sd.SampleDesc.Quality = 0;
- sd.Flags = 0;
- sd.BufferDesc.Width = width;
- sd.BufferDesc.Height = height;
- sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
- sd.BufferDesc.RefreshRate.Numerator = 60;
- sd.BufferDesc.RefreshRate.Denominator = 1;
- hr = D3D10CreateDeviceAndSwapChain(
- NULL,
- D3D10_DRIVER_TYPE_HARDWARE,
- NULL,
- 0,
- D3D10_SDK_VERSION,
- &sd,
- &swapChain,
- &device);
- if(FAILED(hr))
- FatalAppExit(0, TEXT("Unable to create device and/or swap chain"));
- ResizeBuffers();
- }
- void ResizeBuffers()
- {
- if(!device || !swapChain)
- return;
- HRESULT hr;
- device->OMSetRenderTargets(0, NULL, NULL);
- if(backBufferRTV)
- backBufferRTV->Release();
- hr = swapChain->ResizeBuffers(1, width, height, DXGI_FORMAT_R8G8B8A8_UNORM, 0);
- if(FAILED(hr))
- FatalAppExit(0, TEXT("Unable to resize backbuffer"));
- ID3D10Texture2D * backBuffer = NULL;
- hr = swapChain->GetBuffer(0, IID_ID3D10Texture2D, (void**)&backBuffer);
- if(FAILED(hr))
- FatalAppExit(0, TEXT("Unable to obtain backbuffer"));
- hr = device->CreateRenderTargetView(backBuffer, NULL, &backBufferRTV);
- if(FAILED(hr))
- FatalAppExit(0, TEXT("Unable to [re]create render target view"));
- backBuffer->Release();
- D3D10_VIEWPORT vp;
- vp.MinDepth = 0.0f;
- vp.MaxDepth = 1.0f;
- vp.TopLeftX = 0;
- vp.TopLeftY = 0;
- vp.Width = width;
- vp.Height = height;
- device->RSSetViewports(1, &vp);
- D3D10_RECT sr;
- sr.top = 0;
- sr.left = 0;
- sr.right = width;
- sr.bottom = height;
- device->RSSetScissorRects(1, &sr);
- }
- void Cleanup()
- {
- if(backBufferRTV)
- backBufferRTV->Release();
- if(swapChain)
- swapChain->Release();
- if(device)
- {
- device->ClearState();
- device->Release();
- }
- }
- void Render()
- {
- device->OMSetRenderTargets(1, &backBufferRTV, NULL);
- FLOAT clearColor[4] = { 0.0f, 0.2f, 1.0f, 0.4f };
- device->ClearRenderTargetView(backBufferRTV, clearColor);
- swapChain->Present(0, 0);
- }
- LRESULT CALLBACK MyWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
- {
- switch(msg)
- {
- case WM_KEYDOWN:
- if(VK_ESCAPE == wParam)
- PostQuitMessage(0);
- return 0;
- case WM_SIZE:
- width = LOWORD(lParam);
- height = HIWORD(lParam);
- ResizeBuffers();
- return 0;
- case WM_PAINT:
- return 0;
- case WM_DESTROY:
- PostQuitMessage(0);
- return 0;
- default:
- return DefWindowProc(hwnd, msg, wParam, lParam);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement