From da96ec5811d67c6692751c27b5dc905a23dce3ed Mon Sep 17 00:00:00 2001 From: Laurent Gomila Date: Wed, 12 Jun 2013 20:27:18 +0200 Subject: [PATCH] Improved the performances of Window::getSize() (the size is now cached) --- include/SFML/Window/Window.hpp | 1 + src/SFML/Window/Window.cpp | 21 +++++++++++++++++---- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/include/SFML/Window/Window.hpp b/include/SFML/Window/Window.hpp index cee0a1d4..21bb2c55 100644 --- a/include/SFML/Window/Window.hpp +++ b/include/SFML/Window/Window.hpp @@ -473,6 +473,7 @@ private: priv::GlContext* m_context; ///< Platform-specific implementation of the OpenGL context Clock m_clock; ///< Clock for measuring the elapsed time between frames Time m_frameTimeLimit; ///< Current framerate limit + Vector2u m_size; ///< Current size of the window }; } // namespace sf diff --git a/src/SFML/Window/Window.cpp b/src/SFML/Window/Window.cpp index 5d5f51de..71f71b2c 100644 --- a/src/SFML/Window/Window.cpp +++ b/src/SFML/Window/Window.cpp @@ -44,7 +44,8 @@ namespace sf Window::Window() : m_impl (NULL), m_context (NULL), -m_frameTimeLimit(Time::Zero) +m_frameTimeLimit(Time::Zero), +m_size (0, 0) { } @@ -54,7 +55,8 @@ m_frameTimeLimit(Time::Zero) Window::Window(VideoMode mode, const String& title, Uint32 style, const ContextSettings& settings) : m_impl (NULL), m_context (NULL), -m_frameTimeLimit(Time::Zero) +m_frameTimeLimit(Time::Zero), +m_size (0, 0) { create(mode, title, style, settings); } @@ -64,7 +66,8 @@ m_frameTimeLimit(Time::Zero) Window::Window(WindowHandle handle, const ContextSettings& settings) : m_impl (NULL), m_context (NULL), -m_frameTimeLimit(Time::Zero) +m_frameTimeLimit(Time::Zero), +m_size (0, 0) { create(handle, settings); } @@ -223,7 +226,7 @@ void Window::setPosition(const Vector2i& position) //////////////////////////////////////////////////////////// Vector2u Window::getSize() const { - return m_impl ? m_impl->getSize() : Vector2u(); + return m_size; } @@ -365,7 +368,14 @@ bool Window::filterEvent(const Event& event) { // Notify resize events to the derived class if (event.type == Event::Resized) + { + // Cache the new size + m_size.x = event.size.width; + m_size.y = event.size.height; + + // Notify the derived class onResize(); + } return true; } @@ -380,6 +390,9 @@ void Window::initialize() setVerticalSyncEnabled(false); setKeyRepeatEnabled(true); + // Get and cache the initial size of the window + m_size = m_impl->getSize(); + // Reset frame time m_clock.restart();