I’m trying to integrate a Qt library with an SDL application. I’d like to convert a QPixmap to an SDL_Surface, and then display that surface. How can I do this? I’ve not been able to find any good examples.
I’ve managed the following code so far:
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);
This works, but the only problem is that each time my QImage-based surface is painted, the underlying area is not cleared and the transparent parts “fade” into solid over the course of a few frames.
I do have SDL_FillRect which I would imagine clears the screen but it doesn’t seem to be doing so. screen is the primary SDL surface.
My original problem of overpainting was because my source image was actually not getting cleared properly. Oops. Once I fixed that, I simply had my masks wrong; not having it click in my mind how SDL was using these. The final, working code is as follows:
Here’s the function to convert a QImage to an SDL_Surface:
And the core of my drawing function: