这在 ClearRenderTargetView 调用上失败。我不确定我的错误在哪里 - 我正在按照教程进行操作,但我不知道什么是不正确的。希望得到一些帮助,谢谢大家!
// DX6.cpp : 定义应用程序的入口点。//
#include "stdafx.h"
#include "DX6.h"
#include <d3d11.h>
#include <d3dx11.h>
#include <d3d10.h>
#include <d3dx9core.h>
// include the Direct3D Library file
#pragma comment (lib, "d3d11.lib")
#pragma comment (lib, "d3dx11.lib")
#pragma comment (lib, "d3dx10.lib")
//forward declares
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void InitD3D(HWND hWnd); // sets up and initializes Direct3D
void RenderFrame(void);
void CleanD3D(void); // closes Direct3D and releases memory
// Global Variables:
HWND hWnd;
HINSTANCE hInst;
IDXGISwapChain *swapchain; // the pointer to the swap chain interface
ID3D11Device *dev; // the pointer to our Direct3D device interface
ID3D11DeviceContext *devcon; // the pointer to our Direct3D device context
ID3D11RenderTargetView *backbuffer; // global declaration
int APIENTRY _tWinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPTSTR lpCmdLine,int nCmdShow)
{
WNDCLASSEX wc;
ZeroMemory(&wc,sizeof(WNDCLASSEX));
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
wc.lpszClassName = L"WindowClass1";
RegisterClassEx(&wc);
// TODO: Place code here.
MSG msg={0};
hInst = hInstance; // Store instance handle in our global variable
hWnd = CreateWindow(L"WindowClass1", L"Test Title", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return 1;
}
ShowWindow(hWnd, nCmdShow);
// Main message loop:
while (true)
{
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
// translate keystroke messages into the right format
TranslateMessage(&msg);
// send the message to the WindowProc function
DispatchMessage(&msg);
// check to see if it's time to quit
if(msg.message == WM_QUIT)
break;
}
else{
RenderFrame();
}
}
return (int) msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
//D3D
void InitD3D(HWND hWnd){
DXGI_SWAP_CHAIN_DESC scd;
ZeroMemory(&scd,sizeof(DXGI_SWAP_CHAIN_DESC));
scd.BufferCount=1;
scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; // use 32-bit color
scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; // how swap chain is to be used
scd.OutputWindow = hWnd; // the window to be used
scd.SampleDesc.Count = 4; // how many multisamples
scd.Windowed = TRUE;
D3D11CreateDeviceAndSwapChain(NULL,D3D_DRIVER_TYPE_HARDWARE,NULL,NULL,NULL,NULL,D3D11_SDK_VERSION,&scd,&swapchain,&dev,NULL,&devcon);
ID3D11Texture2D *pBackBuffer;
swapchain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&pBackBuffer);
// use the back buffer address to create the render target
dev->CreateRenderTargetView(pBackBuffer, NULL, &backbuffer);
pBackBuffer->Release();
// set the render target as the back buffer
devcon->OMSetRenderTargets(1, &backbuffer, NULL);
//create viewport
D3D11_VIEWPORT viewport;
ZeroMemory(&viewport,sizeof(D3D11_VIEWPORT));
viewport.TopLeftX=0;
viewport.TopLeftY=0;
viewport.Width=800;
viewport.Height=800;
//set viewport
devcon->RSSetViewports(1,&viewport);
}
void RenderFrame(void){
//FAILS HERE!!!
//Unhandled exception at 0x002e1a9f in DX6.exe: 0xC0000005: Access violation //reading location 0x00000000.
devcon->ClearRenderTargetView(backbuffer,D3DXCOLOR(0.0f,0.2f,0.4f,1.0f));
//switch back & front buffer
swapchain->Present(0,0);
}
void CleanD3D()
{
// close and release all existing COM objects
swapchain->Release();
dev->Release();
devcon->Release();
}