SDL2 中不显示具有透明度的 Bmp 纹理

问题描述 投票:0回答:1

我正在研究在 Windows 10 上用 C++ 在 SDL2 中创建的纹理。

我想以透明方式显示 bmp 文件(light.bmp)的纹理。

我编写了这个代码示例,它应该显示具有透明度的 bmp 纹理“light.bmp”,但实际上并没有显示任何内容:

#include <iostream>

#include <SDL.h>
//#include <SDL_image.h>

const int WIN_WIDTH = 640;
const int WIN_HEIGHT = 480;

int main(int argc, char* argv[])
{
    if (SDL_Init(SDL_INIT_EVERYTHING) != 0) 
    {
        SDL_Log("SDL_Init failed: %s", SDL_GetError());
        return 1;
    }
    SDL_Window* win = SDL_CreateWindow("Rendering to a texture!", 
                                       SDL_WINDOWPOS_CENTERED,
                                       SDL_WINDOWPOS_CENTERED, WIN_WIDTH, WIN_HEIGHT, 0);
    SDL_Renderer* renderer = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED |       SDL_RENDERER_TARGETTEXTURE);

    //Put your own bmp image here
    SDL_Surface* pngSurf = SDL_LoadBMP("light.bmp");   //IMG_Load("light.png");
    if (pngSurf == NULL)
    {
        SDL_Log("IMG_Load('light.bmp') failed: %s", SDL_GetError());
        return 2;
    }
    SDL_Texture* pngTex = SDL_CreateTextureFromSurface(renderer, pngSurf);
    SDL_FreeSurface(pngSurf);

    //Make a target texture to render too
    SDL_Texture* texTarget = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, WIN_WIDTH, WIN_HEIGHT);

    //Now render to the texture
    SDL_SetRenderTarget(renderer, texTarget);
    SDL_RenderClear(renderer);
    SDL_RenderCopy(renderer, pngTex, NULL, NULL);
    //Detach the texture
    SDL_SetRenderTarget(renderer, NULL);

    SDL_RenderClear(renderer);
    //SDL_RenderCopyEx(renderer, texTarget, NULL, NULL, 0, NULL, SDL_FLIP_VERTICAL);
    SDL_RenderCopy(renderer, texTarget, NULL, NULL);
    SDL_RenderPresent(renderer);

    SDL_Delay(3000);
    SDL_DestroyTexture(texTarget);
    texTarget = NULL;
    SDL_DestroyTexture(pngTex);
    pngTex = NULL;
    SDL_DestroyRenderer(renderer);
    renderer = NULL;
    SDL_DestroyWindow(win);
    win = NULL;

    SDL_Quit();

    return 0;
}

我做错了什么? 我附上 light.bmp 文件:

带有透明圆圈的Light.bmp

我希望看到具有透明度的纹理“light.bmp”。

c++ sdl-2 render-to-texture
1个回答
0
投票

@genpfault:抱歉,我插入了错误的图像。

© www.soinside.com 2019 - 2024. All rights reserved.