简体   繁体   English

透明SDL2游戏(WIN32色键无法正常工作)

[英]Transparent SDL2 Game (WIN32 Colorkeying not working correctly)

I have a problem with my SDL2 Game. 我的SDL2游戏有问题。 I want to make it transparent but it isn't working and I don't know why. 我想使其透明,但是它不起作用,我也不知道为什么。 I already tested it with SDL(1) and there it worked perfectly with surfaces. 我已经用SDL(1)对其进行了测试,并且在表面上它可以完美地工作。 But now when I use textures with SDL_Render functions it doesn't work anymore. 但是现在当我将纹理与SDL_Render函数一起使用时,它将不再起作用。 First I get the HWND and then I call SetLayeredWindowAttributes as shown below but ONLY the LWA_ALPHA flag works in this case but when I change it to LWA_COLORKEY nothing happens to the window. 首先,我获得了HWND ,然后调用了SetLayeredWindowAttributes ,如下所示,但是在这种情况下,只有LWA_ALPHA标志起作用,但是当我将其更改为LWA_COLORKEY时,窗口没有任何反应。 That's quite strange because SetLayeredWindowAttributes still returns 1 even if you don't see the colorkeying. 这很奇怪,因为即使您看不到colorkeying, SetLayeredWindowAttributes仍然返回1。

Maybe some of you can help me to fix this. 也许有些人可以帮助我解决这个问题。 Thank you in advance! 先感谢您! Here my code and some screenshots: 这里是我的代码和一些屏幕截图:

#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;
}

SetLayeredWindowAttributes with LWA_ALPHA and 100 for alpha, Console outpout: success?: 1 With alpha screenshot 带有LWA_ALPHA SetLayeredWindowAttributes和100的alpha值,控制台输出: success?: 1 带有alpha屏幕截图

SetLayeredWindowAttributes with LWA_COLORKEY , Console outpout: success?: 1 With colorkey screenshot (The texture's background color is RGB(255, 0, 255) so it should work but it doesn't.) 具有LWA_COLORKEY SetLayeredWindowAttributes ,控制台输出: success?: 1 使用colorkey屏幕截图 (纹理的背景颜色为RGB(255, 0, 255) 255,0,255 RGB(255, 0, 255)因此它应该可以工作,但不能。)

You may be working the way you are for a reason and if so that is fine but if not, why don't you just set the colour key on the surface before texture creation: 您可能因为某种原因而按自己的方式工作,如果可以,那么很好,但是如果不能,那么为什么不在创建纹理之前就在表面上设置颜色键:

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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