#include//include the sf::Texture package int main() { sf::RenderWindow window(sf::VideoMode(640, 480), "SFML Texture Example"); sf::Texture texture; if (!texture.loadFromFile("image.png")) return EXIT_FAILURE; sf::Sprite sprite(texture); while (window.isOpen()) { sf::Event event; while (window.pollEvent(event)) { if (event.type == sf::Event::Closed) window.close(); } window.clear(); window.draw(sprite); window.display(); } return EXIT_SUCCESS; }
#includeThis example uses a texture atlas, which is a single texture that contains multiple images (sprites). The example defines two rectangles that correspond to the locations of two sprites within the atlas. Then, it creates two separate sprites, each with their own rectangle specifying the sprite's location in the atlas. Overall, the sf::Texture class is a powerful tool for working with textures in SFML. It provides a simple, yet flexible interface for loading, manipulating, and using textures in your programs.#include int main() { sf::RenderWindow window(sf::VideoMode(640, 480), "SFML Texture Example"); sf::Texture texture; if (!texture.loadFromFile("atlas.png")) return EXIT_FAILURE; //Define two rectangles for the texture atlas (assuming each sprite in the atlas is 32x32 pixels) sf::IntRect rect1(0, 0, 32, 32); sf::IntRect rect2(32, 0, 32, 32); //Create two sprites using the texture and the pre-defined rectangles sf::Sprite sprite1(texture, rect1); sprite1.setPosition(100, 100); sf::Sprite sprite2(texture, rect2); sprite2.setPosition(200, 100); while (window.isOpen()) { sf::Event event; while (window.pollEvent(event)) { if (event.type == sf::Event::Closed) window.close(); } window.clear(); window.draw(sprite1); window.draw(sprite2); window.display(); } return EXIT_SUCCESS; }