Removed all the remaining const_cast, and replaced them with mutable members (this is needed for optimized lazy calculations)

git-svn-id: https://sfml.svn.sourceforge.net/svnroot/sfml/branches/sfml2@1353 4e206d99-4929-0410-ac5d-dfc041789085
This commit is contained in:
LaurentGom 2010-01-13 13:55:20 +00:00
parent 9f1354b2a9
commit 40f13c7302
14 changed files with 217 additions and 193 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -352,10 +352,10 @@ private :
float myRotation; ///< Orientation of the object, in degrees
Color myColor; ///< Overlay color of the object
Blend::Mode myBlendMode; ///< Blending mode
mutable bool myNeedUpdate; ///< Do we need to recompute the transform matrix ?
mutable bool myInvNeedUpdate; ///< Do we need to recompute the inverse transform matrix ?
mutable Matrix3 myMatrix; ///< Precomputed transform matrix gathering the translation / rotation / scale / center
mutable Matrix3 myInvMatrix; ///< Precomputed inverse transform matrix gathering the translation / rotation / scale / center
mutable bool myMatrixUpdated; ///< Do we need to recompute the transform matrix ?
mutable bool myInvMatrixUpdated; ///< Do we need to recompute the inverse transform matrix ?
};
} // namespace sf

View File

@ -306,14 +306,14 @@ private :
/// array of pixels
///
////////////////////////////////////////////////////////////
void EnsureTextureUpdate();
void EnsureTextureUpdate() const;
////////////////////////////////////////////////////////////
/// Make sure the array of pixels is updated with the
/// texture in video memory
///
////////////////////////////////////////////////////////////
void EnsureArrayUpdate();
void EnsureArrayUpdate() const;
////////////////////////////////////////////////////////////
/// Reset the image attributes
@ -327,6 +327,11 @@ private :
////////////////////////////////////////////////////////////
void DestroyTexture();
////////////////////////////////////////////////////////////
// Types
////////////////////////////////////////////////////////////
typedef std::vector<Color> ColorArray; ///< Array of colors
////////////////////////////////////////////////////////////
// Member data
////////////////////////////////////////////////////////////
@ -336,10 +341,10 @@ private :
unsigned int myTextureHeight; ///< Actual texture height (can be greater than image height because of padding)
unsigned int myTexture; ///< Internal texture identifier
bool myIsSmooth; ///< Status of the smooth filter
std::vector<Color> myPixels; ///< Pixels of the image
bool myNeedTextureUpdate; ///< Status of synchronization between pixels in central memory and the internal texture un video memory
bool myNeedArrayUpdate; ///< Status of synchronization between pixels in central memory and the internal texture un video memory
bool myPixelsFlipped; ///< To work around the inconsistency in Y orientation
mutable ColorArray myPixels; ///< Pixels of the image
mutable bool myTextureUpdated; ///< Status of synchronization between pixels in central memory and the internal texture un video memory
mutable bool myArrayUpdated; ///< Status of synchronization between pixels in central memory and the internal texture un video memory
mutable bool myPixelsFlipped; ///< To work around the inconsistency in Y orientation
};
} // namespace sf

View File

@ -172,7 +172,7 @@ private :
/// Recompute the bounding rectangle of the text
///
////////////////////////////////////////////////////////////
void RecomputeRect();
void UpdateRect() const;
////////////////////////////////////////////////////////////
// Member data
@ -181,8 +181,8 @@ private :
ResourcePtr<Font> myFont; ///< Font used to display the string
unsigned int myCharacterSize; ///< Base size of characters, in pixels
unsigned long myStyle; ///< Text style (see Style enum)
FloatRect myBaseRect; ///< Bounding rectangle of the text in object coordinates
bool myNeedRectUpdate; ///< Does the bounding rect need an update ?
mutable FloatRect myBaseRect; ///< Bounding rectangle of the text in object coordinates
mutable bool myRectUpdated; ///< Is the bounding rectangle up-to-date ?
};
} // namespace sf

View File

@ -282,8 +282,8 @@ private :
FloatRect myViewport; ///< Viewport rectangle, expressed as a factor of the render-target's size
mutable Matrix3 myMatrix; ///< Precomputed projection matrix corresponding to the view
mutable Matrix3 myInverseMatrix; ///< Precomputed inverse projection matrix corresponding to the view
mutable bool myNeedUpdate; ///< Internal state telling if the matrix needs to be updated
mutable bool myNeedInvUpdate; ///< Internal state telling if the matrix needs to be updated
mutable bool myMatrixUpdated; ///< Internal state telling if the matrix needs to be updated
mutable bool myInvMatrixUpdated; ///< Internal state telling if the matrix needs to be updated
};
} // namespace sf

