SetLayeredWindowAttributes

SetLayeredWindowAttributes

我的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,alpha为100,
控制台输出:success?: 1 With alpha screenshot

SetLayeredWindowAttributesLWA_COLORKEY
控制台输出:success?: 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色键无法正常工作),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25038347/

10-12 18:43