c++ - 透明 SDL2 游戏(WIN32 Colorkeying 无法正常工作)

标签 c++ sdl transparency transparent

我的 SDL2 游戏有问题。我想让它透明,但它不起作用,我不知道为什么。我已经使用 SDL(1) 对其进行了测试,并且它与表面完美配合。 但是现在,当我将纹理与 SDL_Render 函数一起使用时,它不再起作用了。 首先,我得到 HWND,然后调用 SetLayeredWindowAttributes,如下所示,但在这种情况下只有 LWA_ALPHA 标志有效,但当我将其更改为 LWA_COLORKEY 时,窗口没有任何反应。这很奇怪,因为即使您没有看到颜色键控,SetLayeredWindowAttributes 仍然会返回 1。

也许你们中的一些人可以帮我解决这个问题。 先感谢您!这是我的代码和一些截图:

#include <SDL2/SDL.h>
#include <SDL2/SDL_syswm.h>
#include <stdio.h>
#include <string>

bool init();
bool loadTexture();
void quit();

SDL_Window* gWindow = NULL;
SDL_Renderer* gRenderer = NULL;
SDL_Texture* gTexture = NULL;

int main(int argc, char* args[]) {
    if (!init()) {
        printf("Initialization failed!\n");
        quit();
        return 0;
    }

    SDL_SysWMinfo SysInfo;
    SDL_VERSION(&SysInfo.version);

    if (SDL_GetWindowWMInfo(gWindow, &SysInfo) <= 0) {
        printf("%s : %d\n", SDL_GetError(), SysInfo.info.win.window);
        quit();
        return 0;
    }

    HWND hWnd = SysInfo.info.win.window;

    // Handles the transparency of the window !!!
    // LWA_ALPHA works fine but LWA_COLORKEY doesn't work even the function returns 1.
    SetWindowLong(hWnd, GWL_EXSTYLE, GetWindowLong(hWnd, GWL_EXSTYLE) | WS_EX_LAYERED);
    printf("success?: %i\n", SetLayeredWindowAttributes(hWnd, RGB(255, 0, 255), 100,         LWA_COLORKEY));

    //Load Texture
    if (!loadTexture()) {
        printf("Texture couldn't be loaded %s\n", SDL_GetError());
        quit();
        return 0;
    }

    bool run = true;
    SDL_Event e;

    while (run) {
        while (SDL_PollEvent(&e) != 0) {
            if (e.type == SDL_QUIT)
            run = false;
        }

        SDL_RenderClear(gRenderer);
        SDL_RenderCopy(gRenderer, gTexture, NULL, NULL);
        SDL_RenderPresent(gRenderer);
    }

    return 0;
}

bool init() {
    if (SDL_Init( SDL_INIT_VIDEO) < 0) return false;

    gWindow = SDL_CreateWindow("Transparency test", SDL_WINDOWPOS_CENTERED,     SDL_WINDOWPOS_CENTERED, 800, 600, SDL_WINDOW_SHOWN);
    if (gWindow == NULL) return false;

    gRenderer = SDL_CreateRenderer(gWindow, -1, SDL_RENDERER_ACCELERATED);
    if (gRenderer == NULL) return false;

    SDL_SetRenderDrawColor(gRenderer, 0, 0, 0, 255);

    return true;
}

void quit() {
    SDL_DestroyTexture(gTexture);
    gTexture = NULL;

    SDL_DestroyRenderer(gRenderer);
    SDL_DestroyWindow(gWindow);
    gWindow = NULL;
    gRenderer = NULL;

    SDL_Quit();
}

bool loadTexture() {
    SDL_Surface* surface = SDL_LoadBMP("texture.bmp");
    if (surface == NULL) return false;

    gTexture = SDL_CreateTextureFromSurface(gRenderer, surface);
    if (gTexture == NULL) return false;

    SDL_FreeSurface(surface);

    return true;
}

SetLayeredWindowAttributesLWA_ALPHA 和 100 为 alpha, 控制台输出:成功?:1 With alpha screenshot

SetLayeredWindowAttributesLWA_COLORKEY, 控制台输出:成功?:1 With colorkey screenshot (纹理的背景颜色是 RGB(255, 0, 255) 所以它应该可以工作,但实际上没有。)

最佳答案

您可能出于某种原因按照您的方式工作,如果是这样那很好,但如果不是,为什么不在创建纹理之前在表面上设置颜色键:

bool loadTexture() {
    SDL_Surface* surface = SDL_LoadBMP("texture.bmp");
    if (surface == NULL) return false;

    // Map the colour key
    Uint32 colorkey = SDL_MapRGB(surface->format, 0xFF, 0x00, 0xFF);
    // Set all pixels of colour R(255), G(0), B(255) to be transparent
    SDL_SetColorKey(surface, SDL_SRCCOLORKEY, colourkey);

    gTexture = SDL_CreateTextureFromSurface(gRenderer, surface);
    if (gTexture == NULL) return false;

    SDL_FreeSurface(surface);

    return true;
}

关于c++ - 透明 SDL2 游戏(WIN32 Colorkeying 无法正常工作),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25038347/

相关文章:

c++ - 错误 : expected primary-expression before ')' token

c++ - C++程序员使用什么样的好方法来存储错误信息?

c++ - 是否可以从 C++ 在 QML 上编写上下文属性?

2d - Gnuplot 将误差线填充为透明阴影

c++ - 为什么两个指针具有相同的内存地址?

c++ - SDL2 可以用 C 语言(相对于 C++)编程吗?

c++ - SDL GPU 为什么在两个单独的 for 循环中位 block 传送两个图像速度更快?

c++ - 改变一个指针后乘法对象改变

android - 透明背景 imageview 使它上面通常不透明的文本也变得透明。我该如何解决?

iphone - iPhone 上的透明 UITableView?