View File

@ -43,8 +43,8 @@ myOrigin (0, 0),
myRotation (rotation),
myColor (color),
myBlendMode (Blend::Alpha),
myNeedUpdate (true),
myInvNeedUpdate(true)
myMatrixUpdated (false),
myInvMatrixUpdated(false)
{
}
@ -84,8 +84,9 @@ void Drawable::SetPosition(const Vector2f& position)
void Drawable::SetX(float x)
{
myPosition.x = x;
myNeedUpdate = true;
myInvNeedUpdate = true;
myMatrixUpdated = false;
myInvMatrixUpdated = false;
}
@ -95,8 +96,9 @@ void Drawable::SetX(float x)
void Drawable::SetY(float y)
{
myPosition.y = y;
myNeedUpdate = true;
myInvNeedUpdate = true;
myMatrixUpdated = false;
myInvMatrixUpdated = false;
}
@ -128,8 +130,9 @@ void Drawable::SetScaleX(float factor)
if (factor > 0)
{
myScale.x = factor;
myNeedUpdate = true;
myInvNeedUpdate = true;
myMatrixUpdated = false;
myInvMatrixUpdated = false;
}
}
@ -142,8 +145,9 @@ void Drawable::SetScaleY(float factor)
if (factor > 0)
{
myScale.y = factor;
myNeedUpdate = true;
myInvNeedUpdate = true;
myMatrixUpdated = false;
myInvMatrixUpdated = false;
}
}
@ -157,8 +161,9 @@ void Drawable::SetOrigin(float x, float y)
{
myOrigin.x = x;
myOrigin.y = y;
myNeedUpdate = true;
myInvNeedUpdate = true;
myMatrixUpdated = false;
myInvMatrixUpdated = false;
}
@ -181,8 +186,9 @@ void Drawable::SetRotation(float angle)
myRotation = static_cast<float>(fmod(angle, 360));
if (myRotation < 0)
myRotation += 360.f;
myNeedUpdate = true;
myInvNeedUpdate = true;
myMatrixUpdated = false;
myInvMatrixUpdated = false;
}
@ -331,10 +337,10 @@ sf::Vector2f Drawable::TransformToGlobal(const sf::Vector2f& point) const
const Matrix3& Drawable::GetMatrix() const
{
// First recompute it if needed
if (myNeedUpdate)
if (!myMatrixUpdated)
{
myMatrix.SetFromTransformations(myOrigin, myPosition, myRotation, myScale);
myNeedUpdate = false;
myMatrixUpdated = true;
}
return myMatrix;
@ -347,10 +353,10 @@ const Matrix3& Drawable::GetMatrix() const
const Matrix3& Drawable::GetInverseMatrix() const
{
// First recompute it if needed
if (myInvNeedUpdate)
if (!myInvMatrixUpdated)
{
myInvMatrix = GetMatrix().GetInverse();
myInvNeedUpdate = false;
myInvMatrixUpdated = true;
}
return myInvMatrix;

View File

@ -48,8 +48,8 @@ myTextureWidth (0),
myTextureHeight (0),
myTexture (0),
myIsSmooth (true),
myNeedTextureUpdate(false),
myNeedArrayUpdate (false),
myTextureUpdated (true),
myArrayUpdated (true),
myPixelsFlipped (false)
{
@ -63,7 +63,7 @@ Image::Image(const Image& copy) :
Resource<Image>(copy)
{
// First make sure that the source image is up-to-date
const_cast<Image&>(copy).EnsureArrayUpdate();
copy.EnsureArrayUpdate();
// Copy all its members
myWidth = copy.myWidth;
@ -73,8 +73,8 @@ Resource<Image>(copy)
myTexture = 0;
myIsSmooth = copy.myIsSmooth;
myPixels = copy.myPixels;
myNeedTextureUpdate = false;
myNeedArrayUpdate = false;
myTextureUpdated = true;
myArrayUpdated = true;
myPixelsFlipped = copy.myPixelsFlipped;
// Create the texture
@ -184,7 +184,7 @@ bool Image::LoadFromPixels(unsigned int width, unsigned int height, const Uint8*
bool Image::SaveToFile(const std::string& filename) const
{
// Check if the array of pixels needs to be updated
const_cast<Image*>(this)->EnsureArrayUpdate();
EnsureArrayUpdate();
// Let the image loader save our pixel array into the image
return priv::ImageLoader::GetInstance().SaveImageToFile(filename, myPixels, myWidth, myHeight);
@ -233,7 +233,7 @@ void Image::CreateMaskFromColor(const Color& transparentColor, Uint8 alpha)
std::replace(myPixels.begin(), myPixels.end(), transparentColor, newColor);
// The texture will need to be updated
myNeedTextureUpdate = true;
myTextureUpdated = false;
}
@ -250,7 +250,7 @@ void Image::Copy(const Image& source, unsigned int destX, unsigned int destY, co
// Make sure both images have up-to-date arrays
EnsureArrayUpdate();
const_cast<Image&>(source).EnsureArrayUpdate();
source.EnsureArrayUpdate();
// Adjust the source rectangle
IntRect srcRect = sourceRect;
@ -323,7 +323,7 @@ void Image::Copy(const Image& source, unsigned int destX, unsigned int destY, co
}
// The texture will need an update
myNeedTextureUpdate = true;
myTextureUpdated = false;
}
@ -368,8 +368,8 @@ bool Image::CopyScreen(RenderWindow& window, const IntRect& sourceRect)
GLCheck(glBindTexture(GL_TEXTURE_2D, previous));
myNeedTextureUpdate = false;
myNeedArrayUpdate = true;
myTextureUpdated = true;
myArrayUpdated = false;
myPixelsFlipped = true;
return true;
@ -401,7 +401,7 @@ void Image::SetPixel(unsigned int x, unsigned int y, const Color& color)
myPixels[x + y * myWidth] = color;
// The texture will need to be updated
myNeedTextureUpdate = true;
myTextureUpdated = false;
}
@ -411,7 +411,7 @@ void Image::SetPixel(unsigned int x, unsigned int y, const Color& color)
const Color& Image::GetPixel(unsigned int x, unsigned int y) const
{
// First check if the array of pixels needs to be updated
const_cast<Image*>(this)->EnsureArrayUpdate();
EnsureArrayUpdate();
// Check if pixel is whithin the image bounds
if ((x >= myWidth) || (y >= myHeight))
@ -433,7 +433,7 @@ const Color& Image::GetPixel(unsigned int x, unsigned int y) const
const Uint8* Image::GetPixelsPtr() const
{
// First check if the array of pixels needs to be updated
const_cast<Image*>(this)->EnsureArrayUpdate();
EnsureArrayUpdate();
if (!myPixels.empty())
{
@ -461,8 +461,8 @@ void Image::UpdatePixels(const Uint8* pixels)
GLCheck(glBindTexture(GL_TEXTURE_2D, previous));
myNeedArrayUpdate = true;
myNeedTextureUpdate = false;
myArrayUpdated = false;
myTextureUpdated = true;
}
@ -484,7 +484,7 @@ void Image::UpdatePixels(const Uint8* pixels, const IntRect& rectangle)
GLCheck(glBindTexture(GL_TEXTURE_2D, previous));
// The pixel cache is no longer up-to-date
myNeedArrayUpdate = true;
myArrayUpdated = false;
}
@ -494,7 +494,7 @@ void Image::UpdatePixels(const Uint8* pixels, const IntRect& rectangle)
void Image::Bind() const
{
// First check if the texture needs to be updated
const_cast<Image*>(this)->EnsureTextureUpdate();
EnsureTextureUpdate();
// Bind it
if (myTexture)
@ -561,6 +561,8 @@ bool Image::IsSmooth() const
/// texture coordinates
////////////////////////////////////////////////////////////
FloatRect Image::GetTexCoords(const IntRect& rect) const
{
if ((myTextureWidth != 0) && (myTextureHeight != 0))
{
float width = static_cast<float>(myTextureWidth);
float height = static_cast<float>(myTextureHeight);
@ -580,6 +582,11 @@ FloatRect Image::GetTexCoords(const IntRect& rect) const
rect.Bottom / height);
}
}
else
{
return FloatRect(0, 0, 0, 0);
}
}
////////////////////////////////////////////////////////////
@ -632,8 +639,8 @@ Image& Image::operator =(const Image& other)
std::swap(myTextureHeight, temp.myTextureHeight);
std::swap(myTexture, temp.myTexture);
std::swap(myIsSmooth, temp.myIsSmooth);
std::swap(myNeedArrayUpdate, temp.myNeedArrayUpdate);
std::swap(myNeedTextureUpdate, temp.myNeedTextureUpdate);
std::swap(myArrayUpdated, temp.myArrayUpdated);
std::swap(myTextureUpdated, temp.myTextureUpdated);
std::swap(myPixelsFlipped, temp.myPixelsFlipped);
myPixels.swap(temp.myPixels);
@ -692,7 +699,7 @@ bool Image::CreateTexture()
GLCheck(glBindTexture(GL_TEXTURE_2D, previous));
}
myNeedTextureUpdate = true;
myTextureUpdated = false;
return true;
}
@ -702,9 +709,9 @@ bool Image::CreateTexture()
/// Make sure the texture in video memory is updated with the
/// array of pixels
////////////////////////////////////////////////////////////
void Image::EnsureTextureUpdate()
void Image::EnsureTextureUpdate() const
{
if (myNeedTextureUpdate)
if (!myTextureUpdated)
{
if (myTexture && !myPixels.empty())
{
@ -719,7 +726,7 @@ void Image::EnsureTextureUpdate()
GLCheck(glBindTexture(GL_TEXTURE_2D, previous));
}
myNeedTextureUpdate = false;
myTextureUpdated = true;
}
}
@ -728,9 +735,9 @@ void Image::EnsureTextureUpdate()
/// Make sure the array of pixels is updated with the
/// texture in video memory
////////////////////////////////////////////////////////////
void Image::EnsureArrayUpdate()
void Image::EnsureArrayUpdate() const
{
if (myNeedArrayUpdate)
if (!myArrayUpdated)
{
// First make sure the texture is up-to-date
// (may not be the case if an external update has been scheduled)
@ -756,7 +763,7 @@ void Image::EnsureArrayUpdate()
// 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);
ColorArray allPixels(myTextureWidth * myTextureHeight);
GLCheck(glBindTexture(GL_TEXTURE_2D, myTexture));
GLCheck(glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, &allPixels[0]));
@ -783,7 +790,7 @@ void Image::EnsureArrayUpdate()
// Restore the previous texture
GLCheck(glBindTexture(GL_TEXTURE_2D, previous));
myNeedArrayUpdate = false;
myArrayUpdated = true;
}
}
@ -801,8 +808,8 @@ void Image::Reset()
myTextureHeight = 0;
myTexture = 0;
myIsSmooth = true;
myNeedTextureUpdate = false;
myNeedArrayUpdate = false;
myTextureUpdated = true;
myArrayUpdated = true;
myPixelsFlipped = false;
myPixels.clear();
}
@ -819,8 +826,8 @@ void Image::DestroyTexture()
GLuint Texture = static_cast<GLuint>(myTexture);
GLCheck(glDeleteTextures(1, &Texture));
myTexture = 0;
myNeedTextureUpdate = false;
myNeedArrayUpdate = false;
myTextureUpdated = true;
myArrayUpdated = true;
}
}

