【发布时间】:2014-01-13 08:37:48
【问题描述】:
我正在 SDL2 中加载一个 PNG 文件,并且我正在尝试在 spritesheet 动画期间找到要跟踪的“特殊”像素颜色。我已将这些像素放入我的图像中,但我的代码没有找到它们。
我正在使用此代码读取像素(取自互联网,包装到我自己的 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 语句中设置了一个断点,以检查是否已找到颜色,但从未到达断点。有谁知道问题出在哪里?
附:我也尝试过使用 SDL_MapRGB() 但这也不起作用。
[编辑]
好的,所以我尝试在整个图像的 0,0 处放置一个像素,RGB 值为 66、77 和 88。它将它们读入为 84、96 和 107,因此很明显颜色要么被改变,要么没有被读入适当地。但是,当我尝试使用特定的 alpha 值时,它会完美地读取它。我会将我的系统更改为仅使用 alpha 值,但似乎我使用的像素编辑器会在您放入像素并将其与图像的其余部分混合后删除 alpha 值。
【问题讨论】: