![](/img/trans.png)
[英]How to transform SDL TTF Surface into GL Texture with Transparent background
[英]SDL Texture transparent background
這可能是一個非常簡單的問題,但是經過一個小時的搜索和嘗試,我仍然無法解決問題。 我有兩個png文件。 一個是背景圖像,第二個是前景。 前景具有Alpha通道。 我想在背景頂部顯示前景。
我正在使用以下方法加載前景:
SDL_Surface *clip = SDL_CreateRGBSurface(0, SCREEN_WIDTH, SCREEN_HEIGHT, 32, 0, 0, 0, 0xff);
SDL_Rect rect = { x, 0, SCREEN_WIDTH, SCREEN_HEIGHT };
SDL_BlitSurface(map, &rect, clip, NULL);
*block = SDL_CreateTextureFromSurface(gRenderer, clip);
這里的地圖是一些SDL_Surface。
我正在使用以下方法加載背景
SDL_Surface* loadedSurface = IMG_Load(path);
//Create texture from surface pixels
SDL_Texture* newTexture = SDL_CreateTextureFromSurface(gRenderer, loadedSurface);
SDL_FreeSurface(loadedSurface);
然后我嘗試連接它們:
SDL_RenderCopy(gRenderer, background, NULL, &cur);
SDL_RenderCopy(gRenderer, map, NULL, &cur);
但這會導致前景圖像具有黑色背景。 我究竟做錯了什么?
您應該添加這兩行,
Uint32 colorkey = SDL_MapRGB(loadedSurface->format, 0, 0, 0);
SDL_SetColorKey(loadedSurface, SDL_TRUE, colorkey);
在代碼中此行之前
SDL_Texture* newTexture = SDL_CreateTextureFromSurface(gRenderer, loadedSurface);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.