我最初的过度绘制问题是因为我的源图像实际上没有被正确清除。哎呀。一旦我解决了这个问题,我只是把我的面具弄错了;没有想到 SDL 是如何使用这些的。最终的工作代码如下:
这是将 QImage 转换为 SDL_Surface 的函数:
/*!
* Converts a QImage to an SDL_Surface.
* The source image is converted to ARGB32 format if it is not already.
* The caller is responsible for deallocating the returned pointer.
*/
SDL_Surface* QImage_toSDLSurface(const QImage &sourceImage)
{
// Ensure that the source image is in the correct pixel format
QImage image = sourceImage;
if (image.format() != QImage::Format_ARGB32)
image = image.convertToFormat(QImage::Format_ARGB32);
// QImage stores each pixel in ARGB format
// Mask appropriately for the endianness
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
Uint32 amask = 0x000000ff;
Uint32 rmask = 0x0000ff00;
Uint32 gmask = 0x00ff0000;
Uint32 bmask = 0xff000000;
#else
Uint32 amask = 0xff000000;
Uint32 rmask = 0x00ff0000;
Uint32 gmask = 0x0000ff00;
Uint32 bmask = 0x000000ff;
#endif
return SDL_CreateRGBSurfaceFrom((void*)image.constBits(),
image.width(), image.height(), image.depth(), image.bytesPerLine(),
rmask, gmask, bmask, amask);
}
而我的绘图功能的核心:
// screen == SDL_GetVideoSurface()
// finalComposite == my QImage that SDL will convert and display
SDL_FillRect(screen, NULL, SDL_MapRGBA(screen->format, 255, 255, 255, 255));
SDL_Surface *surf = QImage_toSDLSurface(finalComposite);
SDL_BlitSurface(surf, NULL, screen, NULL);
SDL_FreeSurface(surf);
SDL_Flip(screen);