From fb7470cbc357d5ed5f74930982ca22db8d198642 Mon Sep 17 00:00:00 2001 From: LaurentGom Date: Sat, 5 Dec 2009 11:35:27 +0000 Subject: [PATCH] FS#133 - Add a function to make fast updates of an image's pixels from an external source Removed sf::Image constructors that called CreateXxx or LoadXxx (there was no way to check errors) git-svn-id: https://sfml.svn.sourceforge.net/svnroot/sfml/branches/sfml2@1307 4e206d99-4929-0410-ac5d-dfc041789085 --- include/SFML/Graphics/Image.hpp | 44 ++++++++++--------- src/SFML/Graphics/Image.cpp | 77 ++++++++++++++++++--------------- 2 files changed, 65 insertions(+), 56 deletions(-) diff --git a/include/SFML/Graphics/Image.hpp b/include/SFML/Graphics/Image.hpp index 9f2032458..77f5e15df 100644 --- a/include/SFML/Graphics/Image.hpp +++ b/include/SFML/Graphics/Image.hpp @@ -62,26 +62,6 @@ public : //////////////////////////////////////////////////////////// Image(const Image& copy); - //////////////////////////////////////////////////////////// - /// Construct an empty image - /// - /// \param width : Image width - /// \param height : Image height - /// \param color : Image color - /// - //////////////////////////////////////////////////////////// - Image(unsigned int width, unsigned int height, const Color& color = Color(0, 0, 0)); - - //////////////////////////////////////////////////////////// - /// Construct the image from pixels in memory - /// - /// \param width : Image width - /// \param height : Image height - /// \param pixels : Pointer to the pixels in memory (assumed format is RGBA) - /// - //////////////////////////////////////////////////////////// - Image(unsigned int width, unsigned int height, const Uint8* pixels); - //////////////////////////////////////////////////////////// /// Destructor /// @@ -209,6 +189,28 @@ public : //////////////////////////////////////////////////////////// const Uint8* GetPixelsPtr() const; + //////////////////////////////////////////////////////////// + /// Update the whole image from an array of pixels + /// + /// \param pixels : Array of pixels to write to the image + /// + //////////////////////////////////////////////////////////// + void UpdatePixels(const Uint8* pixels); + + //////////////////////////////////////////////////////////// + /// Update a sub-rectangle of the image from an array of pixels + /// + /// Warning: for performances reasons, this function doesn't + /// perform any check; thus you're responsible of ensuring that + /// \a rectangle does not exceed the image size, and that + /// \a pixels contain enough elements. + /// + /// \param rectangle : Sub-rectangle of the image to update + /// \param pixels : Array of pixels to write to the image + /// + //////////////////////////////////////////////////////////// + void UpdatePixels(const Uint8* pixels, const IntRect& rectangle); + //////////////////////////////////////////////////////////// /// Bind the image for rendering /// @@ -302,12 +304,14 @@ private : //////////////////////////////////////////////////////////// /// Make sure the texture in video memory is updated with the /// array of pixels + /// //////////////////////////////////////////////////////////// void EnsureTextureUpdate(); //////////////////////////////////////////////////////////// /// Make sure the array of pixels is updated with the /// texture in video memory + /// //////////////////////////////////////////////////////////// void EnsureArrayUpdate(); diff --git a/src/SFML/Graphics/Image.cpp b/src/SFML/Graphics/Image.cpp index f9045d988..d706daae3 100644 --- a/src/SFML/Graphics/Image.cpp +++ b/src/SFML/Graphics/Image.cpp @@ -82,42 +82,6 @@ Resource(copy) } -//////////////////////////////////////////////////////////// -/// Construct an empty image -//////////////////////////////////////////////////////////// -Image::Image(unsigned int width, unsigned int height, const Color& color) : -myWidth (0), -myHeight (0), -myTextureWidth (0), -myTextureHeight (0), -myTexture (0), -myIsSmooth (true), -myNeedTextureUpdate(false), -myNeedArrayUpdate (false), -myPixelsFlipped (false) -{ - Create(width, height, color); -} - - -//////////////////////////////////////////////////////////// -/// Construct the image from pixels in memory -//////////////////////////////////////////////////////////// -Image::Image(unsigned int width, unsigned int height, const Uint8* data) : -myWidth (0), -myHeight (0), -myTextureWidth (0), -myTextureHeight (0), -myTexture (0), -myIsSmooth (true), -myNeedTextureUpdate(false), -myNeedArrayUpdate (false), -myPixelsFlipped (false) -{ - LoadFromPixels(width, height, data); -} - - //////////////////////////////////////////////////////////// /// Destructor //////////////////////////////////////////////////////////// @@ -482,6 +446,47 @@ const Uint8* Image::GetPixelsPtr() const } +//////////////////////////////////////////////////////////// +/// Update the whole image from an array of pixels +//////////////////////////////////////////////////////////// +void Image::UpdatePixels(const Uint8* pixels) +{ + GLint previous; + GLCheck(glGetIntegerv(GL_TEXTURE_BINDING_2D, &previous)); + + // Update the texture from the array of pixels + GLCheck(glBindTexture(GL_TEXTURE_2D, myTexture)); + GLCheck(glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, myWidth, myHeight, GL_RGBA, GL_UNSIGNED_BYTE, pixels)); + + GLCheck(glBindTexture(GL_TEXTURE_2D, previous)); + + myNeedArrayUpdate = true; + myNeedTextureUpdate = false; +} + + +//////////////////////////////////////////////////////////// +/// Update a sub-rectangle of the image from an array of pixels +//////////////////////////////////////////////////////////// +void Image::UpdatePixels(const Uint8* pixels, const IntRect& rectangle) +{ + // Make sure that the texture is up-to-date + EnsureTextureUpdate(); + + GLint previous; + GLCheck(glGetIntegerv(GL_TEXTURE_BINDING_2D, &previous)); + + // Update the texture from the array of pixels + GLCheck(glBindTexture(GL_TEXTURE_2D, myTexture)); + GLCheck(glTexSubImage2D(GL_TEXTURE_2D, 0, rectangle.Left, rectangle.Top, rectangle.GetSize().x, rectangle.GetSize().y, GL_RGBA, GL_UNSIGNED_BYTE, pixels)); + + GLCheck(glBindTexture(GL_TEXTURE_2D, previous)); + + // The pixel cache is no longer up-to-date + myNeedArrayUpdate = true; +} + + //////////////////////////////////////////////////////////// /// Bind the image for rendering ////////////////////////////////////////////////////////////