引用参数以在头文件中起作用

时间:2013-09-16 18:07:22

标签: c++ pointers com header

做一些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));
}

(标题格式示例)

确定。好吧,尝试从头传递一个函数时出现了一个noob指针问题,我将几个COM对象传递给:

void InitializeD3D(HWND hWnd,
         IDXGISwapChain *Swapchain,
         ID3D11Device *Device,
         ID3D11DeviceContext *DeviceContext,
         ID3D11RenderTargetView *RenderTargetView,
         int ScreenWidth,
         int ScreenHeight){ ...

我使用& Swapchain和& Device ......调用主cpp文件中的函数参数:

   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()。

1 个答案:

答案 0 :(得分:0)

InitializeD3D看起来指向指向ID3D11Device的指针。你只传递一个指针。

ID3D11Device是一个界面;你不应该直接创建它的实例。您应该只声​​明一个指向该接口的指针,然后将该指针的地址传递给InitializeD3D,然后(如果成功)您的指针将被分配一个有效的地址。

    ID3D11Device *pDevice = NULL;
    InitializeD3D(hWnd,&Swapchain,&pDevice,&DeviceContext,
                  &RenderTargetView,ScreenWidth,ScreenHeight);