From 043fb83cd84abe26ba2ac299bf8a7179c6747394 Mon Sep 17 00:00:00 2001 From: binary1248 Date: Thu, 12 Nov 2015 22:08:38 +0100 Subject: [PATCH] Rewrite RenderWindow::capture to make use of a single texture transfer instead of transferring each row of the framebuffer individually. --- include/SFML/Graphics/RenderWindow.hpp | 13 ++++++++++++- src/SFML/Graphics/RenderWindow.cpp | 23 ++++++----------------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/include/SFML/Graphics/RenderWindow.hpp b/include/SFML/Graphics/RenderWindow.hpp index 98e5b9a0f..e415b003a 100644 --- a/include/SFML/Graphics/RenderWindow.hpp +++ b/include/SFML/Graphics/RenderWindow.hpp @@ -115,6 +115,17 @@ public: //////////////////////////////////////////////////////////// /// \brief Copy the current contents of the window to an image /// + /// \deprecated + /// Use a sf::Texture and its sf::Texture::update(const Window&) + /// function and copy its contents into an sf::Image instead. + /// \code + /// sf::Vector2u windowSize = window.getSize(); + /// sf::Texture texture; + /// texture.create(windowSize.x, windowSize.y); + /// texture.update(window); + /// sf::Image screenshot = texture.copyToImage(); + /// \endcode + /// /// This is a slow operation, whose main purpose is to make /// screenshots of the application. If you want to update an /// image with the contents of the window and then use it for @@ -126,7 +137,7 @@ public: /// \return Image containing the captured contents /// //////////////////////////////////////////////////////////// - Image capture() const; + SFML_DEPRECATED Image capture() const; protected: diff --git a/src/SFML/Graphics/RenderWindow.cpp b/src/SFML/Graphics/RenderWindow.cpp index d9e884d90..d5df65b8f 100644 --- a/src/SFML/Graphics/RenderWindow.cpp +++ b/src/SFML/Graphics/RenderWindow.cpp @@ -26,7 +26,7 @@ // Headers //////////////////////////////////////////////////////////// #include -#include +#include namespace sf @@ -78,24 +78,13 @@ Vector2u RenderWindow::getSize() const //////////////////////////////////////////////////////////// Image RenderWindow::capture() const { - Image image; - if (setActive()) - { - int width = static_cast(getSize().x); - int height = static_cast(getSize().y); + Vector2u windowSize = getSize(); - // copy rows one by one and flip them (OpenGL's origin is bottom while SFML's origin is top) - std::vector pixels(width * height * 4); - for (int i = 0; i < height; ++i) - { - Uint8* ptr = &pixels[i * width * 4]; - glCheck(glReadPixels(0, height - i - 1, width, 1, GL_RGBA, GL_UNSIGNED_BYTE, ptr)); - } + Texture texture; + texture.create(windowSize.x, windowSize.y); + texture.update(*this); - image.create(width, height, &pixels[0]); - } - - return image; + return texture.copyToImage(); }