8

我遇到以下问题:

我需要在另一个纹理之上渲染一个纹理,然后渲染那个主纹理。例如,我有蓝色矩形纹理,我想在这个蓝色矩形上绘制红色矩形。但是我希望他们只在这个矩形上限制渲染。如下图: 在此处输入图像描述

我读过一些关于它们之间的纹理 blit 或类似的东西,但我不确定这是否可行。

我的代码如下所示:

SDL_RenderCopy(ren,bluetexture,NULL,dBLUErect);
SDL_RenderCopy(ren,redtexture,NULL,dREDrect);
SDL_RenderPresent(ren);

任何人都知道如何在 SDL 2.0 中做到这一点?顺便说一句,这就是我使用的。

4

2 回答 2

8

火星的回答没有用,因为它画了一个黑色的纹理,什么都不能画。

但这有效!:

SDL_Texture* auxtexture = SDL_CreateTexture(ren, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, 500, 500);

//change the rendering target

SDL_SetTextureBlendMode(auxtexture, SDL_BLENDMODE_BLEND);
SDL_SetRenderTarget(ren, auxtexture);

//render what we want
triangle->render(ren); //render my class triangle e.g


//change the target back to the default and then render the aux

SDL_SetRenderTarget(ren, NULL); //NULL SETS TO DEFAULT
SDL_RenderCopy(ren, auxtexture, NULL, canvas->drect);
SDL_DestroyTexture(auxtexture);

干杯。

于 2013-09-06T20:36:35.797 回答
3

First, you need to create your texture on which you want to draw with SDL_TEXTUREACCESS_TARGET flag. So create back texture like this:

back = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, 50, 50);

Then, when calling drawing functions, you need to set the back texture as the target, like so:

SDL_SetRenderTarget(renderer, back);

Then you draw what you want, and after that you change the target to null:

SDL_SetRenderTarget(renderer, NULL);

And render back texture:

SDL_RenderCopy(renderer, back, NULL, &some_rect);
于 2013-09-06T00:16:33.420 回答