0

I'm trying to convert this C++ Direct3D function to Delphi, but I'm with a trouble..

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;
}

This is my Delphi converted function with errors:

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;

I guess I'm converting something wrong, but I don't know what...

Can anyone help-me? thanks

paulohr
  • 576
  • 1
  • 9
  • 24

3 Answers3

2

Your variable is called colour16, not color16.

You also have another error higher up. Remember that in C, return exits the function immediately, which is not the case in Delphi, so you'll want something like this on the if failed call:

if failed(pD3Ddev.CreateTexture(8, 8, 1, 0, D3DFMT_A4R4G4B4, D3DPOOL_MANAGED, ppD3Dtex, nil)) then
begin
  result := E_FAIL;
  Exit;
end;
ESG
  • 8,988
  • 3
  • 35
  • 52
  • As far as I can tell, but you do have a compile warning on LockRect (3rd parameter should be a pointer, not an int) – ESG Apr 12 '12 at 16:36
1

Just one small thing I noticed : Inc(pDst16^) should be below the assignment, as the c++ version uses the post-increment notation, not pre-increment.

PatrickvL
  • 4,104
  • 2
  • 29
  • 45
0
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;
Slex
  • 1