2012-11-01 29 views
2

如果我想創建一個D3D表面,我會像下面這樣做。相似的,如果我想創建一個類型爲IDirect3DSurface9的D3D曲面數組*我該如何在C++中完成?如何在C++中創建一個IDirect3DSurface9(D3D曲面)曲面的數組?

IDirect3DSurface9** ppdxsurface = NULL; 
IDirect3DDevice9 * pdxDevice = getdevice(); // getdevice is a custom function which gives me //the d3d device. 

pdxDevice->CreateOffscreenPlainSurface(720,480, 
               D3DFMT_A8R8G8B8, 
               D3DPOOL_DEFAULT, 
               pdxsurface, 
               NULL); 

QUERY ::如何創建D3D設備的C++數組?

回答

5

ppdxsurface沒有正確聲明,你需要提供指針指向指針對象,不只是指向指針的指針。它應是IDirect3DSurface9*,不IDirect3DSurface9**

IDirect3DSurface9* pdxsurface = NULL; 
IDirect3DDevice9* pdxDevice = getdevice(); 

pdxDevice->CreateOffscreenPlainSurface(720, 480, 
    D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, 
    &pdxsurface, // Pass pointer to pointer 
    NULL); 

// Usage: 
HDC hDC = NULL; 
pdxsurface->GetDC(hDC); 

要創建面的排列只是把它的循環:

// Define array of 10 surfaces 
const int maxSurfaces = 10; 
IDirect3DSurface9* pdxsurface[maxSurfaces] = { 0 }; 

for(int i = 0; i < maxSurfaces; ++i) 
{ 
    pdxDevice->CreateOffscreenPlainSurface(720, 480, 
     D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, 
     &pdxsurface[i], 
     NULL); 
} 

或者使用std::vector如果你喜歡動態數組:

std::vector<IDirect3DSurface9*> surfVec; 

for(int i = 0; i < maxSurfaces; ++i) 
{ 
    IDirect3DSurface9* pdxsurface = NULL; 
    pdxDevice->CreateOffscreenPlainSurface(720, 480, 
     D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, 
     &pdxsurface, 
     NULL); 
    surfVec.push_back(pdxsurface); 
} 
+0

謝謝。將「IDirect3DSurface9 * pdxsurface [maxSurfaces] = {0};」爲10個元素的數組創建內存,因爲我們剛剛實例化了1個元素{0}。我們不應該做{0,0,...... maxSurfaces(10)否:時間}? – codeLover

+0

@codeLover不,數組內存由declarartion創建。 '{}'只是初始化列表。我們用'0'初始化了第一個元素,以顯示該數組已初始化。其餘元素將被自動默認初始化(歸零)。實際上它等價於你的命題:'{0,0,0,0,0,0,0,0,0}',但要短得多。 – Rost

+0

::當我這樣做{0}甚至{0,0,...... 0},我得到構建錯誤,預期的常量表達式&不能分配一個大小爲0的數組。 – codeLover