我的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;
}
带有SetLayeredWindowAttributes
的 LWA_ALPHA
和alpha的100,
控制台输出:success?: 1
With alpha screenshot
SetLayeredWindowAttributes
的 LWA_COLORKEY
,
控制台输出:success?: 1
With colorkey screenshot
(纹理的背景颜色为RGB(255, 0, 255)
因此它应该可以工作,但它没有。)
答案 0 :(得分:0)
你可能因为某种原因而按照自己的方式工作,如果是这样,那就没问题,为什么不在纹理创建之前在表面上设置颜色键:
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;
}