簡體   English   中英

將Direct3D C ++函數轉換為Delphi

[英]Convert Direct3D C++ function to Delphi

我正在嘗試將此C ++ Direct3D函數轉換為Delphi,但是我遇到了麻煩。

HRESULT GenerateTexture(IDirect3DDevice9 *pD3Ddev, IDirect3DTexture9 **ppD3Dtex, DWORD colour32)
{
    if( FAILED(pD3Ddev->CreateTexture(8, 8, 1, 0, D3DFMT_A4R4G4B4, D3DPOOL_MANAGED, ppD3Dtex, NULL)) )
        return E_FAIL;

    WORD colour16 =    ((WORD)((colour32>>28)&0xF)<<12)
            |(WORD)(((colour32>>20)&0xF)<<8)
            |(WORD)(((colour32>>12)&0xF)<<4)
            |(WORD)(((colour32>>4)&0xF)<<0);

    D3DLOCKED_RECT d3dlr;  
    (*ppD3Dtex)->LockRect(0, &d3dlr, 0, 0);
    WORD *pDst16 = (WORD*)d3dlr.pBits;

    for(int xy=0; xy < 8*8; xy++)
        *pDst16++ = colour16;

    (*ppD3Dtex)->UnlockRect(0);

    return S_OK;
}

這是我的Delphi轉換函數,但有錯誤:

function GenerateTexture(pD3Ddev: IDirect3DDevice9; ppD3Dtex: IDirect3DTexture9; colour32: dword):HRESULT;
var
 colour16: word;
 d3dlr: D3DLOCKED_RECT;
 pDst16: pword;
 xy: integer;
begin
 if failed(pD3Ddev.CreateTexture(8, 8, 1, 0, D3DFMT_A4R4G4B4, D3DPOOL_MANAGED, ppD3Dtex, nil)) then result := E_FAIL;

 colour16 := (word(((colour32 shr 28)and $F) shl 12)
             or word((((colour32 shr 20)and $F) shl 8))
             or word((((colour32 shr 12)and $F) shl 4))
             or word((((colour32 shr 4)and $F) shl 0)));

  ppD3Dtex.LockRect(0, d3dlr, 0, 0);
  pDst16 := PWORD(d3dlr.pBits);
  xy:=0;
  while xy<(8*8) do begin
   Inc(pDst16^);
   pDst16^ := color16; //THIS IS THE LINE WITH ERROR: '('Expected but ';' found.
   inc(xy);
  end;
  ppD3Dtex.UnlockRect(0);

  Result := S_OK;
end;

我想我正在轉換錯誤,但是我不知道是什么...

誰能幫我? 謝謝

您的變量稱為colour16,而不是color16。

您還會遇到另一個錯誤。 請記住,在C語言中,return立即退出函數,而在Delphi中則不是這種情況,因此,如果if調用失敗,您將需要以下代碼:

if failed(pD3Ddev.CreateTexture(8, 8, 1, 0, D3DFMT_A4R4G4B4, D3DPOOL_MANAGED, ppD3Dtex, nil)) then
begin
  result := E_FAIL;
  Exit;
end;

我注意到的一件小事情:Inc(pDst16 ^)應該在賦值之下,因為C ++版本使用后遞增表示法,而不是前遞增表示法。

function GenerateTexture(pD3Ddev: IDirect3DDevice9; ppD3Dtex: IDirect3DTexture9; colour32: dword):HRESULT;
var
    colour16: word;
    d3dlr: D3DLOCKED_RECT;
    pDst16: pword;
    xy: integer;
begin
     if Failed(pD3Ddev.CreateTexture(8, 8, 1, 0, D3DFMT_A4R4G4B4, D3DPOOL_MANAGED, ppD3Dtex, nil)) then
     begin
        result := E_FAIL;
        Exit;
    end;
    colour16 := (word(((colour32 shr 28)and $F) shl 12)
        or word((((colour32 shr 20)and $F) shl 8))
        or word((((colour32 shr 12)and $F) shl 4))
        or word((((colour32 shr 4)and $F) shl 0)));

    ppD3Dtex.LockRect(0, d3dlr, nil, 0);
    pDst16 := PWORD(d3dlr.pBits);
    xy:=0;
    while xy<(8*8) do begin
        Inc(pDst16^);
        pDst16^ := colour16;
        Inc(xy);
    end;
    ppD3Dtex.UnlockRect(0);
    Result := S_OK;
end;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM