Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Updating texture with images with transparent background  (Read 1402 times)

0 Members and 1 Guest are viewing this topic.

JohnD

  • Newbie
  • *
  • Posts: 5
    • View Profile
Updating texture with images with transparent background
« on: July 25, 2019, 03:48:20 am »
Hi guys! New user here.

Is there a way to update a texture with images with transparent background?

I've tried texture.update() but it literally copies the whole image into the texture including the transparent pixels. This results in the texture having holes in it. Is that intended? If so, is there no way to a create a new image using images with transparent backgrounds in SFML?

sf::Texture texture;
if (!texture.loadFromFile("backgroundimage.png"))
     return EXIT_FAILURE;

sf::Image item;
if (!item.loadFromFile("imagewithtransparentbackground.png"))
     return EXIT_FAILURE;

texture.update(item,50,50);
sf::Sprite sprite (texture);

~Edit: I just found a way around it using RenderTexture. It seems transparency only works when drawing? I'm still curious if the above code should work. With RenderTexture I'm having to pass texture to sprite to rendertexture and back to sprite to get the result I want.
« Last Edit: July 25, 2019, 04:22:40 am by JohnD »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Updating texture with images with transparent background
« Reply #1 on: July 25, 2019, 06:30:27 pm »
A pixel of an image or texture is defined as RGBA, where the A stands for alpha, meaning the opacity. As such when you update a texture all the pixels are replaced with the nee value including transparency.
When you draw to the window or the render texture the blend mode will define how the alpha values are treated, e.g. if you would use the blend mode none, the alpha value would be replaced fully.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

JohnD

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Updating texture with images with transparent background
« Reply #2 on: July 26, 2019, 01:00:30 am »
Cheers! Appreciate the info.