做一些directx编程。我是自学成才的,这意味着在最“方便”的地方会弹出未知的教育漏洞。任何类型的答案都值得赞赏。你知道,撕开代码!xD
我将代码封装到一个静态库中,以消除我的测试和运行代码的可怕视图。我已经这样定义了标题:
#include "windows.h"
#include "windowsx.h"
#include "D3D11.h"
#include "D3Dx11.h"
#include "D3DX10.h"
//Infinity Machine Library
#pragma comment (lib, "D3D11.lib")
#pragma comment (lib, "D3Dx11.lib")
#pragma comment (lib, "D3Dx10.lib")
namespace InfinityEngine{
HWND hWnd;
HRESULT Compile_Monitor;
struct VERTEX{
float x,y,z;D3DXCOLOR Color;
};
//Initialize Graphics
void BuildBufferDescription(){
D3D11_BUFFER_DESC BufferDescription;
ZeroMemory(&BufferDescription, sizeof(BufferDescription));
BufferDescription.Usage =D3D11_USAGE_DYNAMIC;
BufferDescription.ByteWidth =sizeof(VERTEX)*3;
BufferDescription.BindFlags =D3D11_BIND_VERTEX_BUFFER;
BufferDescription.CPUAccessFlags =D3D11_CPU_ACCESS_WRITE;
BufferDescription.MiscFlags =0;
}
void BuildSubresourceData(VERTEX VertStruct[]){
D3D11_SUBRESOURCE_DATA SRData;
ZeroMemory(&SRData, sizeof(SRData));
SRData.pSysMem=VertStruct;
}
void CreateBuffer(D3D11_BUFFER_DESC BufferDescription,
D3D11_SUBRESOURCE_DATA SRData,
ID3D11Buffer* D3DBuffer,
ID3D11Device* Device){
Compile_Monitor = Device->CreateBuffer(&BufferDescription, &SRData, &D3DBuffer);
if(FAILED(Compile_Monitor))
{
MessageBox(hWnd, L"CreateBuffer() failed. Check for: \n Possible Bad or NULL value in D3D11_BUFFER_DESC struct.",
L"Program Error!",
MB_OK);
PostQuitMessage(0);
}
}
void MaptoVideoMemory(ID3D11Buffer* D3DBuffer,
D3D11_MAPPED_SUBRESOURCE MappedResource,
ID3D11DeviceContext* DeviceContext){
Compile_Monitor = DeviceContext->Map(D3DBuffer, NULL, D3D11_MAP_WRITE_DISCARD, NULL, &MappedResource);
if(FAILED(Compile_Monitor))
{
MessageBox(hWnd, L"Map() failed.",
L"Program Error!",
MB_OK);
PostQuitMessage(0);
}
}
void CopyBuffer(D3D11_MAPPED_SUBRESOURCE MappedResource,
VERTEX VertStruct[]){
memcpy(MappedResource.pData, VertStruct, sizeof(VertStruct));
}
(标题格式示例)
行。好吧,当我试图从头文件中声明一个函数时,出现了一个新手指针问题,我将几个 COM 对象传递给:
void InitializeD3D(HWND hWnd,
IDXGISwapChain *Swapchain,
ID3D11Device *Device,
ID3D11DeviceContext *DeviceContext,
ID3D11RenderTargetView *RenderTargetView,
int ScreenWidth,
int ScreenHeight){ ...
我在主 cpp 文件中调用函数参数,使用 &Swapchain, &Device... 如下所示:
InitializeD3D(hWnd,&Swapchain,&Device,&DeviceContext,&RenderTargetView,ScreenWidth,ScreenHeight);
当然,该函数需要格式中的类型(即):ID3D11Device** 但我传递的内容被读取为 ID3D11Device*。
我想我对指针有一般的理解,但缺少一些基本的东西——尤其是在实现它们的知识时我有点紧张。任何帮助表示赞赏。
编辑:
最初的 D3D11CreateDeviceAndSwapChain() 声明:
HRESULT WINAPI D3D11CreateDeviceAndSwapChain(
__in_opt IDXGIAdapter* pAdapter,
D3D_DRIVER_TYPE DriverType,
HMODULE Software,
UINT Flags,
__in_ecount_opt( FeatureLevels ) CONST D3D_FEATURE_LEVEL* pFeatureLevels,
UINT FeatureLevels,
UINT SDKVersion,
__in_opt CONST DXGI_SWAP_CHAIN_DESC* pSwapChainDesc,
__out_opt IDXGISwapChain** ppSwapChain,
__out_opt ID3D11Device** ppDevice,
__out_opt D3D_FEATURE_LEVEL* pFeatureLevel,
__out_opt ID3D11DeviceContext** ppImmediateContext );
它需要一个指向 ID3D11Device 的指针,我用相应类型的 &Device 填充该指针。当 D3D11CreateDeviceAndSwapChain() 移动到函数内部的标题时, &Device 不再用作参数,并调用 ID3D11Device** 。
编辑:
void InitializeD3D(HWND hWnd,
IDXGISwapChain* Swapchain,
ID3D11Device* Device,
ID3D11DeviceContext* DeviceContext,
ID3D11RenderTargetView* RenderTargetView,
int ScreenWidth,
int ScreenHeight){
DXGI_SWAP_CHAIN_DESC SwapChainDesc;
ZeroMemory(&SwapChainDesc, sizeof(DXGI_SWAP_CHAIN_DESC));
SwapChainDesc.BufferCount = 1;
SwapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
SwapChainDesc.BufferDesc.Width = ScreenWidth;
SwapChainDesc.BufferDesc.Height = ScreenHeight;
SwapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
SwapChainDesc.OutputWindow = hWnd;
SwapChainDesc.BufferDesc.RefreshRate.Numerator =60;
SwapChainDesc.BufferDesc.RefreshRate.Denominator =1;
SwapChainDesc.SampleDesc.Count = 4;
SwapChainDesc.Windowed = TRUE;
SwapChainDesc.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;
Compile_Monitor = D3D11CreateDeviceAndSwapChain(NULL,
D3D_DRIVER_TYPE_HARDWARE,
NULL,
NULL,
NULL,
NULL,
D3D11_SDK_VERSION,
&SwapChainDesc,
&Swapchain,
&Device,
NULL,
&DeviceContext);
if(FAILED(Compile_Monitor))
{
MessageBox(hWnd, L"D3D11CreateDeviceAndSwapChain() failed.",
L"Program Error!",
MB_OK);
}
ID3D11Texture2D *Texture2d;
Swapchain->GetBuffer(0,__uuidof(ID3D11Texture2D),(LPVOID*)&Texture2d);
Device->CreateRenderTargetView(Texture2d,NULL,&RenderTargetView);
Texture2d->Release();
DeviceContext->OMSetRenderTargets(1, &RenderTargetView, NULL);
D3D11_VIEWPORT Viewport;
ZeroMemory(&Viewport, sizeof(D3D11_VIEWPORT));
Viewport.Height =ScreenHeight;
Viewport.Width =ScreenWidth;
Viewport.MaxDepth =1.0;
Viewport.MinDepth =0.0;
Viewport.TopLeftX =0;
Viewport.TopLeftY =0;
DeviceContext->RSSetViewports(1, &Viewport);
}
以上是我来自静态标题的 InitializeD3D() 。