summaryrefslogtreecommitdiff
path: root/src/SFML/Graphics/Image.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/SFML/Graphics/Image.cpp')
-rw-r--r--src/SFML/Graphics/Image.cpp39
1 files changed, 33 insertions, 6 deletions
diff --git a/src/SFML/Graphics/Image.cpp b/src/SFML/Graphics/Image.cpp
index e8ea322..09b0ef8 100644
--- a/src/SFML/Graphics/Image.cpp
+++ b/src/SFML/Graphics/Image.cpp
@@ -363,6 +363,7 @@ bool Image::CopyScreen(RenderWindow& Window, const IntRect& SourceRect)
GLCheck(glBindTexture(GL_TEXTURE_2D, PreviousTexture));
+ myNeedTextureUpdate = false;
myNeedArrayUpdate = true;
return true;
@@ -622,7 +623,7 @@ bool Image::CreateTexture()
GLuint Texture = 0;
GLCheck(glGenTextures(1, &Texture));
GLCheck(glBindTexture(GL_TEXTURE_2D, Texture));
- GLCheck(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, myTextureWidth, myTextureHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, GetPixelsPtr()));
+ GLCheck(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, myTextureWidth, myTextureHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL));
GLCheck(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP));
GLCheck(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP));
GLCheck(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, myIsSmooth ? GL_LINEAR : GL_NEAREST));
@@ -632,7 +633,7 @@ bool Image::CreateTexture()
GLCheck(glBindTexture(GL_TEXTURE_2D, PreviousTexture));
}
- myNeedTextureUpdate = false;
+ myNeedTextureUpdate = true;
return true;
}
@@ -672,16 +673,42 @@ void Image::EnsureArrayUpdate() const
{
if (myNeedArrayUpdate)
{
+ // Save the previous texture
GLint PreviousTexture;
GLCheck(glGetIntegerv(GL_TEXTURE_BINDING_2D, &PreviousTexture));
- // Resize the array of pixels
+ // Resize the destination array of pixels
myPixels.resize(myWidth * myHeight);
- // Copy them from texture to array
- GLCheck(glBindTexture(GL_TEXTURE_2D, myTexture));
- GLCheck(glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, &myPixels[0]));
+ if ((myWidth == myTextureWidth) && (myHeight == myTextureHeight))
+ {
+ // Texture and array have the same size, we can use a direct copy
+
+ // Copy pixels from texture to array
+ GLCheck(glBindTexture(GL_TEXTURE_2D, myTexture));
+ GLCheck(glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, &myPixels[0]));
+ }
+ else
+ {
+ // Texture and array don't have the same size, we have to use a slower algorithm
+
+ // All the pixels will first be copied to a temporary array
+ std::vector<Color> AllPixels(myTextureWidth * myTextureHeight);
+ GLCheck(glBindTexture(GL_TEXTURE_2D, myTexture));
+ GLCheck(glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, &AllPixels[0]));
+
+ // The we copy the useful pixels from the temporary array to the final one
+ const Color* Src = &AllPixels[0];
+ Color* Dst = &myPixels[0];
+ for (unsigned int i = 0; i < myHeight; ++i)
+ {
+ std::copy(Src, Src + myWidth, Dst);
+ Src += myTextureWidth;
+ Dst += myWidth;
+ }
+ }
+ // Restore the previous texture
GLCheck(glBindTexture(GL_TEXTURE_2D, PreviousTexture));
myNeedArrayUpdate = false;