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 に答える