简体   繁体   中英

SDL2 Transparent surface

I am trying to create a transparent surface, blit it bigger surface (size of my screen), then create a texture of it, copy to the renderer, and finaly render present.

I have seen many other forums saying I have to use SetBlendMode (for surface, texture, and renderer), ColorKey, SetSurfaceBlendMode. I tried them all but I can't seem to get it to work. I read that SDL_BlitScaled is not suitable for combining surfaces that have transparent pixels, but I am completely lost as to what I have to do instead.

What I noticed, when I use SDL_CreateRGBSurface (with an alpha value) to create a surface, the surface's PixelFormat is RGB888 instead of RGBA8888 (what I am expecting since I provide the alpha value). Using SDL_ConvertSurfaceFormat did not help.

Can somebody tell me what I am missing?

Complete code: removed, my appologies

Please note that I have removed the attempts to get transparency working

The renderer:

mRenderer = SDL_CreateRenderer(mWindow, -1, SDL_RENDERER_ACCELERATED);

My render loop:

void CApp::Render()
{
    SDL_RenderClear(mRenderer);

    mBackGround->Render(mRenderer);
    mForeGround->Render(mRenderer);

    SDL_RenderPresent(mRenderer);
}

The big surface I am blitting to:

mSurface = SDL_CreateRGBSurface(0, CApp::Window_W(), CApp::Window_H(), 32, 0, 0, 0, 0);

The code to get tiles from a spritesheet:

bool Map::GetTilesFromSpriteSheet(SDL_Surface *pSpriteSheet, int pTile_w, int pTile_h)
{
    if(pSpriteSheet->w % pTile_w == 0 && pSpriteSheet->h % pTile_h == 0) {
        SDL_Rect srcRect;
        srcRect.w = pTile_w;
        srcRect.h = pTile_h;
        for(int y = 0; y < pSpriteSheet->h / pTile_h; y++) {
            for(int x = 0; x < pSpriteSheet->w / pTile_w; x++) {
                srcRect.x = x*pTile_w;
                srcRect.y = y*pTile_h;
                SDL_Surface* tempSurface = SDL_CreateRGBSurface(0, pTile_w, pTile_h, 32, 0, 0, 0, 0);
                if(SDL_BlitSurface(pSpriteSheet, &srcRect, tempSurface, nullptr)==0) {
                    mTiles.push_back(tempSurface);
                } else {
                    Log("Error extracting tile (%d,%d)(w,h): %s", x, y, SDL_GetError());
                    return false;
                }
            }
        }
        Log("Number of tiles: %d", static_cast<int>(mTiles.size()));
    } else {
        Log("Background spritesheet is incompatible with tile dimensions (%d,%d)(w,h).", pTile_w, pTile_h);
        return false;
    }
    return true;
}

This is where I combine the tiles to create the big surface:

bool Map::GenerateMap(std::vector<std::vector<int>> &pMap, std::vector<SDL_Surface*> &pTiles, SDL_Surface* pDestination)
{
    SDL_Rect rect;
    rect.w = mDstTile_W;
    rect.h = mDstTile_H;
    SDL_Surface* transparent = SDL_CreateRGBSurface(0, mDstTile_W, mDstTile_H, 32, 0, 0, 0, 0);

    for(int y = 0; y < static_cast<int>(pMap.size()); y++) {
        for(int x = 0; x < static_cast<int>(pMap.at(static_cast<unsigned long>(y)).size()); x++) {
            rect.x = x*mDstTile_W;
            rect.y = y*mDstTile_H;
            int index = static_cast<int>(pMap.at(static_cast<unsigned long>(y)).at(static_cast<unsigned long>(x)));
            if(index < 0) {
                if(SDL_BlitScaled(transparent, nullptr, pDestination, &rect) != 0) {
                    Log("Error blitting transparent surface to destination: %s", SDL_GetError());
                }
            } else if(SDL_BlitScaled(pTiles[static_cast<unsigned long>(index)],
                              nullptr, pDestination, &rect) != 0) {
                Log("Error blitting surface to destination: %s", SDL_GetError());
                return false;
            }
        }
    }
    SDL_FreeSurface(transparent);
    return true;
}

And finaly, this is the code to render the big surface to the screen, first by creating a texture:

void ForeGround::Render(SDL_Renderer* pRenderer)
{
    if(mTexture) SDL_DestroyTexture(mTexture);
    mTexture = SDL_CreateTextureFromSurface(pRenderer, mSurface);

    if(mTexture == nullptr) {
        Log("Unable to create foreground texture: %s", SDL_GetError());
    } else if (SDL_RenderCopy(pRenderer, mTexture, nullptr, nullptr)) {
        Log("Unable to render foreground: %s", SDL_GetError());
    }
}

As @keltar mentioned, I was not creating surfaces with an alpha value.

I had to change the SDL_CreateRGBSurface call. The code snippet below creates an empty, transparent surface where I can blit to if needed (for example, tiles from a spritesheet).

SDL_Surface* tempSurface = SDL_CreateRGBSurface(..., 32, 0xff, 0xff00, 0xff0000, 0xff000000);

The code snippet below creates a black, non-transparent surface.

SDL_Surface* tempSurface = SDL_CreateRGBSurface(..., 32, 0xff, 0xff00, 0xff0000, 0x00000000);

This was enough for me, I did not have to use SDL_SetRenderDrawBlendMode , SDL_SetSurfaceBlendMode , SDL_SetTextureBlendMode , or SDL_ConvertSurface . A quick look at the SDL wiki showed:

By default surfaces with an alpha mask are set up for blending as with

SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_BLEND)

Which explained why I did not have to call any of those functions. A big shout-out to keltar for his quick answer!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM