D3D 基础彩色矩形的绘制

#include  
#include  
#pragma comment(lib, "D3D9.lib")  
  
const TCHAR *kClassName = "Rectangle_D3D";  
  
IDirect3D9 *d3d9 = nullptr;  
IDirect3DDevice9 *device9 = nullptr;  
IDirect3DVertexBuffer9 *vectex_buffer9 = nullptr;  
D3DPRESENT_PARAMETERS params;  
D3DPRESENT_PARAMETERS *p;
BOOL window_mode = TRUE;  
#define D3D_FVF_VECTOR (D3DFVF_XYZRHW | D3DFVF_DIFFUSE)  
const int kWndWidth = 640;  
const int kWndHeight = 480;  
  
struct Vector {  
    Vector(float v_x, float v_y, float v_z, DWORD c) : x(v_x), y(v_y), z(v_z), color(c) {  
        rhw = 1.0f;  
    }  
    float x, y, z, rhw;  
    DWORD color;  
};  
  
LRESULT CALLBACK WindowProc(  
    _In_ HWND   hwnd,  
    _In_ UINT   uMsg,  
    _In_ WPARAM wParam,  
    _In_ LPARAM lParam  
    );  
  
bool InitD3D(HWND hwnd);//初始化  
bool Done();//完成  
bool ReDrawRectangle();  
  
int CALLBACK WinMain(  
    _In_ HINSTANCE hInstance,  
    _In_ HINSTANCE hPrevInstance,  
    _In_ LPSTR     lpCmdLine,  
    _In_ int       nCmdShow  
    ) {  
  
    //init app  
    WNDCLASSEX wndclassex;  
    wndclassex.cbSize = sizeof(WNDCLASSEX);  
    wndclassex.style = CS_HREDRAW | CS_VREDRAW;  
    wndclassex.lpfnWndProc = WindowProc;  
    wndclassex.cbClsExtra = 0;  
    wndclassex.cbWndExtra = 0;  
    wndclassex.hInstance = hInstance;  
    wndclassex.hIcon = nullptr;  
    wndclassex.hCursor = LoadCursor(hInstance, IDC_ARROW);  
    wndclassex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);  
    wndclassex.lpszMenuName = nullptr;  
    wndclassex.lpszClassName = kClassName;  
    wndclassex.hIconSm = nullptr;  
    RegisterClassEx(&wndclassex);  
  
    HWND  hwnd = CreateWindowEx(0, kClassName, "主宰", WS_OVERLAPPEDWINDOW, 100, 100, 640, 480, nullptr, nullptr, hInstance, nullptr);  
  
    ShowWindow(hwnd, nCmdShow);  
  
    InitD3D(hwnd);  
    UpdateWindow(hwnd);  
    MSG msg;  
    while (true) {  
        if (PeekMessage(&msg,nullptr, 0, 0, PM_REMOVE)) {  
            if (msg.message == WM_QUIT) {  
                break;  
            }  
            ::TranslateMessage(&msg);  
            ::DispatchMessage(&msg);  
        }  
    }  
    Done();  
    return 0;  
}  
  
