Advertisement
snake5

d3d11 not working right

Jun 3rd, 2014
384
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.26 KB | None | 0 0
  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <assert.h>
  4. #include <D3D11.h>
  5.  
  6.  
  7. #define D3DCALL( obj, m ) obj->lpVtbl->m( obj )
  8. #define D3DCALL_( obj, m, args... ) obj->lpVtbl->m( obj, args )
  9. #define SAFE_RELEASE( obj ) if( obj ){ D3DCALL( obj, Release ); obj = NULL; }
  10.  
  11.  
  12. #define MY_WINDOW_CLASS "myWindowClass"
  13.  
  14. // Step 4: the Window Procedure
  15. LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  16. {
  17.     switch(msg)
  18.     {
  19.     case WM_CLOSE:
  20.         DestroyWindow(hwnd);
  21.         break;
  22.     case WM_DESTROY:
  23.         PostQuitMessage(0);
  24.         break;
  25.     default:
  26.         return DefWindowProc(hwnd, msg, wParam, lParam);
  27.     }
  28.     return 0;
  29. }
  30.  
  31. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  32. {
  33.     WNDCLASSEX wc;
  34.     HWND hwnd;
  35.     MSG Msg;
  36.     HRESULT hr;
  37.    
  38.     DXGI_SWAP_CHAIN_DESC swapChainDesc;
  39.     ID3D11Device* device = NULL;
  40.     ID3D11DeviceContext* context = NULL;
  41.     IDXGISwapChain* swapChain = NULL;
  42.     ID3D11Texture2D* backBuffer;
  43.     ID3D11RenderTargetView* rtView;
  44.    
  45.     wc.cbSize        = sizeof(WNDCLASSEX);
  46.     wc.style         = 0;
  47.     wc.lpfnWndProc   = WndProc;
  48.     wc.cbClsExtra    = 0;
  49.     wc.cbWndExtra    = 0;
  50.     wc.hInstance     = hInstance;
  51.     wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
  52.     wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  53.     wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  54.     wc.lpszMenuName  = NULL;
  55.     wc.lpszClassName = MY_WINDOW_CLASS;
  56.     wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
  57.    
  58.     if(!RegisterClassEx(&wc))
  59.     {
  60.         MessageBox(NULL, "Window Registration Failed!", "Error!",
  61.             MB_ICONEXCLAMATION | MB_OK);
  62.         return 0;
  63.     }
  64.    
  65.     hwnd = CreateWindowEx(
  66.         WS_EX_CLIENTEDGE,
  67.         MY_WINDOW_CLASS,
  68.         "The title of my window",
  69.         WS_OVERLAPPEDWINDOW,
  70.         CW_USEDEFAULT, CW_USEDEFAULT, 1000, 800,
  71.         NULL, NULL, hInstance, NULL);
  72.    
  73.     if(hwnd == NULL)
  74.     {
  75.         MessageBox(NULL, "Window Creation Failed!", "Error!",
  76.             MB_ICONEXCLAMATION | MB_OK);
  77.         return 0;
  78.     }
  79.    
  80.     memset( &swapChainDesc, 0, sizeof(swapChainDesc) );
  81.    
  82.     swapChainDesc.BufferDesc.Width = 800;
  83.     swapChainDesc.BufferDesc.Height = 600;
  84.     // - default refresh rate
  85.     swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
  86. //  swapChainDesc.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_PROGRESSIVE;
  87. //  swapChainDesc.BufferDesc.Scaling = DXGI_MODE_SCALING_STRETCHED;
  88.     swapChainDesc.SampleDesc.Count = 1;
  89.     swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
  90.     swapChainDesc.BufferCount = 1;
  91.     swapChainDesc.OutputWindow = hwnd;
  92.     swapChainDesc.Windowed = TRUE;
  93. //  swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
  94. //  swapChainDesc.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;
  95.    
  96.     hr = D3D11CreateDeviceAndSwapChain(
  97.         NULL, // adapter
  98.         D3D_DRIVER_TYPE_HARDWARE,
  99.         NULL, // software rasterizer (unused)
  100.         0, // flags
  101.         NULL, // feature levels
  102.         0, // ^^
  103.         D3D11_SDK_VERSION,
  104.         &swapChainDesc,
  105.         &swapChain,
  106.         &device,
  107.         NULL,
  108.         &context
  109.     );
  110.     assert( SUCCEEDED( hr ) );
  111.    
  112.     const GUID myguid_ID3D11Texture2D = {0x6f15aaf2, 0xd208, 0x4e89, {0x9a,0xb4, 0x48,0x95,0x35,0xd3,0x4f,0x9c}};
  113.     hr = D3DCALL_( swapChain, GetBuffer, 0, &myguid_ID3D11Texture2D, (void**) &backBuffer );
  114.     assert( SUCCEEDED( hr ) );
  115.     assert( backBuffer );
  116.     D3DCALL_( device, CreateRenderTargetView, (ID3D11Resource*) backBuffer, NULL, &rtView );
  117.     assert( rtView );
  118.     D3DCALL_( context, OMSetRenderTargets, 1, &rtView, NULL );
  119.    
  120.     D3D11_VIEWPORT vp;
  121.     vp.Width = 800;
  122.     vp.Height = 600;
  123.     vp.MinDepth = -1.0f;
  124.     vp.MaxDepth = 1.0f;
  125.     vp.TopLeftX = 0;
  126.     vp.TopLeftY = 0;
  127.     D3DCALL_( context, RSSetViewports, 1, &vp );
  128.    
  129.     ShowWindow(hwnd, nCmdShow);
  130.     UpdateWindow(hwnd);
  131.    
  132.     while(GetMessage(&Msg, NULL, 0, 0) > 0)
  133.     {
  134.         // Uncomment this line to create problems for WM_KEY* events to translate messages
  135.         TranslateMessage(&Msg);
  136.         DispatchMessage(&Msg);
  137.        
  138.         float ClearColor[4] = { 0.2f, 0.225f, 0.3f, 0.3f };
  139.         // here be dragons... or something
  140.         // only the blue color is written, others are set to 0
  141.         // if I pass a valid pFeatureLevel pointer to D3D11CreateDeviceAndSwapChain, no colors are written
  142.         D3DCALL_( context, ClearRenderTargetView, rtView, ClearColor );
  143.        
  144.         D3DCALL_( swapChain, Present, 0, 0 );
  145.     }
  146.    
  147.     SAFE_RELEASE( rtView );
  148.     SAFE_RELEASE( backBuffer );
  149.     SAFE_RELEASE( swapChain );
  150.     SAFE_RELEASE( context );
  151.     SAFE_RELEASE( device );
  152.    
  153.     return Msg.wParam;
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement