gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 13:47:47 25 4
gpt4 key购买 nike

我的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com