参考
https://www.braynzarsoft.net/viewtutorial/q16390-3-initializing-direct3d-11
背景颜色那里有问题,改了一下表达方式
const float bgColor[] = {red, green, blue, 1.0f};
sp_context->ClearRenderTargetView(sp_rtv, bgColor);
效果就是背景颜色一直在变化
代码如下:
#pragma comment(lib,"d3d11.lib")
#include <windows.h>
#include <d3d11.h>
#include <directxcolors.h>
IDXGISwapChain* sp_swapchain;
ID3D11Device* sp_device;
ID3D11DeviceContext* sp_context;
ID3D11RenderTargetView* sp_rtv;
float red = 0.0f, green = 0.0f, blue = 0.0f;
int colormodr = 1, colormodg = 1, colormodeb = 1;
const int Width = 300;
const int Height = 300;
LPCTSTR g_szClassName = L"BzTutsApp";
WNDCLASSEX wc;
HWND hwnd;
MSG Msg;
bool InitWinWindow(HINSTANCE hInstance, int nCmdShow);
bool Initd3dWindow(HINSTANCE hInstance);
bool InitScene();
int messageloop();
void UpdateScene();
void DrawScene();
// 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)
{
if (!InitWinWindow(hInstance,nCmdShow))
{
MessageBox(NULL, L"Init WinWindow Failed!", L"Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
if (!Initd3dWindow(hInstance))
{
MessageBox(NULL, L"Init d3dWindow Failed!", L"Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
if (!InitScene())
{
MessageBox(NULL, L"Init Scene Failed!", L"Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
messageloop();
}
bool InitWinWindow(HINSTANCE hInstance, int nCmdShow)
{
//Step 1: Registering the Window Class
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 = g_szClassName;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if (!RegisterClassEx(&wc))
{
MessageBox(NULL, L"Window Registration Failed!", L"Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
// Step 2: Creating the Window
hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
g_szClassName,
L"The title of my window",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
NULL, NULL, hInstance, NULL);
if (hwnd == NULL)
{
MessageBox(NULL, L"Window Creation Failed!", L"Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
return true;
}
bool Initd3dWindow(HINSTANCE hInstance)
{
DXGI_MODE_DESC bufferDesc;
ZeroMemory(&bufferDesc, sizeof(DXGI_MODE_DESC));
bufferDesc.Width = Width;
bufferDesc.Height = Height;
bufferDesc.RefreshRate.Numerator = 60;
bufferDesc.RefreshRate.Denominator = 1;
bufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
bufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
bufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
DXGI_SWAP_CHAIN_DESC swapChianDesc;
ZeroMemory(&swapChianDesc, sizeof(DXGI_SWAP_CHAIN_DESC));
swapChianDesc.BufferDesc = bufferDesc;
swapChianDesc.SampleDesc.Count = 1;
swapChianDesc.SampleDesc.Quality = 0;
swapChianDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
swapChianDesc.BufferCount = 1;
swapChianDesc.OutputWindow = hwnd;
swapChianDesc.Windowed = true;
swapChianDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, NULL, NULL,
NULL, D3D11_SDK_VERSION, &swapChianDesc, &sp_swapchain, &sp_device, NULL, &sp_context);
ID3D11Texture2D* BackBuffer;
sp_swapchain->GetBuffer(0, __uuidof(ID3D11Texture2D), (void**)&BackBuffer);
sp_device->CreateRenderTargetView(BackBuffer, NULL, &sp_rtv);
sp_context->OMSetRenderTargets(1, &sp_rtv, NULL);
return true;
}
bool InitScene()
{
return true;
}
void UpdateScene()
{
red += colormodr * 0.0005f;
green += colormodg * 0.0002f;
blue += colormodeb * 0.0001;
if (red >= 1.0f || red <= 0.0f)
colormodr *= -1;
if (green >= 1.0f || green <= 0.0f)
colormodg *= -1;
if (blue >= 1.0f || blue <= 0.0f)
colormodeb *= -1;
}
void DrawScene()
{
const float bgColor[] = {red, green, blue, 1.0f};
sp_context->ClearRenderTargetView(sp_rtv, bgColor);
sp_swapchain->Present(0, 0);
}
int messageloop()
{
MSG msg;
ZeroMemory(&msg, sizeof(MSG));
while (true)
{
BOOL PeekMessageL(LPMSG lpMsg, HWND hWnd, UINT wMsgFilterMin,
UINT wMsgFilterMax, UINT wRemoveMsg);
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if (msg.message == WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
UpdateScene();
DrawScene();
}
}
return msg.wParam;
}