1

我正在尝试将 Qt 库与 SDL 应用程序集成。我想将 QPixmap 转换为 SDL_Surface,然后显示该表面。我怎样才能做到这一点?我找不到任何好的例子。

到目前为止,我已经管理了以下代码:

Uint32 rmask = 0x000000ff;
Uint32 gmask = 0x0000ff00;
Uint32 bmask = 0x00ff0000;
Uint32 amask = 0xff000000;

SDL_FillRect(screen, NULL, SDL_MapRGBA(screen->format, 255, 255, 255, 255));

const QImage *qsurf = ...;

SDL_Surface *surf = SDL_CreateRGBSurfaceFrom((void*)qsurf->constBits(), qsurf->width(), qsurf->height(), 32, qsurf->width() * 4, rmask, gmask, bmask, amask);
SDL_BlitSurface(surf, NULL, screen, NULL);
SDL_FreeSurface(surf);
SDL_Flip(screen);

这行得通,但唯一的问题是,每次绘制基于 QImage 的表面时,底层区域都不会被清除,并且透明部分会在几帧的过程中“淡化”为实体。

我确实有SDL_FillRect我想象的清除屏幕的方法,但它似乎没有这样做。screen是主要的 SDL 表面。

4

1 回答 1

0

我最初的过度绘制问题是因为我的源图像实际上没有被正确清除。哎呀。一旦我解决了这个问题,我只是把我的面具弄错了;没有想到 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);
于 2012-09-14T06:27:30.890 回答