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

Author Topic: Image with out of objects sf::Texture and sf::Sprite  (Read 5326 times)

0 Members and 1 Guest are viewing this topic.

Evlampiy

  • Newbie
  • *
  • Posts: 10
    • View Profile
Image with out of objects sf::Texture and sf::Sprite
« on: August 20, 2021, 12:35:38 am »
Hello! I want to draw an image on user's screen, but I don't want to create objects sf::Texture and sf::Sprite for it because with these method I need a lot of textures and sprites. I want to do it like:
window.draw(sf::Image(x, y, way));

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: Image with out of objects sf::Texture and sf::Sprite
« Reply #1 on: August 20, 2021, 01:56:47 am »
you don't need a lot of textures and sprites, you just need one of each for every image.
the way you are trying to do is simply wrong, you'd be creating and destroying the images every frame. thats too expensive to the cpu.

and sf::Image can't be drawn to the screen. you use it only when you need to change something in the image (as changing some pixel color).
Visit my game site (and hopefully help funding it? )
Website | IndieDB

Evlampiy

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Image with out of objects sf::Texture and sf::Sprite
« Reply #2 on: August 20, 2021, 02:24:29 am »
you don't need a lot of textures and sprites, you just need one of each for every image.
the way you are trying to do is simply wrong, you'd be creating and destroying the images every frame. thats too expensive to the cpu.

and sf::Image can't be drawn to the screen. you use it only when you need to change something in the image (as changing some pixel color).
Okay, so, I'll try to minimize number of textures and sprite. Thanks you.

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: Image with out of objects sf::Texture and sf::Sprite
« Reply #3 on: August 20, 2021, 01:31:53 pm »
the point in having separated textures and sprites is that you can reuse the same texture. sf::Texture is a heavier object that uses video ram. sf::Sprite is a lightweight object, that uses any texture loaded.
example: if you want to create 5 birds flying in your game, you just load one texture, like "bird.png", and create 5 sprites using the same texture. one 'heavy' object and 5 light objects. and you can also change the color of each sprite, even if they use the same texture.
Visit my game site (and hopefully help funding it? )
Website | IndieDB

kojack

  • Sr. Member
  • ****
  • Posts: 318
  • C++/C# game dev teacher.
    • View Profile
Re: Image with out of objects sf::Texture and sf::Sprite
« Reply #4 on: August 20, 2021, 08:10:52 pm »
Hello! I want to draw an image on user's screen, but I don't want to create objects sf::Texture and sf::Sprite for it because with these method I need a lot of textures and sprites. I want to do it like:
window.draw(sf::Image(x, y, way));
It sounds to me like maybe you mean progressively drawing onto the screen, like a painting program (hence the lots of sprites). If that's the case, have a look at render textures.
A render texture is created in code and can act like a window, you draw sprites into it. But whatever you draw into it will be preserved as part of the texture. You can then draw the render texture into a window just like a normal texture. Good for things like leaving trails, user controlled drawing, etc.
https://www.sfml-dev.org/tutorials/2.5/graphics-draw.php#off-screen-drawing