Alpha通道在绘制到纹理并粘贴到窗口时没有效果。

huangapple go评论146阅读模式
英文:

Alpha channel have no effect when drawn to texture and pasted to window

问题

使用SDL2,我想在纹理上绘制一些图形并将其粘贴到窗口以提高速度。在粘贴时,我希望保留我的 alpha 通道。

这段代码应该显示一个旋转的白色透明矩形。

但它显示不透明的白色矩形。

如何获得一个透明的白色矩形?

#ifdef __linux__
#include <SDL2/SDL.h>
#elif defined(_WIN32)
#include <SDL.h>
#endif

const int WIN_WIDTH = 640;
const int WIN_HEIGHT = 480;

int main(int argc, char **argv) {
    if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
        std::cerr << "SDL_Init failed: " << SDL_GetError() << "\n";
        return 1;
    }
    SDL_Window *win = SDL_CreateWindow("Rendering to a texture!", SDL_WINDOWPOS_CENTERED,
        SDL_WINDOWPOS_CENTERED, WIN_WIDTH, WIN_HEIGHT, SDL_WINDOW_SHOWN | SDL_WINDOW_ALLOW_HIGHDPI);

    SDL_Renderer *renderer = SDL_CreateRenderer(win, -1,
        SDL_RENDERER_ACCELERATED | SDL_RENDERER_TARGETTEXTURE);

    //Put your own bmp image here
    SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);

    //Make a target texture to render too
    SDL_Texture *texTarget = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888,
        SDL_TEXTUREACCESS_TARGET, WIN_WIDTH, WIN_HEIGHT);

    //Now render to the texture
    SDL_SetRenderTarget(renderer, texTarget);
    SDL_SetRenderDrawColor(renderer, 255, 255, 255, 100);
    SDL_RenderClear(renderer);
    //Detach the texture
    SDL_SetRenderTarget(renderer, NULL);
    SDL_SetRenderDrawColor(renderer, 255, 0, 0, 0);
    SDL_RenderClear(renderer);

    //Now render the texture target to our screen, but upside down
    for (int i = 0; i < 255; i++) {
        SDL_RenderClear(renderer);
        SDL_RenderCopyEx(renderer, texTarget, NULL, NULL, i, NULL, SDL_FLIP_VERTICAL);
        SDL_RenderPresent(renderer);

        SDL_Delay(10);
    }

    SDL_DestroyTexture(texTarget);
    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(win);
    SDL_Quit();
    return 0;
}
英文:

Using SDL2, I want to draw some graphics on texture and paste it on window for speed enhancement. When I paste it I want my alpha channel preserved.

This code is supposed to show a white rotating transparent rectangle.

But it shows opaque white rectangle.

How can I get a transparent white rectangle?

#ifdef __linux__
#include &lt;SDL2/SDL.h&gt;
#elif defined(_WIN32)
#include &lt;SDL.h&gt;
#endif

const int WIN_WIDTH = 640;
const int WIN_HEIGHT = 480;

int main(int argc, char **argv){
	if (SDL_Init(SDL_INIT_EVERYTHING) != 0){
		std::cerr &lt;&lt; &quot;SDL_Init failed: &quot; &lt;&lt; SDL_GetError() &lt;&lt; &quot;\n&quot;;
		return 1;
	}
	SDL_Window *win = SDL_CreateWindow(&quot;Rendering to a texture!&quot;, SDL_WINDOWPOS_CENTERED,
		SDL_WINDOWPOS_CENTERED, WIN_WIDTH, WIN_HEIGHT, SDL_WINDOW_SHOWN | SDL_WINDOW_ALLOW_HIGHDPI);

	SDL_Renderer *renderer = SDL_CreateRenderer(win, -1,
		SDL_RENDERER_ACCELERATED | SDL_RENDERER_TARGETTEXTURE);

	//Put your own bmp image here
	SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);

	//Make a target texture to render too
	SDL_Texture *texTarget = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888,
		SDL_TEXTUREACCESS_TARGET, WIN_WIDTH, WIN_HEIGHT);
	
	//Now render to the texture
	SDL_SetRenderTarget(renderer, texTarget);
	SDL_SetRenderDrawColor(renderer, 255, 255, 255, 100);
	SDL_RenderClear(renderer);
	//Detach the texture
	SDL_SetRenderTarget(renderer, NULL);
	SDL_SetRenderDrawColor(renderer, 255, 0, 0, 0);
	SDL_RenderClear(renderer);

	//Now render the texture target to our screen, but upside down
	for(int i=0; i&lt;255; i++) {
		SDL_RenderClear(renderer);
		SDL_RenderCopyEx(renderer, texTarget, NULL, NULL, i, NULL, SDL_FLIP_VERTICAL);
		SDL_RenderPresent(renderer);

		SDL_Delay(10);
	}

	SDL_DestroyTexture(texTarget);
	SDL_DestroyRenderer(renderer);
	SDL_DestroyWindow(win);
	SDL_Quit();
	return 0;
}

答案1

得分: 1

使用SDL_SetTextureBlendMode设置纹理的混合模式,而不是SDL_SetRenderDrawBlendMode

SDL_SetTextureBlendMode(texTarget, SDL_BLENDMODE_BLEND);

关于SDL_SetRenderDrawBlendMode的文档中提到:

> 设置用于绘制操作(填充和线条)的混合模式。

绘制操作包括SDL_RenderDrawLine等。

另一方面,SDL_RenderCopy的文档中提到:

> 纹理根据使用SDL_SetTextureBlendMode设置的混合模式与目标混合。

英文:

Use SDL_SetTextureBlendMode on the texture, not SDL_SetRenderDrawBlendMode.

SDL_SetTextureBlendMode(texTarget, SDL_BLENDMODE_BLEND);

The wiki for SDL_SetRenderDrawBlendMode says:

> Set the blend mode used for drawing operations (Fill and Line).

Drawing operations are SDL_RenderDrawLine and such.

On the other hand, SDL_RenderCopy says:

> The texture is blended with the destination based on its blend mode set with SDL_SetTextureBlendMode().

huangapple
  • 本文由 发表于 2023年6月6日 12:58:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76411554.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定