SFML community forums

Help => Graphics => Topic started by: Evlampiy on August 20, 2021, 12:35:38 am

Title: Image with out of objects sf::Texture and sf::Sprite
Post by: Evlampiy 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));
Title: Re: Image with out of objects sf::Texture and sf::Sprite
Post by: Stauricus 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).
Title: Re: Image with out of objects sf::Texture and sf::Sprite
Post by: Evlampiy 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.
Title: Re: Image with out of objects sf::Texture and sf::Sprite
Post by: Stauricus 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 (https://www.sfml-dev.org/tutorials/2.5/graphics-sprite.php#ok-can-i-have-my-sprite-now), even if they use the same texture.
Title: Re: Image with out of objects sf::Texture and sf::Sprite
Post by: kojack 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