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 ////////////////////////////////////////////////////////////