DirectX初始化问题 [英] DirectX initializing problem
问题描述
#include < Windows.h >
#include < windowsx.h >
#include < d3d11.h >
#include < D3DX11.h >
#include < D3DX10.h >
#pragma comment(lib,d3d11.lib)
#pragma comment(lib,d3dx11.lib)
#pragma comment(lib,d3dx10.lib)
IDXGISwapChain * swapChain;
ID3D11Device * dev;
ID3D11DeviceContext * devcon;
ID3D11RenderTargetView * backBuffer;
// 初始化并准备使用direct3D
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;
scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
scd.OutputWindow = hWnd;
scd.SampleDesc.Count = 4 ;
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);
// swapChain-> GetBuffer(0,__ uuidof(ID3D11Texture2D),(LPVOID *)pBackBuffer);
// 使用后台缓冲区地址创建渲染目标
dev-> CreateRenderTargetView(pBackBuffer,NULL和& backBuffer);
pBackBuffer-> Release();
// 将渲染目标设置为后台缓冲区
devcon-> OMSetRenderTargets( 1 ,& backBuffer,NULL);
// 设置视口
D3D11_VIEWPORT viewPort;
ZeroMemory(& viewPort, sizeof (D3D11_VIEWPORT));
viewPort.TopLeftX = 0 ;
viewPort.TopLeftY = 0 ;
viewPort.Width = 500 ;
viewPort.Height = 400 ;
devcon-> RSSetViewports( 1 ,& viewPort);
}
// 以呈现单个框架
void renderFrame()
{
devcon-> ClearRenderTargetView(backBuffer,D3DXCOLOR( 0 .0f, 0 .2f, 0 .4f, 1 跨度> .0f));
// 切换后备缓冲区和前缓冲区
swapChain- >存在( 0 , 0 );
}
// 清理direct3D和COM
void cleanD3D( void )
{
swapChain-> Release( );
dev-> Release();
devcon-> Release();
}
LRESULT CALLBACK winproc(HWND,UINT,WPARAM,LPARAM);
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR cmdLine, int cmdShow)
{
WNDCLASSEX wc;
ZeroMemory(& wc, sizeof (WNDCLASSEX));
wc.cbSize = sizeof (WNDCLASSEX);
wc.style = CS_VREDRAW | CS_HREDRAW;
wc.lpfnWndProc = winproc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground =(HBRUSH)COLOR_WINDOW;
wc.lpszClassName = L windowclass;
RegisterClassEx(& wc);
RECT clientRect = { 0 , 0 , 500 , 400 };
AdjustWindowRect(& clientRect,WS_OVERLAPPEDWINDOW,FALSE);
HWND hWnd = CreateWindowEx(NULL,L windowclass,L < span class =code-string> window,
WS_OVERLAPPEDWINDOW,
10 , 10 ,
clientRect.right - clientRect.left,
clientRect.bottom - clientRect.top,
NULL,NULL,hInstance,NULL);
ShowWindow(hWnd,cmdShow);
MSG msg;
while ( true )
{
if (PeekMessage(& msg,NULL, 0 , 0 ,PM_REMOVE))
{
TranslateMessage(& msg);
DispatchMessage(& msg);
// 如果消息非常
<,则打破循环span class =code-keyword> if (msg.message == WM_QUIT)
break ;
}
其他
{
// < span class =code-comment>其他一切
}
}
return 0 跨度>;
}
LRESULT CALLBACK winproc(HWND hWnd,UINT消息,WPARAM wParam,LPARAM lParam)
{
if (message == WM_QUIT)
{
cleanD3D();
}
if (message == WM_CREATE)
{
initD3D(hWnd);
renderFrame();
}
return DefWindowProc(hWnd,message,wParam,lParam);
}
嗨..我是DirectX编程的初学者..
以上是代码我试图实现一个directX窗口..
但是当我运行它时,它会因错误而停止。
它说
myprogram.exe中0x00EB167C处的未处理异常:0xC000041D:在用户回调期间遇到未处理的异常。
如果我更改此
D3D11CreateDeviceAndSwapChain
(
NULL,
D3D_DRIVER_TYPE_HARDWARE,
NULL,
NULL,
NULL,
NULL,
D3D11_SDK_VERSION,
& scd,
& swapChain,
& dev,
NULL,
& ; devcon
);
这个
< pre lang =c ++> D3D11CreateDeviceAndSwapChain
(
NULL,
D3D_DRIVER_TYPE_REFERENCE,
NULL,
NULL,
NULL,
NULL,
D3D11_SDK_VERSION,
& scd,
& swapChain,
& amp; dev,
NULL,
& devcon
);
它工作正常。 。
但是客户区是正常的。我的意思是它不是蓝色或类似于教程中的内容。它作为一个普通窗口着色..
我的代码有什么问题?或者我的硬件有什么问题吗?
提前致谢。
#include <Windows.h>
#include <windowsx.h>
#include <d3d11.h>
#include <D3DX11.h>
#include <D3DX10.h>
#pragma comment(lib, "d3d11.lib")
#pragma comment(lib, "d3dx11.lib")
#pragma comment(lib, "d3dx10.lib")
IDXGISwapChain *swapChain;
ID3D11Device *dev;
ID3D11DeviceContext *devcon;
ID3D11RenderTargetView* backBuffer;
//to initialize and prepare the direct3D for use
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;
scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
scd.OutputWindow = hWnd;
scd.SampleDesc.Count = 4;
scd.Windowed = TRUE;
//creating a device using above information
D3D11CreateDeviceAndSwapChain
(
NULL,
D3D_DRIVER_TYPE_HARDWARE,
NULL,
NULL,
NULL,
NULL,
D3D11_SDK_VERSION,
&scd,
&swapChain,
&dev,
NULL,
&devcon
);
//get the address of the back buffer
ID3D11Texture2D *pBackBuffer;
swapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&pBackBuffer);
//swapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)pBackBuffer);
//create the render target using back buffer address
dev->CreateRenderTargetView(pBackBuffer, NULL, &backBuffer);
pBackBuffer->Release();
//set the render target as the back buffer
devcon->OMSetRenderTargets(1, &backBuffer, NULL);
//set the viewport
D3D11_VIEWPORT viewPort;
ZeroMemory(&viewPort, sizeof(D3D11_VIEWPORT));
viewPort.TopLeftX = 0;
viewPort.TopLeftY = 0;
viewPort.Width = 500;
viewPort.Height = 400;
devcon->RSSetViewports(1, &viewPort);
}
//to render a single frame
void renderFrame()
{
devcon->ClearRenderTargetView(backBuffer, D3DXCOLOR(0.0f, 0.2f, 0.4f, 1.0f));
//switch the backbuffer and the frontbuffer
swapChain->Present(0, 0);
}
//to cleanup direct3D and COM
void cleanD3D(void)
{
swapChain->Release();
dev->Release();
devcon->Release();
}
LRESULT CALLBACK winproc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR cmdLine, int cmdShow)
{
WNDCLASSEX wc;
ZeroMemory(&wc, sizeof(WNDCLASSEX));
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_VREDRAW | CS_HREDRAW;
wc.lpfnWndProc = winproc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
wc.lpszClassName = L"windowclass";
RegisterClassEx(&wc);
RECT clientRect = { 0, 0, 500, 400 };
AdjustWindowRect(&clientRect, WS_OVERLAPPEDWINDOW, FALSE);
HWND hWnd = CreateWindowEx(NULL, L"windowclass", L"window",
WS_OVERLAPPEDWINDOW,
10, 10,
clientRect.right - clientRect.left,
clientRect.bottom - clientRect.top,
NULL, NULL, hInstance, NULL);
ShowWindow(hWnd, cmdShow);
MSG msg;
while (true)
{
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
//break the loop if message is quite
if (msg.message == WM_QUIT)
break;
}
else
{
//everything else
}
}
return 0;
}
LRESULT CALLBACK winproc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
if (message == WM_QUIT)
{
cleanD3D();
}
if (message == WM_CREATE)
{
initD3D(hWnd);
renderFrame();
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
Hi.. Im beginner to DirectX programming..
And above is the code I've tried to implement a directX window..
But when I run it, it stops with an error.
It says
"Unhandled exception at 0x00EB167C in myprogram.exe: 0xC000041D: An unhandled exception was encountered during a user callback."
if I change this
D3D11CreateDeviceAndSwapChain
(
NULL,
D3D_DRIVER_TYPE_HARDWARE,
NULL,
NULL,
NULL,
NULL,
D3D11_SDK_VERSION,
&scd,
&swapChain,
&dev,
NULL,
&devcon
);
as this
D3D11CreateDeviceAndSwapChain
(
NULL,
D3D_DRIVER_TYPE_REFERENCE,
NULL,
NULL,
NULL,
NULL,
D3D11_SDK_VERSION,
&scd,
&swapChain,
&dev,
NULL,
&devcon
);
it works fine..
But the client area is normal. I mean it's not bluish or something like what's in the tutorial. It colored as a normal window..
What's the wrong with my code ? or is there anything wrong with my hardware ?
Thanks in advance.
这篇关于DirectX初始化问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!