我有我一直在使用的代码来加载并将SDL_Surface
s 转换为 OpenGL 纹理,但是我意识到它们只适用于 RGB(A) 表面。我需要将支持扩展到索引模式图像(有或没有透明度)。
最初,我正在研究::SDL_SetColorKey()
,但它似乎只适用于 SDL blits。我已经阅读了SDL_Surface
、SDL_PixelFormat
和SDL_Color
,然后开始草拟以下内容(//# 是伪代码):
SDL_Surface *pSurf(::IMG_Load(image));
::SDL_LockSurface(pSurf);
Uint32 bytesPerPixel(pSurf->format->bytesPerPixel);
GLenum pixelFormat;
Uint8 *pixelData(pSurf->pixels);
bool allocated(false); // pixelData isn't allocated
if(pSurf->format->palette != 0) // indexed mode image
{
//# Determine transparency. // HOW?
//# bytesPerPixel = 3 or 4 depending on transparency being present;
//# pixelFormat = GL_RGB or GL_RGBA depending on bytesPerPixel;
Uint32 blockSize(pSurf->w * pSurf->h * bytesPerPixel);
pixelData = new Uint8[blockSize];
allocated = true;
//# traverse pSurf->pixels, look up pSurf->format->palette references and copy
// colors into pixelData;
}
else
{
//# Determine pixelFormat based on bytesPerPixel and pSurf->format->Rmask
// (GL_RGB(A) or GL_BGR(A)).
}
//# Pass bytesPerPixel, pixelFormat and pixelData to OpenGL (generate texture,
// set texture parameters, glTexImage2D etc).
if(allocated)
{
delete[] pixelData;
pixelData = 0;
}
::SDL_UnlockSurface(pSurf);
::SDL_FreeSurface(pSurf);
所以,问题是:如何确定我传递给该例程的索引模式图像是否具有透明度?