Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h>
- #include <stdio.h>
- #include <assert.h>
- #include <D3D11.h>
- #define D3DCALL( obj, m ) obj->lpVtbl->m( obj )
- #define D3DCALL_( obj, m, args... ) obj->lpVtbl->m( obj, args )
- #define SAFE_RELEASE( obj ) if( obj ){ D3DCALL( obj, Release ); obj = NULL; }
- #define MY_WINDOW_CLASS "myWindowClass"
- // Step 4: the Window Procedure
- LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
- {
- switch(msg)
- {
- case WM_CLOSE:
- DestroyWindow(hwnd);
- break;
- case WM_DESTROY:
- PostQuitMessage(0);
- break;
- default:
- return DefWindowProc(hwnd, msg, wParam, lParam);
- }
- return 0;
- }
- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
- {
- WNDCLASSEX wc;
- HWND hwnd;
- MSG Msg;
- HRESULT hr;
- DXGI_SWAP_CHAIN_DESC swapChainDesc;
- ID3D11Device* device = NULL;
- ID3D11DeviceContext* context = NULL;
- IDXGISwapChain* swapChain = NULL;
- ID3D11Texture2D* backBuffer;
- ID3D11RenderTargetView* rtView;
- wc.cbSize = sizeof(WNDCLASSEX);
- wc.style = 0;
- wc.lpfnWndProc = WndProc;
- wc.cbClsExtra = 0;
- wc.cbWndExtra = 0;
- wc.hInstance = hInstance;
- wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
- wc.hCursor = LoadCursor(NULL, IDC_ARROW);
- wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
- wc.lpszMenuName = NULL;
- wc.lpszClassName = MY_WINDOW_CLASS;
- wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
- if(!RegisterClassEx(&wc))
- {
- MessageBox(NULL, "Window Registration Failed!", "Error!",
- MB_ICONEXCLAMATION | MB_OK);
- return 0;
- }
- hwnd = CreateWindowEx(
- WS_EX_CLIENTEDGE,
- MY_WINDOW_CLASS,
- "The title of my window",
- WS_OVERLAPPEDWINDOW,
- CW_USEDEFAULT, CW_USEDEFAULT, 1000, 800,
- NULL, NULL, hInstance, NULL);
- if(hwnd == NULL)
- {
- MessageBox(NULL, "Window Creation Failed!", "Error!",
- MB_ICONEXCLAMATION | MB_OK);
- return 0;
- }
- memset( &swapChainDesc, 0, sizeof(swapChainDesc) );
- swapChainDesc.BufferDesc.Width = 800;
- swapChainDesc.BufferDesc.Height = 600;
- // - default refresh rate
- swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
- // swapChainDesc.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_PROGRESSIVE;
- // swapChainDesc.BufferDesc.Scaling = DXGI_MODE_SCALING_STRETCHED;
- swapChainDesc.SampleDesc.Count = 1;
- swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
- swapChainDesc.BufferCount = 1;
- swapChainDesc.OutputWindow = hwnd;
- swapChainDesc.Windowed = TRUE;
- // swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
- // swapChainDesc.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;
- hr = D3D11CreateDeviceAndSwapChain(
- NULL, // adapter
- D3D_DRIVER_TYPE_HARDWARE,
- NULL, // software rasterizer (unused)
- 0, // flags
- NULL, // feature levels
- 0, // ^^
- D3D11_SDK_VERSION,
- &swapChainDesc,
- &swapChain,
- &device,
- NULL,
- &context
- );
- assert( SUCCEEDED( hr ) );
- const GUID myguid_ID3D11Texture2D = {0x6f15aaf2, 0xd208, 0x4e89, {0x9a,0xb4, 0x48,0x95,0x35,0xd3,0x4f,0x9c}};
- hr = D3DCALL_( swapChain, GetBuffer, 0, &myguid_ID3D11Texture2D, (void**) &backBuffer );
- assert( SUCCEEDED( hr ) );
- assert( backBuffer );
- D3DCALL_( device, CreateRenderTargetView, (ID3D11Resource*) backBuffer, NULL, &rtView );
- assert( rtView );
- D3DCALL_( context, OMSetRenderTargets, 1, &rtView, NULL );
- D3D11_VIEWPORT vp;
- vp.Width = 800;
- vp.Height = 600;
- vp.MinDepth = -1.0f;
- vp.MaxDepth = 1.0f;
- vp.TopLeftX = 0;
- vp.TopLeftY = 0;
- D3DCALL_( context, RSSetViewports, 1, &vp );
- ShowWindow(hwnd, nCmdShow);
- UpdateWindow(hwnd);
- while(GetMessage(&Msg, NULL, 0, 0) > 0)
- {
- // Uncomment this line to create problems for WM_KEY* events to translate messages
- TranslateMessage(&Msg);
- DispatchMessage(&Msg);
- float ClearColor[4] = { 0.2f, 0.225f, 0.3f, 0.3f };
- // here be dragons... or something
- // only the blue color is written, others are set to 0
- // if I pass a valid pFeatureLevel pointer to D3D11CreateDeviceAndSwapChain, no colors are written
- D3DCALL_( context, ClearRenderTargetView, rtView, ClearColor );
- D3DCALL_( swapChain, Present, 0, 0 );
- }
- SAFE_RELEASE( rtView );
- SAFE_RELEASE( backBuffer );
- SAFE_RELEASE( swapChain );
- SAFE_RELEASE( context );
- SAFE_RELEASE( device );
- return Msg.wParam;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement