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

Author Topic: PNG texture not transparent  (Read 1995 times)

0 Members and 1 Guest are viewing this topic.

Danielsann5

  • Newbie
  • *
  • Posts: 3
    • View Profile
PNG texture not transparent
« on: May 07, 2020, 04:49:34 pm »
Hello,

I have this problem that I can't find a solution to.

I'm trying to do a simple game tutorial, with SFML 2.5 and Visual Studio 2019. But when I draw my screen, the .png textures that I load do not show as transparent background. Tried different png files. Same problem. No error.

Here is the code. Help !?!

#include <stdio.h>
#include <SFML/Graphics.hpp>

//Using
using namespace sf;

int main()
{
   //Create video mode
   VideoMode vm(1920, 1080);
   
   //Create and open window
   RenderWindow window(vm, "Game-x", Style::Fullscreen);

   sf::ContextSettings settings = window.getSettings();

   Texture textureBackground;
   Texture textureSpaceShip1;
   textureBackground.loadFromFile("background/background.png");
   textureSpaceShip1.loadFromFile("sprites/spaceship2.png");


   Sprite spriteBackground;
   spriteBackground.setTexture(textureBackground);
   spriteBackground.setPosition(0, 0);

   Sprite spriteSpaceShip1;
   spriteSpaceShip1.setTexture(textureSpaceShip1);
   spriteSpaceShip1.setScale(0.2, 0.2);
   spriteSpaceShip1.setPosition(100, 100);
   
   while (window.isOpen())
   {
      //Player input

      if (Keyboard::isKeyPressed(Keyboard::Escape))
      {
         window.close();
      }

      //Update screen

      //Draw screen
      window.clear();      //clear last game screen

      //Draw elements
      window.draw(spriteBackground);
      window.draw(spriteSpaceShip1);

      //Show new game screen
      window.display();
   }
   
   return 0;
}

Thank you!

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11016
    • View Profile
    • development blog
    • Email
Re: PNG texture not transparent
« Reply #1 on: May 07, 2020, 05:16:44 pm »
Does the background not draw at all?
Or does the background draw, but the parts you want to have transparent are not transparent?

By "transparent" do you expect that the whole window because transparent?
If so, that's not how SFML works, your transparent texture will simply show the background color (black by default) in the transparent places.

Can you provide a texture?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Danielsann5

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: PNG texture not transparent
« Reply #2 on: May 07, 2020, 05:33:22 pm »
The background texture is ok. Because it has no transparent parts, so to say.
But the spaceship texture, has a white-gray background, instead of being transparent.

Here are the textures.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11016
    • View Profile
    • development blog
    • Email
Re: PNG texture not transparent
« Reply #3 on: May 07, 2020, 05:40:50 pm »
Well the image itself has a white-gray background and is not transparent. :D

Open the image in an image editor and you'll see that the parts you thought are transparent, are actually not. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Danielsann5

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: PNG texture not transparent
« Reply #4 on: May 07, 2020, 06:12:17 pm »
Can't believe I made the same mistake with different png files. Thank you!

 

anything