2D Collision Mask DX9

Started by
2 comments, last by RogDX9 8 years, 11 months ago

Hello,

i'm trying to create 2D Collision Mask for A8R8G8B8 Texture, i'm not sure whats wrong, here is the code:



D3DLOCKED_RECT rect;
HRESULT hResult = _currentTexture->LockRect(0, &rect, NULL, D3DLOCK_READONLY);


if (SUCCEEDED(hResult))
{

  for (int y = 0; y < GetHeight(); ++y)
  { 
    for (int x = 0; x < GetWidth(); ++x)
    {
      //Should i keep getting this or just put it up outside of the loops will do?
      D3DCOLOR* pixels = (D3DCOLOR*)rect.pBits;

      //It fails here sometimes, there is something off when getting the pixel
      D3DCOLOR a = (pixels[y * rect.Pitch + x] & 0xFF000000) >> 24;  
    }
  }
 }

i appreciate all the help, thanks.

Advertisement

I'm guessing a bool * mask :

bool * mask = new bool[width*height];

Maybe something like this?:

For each y, I'd get the start pixel of the row (cast to BYTE* - pitch is in bytes) like this:
D3DCOLOR* pixline = (D3DCOLOR*)((BYTE*)rect.pBits + rect.Pitch*y);

Then inside the x loop:
D3DCOLOR pixel = pixline[x];

Then set mask to true or false based on alpha bits:
mask[x+y*width] = ((pixel&0xFF000000)!=0x00);


      //It fails here sometimes, there is something off when getting the pixel
      D3DCOLOR a = (pixels[y * rect.Pitch + x] & 0xFF000000) >> 24;  

pixels is a 4-byte structure, so array-access on it spans 4 bytes per index.
Your array access here is 4 times the bytes deep (roughly) it should be.
See above for improved version.


L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

Thanks guys for replies and help.

This topic is closed to new replies.

Advertisement