我正在加载SDL2中的PNG文件,我试图在spritesheet动画中查找'特殊'像素颜色来跟踪。我已将这些像素放入图像中,但我的代码没有找到它们。SDL2中的像素读取(和比较)
我使用此代码读取像素(从互联网上采取缠到我自己的Texture类):
Uint32 getpixel(SDL_Surface *surface, int x, int y)
{
int bpp = surface->format->BytesPerPixel;
/* Here p is the address to the pixel we want to retrieve */
Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
switch(bpp) {
case 1:
return *p;
break;
case 2:
return *(Uint16 *)p;
break;
case 3:
if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
return p[0] << 16 | p[1] << 8 | p[2];
else
return p[0] | p[1] << 8 | p[2] << 16;
break;
case 4:
return *(Uint32 *)p;
break;
default:
return 0; /* shouldn't happen, but avoids warnings */
}
}
而这些代码的重要位我使用的像素比较在“特殊”值我以前设置:
// convert special SDL_Color to Uint32
Uint32 spec1 = SDL_MapRGBA(_texture->GetSDLSurface()->format, _spec1.r, _spec1.g, _spec1.b, 255);
Uint32 spec2 = SDL_MapRGBA(_texture->GetSDLSurface()->format, _spec2.r, _spec2.g, _spec2.b, 255);
...和,同时通过在每个精灵帧的所有像素循环...
// get pixel at (x, y)
Uint32 pix = _texture->GetPixel(x, y);
// if pixel is a special value, store it in animation
if (pix == spec1)
{
SDL_Point pt = {x, y};
anim->Special1.push_back(pt);
found1 = true;
}
else if (pix == spec2)
{
SDL_Point pt = {x, y};
anim->Special2.push_back(pt);
found2 = true;
}
现在,我在这些if语句中设置一个断点来检查颜色是否被找到,但断点永远不会到达。有谁知道问题是什么?
P.S.我也尝试过使用SDL_MapRGB(),但这也不起作用。
[编辑]
好了,所以我试图把一个像素与在RGB整个图像值66,77和88的0,0它读取它们在84,96和107,所以很明显的颜色正在改变或者没有正确阅读。但是,当我用特定的alpha值尝试它时,它会完美地读取它。我会将我的系统更改为仅使用alpha值,但似乎我使用的像素编辑器在您放入像素并将其与图像的其余部分混合后,将删除Alpha值。