英文:
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 <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;
}
答案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().
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论