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 float myRotation; ///< Orientation of the object, in degrees
Color myColor; ///< Overlay color of the object Color myColor; ///< Overlay color of the object
Blend::Mode myBlendMode; ///< Blending mode 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 myMatrix; ///< Precomputed transform matrix gathering the translation / rotation / scale / center
mutable Matrix3 myInvMatrix; ///< Precomputed inverse 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 } // namespace sf

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -140,8 +140,8 @@ void RenderImage::Display()
{ {
bool pixelsFlipped = myRenderImage->UpdateTexture(myImage.myTexture); bool pixelsFlipped = myRenderImage->UpdateTexture(myImage.myTexture);
myImage.myPixelsFlipped = pixelsFlipped; myImage.myPixelsFlipped = pixelsFlipped;
myImage.myNeedArrayUpdate = true; myImage.myArrayUpdated = false;
myImage.myNeedTextureUpdate = 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 // Check if the image is valid, and calculate the texture coordinates
FloatRect coords; FloatRect coords;
if (myImage && (myImage->GetWidth() > 0) && (myImage->GetHeight() > 0)) if (myImage)
{ {
coords = myImage->GetTexCoords(mySubRect); coords = myImage->GetTexCoords(mySubRect);
if (myIsFlippedX) std::swap(coords.Left, coords.Right); if (myIsFlippedX) std::swap(coords.Left, coords.Right);

View File

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

View File

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