Skip to content

Commit

Permalink
Optional interface for updating window contents during resizes etc. -…
Browse files Browse the repository at this point in the history
… Win32 implementation
  • Loading branch information
hobby8 committed Jul 19, 2019
1 parent 86672a3 commit ab342ec
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 6 deletions.
41 changes: 39 additions & 2 deletions include/SFML/Window/Window.hpp
Expand Up @@ -41,6 +41,7 @@ namespace priv
}

class Event;
class WindowCallbacks;

////////////////////////////////////////////////////////////
/// \brief Window that serves as a target for OpenGL rendering
Expand Down Expand Up @@ -251,6 +252,15 @@ class SFML_WINDOW_API Window : public WindowBase, GlResource
////////////////////////////////////////////////////////////
void display();

////////////////////////////////////////////////////////////
/// \brief TODO
///
/// TODO
/// Can be called only after window is created.
///
////////////////////////////////////////////////////////////
void setWindowCallbacks(WindowCallbacks* windowCallbacks);

private:

////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -281,10 +291,25 @@ class SFML_WINDOW_API Window : public WindowBase, GlResource
Time m_frameTimeLimit; ///< Current framerate limit
};

} // namespace sf

////////////////////////////////////////////////////////////
/// \brief TODO
///
////////////////////////////////////////////////////////////
class SFML_WINDOW_API WindowCallbacks
{
public:

#endif // SFML_WINDOW_HPP
////////////////////////////////////////////////////////////
/// \brief TODO
///
/// \param optionalResizedEvent an sf::Event::Resized event if window size has changed, or null otherwise
///
////////////////////////////////////////////////////////////
virtual void asyncRender(const Event* optionalResizedEvent) = 0;
};

} // namespace sf


////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -352,3 +377,15 @@ class SFML_WINDOW_API Window : public WindowBase, GlResource
/// \endcode
///
////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////
/// \class sf::WindowCallbacks
/// \ingroup window
///
/// TODO
///
////////////////////////////////////////////////////////////


#endif // SFML_WINDOW_HPP
24 changes: 23 additions & 1 deletion include/SFML/Window/WindowBase.hpp
Expand Up @@ -48,11 +48,27 @@ namespace priv

class Event;

////////////////////////////////////////////////////////////
/// \brief TODO
///
////////////////////////////////////////////////////////////
class SFML_WINDOW_API Resizeable
{
public:

////////////////////////////////////////////////////////////
/// \brief TODO
///
////////////////////////////////////////////////////////////
virtual void asyncSetSize(const Vector2u& size) = 0;
};


