#includeint main() { sf::RenderWindow window(sf::VideoMode(800, 600), "Texture2D Example"); sf::Texture texture; if(!texture.loadFromFile("texture.png")) { return EXIT_FAILURE; } sf::RectangleShape quad(sf::Vector2f(200.0f, 150.0f)); quad.setTexture(&texture); while(window.isOpen()) { sf::Event event; while(window.pollEvent(event)) { if(event.type == sf::Event::Closed) { window.close(); } } window.clear(); window.draw(quad); window.display(); } return EXIT_SUCCESS; }
#includeThis example creates a texture with size 256x256 and fills it with pixels of color (255, 0, 0, 255) which represents pure red. The texture is then applied to a quad and drawn on the screen. The texture is created and updated using the member functions create() and update() of the Texture2D class.int main() { sf::RenderWindow window(sf::VideoMode(800, 600), "Texture2D Example"); sf::Texture texture; texture.create(256, 256); sf::Uint8* pixels = new sf::Uint8[256 * 256 * 4]; for(int i = 0; i < 256 * 256 * 4; i += 4) { pixels[i] = 255; // red channel pixels[i + 1] = 0; // green channel pixels[i + 2] = 0; // blue channel pixels[i + 3] = 255; // alpha channel } texture.update(pixels); sf::RectangleShape quad(sf::Vector2f(256.0f, 256.0f)); quad.setTexture(&texture); while(window.isOpen()) { sf::Event event; while(window.pollEvent(event)) { if(event.type == sf::Event::Closed) { window.close(); } } window.clear(); window.draw(quad); window.display(); } delete [] pixels; return EXIT_SUCCESS; }