0
\$\begingroup\$

When drawing transparent PNG data on to the renderer, pixel data of any previous drawing (layer beneath) is replaced with the renderer's drawing color (green).

FPS counter with a green background over a blue gradient

I have tried setting blend mode in the renderer, surface (used to load pixel data), and texture.

Code snippet:

// creates the renderer void Viewport::init(SDL_Window* window) { this->renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); SDL_SetRenderDrawBlendMode(this->renderer, SDL_BLENDMODE_BLEND); SDL_SetRenderDrawColor(this->renderer, 0, 255, 0, SDL_ALPHA_OPAQUE); } // creates a texture from PNG image data SDL_Texture* TextureLoader::load(string adpath) { SDL_Surface* surface = IMG_Load(adpath.c_str()); if (surface == nullptr) { return nullptr; } SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_BLEND); SDL_Texture* texture = SDL_CreateTextureFromSurface(GetViewport()->getRenderer(), surface); SDL_FreeSurface(surface); if (texture == nullptr) { return nullptr; } SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND); return texture; } // draws a texture on renderer void Viewport::drawTexture(SDL_Texture* texture, SDL_Rect s_rect, SDL_Rect t_rect) { if (texture == nullptr) { return; } SDL_RenderCopy(this->renderer, texture, &s_rect, &t_rect); } // method called during game loop to draw available textures (pseudo code for simplicity) void Viewport::draw() { SDL_RenderClear(this->renderer); // draw background underneath this->drawTexture(this->background, this->b_rect, this->r_rect); // draw fps above (PNG bitmap font with transparent pixels around each glyph) this->drawTexture(this->fps, this->f_rect, this->f_rect); SDL_RenderPresent(this->renderer); } 

When searching for a solution, the suggestions are to use SDL_SetRenderDrawBlendMode, SDL_SetSurfaceBlendMode, & SDL_SetTextureBlendMode & set them to SDL_BLENDMODE_BLEND. But none are working.

I have also tried the IMG_LoadTexture to load PNGs with the same result.

Note: All my testing so far has been on Windows. I haven't yet had the opportunity to see the result on other systems. So I wouldn't be surprised if this is a platform-specific issue.

\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

The problem was in a different part of my code where a new texture is created to represent a line of text collaged from a character map texture. I wasn't calling SDL_SetTextureBlendMode on the newly created one.

SDL_Texture* t_texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA32, SDL_TEXTUREACCESS_TARGET, full_width, c_height); if (t_texture == nullptr) { logger.error((string) "Cannot build text sprite: " + SDL_GetError()); return nullptr; } SDL_SetTextureBlendMode(t_texture, SDL_BLENDMODE_BLEND); // this was missing 
\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.