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
This commit is contained in:
LaurentGom 2009-12-05 11:35:27 +00:00
parent aff5c1a47c
commit fb7470cbc3
2 changed files with 65 additions and 56 deletions

View File

@ -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();

View File

@ -82,42 +82,6 @@ Resource<Image>(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
////////////////////////////////////////////////////////////