我正在编写一个基于DirectX11的游戏框架,但是我遇到了问题,我的纹理在屏幕上显示得很糟糕,这是一个截图:
正如你所看到的那样,图像并不完美,但我注意到只有当我将交换链初始化为窗口化时才会发生这种情况,如果我不这样做并且我将其初始化为全屏,则精灵正确显示,即使在运行时我从全屏切换到窗口仍然显示正确,这是屏幕上显示的图像:
我的交换链初始化:
RECT dimensions;
GetClientRect(game->Window, &dimensions);
unsigned int width = dimensions.right - dimensions.left;
unsigned int height = dimensions.bottom - dimensions.top;
D3D_DRIVER_TYPE driverTypes[] =
{
D3D_DRIVER_TYPE_HARDWARE,
D3D_DRIVER_TYPE_WARP,
D3D_DRIVER_TYPE_REFERENCE,
D3D_DRIVER_TYPE_SOFTWARE
};
unsigned int totalDriverTypes = ARRAYSIZE(driverTypes);
D3D_FEATURE_LEVEL featureLevels[] =
{
D3D_FEATURE_LEVEL_11_0,
D3D_FEATURE_LEVEL_10_1,
D3D_FEATURE_LEVEL_10_0
};
unsigned int totalFeatureLevels = ARRAYSIZE(featureLevels);
DXGI_SWAP_CHAIN_DESC swapChainDesc;
ZeroMemory(&swapChainDesc, sizeof(swapChainDesc));
swapChainDesc.BufferCount = DXGI_SWAP_EFFECT_SEQUENTIAL;
swapChainDesc.BufferDesc.Width = width;
swapChainDesc.BufferDesc.Height = height;
swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
swapChainDesc.BufferDesc.RefreshRate.Numerator = 60;
swapChainDesc.BufferDesc.RefreshRate.Denominator = 1;
swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
swapChainDesc.OutputWindow = game->Window;
swapChainDesc.Windowed = true;
swapChainDesc.SampleDesc.Count = 1;
swapChainDesc.SampleDesc.Quality = 0;
unsigned int creationFlags = 0;
#ifdef _DEBUG
creationFlags |= D3D11_CREATE_DEVICE_DEBUG;
#endif
HRESULT result;
unsigned int driver = 0;
pin_ptr<IDXGISwapChain*> swapChainPointer;
swapChainPointer = &swapChain_;
pin_ptr<ID3D11Device*> d3dDevicePointer;
d3dDevicePointer = &d3dDevice_;
pin_ptr<D3D_FEATURE_LEVEL> featureLevelPointer;
featureLevelPointer = &featureLevel_;
pin_ptr<ID3D11DeviceContext*> d3dContextPointer;
d3dContextPointer = &d3dContext_;
for (driver = 0; driver < totalDriverTypes; ++driver)
{
result = D3D11CreateDeviceAndSwapChain(0, driverTypes[driver], 0, creationFlags, featureLevels, totalFeatureLevels,
D3D11_SDK_VERSION, &swapChainDesc, swapChainPointer,
d3dDevicePointer, featureLevelPointer, d3dContextPointer);
if (SUCCEEDED(result))
{
driverType_ = driverTypes[driver];
break;
}
}
这是切换全屏的代码:
swapChain_->SetFullscreenState(isFullScreen, NULL);
其中IsFullScreen是传递给containsig函数的布尔值。
任何人都可以帮助我吗?提前谢谢!
修改
解决:
我在创建窗口时更改了WS_OVERLAPPED参数:
RECT rc = { 0, 0, WindowWidth, WindowHeight };
AdjustWindowRect(&rc, WS_OVERLAPPED | WS_MINIMIZEBOX | WS_SYSMENU, FALSE);
LPCTSTR title = Utilities::StringToLPCSTR(Title);
HWND hwnd = CreateWindowA("BSGame", title, WS_OVERLAPPED | WS_MINIMIZEBOX | WS_SYSMENU, CW_USEDEFAULT, CW_USEDEFAULT, rc.right - rc.left, rc.bottom - rc.top, NULL, NULL, windowHandler, NULL);
到WS_OVERLAPPEDWINDOW
答案 0 :(得分:0)
当您在SwapChain上调用SetFullScreen时,您只需要更改模式。
通常在通话结束后,您应该从主表单收到WM_SIZE消息,然后您需要执行以下操作:
释放绑定到交换链的任何相关资源(例如:Texture和RenderTargetView) 确保你也在设备上下文中调用ClearState,因为如果SwapChain仍然绑定到管道,你也会遇到问题(如果绑定到管道,运行时不会销毁资源)。
接下来你在swapchain上调用resize,例如:
HRESULT result = mSwapChain->ResizeBuffers(0,0,0,DXGI_FORMAT_UNKNOWN,0);
最后,您可以再次查询纹理:
ID3D11Texture2D* texture;
mSwapChain->GetBuffer(0,__uuidof(ID3D11Texture2D),(void**)(&texture));
//Get Buffer does an AddRef on top so we release
texture->Release();
创建一个新的RenderTargetView,并将ViewPort更新为新的大小(从纹理中获取描述)。
答案 1 :(得分:0)
就我而言,问题出在窗口创建期间,这是我的情况:
RECT rc = { 0, 0, WindowWidth, WindowHeight };
AdjustWindowRect(&rc, WS_OVERLAPPED | WS_MINIMIZEBOX | WS_SYSMENU, FALSE);
LPCTSTR title = Utilities::StringToLPCSTR(Title);
HWND hwnd = CreateWindowA("BSGame", title, WS_OVERLAPPED | WS_MINIMIZEBOX | WS_SYSMENU, CW_USEDEFAULT, CW_USEDEFAULT, rc.right - rc.left, rc.bottom - rc.top, NULL, NULL, windowHandler, NULL);
要解决我已将WS_OVERLAPPED更改为WS_OVERLAPPEDWINDOW