| Summary: | SDL_RenderFillRect disregards transparency in SDL_SetRenderDrawColor | ||
|---|---|---|---|
| Product: | SDL | Reporter: | Ellie <etc0de> |
| Component: | video | Assignee: | Sam Lantinga <slouken> |
| Status: | RESOLVED INVALID | QA Contact: | Sam Lantinga <slouken> |
| Severity: | major | ||
| Priority: | P2 | ||
| Version: | HG 2.0 | ||
| Hardware: | x86 | ||
| OS: | Linux | ||
This isn't technically a bug, though I can see why you would think so. Since the alpha can be interpreted in a couple different ways, you need to specify the blend mode. What you want is this: SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_BLEND); Oh thanks, I simply missed that. I just wanted to add a remark on the wiki page http://wiki.libsdl.org/moin.cgi/SDL_SetRenderDrawColor so others won't run into the same thing but discovered it is apparently impossible to edit pages. Aren't users expected to improve the wiki and fill in the gaps? I noticed a few other empty spots I could possibly fill in at other places aswell. From http://wiki.libsdl.org/moin.cgi/Contributing: Due to spammers, write access to this wiki is disabled by default. If you haven't already, please email your wiki username and the kind(s) of edits you would like to make to wiki@libsdl.org to be given write access and to coordinate efforts. |
SDL_RenderFillRect disregards transparency in SDL_SetRenderDrawColor which makes drawing transparent rectangles impossible without complicated workarounds (creating a new texture for rectangle blitting and using rendercopy with alpha mod which works fine - assuming a renderer without color blit support is used, a new texture needs to be created for each color a rectangle has at some point). See this simple test case that demonstrates the bug: #include "SDL.h" #include <stdio.h> #include <unistd.h> static SDL_Window* mainwindow; static SDL_Renderer* mainrenderer; int main(int argc, char** argv) { if (SDL_Init(SDL_INIT_VIDEO) < 0) { return -1; } mainwindow = SDL_CreateWindow("SDL bug", 0,0, 512,512, 0); if (!mainwindow) { SDL_Quit(); return -1; } mainrenderer = SDL_CreateRenderer(mainwindow, -1, SDL_RENDERER_ACCELERATED|SDL_RENDERER_PRESENTVSYNC); if (!mainrenderer) { SDL_Quit(); return -1; } //fill with black SDL_SetRenderDrawColor(mainrenderer, 0, 0, 0, 255); SDL_Rect rect; memset(&rect, 0, sizeof(rect)); rect.x = 0; rect.y = 0; rect.w = 512; rect.h = 512; SDL_RenderFillRect(mainrenderer, &rect); //draw smaller red rectangle SDL_SetRenderDrawColor(mainrenderer, 255, 0, 0, 255); rect.x = 100; rect.y = 100; rect.w = 100; rect.h = 100; SDL_RenderFillRect(mainrenderer, &rect); //draw bigger white rectangle with transparency SDL_SetRenderDrawColor(mainrenderer, 255, 255, 255, 127); rect.x = 150; rect.y = 150; rect.w = 150; rect.h = 150; SDL_RenderFillRect(mainrenderer, &rect); SDL_Delay(500); SDL_RenderPresent(mainrenderer); SDL_Delay(5000); SDL_Quit(); } Expected result: Transparent white rectangle partially occludes the red rectangle Actual result: Opaque white rectangle fully occludes red rectangle Since I require this for fading effects and the workaround cannot really be done cleanly in a generic fashion (which would be supporting any rectangle color, not just e.g. black for fading), a fix would be greatly appreciated.