View File

@ -140,8 +140,8 @@ void RenderImage::Display()
{
bool pixelsFlipped = myRenderImage->UpdateTexture(myImage.myTexture);
myImage.myPixelsFlipped = pixelsFlipped;
myImage.myNeedArrayUpdate = true;
myImage.myNeedTextureUpdate = false;
myImage.myArrayUpdated = false;
myImage.myTextureUpdated = true;
}
}

View File

@ -190,7 +190,7 @@ void Sprite::Render(RenderTarget&, RenderQueue& queue) const
// Check if the image is valid, and calculate the texture coordinates
FloatRect coords;
if (myImage && (myImage->GetWidth() > 0) && (myImage->GetHeight() > 0))
if (myImage)
{
coords = myImage->GetTexCoords(mySubRect);
if (myIsFlippedX) std::swap(coords.Left, coords.Right);

View File

@ -39,7 +39,7 @@ Text::Text() :
myFont (&Font::GetDefaultFont()),
myCharacterSize(30),
myStyle (Regular),
myNeedRectUpdate(true)
myRectUpdated (true)
{
}
@ -52,7 +52,7 @@ Text::Text(const String& string, const Font& font, unsigned int characterSize) :
myFont (&font),
myCharacterSize(characterSize),
myStyle (Regular),
myNeedRectUpdate(true)
myRectUpdated (true)
{
SetString(string);
}
@ -63,8 +63,8 @@ myNeedRectUpdate(true)
////////////////////////////////////////////////////////////
void Text::SetString(const String& string)
{
myNeedRectUpdate = true;
myString = string;
myRectUpdated = false;
}
@ -75,8 +75,8 @@ void Text::SetFont(const Font& font)
{
if (myFont != &font)
{
myNeedRectUpdate = true;
myFont = &font;
myRectUpdated = false;
}
}
@ -88,8 +88,8 @@ void Text::SetCharacterSize(unsigned int size)
{
if (myCharacterSize != size)
{
myNeedRectUpdate = true;
myCharacterSize = size;
myRectUpdated = false;
}
}
@ -102,8 +102,8 @@ void Text::SetStyle(unsigned long style)
{
if (myStyle != style)
{
myNeedRectUpdate = true;
myStyle = style;
myRectUpdated = false;
}
}
@ -197,8 +197,7 @@ Vector2f Text::GetCharacterPos(std::size_t index) const
////////////////////////////////////////////////////////////
FloatRect Text::GetRect() const
{
if (myNeedRectUpdate)
const_cast<Text*>(this)->RecomputeRect();
UpdateRect();
FloatRect rect;
rect.Left = (myBaseRect.Left - GetOrigin().x) * GetScale().x + GetPosition().x;
@ -312,10 +311,13 @@ void Text::Render(RenderTarget&, RenderQueue& queue) const
////////////////////////////////////////////////////////////
/// Recompute the bounding rectangle of the text
////////////////////////////////////////////////////////////
void Text::RecomputeRect()
void Text::UpdateRect() const
{
if (myRectUpdated)
return;
// Reset the previous states
myNeedRectUpdate = false;
myRectUpdated = true;
myBaseRect = FloatRect(0, 0, 0, 0);
// No text or not font: empty box

View File

@ -36,8 +36,8 @@ myCenter (),
mySize (),
myRotation (0),
myViewport (0, 0, 1, 1),
myNeedUpdate (true),
myNeedInvUpdate(true)
myMatrixUpdated (false),
myInvMatrixUpdated(false)
{
Reset(FloatRect(0, 0, 1000, 1000));
}
@ -49,8 +49,8 @@ myCenter (),
mySize (),
myRotation (0),
myViewport (0, 0, 1, 1),
myNeedUpdate (true),
myNeedInvUpdate(true)
myMatrixUpdated (false),
myInvMatrixUpdated(false)
{
Reset(rectangle);
}
@ -62,8 +62,8 @@ myCenter (center),
mySize (size),
myRotation (0),
myViewport (0, 0, 1, 1),
myNeedUpdate (true),
myNeedInvUpdate(true)
myMatrixUpdated (false),
myInvMatrixUpdated(false)
{
}
@ -73,8 +73,9 @@ void View::SetCenter(float x, float y)
{
myCenter.x = x;
myCenter.y = y;
myNeedUpdate = true;
myNeedInvUpdate = true;
myMatrixUpdated = false;
myInvMatrixUpdated = false;
}
@ -90,8 +91,9 @@ void View::SetSize(float width, float height)
{
mySize.x = width;
mySize.y = height;
myNeedUpdate = true;
myNeedInvUpdate = true;
myMatrixUpdated = false;
myInvMatrixUpdated = false;
}
@ -108,8 +110,9 @@ void View::SetRotation(float angle)
myRotation = static_cast<float>(fmod(angle, 360));
if (myRotation < 0)
myRotation += 360.f;
myNeedUpdate = true;
myNeedInvUpdate = true;
myMatrixUpdated = false;
myInvMatrixUpdated = false;
}
@ -126,8 +129,9 @@ void View::Reset(const FloatRect& rectangle)
myCenter = rectangle.GetCenter();
mySize = rectangle.GetSize();
myRotation = 0;
myNeedUpdate = true;
myNeedInvUpdate = true;
myMatrixUpdated = false;
myInvMatrixUpdated = false;
}
@ -191,10 +195,10 @@ void View::Zoom(float factor)
const Matrix3& View::GetMatrix() const
{
// Recompute the matrix if needed
if (myNeedUpdate)
if (!myMatrixUpdated)
{
myMatrix.SetFromProjection(myCenter, mySize, myRotation);
myNeedUpdate = false;
myMatrixUpdated = true;
}
return myMatrix;
@ -205,10 +209,10 @@ const Matrix3& View::GetMatrix() const
const Matrix3& View::GetInverseMatrix() const
{
// Recompute the matrix if needed
if (myNeedInvUpdate)
if (!myInvMatrixUpdated)
{
myInverseMatrix = GetMatrix().GetInverse();
myNeedInvUpdate = false;
myInvMatrixUpdated = true;
}
return myInverseMatrix;