LRESULT CALLBACK WindowProc(  
    _In_ HWND   hwnd,  
    _In_ UINT   uMsg,  
    _In_ WPARAM wParam,  
    _In_ LPARAM lParam  
    ) {  
         
      PAINTSTRUCT ps;  
      HDC hdc;  
      if (uMsg == WM_PAINT) {  
          hdc = BeginPaint(hwnd, &ps);  
            ReDrawRectangle();  
          EndPaint(hwnd, &ps);  
      } else if (uMsg == WM_KEYDOWN) {  
          if (wParam == VK_SPACE) {  
              //空格键  
              //全屏模式<-->窗口模式之间切换  
              window_mode = !window_mode;  
              D3DDISPLAYMODE mode;  
              if (!window_mode) {  
                  UINT adapter_mode_count = d3d9->GetAdapterModeCount(D3DADAPTER_DEFAULT, D3DFMT_X8R8G8B8);  
                  for (UINT adapter_mode = 0; adapter_mode < adapter_mode_count; ++adapter_mode) {  
                      d3d9->EnumAdapterModes(D3DADAPTER_DEFAULT, D3DFMT_X8R8G8B8, adapter_mode, &mode);  
                      if (mode.Width == kWndWidth && mode.Height == kWndHeight) {  
                          break;  
                      }  
                  }  
              }  
              if (!window_mode && (mode.Width != kWndWidth || mode.Height != kWndHeight)) {  
                  return DefWindowProc(hwnd, uMsg, wParam, lParam);  
              }  
              if (nullptr != device9) {  
                  params.BackBufferWidth = window_mode ? kWndWidth : mode.Width;  
                  params.BackBufferHeight = window_mode ? kWndHeight : mode.Height;  
                  params.Windowed = window_mode;  
                  HRESULT  hr = device9->Reset(p);  
                  if (FAILED(hr)) {  
                      int i = 0;  
                  }  
              }  
          }  
      }else if (uMsg == WM_DESTROY) {  
          PostMessage(hwnd, WM_QUIT, 0, 0);  
      }  
  
    return DefWindowProc(hwnd, uMsg, wParam, lParam);  
}  
  
bool InitD3D(HWND hwnd) {  
    d3d9 = Direct3DCreate9(D3D_SDK_VERSION);  
    if (nullptr == d3d9) {  
        return false;  
    }  
  
    params.BackBufferWidth = kWndWidth;  
    params.BackBufferHeight = kWndHeight;  
    params.BackBufferFormat = D3DFMT_X8R8G8B8;  
    params.BackBufferCount = 1;  
    params.MultiSampleType = D3DMULTISAMPLE_NONE;  
    params.MultiSampleQuality = 0;  
    params.SwapEffect = D3DSWAPEFFECT_COPY;  
    params.hDeviceWindow = hwnd;  
    params.Windowed = window_mode;  
    params.EnableAutoDepthStencil = FALSE;  
    params.AutoDepthStencilFormat = D3DFMT_UNKNOWN;  
    params.Flags = 0;  
    params.FullScreen_RefreshRateInHz = 0;  
    params.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;  
  
HRESULT hr = d3d9->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, p, &device9);
    if (FAILED(hr)) {  
        return false;  
    }  
  
    device9->SetRenderState(D3DRS_LIGHTING, false);  
    hr = device9->CreateVertexBuffer(6 * sizeof(Vector), 0, D3D_FVF_VECTOR, D3DPOOL_SYSTEMMEM, &vectex_buffer9, nullptr);  
    if (FAILED(hr)) {  
        return false;  
    }  
    Vector *vectors;  
    vectex_buffer9->Lock(0, 0, (void**)&vectors, 0);  
    //创建Rectangle  
    vectors[0] = Vector(220, 340, 0, 0xffff0000);  
    vectors[1] = Vector(220, 140, 0, 0xffff0000);  
    vectors[2] = Vector(420, 140, 0, 0xffff0000);  
    vectors[3] = Vector(220, 340, 0, 0xffff0000);  
    vectors[4] = Vector(420, 140, 0, 0xffff0000);  
    vectors[5] = Vector(420, 340, 0, 0xffff0000);  
    vectex_buffer9->Unlock();  
  
      
    return true;  
}  
  
bool ReDrawRectangle() {  
    if (nullptr == device9) {  
        return false;  
    }  
    device9->Clear(0, nullptr, D3DCLEAR_TARGET, D3DCOLOR_ARGB(255, 0, 0, 0), 0.0f, 0);  
    device9->SetStreamSource(0, vectex_buffer9, 0, sizeof(Vector));  
    device9->SetFVF(D3D_FVF_VECTOR);  
    device9->BeginScene();  
    device9->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 2);  
    device9->EndScene();  
    device9->Present(0, 0, 0, 0);  
    return true;  
}  
  
bool Done() {  
    if (nullptr != vectex_buffer9) {  
        vectex_buffer9->Release();  
    }  
    if (nullptr != device9) {  
        device9->Release();  
    }  
    if (nullptr != d3d9) {  
        d3d9->Release();  
    }  
    return true;  
}  

你可能感兴趣的:(D3D)