////////////////////////////////////////////////////////////
/// \brief Window that serves as a base for other windows
///
////////////////////////////////////////////////////////////
class SFML_WINDOW_API WindowBase : NonCopyable
class SFML_WINDOW_API WindowBase : Resizeable, NonCopyable
{
public:

Expand Down Expand Up @@ -414,6 +430,12 @@ class SFML_WINDOW_API WindowBase : NonCopyable
////////////////////////////////////////////////////////////
virtual void onResize();

////////////////////////////////////////////////////////////
/// \brief TODO
///
////////////////////////////////////////////////////////////
virtual void asyncSetSize(const Vector2u& size);

private:

friend class Window;
Expand Down
35 changes: 33 additions & 2 deletions src/SFML/Window/Win32/WindowImplWin32.cpp
Expand Up @@ -465,7 +465,7 @@ bool WindowImplWin32::hasFocus() const
void WindowImplWin32::registerWindowClass()
{
WNDCLASSW windowClass;
windowClass.style = 0;
windowClass.style = CS_HREDRAW | CS_VREDRAW;
windowClass.lpfnWndProc = &WindowImplWin32::globalOnEvent;
windowClass.cbClsExtra = 0;
windowClass.cbWndExtra = 0;
Expand Down Expand Up @@ -632,7 +632,7 @@ void WindowImplWin32::processEvent(UINT message, WPARAM wParam, LPARAM lParam)
m_resizing = false;

// Ignore cases where the window has only been moved
if(m_lastSize != getSize())
if (m_lastSize != getSize())
{
// Update the last handled size
m_lastSize = getSize();
Expand Down Expand Up @@ -685,6 +685,37 @@ void WindowImplWin32::processEvent(UINT message, WPARAM wParam, LPARAM lParam)
break;
}

// Paint event
case WM_PAINT:
{
WindowCallbacks *windowCallbacks = getWindowCallbacks();
if (windowCallbacks && (m_resizing || GetActiveWindow() != NULL && GetActiveWindow() != m_handle))
{
// Either moving/resizing is in progress or an owned window is active

if (m_resizing && m_lastSize != getSize())
{
// Update the last handled size
m_lastSize = getSize();

// Notify the Window class
asyncSetSize(m_lastSize);

Event event;
event.type = Event::Resized;
event.size.width = m_lastSize.x;
event.size.height = m_lastSize.y;

windowCallbacks->asyncRender(&event);
}
else
{
windowCallbacks->asyncRender(NULL);
}
}
break;
}

// Text event
case WM_CHAR:
{
Expand Down
10 changes: 10 additions & 0 deletions src/SFML/Window/Window.cpp
Expand Up @@ -227,6 +227,16 @@ void Window::display()
}


////////////////////////////////////////////////////////////
void Window::setWindowCallbacks(WindowCallbacks* windowCallbacks)
{
if (m_impl)
{
m_impl->setWindowCallbacks(this, windowCallbacks);
}
}


////////////////////////////////////////////////////////////
void Window::initialize()
{
Expand Down
11 changes: 11 additions & 0 deletions src/SFML/Window/WindowBase.cpp
Expand Up @@ -322,6 +322,17 @@ void WindowBase::onResize()
}


////////////////////////////////////////////////////////////
void WindowBase::asyncSetSize(const Vector2u& size)
{
// Cache the new size
m_size = size;

// Notify the derived class
onResize();
}


////////////////////////////////////////////////////////////
bool WindowBase::filterEvent(const Event& event)
{
Expand Down
26 changes: 25 additions & 1 deletion src/SFML/Window/WindowImpl.cpp
Expand Up @@ -81,7 +81,9 @@ WindowImpl* WindowImpl::create(WindowHandle handle)

////////////////////////////////////////////////////////////
WindowImpl::WindowImpl() :
m_joystickThreshold(0.1f)
m_joystickThreshold(0.1f),
m_resizeable (NULL),
m_windowCallbacks (NULL)
{
// Get the initial joystick states
JoystickManager::getInstance().update();
Expand Down Expand Up @@ -158,6 +160,20 @@ void WindowImpl::pushEvent(const Event& event)
}


////////////////////////////////////////////////////////////
void WindowImpl::asyncSetSize(const Vector2u& size)
{
m_resizeable->asyncSetSize(size);
}


////////////////////////////////////////////////////////////
WindowCallbacks* WindowImpl::getWindowCallbacks()
{
return m_windowCallbacks;
}


////////////////////////////////////////////////////////////
void WindowImpl::processJoystickEvents()
{
Expand Down Expand Up @@ -261,6 +277,14 @@ void WindowImpl::processSensorEvents()
}
}


////////////////////////////////////////////////////////////
void WindowImpl::setWindowCallbacks(Resizeable* resizeable, WindowCallbacks* windowCallbacks)
{
m_resizeable = resizeable;
m_windowCallbacks = windowCallbacks;
}

} // namespace priv

} // namespace sf
20 changes: 20 additions & 0 deletions src/SFML/Window/WindowImpl.hpp
Expand Up @@ -227,6 +227,12 @@ class WindowImpl : NonCopyable
////////////////////////////////////////////////////////////
virtual bool hasFocus() const = 0;

////////////////////////////////////////////////////////////
/// \brief TODO
///
////////////////////////////////////////////////////////////
void setWindowCallbacks(Resizeable* resizeable, WindowCallbacks* windowCallbacks);

protected:

////////////////////////////////////////////////////////////
Expand All @@ -253,6 +259,18 @@ class WindowImpl : NonCopyable
////////////////////////////////////////////////////////////
virtual void processEvents() = 0;

////////////////////////////////////////////////////////////
/// \brief TODO
///
////////////////////////////////////////////////////////////
void asyncSetSize(const Vector2u& size);

////////////////////////////////////////////////////////////
/// \brief TODO
///
////////////////////////////////////////////////////////////
WindowCallbacks* getWindowCallbacks();

private:

////////////////////////////////////////////////////////////
Expand All @@ -275,6 +293,8 @@ class WindowImpl : NonCopyable
Vector3f m_sensorValue[Sensor::Count]; ///< Previous value of the sensors
float m_joystickThreshold; ///< Joystick threshold (minimum motion for "move" event to be generated)
float m_previousAxes[Joystick::Count][Joystick::AxisCount]; ///< Position of each axis last time a move event triggered, in range [-100, 100]
Resizeable* m_resizeable; ///< Optional interface for notifying the Window of resizing
WindowCallbacks* m_windowCallbacks; ///< Optional interface for asynchronous handling of events
};

} // namespace priv
Expand Down

0 comments on commit ab342ec

Please sign in to comment.