Search code examples
c++directxsetpixelsurface

setting DirectX9 surface pixels


I'm trying to set the individual pixels in a D3DSURFACE9 but they're going all over the place. I think I've done this before but can't seem to get it right this time.

3DLOCKED_RECT lrt;
if(D3D_OK == lpThis->sfRenderingCanvas->LockRect(&lrt,NULL,0))
{
   UINT pitch = lrt.Pitch;
   VOID *data;
   data = lrt.pBits;
   UINT Y = (UINT)xmsg.Y;
   UINT X = (UINT)xmsg.X;
   for(int z=0;xmsg.iNum;z++)
   {
      if( xmsg.iDataBlock[z]>0 )
         ((DWORD*)data)[X+Y*pitch+z] = 0xFFFFFF00;
      else
         ((DWORD*)data)[X+Y*pitch+z] = 0xFF000000;
      }
   }
}

Y is between 0 and the height used when creating the surface
X is between 0 and the pitch of the surface

Can anybody tell what I'm doing wrong? Also, it seems to go about twice as far down as my window. (^If I try to draw over 1/4 the rows, it covers 1/2 of them.)


Solution

  • The pitch value that comes back in the D3DLOCKED_RECT is the number of bytes between the start of each row, not the number of DWORDs. You're indexing into the buffer using a DWORD pointer, so you are effectively using a pitch four times too large.

    Try something like this...

    DWORD * row = (DWORD *)((char *)lrt.pBits + pitch * Y);
    for(int z=0;xmsg.iNum;z++)
    {
       if( xmsg.iDataBlock[z]>0 )
          row[X+z] = 0xFFFFFF00;
       else
          row[X+z] = 0xFF000000;
       }
    }