diff --git a/CSFML/build/VC2005/csfml-graphics-d.def b/CSFML/build/VC2005/csfml-graphics-d.def index 701ec03e..dfbec207 100644 --- a/CSFML/build/VC2005/csfml-graphics-d.def +++ b/CSFML/build/VC2005/csfml-graphics-d.def @@ -193,6 +193,7 @@ EXPORTS sfRenderWindow_GetHeight sfRenderWindow_GetSettings sfRenderWindow_GetEvent + sfRenderWindow_WaitEvent sfRenderWindow_UseVerticalSync sfRenderWindow_ShowMouseCursor sfRenderWindow_SetCursorPosition diff --git a/CSFML/build/VC2005/csfml-graphics.def b/CSFML/build/VC2005/csfml-graphics.def index 710d629d..29afa47d 100644 --- a/CSFML/build/VC2005/csfml-graphics.def +++ b/CSFML/build/VC2005/csfml-graphics.def @@ -194,6 +194,7 @@ EXPORTS sfRenderWindow_GetHeight sfRenderWindow_GetSettings sfRenderWindow_GetEvent + sfRenderWindow_WaitEvent sfRenderWindow_UseVerticalSync sfRenderWindow_ShowMouseCursor sfRenderWindow_SetCursorPosition diff --git a/CSFML/build/VC2005/csfml-window-d.def b/CSFML/build/VC2005/csfml-window-d.def index 0003ae98..8a32efd4 100644 --- a/CSFML/build/VC2005/csfml-window-d.def +++ b/CSFML/build/VC2005/csfml-window-d.def @@ -19,6 +19,7 @@ EXPORTS sfWindow_GetHeight sfWindow_GetSettings sfWindow_GetEvent + sfWindow_WaitEvent sfWindow_UseVerticalSync sfWindow_ShowMouseCursor sfWindow_SetCursorPosition diff --git a/CSFML/build/VC2005/csfml-window.def b/CSFML/build/VC2005/csfml-window.def index 4e1c05c4..8b7f0b10 100644 --- a/CSFML/build/VC2005/csfml-window.def +++ b/CSFML/build/VC2005/csfml-window.def @@ -19,6 +19,7 @@ EXPORTS sfWindow_GetHeight sfWindow_GetSettings sfWindow_GetEvent + sfWindow_WaitEvent sfWindow_UseVerticalSync sfWindow_ShowMouseCursor sfWindow_SetCursorPosition diff --git a/CSFML/build/VC2008/csfml-graphics-d.def b/CSFML/build/VC2008/csfml-graphics-d.def index 701ec03e..dfbec207 100644 --- a/CSFML/build/VC2008/csfml-graphics-d.def +++ b/CSFML/build/VC2008/csfml-graphics-d.def @@ -193,6 +193,7 @@ EXPORTS sfRenderWindow_GetHeight sfRenderWindow_GetSettings sfRenderWindow_GetEvent + sfRenderWindow_WaitEvent sfRenderWindow_UseVerticalSync sfRenderWindow_ShowMouseCursor sfRenderWindow_SetCursorPosition diff --git a/CSFML/build/VC2008/csfml-graphics.def b/CSFML/build/VC2008/csfml-graphics.def index 710d629d..29afa47d 100644 --- a/CSFML/build/VC2008/csfml-graphics.def +++ b/CSFML/build/VC2008/csfml-graphics.def @@ -194,6 +194,7 @@ EXPORTS sfRenderWindow_GetHeight sfRenderWindow_GetSettings sfRenderWindow_GetEvent + sfRenderWindow_WaitEvent sfRenderWindow_UseVerticalSync sfRenderWindow_ShowMouseCursor sfRenderWindow_SetCursorPosition diff --git a/CSFML/build/VC2008/csfml-window-d.def b/CSFML/build/VC2008/csfml-window-d.def index 0003ae98..8a32efd4 100644 --- a/CSFML/build/VC2008/csfml-window-d.def +++ b/CSFML/build/VC2008/csfml-window-d.def @@ -19,6 +19,7 @@ EXPORTS sfWindow_GetHeight sfWindow_GetSettings sfWindow_GetEvent + sfWindow_WaitEvent sfWindow_UseVerticalSync sfWindow_ShowMouseCursor sfWindow_SetCursorPosition diff --git a/CSFML/build/VC2008/csfml-window.def b/CSFML/build/VC2008/csfml-window.def index 4e1c05c4..8b7f0b10 100644 --- a/CSFML/build/VC2008/csfml-window.def +++ b/CSFML/build/VC2008/csfml-window.def @@ -19,6 +19,7 @@ EXPORTS sfWindow_GetHeight sfWindow_GetSettings sfWindow_GetEvent + sfWindow_WaitEvent sfWindow_UseVerticalSync sfWindow_ShowMouseCursor sfWindow_SetCursorPosition diff --git a/CSFML/include/SFML/Graphics/RenderWindow.h b/CSFML/include/SFML/Graphics/RenderWindow.h index e31a7952..ca695446 100644 --- a/CSFML/include/SFML/Graphics/RenderWindow.h +++ b/CSFML/include/SFML/Graphics/RenderWindow.h @@ -123,6 +123,17 @@ CSFML_API sfContextSettings sfRenderWindow_GetSettings(sfRenderWindow* renderWin //////////////////////////////////////////////////////////// CSFML_API sfBool sfRenderWindow_GetEvent(sfRenderWindow* renderWindow, sfEvent* event); +//////////////////////////////////////////////////////////// +/// Wait for an event and return it +/// +/// \param renderWindow : Renderwindow object +/// \param event : Event to fill +/// +/// \return sfFalse if an error occured +/// +//////////////////////////////////////////////////////////// +CSFML_API sfBool sfRenderWindow_WaitEvent(sfRenderWindow* renderWindow, sfEvent* event); + //////////////////////////////////////////////////////////// /// Enable / disable vertical synchronization on a window /// diff --git a/CSFML/include/SFML/Graphics/Shape.h b/CSFML/include/SFML/Graphics/Shape.h index 719c2684..12f79944 100644 --- a/CSFML/include/SFML/Graphics/Shape.h +++ b/CSFML/include/SFML/Graphics/Shape.h @@ -22,8 +22,8 @@ // //////////////////////////////////////////////////////////// -#ifndef SFML_Shape_H -#define SFML_Shape_H +#ifndef SFML_SHAPE_H +#define SFML_SHAPE_H //////////////////////////////////////////////////////////// // Headers @@ -451,4 +451,4 @@ CSFML_API void sfShape_SetPointColor(sfShape* shape, unsigned int index, sfColor CSFML_API void sfShape_SetPointOutlineColor(sfShape* shape, unsigned int index, sfColor color); -#endif // SFML_Shape_H +#endif // SFML_SHAPE_H diff --git a/CSFML/include/SFML/Window/Window.h b/CSFML/include/SFML/Window/Window.h index d39ecf41..064c9443 100644 --- a/CSFML/include/SFML/Window/Window.h +++ b/CSFML/include/SFML/Window/Window.h @@ -145,6 +145,17 @@ CSFML_API sfContextSettings sfWindow_GetSettings(sfWindow* window); //////////////////////////////////////////////////////////// CSFML_API sfBool sfWindow_GetEvent(sfWindow* window, sfEvent* event); +//////////////////////////////////////////////////////////// +/// Wait for an event and return it +/// +/// \param window : Window object +/// \param event : Event to fill +/// +/// \return sfFalse if an error occured +/// +//////////////////////////////////////////////////////////// +CSFML_API sfBool sfWindow_WaitEvent(sfWindow* window, sfEvent* event); + //////////////////////////////////////////////////////////// /// Enable / disable vertical synchronization on a window /// diff --git a/CSFML/src/SFML/ConvertEvent.h b/CSFML/src/SFML/ConvertEvent.h new file mode 100644 index 00000000..acfb6812 --- /dev/null +++ b/CSFML/src/SFML/ConvertEvent.h @@ -0,0 +1,96 @@ +//////////////////////////////////////////////////////////// +// +// SFML - Simple and Fast Multimedia Library +// Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com) +// +// This software is provided 'as-is', without any express or implied warranty. +// In no event will the authors be held liable for any damages arising from the use of this software. +// +// Permission is granted to anyone to use this software for any purpose, +// including commercial applications, and to alter it and redistribute it freely, +// subject to the following restrictions: +// +// 1. The origin of this software must not be misrepresented; +// you must not claim that you wrote the original software. +// If you use this software in a product, an acknowledgment +// in the product documentation would be appreciated but is not required. +// +// 2. Altered source versions must be plainly marked as such, +// and must not be misrepresented as being the original software. +// +// 3. This notice may not be removed or altered from any source distribution. +// +//////////////////////////////////////////////////////////// + +#ifndef SFML_CONVERTEVENT_H +#define SFML_CONVERTEVENT_H + +//////////////////////////////////////////////////////////// +// Headers +//////////////////////////////////////////////////////////// +#include +#include + + +//////////////////////////////////////////////////////////// +// Define a function to convert a sf::Event ot a sfEvent +//////////////////////////////////////////////////////////// +inline void ConvertEvent(const sf::Event& SFMLEvent, sfEvent* event) +{ + // Convert its type + event->Type = static_cast(SFMLEvent.Type); + + // Fill its fields + switch (event->Type) + { + case sfEvtResized : + event->Size.Width = SFMLEvent.Size.Width; + event->Size.Height = SFMLEvent.Size.Height; + break; + + case sfEvtTextEntered : + event->Text.Unicode = SFMLEvent.Text.Unicode; + break; + + case sfEvtKeyReleased : + case sfEvtKeyPressed : + event->Key.Code = static_cast(SFMLEvent.Key.Code); + event->Key.Alt = SFMLEvent.Key.Alt ? sfTrue : sfFalse; + event->Key.Control = SFMLEvent.Key.Control ? sfTrue : sfFalse; + event->Key.Shift = SFMLEvent.Key.Shift ? sfTrue : sfFalse; + break; + + case sfEvtMouseWheelMoved : + event->MouseWheel.Delta = SFMLEvent.MouseWheel.Delta; + break; + + case sfEvtMouseButtonPressed : + case sfEvtMouseButtonReleased : + event->MouseButton.Button = static_cast(SFMLEvent.MouseButton.Button); + event->MouseButton.X = SFMLEvent.MouseButton.X; + event->MouseButton.Y = SFMLEvent.MouseButton.Y; + break; + + case sfEvtMouseMoved : + event->MouseMove.X = SFMLEvent.MouseMove.X; + event->MouseMove.Y = SFMLEvent.MouseMove.Y; + break; + + case sfEvtJoyButtonPressed : + case sfEvtJoyButtonReleased : + event->JoyButton.JoystickId = SFMLEvent.JoyButton.JoystickId; + event->JoyButton.Button = SFMLEvent.JoyButton.Button; + break; + + case sfEvtJoyMoved : + event->JoyMove.JoystickId = SFMLEvent.JoyMove.JoystickId; + event->JoyMove.Axis = static_cast(SFMLEvent.JoyMove.Axis); + event->JoyMove.Position = SFMLEvent.JoyMove.Position; + break; + + default : + break; + } +} + +#endif // SFML_CONVERTEVENT_H diff --git a/CSFML/src/SFML/Graphics/RenderWindow.cpp b/CSFML/src/SFML/Graphics/RenderWindow.cpp index e919c939..9bc2984e 100644 --- a/CSFML/src/SFML/Graphics/RenderWindow.cpp +++ b/CSFML/src/SFML/Graphics/RenderWindow.cpp @@ -33,6 +33,7 @@ #include #include #include +#include //////////////////////////////////////////////////////////// @@ -150,60 +151,31 @@ sfBool sfRenderWindow_GetEvent(sfRenderWindow* renderWindow, sfEvent* event) if (!ret) return sfFalse; - // Convert its type - event->Type = static_cast(SFMLEvent.Type); + // Convert the sf::Event event to a sfEvent + ConvertEvent(SFMLEvent, event); - // Fill its fields - switch (event->Type) - { - case sfEvtResized : - event->Size.Width = SFMLEvent.Size.Width; - event->Size.Height = SFMLEvent.Size.Height; - break; + return sfTrue; +} - case sfEvtTextEntered : - event->Text.Unicode = SFMLEvent.Text.Unicode; - break; - case sfEvtKeyReleased : - case sfEvtKeyPressed : - event->Key.Code = static_cast(SFMLEvent.Key.Code); - event->Key.Alt = SFMLEvent.Key.Alt ? sfTrue : sfFalse; - event->Key.Control = SFMLEvent.Key.Control ? sfTrue : sfFalse; - event->Key.Shift = SFMLEvent.Key.Shift ? sfTrue : sfFalse; - break; +//////////////////////////////////////////////////////////// +/// Wait for an event and return it +//////////////////////////////////////////////////////////// +sfBool sfRenderWindow_WaitEvent(sfRenderWindow* renderWindow, sfEvent* event) +{ + CSFML_CHECK_RETURN(renderWindow, sfFalse); + CSFML_CHECK_RETURN(event, sfFalse); - case sfEvtMouseWheelMoved : - event->MouseWheel.Delta = SFMLEvent.MouseWheel.Delta; - break; + // Get the event + sf::Event SFMLEvent; + sfBool ret = renderWindow->This.WaitEvent(SFMLEvent); - case sfEvtMouseButtonPressed : - case sfEvtMouseButtonReleased : - event->MouseButton.Button = static_cast(SFMLEvent.MouseButton.Button); - event->MouseButton.X = SFMLEvent.MouseButton.X; - event->MouseButton.Y = SFMLEvent.MouseButton.Y; - break; + // Error, return + if (!ret) + return sfFalse; - case sfEvtMouseMoved : - event->MouseMove.X = SFMLEvent.MouseMove.X; - event->MouseMove.Y = SFMLEvent.MouseMove.Y; - break; - - case sfEvtJoyButtonPressed : - case sfEvtJoyButtonReleased : - event->JoyButton.JoystickId = SFMLEvent.JoyButton.JoystickId; - event->JoyButton.Button = SFMLEvent.JoyButton.Button; - break; - - case sfEvtJoyMoved : - event->JoyMove.JoystickId = SFMLEvent.JoyMove.JoystickId; - event->JoyMove.Axis = static_cast(SFMLEvent.JoyMove.Axis); - event->JoyMove.Position = SFMLEvent.JoyMove.Position; - break; - - default : - break; - } + // Convert the sf::Event event to a sfEvent + ConvertEvent(SFMLEvent, event); return sfTrue; } diff --git a/CSFML/src/SFML/Internal.h b/CSFML/src/SFML/Internal.h index 7817aa87..e6dd6cfc 100644 --- a/CSFML/src/SFML/Internal.h +++ b/CSFML/src/SFML/Internal.h @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////// // // SFML - Simple and Fast Multimedia Library -// Copyright (C) 2007 Laurent Gomila (laurent.gom@gmail.com) +// Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com) // // This software is provided 'as-is', without any express or implied warranty. // In no event will the authors be held liable for any damages arising from the use of this software. @@ -22,6 +22,8 @@ // //////////////////////////////////////////////////////////// +#ifndef SFML_INTERNAL_H +#define SFML_INTERNAL_H //////////////////////////////////////////////////////////// // Define macros to check the validity of CSFML objects @@ -102,3 +104,5 @@ #define CSFML_CALL_PTR_RETURN(Object, Function, Default) (void)Default; return (Object->This->Function); #endif + +#endif // SFML_INTERNAL_H diff --git a/CSFML/src/SFML/Window/Window.cpp b/CSFML/src/SFML/Window/Window.cpp index 55b870a2..d4389fba 100644 --- a/CSFML/src/SFML/Window/Window.cpp +++ b/CSFML/src/SFML/Window/Window.cpp @@ -28,6 +28,7 @@ #include #include #include +#include //////////////////////////////////////////////////////////// @@ -139,60 +140,31 @@ sfBool sfWindow_GetEvent(sfWindow* window, sfEvent* event) if (!ret) return sfFalse; - // Convert its type - event->Type = static_cast(SFMLEvent.Type); + // Convert the sf::Event event to a sfEvent + ConvertEvent(SFMLEvent, event); - // Fill its fields - switch (event->Type) - { - case sfEvtResized : - event->Size.Width = SFMLEvent.Size.Width; - event->Size.Height = SFMLEvent.Size.Height; - break; + return sfTrue; +} - case sfEvtTextEntered : - event->Text.Unicode = SFMLEvent.Text.Unicode; - break; - case sfEvtKeyReleased : - case sfEvtKeyPressed : - event->Key.Code = static_cast(SFMLEvent.Key.Code); - event->Key.Alt = SFMLEvent.Key.Alt ? sfTrue : sfFalse; - event->Key.Control = SFMLEvent.Key.Control ? sfTrue : sfFalse; - event->Key.Shift = SFMLEvent.Key.Shift ? sfTrue : sfFalse; - break; +//////////////////////////////////////////////////////////// +/// Wait for an event and return it +//////////////////////////////////////////////////////////// +sfBool sfWindow_WaitEvent(sfWindow* window, sfEvent* event) +{ + CSFML_CHECK_RETURN(window, sfFalse); + CSFML_CHECK_RETURN(event, sfFalse); - case sfEvtMouseWheelMoved : - event->MouseWheel.Delta = SFMLEvent.MouseWheel.Delta; - break; + // Get the event + sf::Event SFMLEvent; + sfBool ret = window->This.WaitEvent(SFMLEvent); - case sfEvtMouseButtonPressed : - case sfEvtMouseButtonReleased : - event->MouseButton.Button = static_cast(SFMLEvent.MouseButton.Button); - event->MouseButton.X = SFMLEvent.MouseButton.X; - event->MouseButton.Y = SFMLEvent.MouseButton.Y; - break; + // Error, return + if (!ret) + return sfFalse; - case sfEvtMouseMoved : - event->MouseMove.X = SFMLEvent.MouseMove.X; - event->MouseMove.Y = SFMLEvent.MouseMove.Y; - break; - - case sfEvtJoyButtonPressed : - case sfEvtJoyButtonReleased : - event->JoyButton.JoystickId = SFMLEvent.JoyButton.JoystickId; - event->JoyButton.Button = SFMLEvent.JoyButton.Button; - break; - - case sfEvtJoyMoved : - event->JoyMove.JoystickId = SFMLEvent.JoyMove.JoystickId; - event->JoyMove.Axis = static_cast(SFMLEvent.JoyMove.Axis); - event->JoyMove.Position = SFMLEvent.JoyMove.Position; - break; - - default : - break; - } + // Convert the sf::Event event to a sfEvent + ConvertEvent(SFMLEvent, event); return sfTrue; } diff --git a/dotnet/extlibs/csfml-audio.dll b/dotnet/extlibs/csfml-audio.dll index 15fbcc8d..502ce538 100644 Binary files a/dotnet/extlibs/csfml-audio.dll and b/dotnet/extlibs/csfml-audio.dll differ diff --git a/dotnet/extlibs/csfml-graphics.dll b/dotnet/extlibs/csfml-graphics.dll index 850d3801..c3b9d894 100644 Binary files a/dotnet/extlibs/csfml-graphics.dll and b/dotnet/extlibs/csfml-graphics.dll differ diff --git a/dotnet/extlibs/csfml-window.dll b/dotnet/extlibs/csfml-window.dll index eb2ca713..b7331f21 100644 Binary files a/dotnet/extlibs/csfml-window.dll and b/dotnet/extlibs/csfml-window.dll differ diff --git a/dotnet/src/Graphics/RenderWindow.cs b/dotnet/src/Graphics/RenderWindow.cs index 1ccf9283..5af31134 100644 --- a/dotnet/src/Graphics/RenderWindow.cs +++ b/dotnet/src/Graphics/RenderWindow.cs @@ -436,6 +436,18 @@ namespace SFML return sfRenderWindow_GetEvent(This, out eventToFill); } + //////////////////////////////////////////////////////////// + /// + /// Internal function to get the next event (blocking) + /// + /// Variable to fill with the raw pointer to the event structure + /// False if any error occured + //////////////////////////////////////////////////////////// + protected override bool WaitEvent(out Event eventToFill) + { + return sfRenderWindow_WaitEvent(This, out eventToFill); + } + //////////////////////////////////////////////////////////// /// /// Handle the destruction of the object @@ -487,6 +499,9 @@ namespace SFML [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern bool sfRenderWindow_GetEvent(IntPtr This, out Event Evt); + + [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] + static extern bool sfRenderWindow_WaitEvent(IntPtr This, out Event Evt); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern void sfRenderWindow_Clear(IntPtr This, Color ClearColor); diff --git a/dotnet/src/Window/Window.cs b/dotnet/src/Window/Window.cs index eb1df9ba..7371b742 100644 --- a/dotnet/src/Window/Window.cs +++ b/dotnet/src/Window/Window.cs @@ -340,97 +340,27 @@ namespace SFML //////////////////////////////////////////////////////////// /// - /// Call the event handlers for each pending event. + /// Wait for a new event and dispatch it to the corresponding + /// event handler + /// + //////////////////////////////////////////////////////////// + public void WaitAndDispatchEvents() + { + Event e; + if (WaitEvent(out e)) + CallEventHandler(e); + } + + //////////////////////////////////////////////////////////// + /// + /// Call the event handlers for each pending event /// //////////////////////////////////////////////////////////// public void DispatchEvents() { Event e; while (GetEvent(out e)) - { - switch (e.Type) - { - case EventType.Closed : - if (Closed != null) - Closed(this, EventArgs.Empty); - break; - - case EventType.GainedFocus : - if (GainedFocus != null) - GainedFocus(this, EventArgs.Empty); - break; - - case EventType.JoyButtonPressed : - if (JoyButtonPressed != null) - JoyButtonPressed(this, new JoyButtonEventArgs(e.JoyButton)); - break; - - case EventType.JoyButtonReleased : - if (JoyButtonReleased != null) - JoyButtonReleased(this, new JoyButtonEventArgs(e.JoyButton)); - break; - - case EventType.JoyMoved : - if (JoyMoved != null) - JoyMoved(this, new JoyMoveEventArgs(e.JoyMove)); - break; - - case EventType.KeyPressed : - if (KeyPressed != null) - KeyPressed(this, new KeyEventArgs(e.Key)); - break; - - case EventType.KeyReleased : - if (KeyReleased != null) - KeyReleased(this, new KeyEventArgs(e.Key)); - break; - - case EventType.LostFocus : - if (LostFocus != null) - LostFocus(this, EventArgs.Empty); - break; - - case EventType.MouseButtonPressed : - if (MouseButtonPressed != null) - MouseButtonPressed(this, new MouseButtonEventArgs(e.MouseButton)); - break; - - case EventType.MouseButtonReleased : - if (MouseButtonReleased != null) - MouseButtonReleased(this, new MouseButtonEventArgs(e.MouseButton)); - break; - - case EventType.MouseEntered : - if (MouseEntered != null) - MouseEntered(this, EventArgs.Empty); - break; - - case EventType.MouseLeft : - if (MouseLeft != null) - MouseLeft(this, EventArgs.Empty); - break; - - case EventType.MouseMoved : - if (MouseMoved != null) - MouseMoved(this, new MouseMoveEventArgs(e.MouseMove)); - break; - - case EventType.MouseWheelMoved : - if (MouseWheelMoved != null) - MouseWheelMoved(this, new MouseWheelEventArgs(e.MouseWheel)); - break; - - case EventType.Resized : - if (Resized != null) - Resized(this, new SizeEventArgs(e.Size)); - break; - - case EventType.TextEntered : - if (TextEntered != null) - TextEntered(this, new TextEventArgs(e.Text)); - break; - } - } + CallEventHandler(e); } //////////////////////////////////////////////////////////// @@ -448,7 +378,7 @@ namespace SFML //////////////////////////////////////////////////////////// /// - /// Internal function to get the next event + /// Internal function to get the next event (non-blocking) /// /// Variable to fill with the raw pointer to the event structure /// True if there was an event, false otherwise @@ -458,6 +388,18 @@ namespace SFML return sfWindow_GetEvent(This, out eventToFill); } + //////////////////////////////////////////////////////////// + /// + /// Internal function to get the next event (blocking) + /// + /// Variable to fill with the raw pointer to the event structure + /// False if any error occured + //////////////////////////////////////////////////////////// + protected virtual bool WaitEvent(out Event eventToFill) + { + return sfWindow_WaitEvent(This, out eventToFill); + } + //////////////////////////////////////////////////////////// /// /// Handle the destruction of the object @@ -469,6 +411,98 @@ namespace SFML sfWindow_Destroy(This); } + //////////////////////////////////////////////////////////// + /// + /// Call the event handler for the given event + /// + /// Event to dispatch + //////////////////////////////////////////////////////////// + private void CallEventHandler(Event e) + { + switch (e.Type) + { + case EventType.Closed : + if (Closed != null) + Closed(this, EventArgs.Empty); + break; + + case EventType.GainedFocus : + if (GainedFocus != null) + GainedFocus(this, EventArgs.Empty); + break; + + case EventType.JoyButtonPressed : + if (JoyButtonPressed != null) + JoyButtonPressed(this, new JoyButtonEventArgs(e.JoyButton)); + break; + + case EventType.JoyButtonReleased : + if (JoyButtonReleased != null) + JoyButtonReleased(this, new JoyButtonEventArgs(e.JoyButton)); + break; + + case EventType.JoyMoved : + if (JoyMoved != null) + JoyMoved(this, new JoyMoveEventArgs(e.JoyMove)); + break; + + case EventType.KeyPressed : + if (KeyPressed != null) + KeyPressed(this, new KeyEventArgs(e.Key)); + break; + + case EventType.KeyReleased : + if (KeyReleased != null) + KeyReleased(this, new KeyEventArgs(e.Key)); + break; + + case EventType.LostFocus : + if (LostFocus != null) + LostFocus(this, EventArgs.Empty); + break; + + case EventType.MouseButtonPressed : + if (MouseButtonPressed != null) + MouseButtonPressed(this, new MouseButtonEventArgs(e.MouseButton)); + break; + + case EventType.MouseButtonReleased : + if (MouseButtonReleased != null) + MouseButtonReleased(this, new MouseButtonEventArgs(e.MouseButton)); + break; + + case EventType.MouseEntered : + if (MouseEntered != null) + MouseEntered(this, EventArgs.Empty); + break; + + case EventType.MouseLeft : + if (MouseLeft != null) + MouseLeft(this, EventArgs.Empty); + break; + + case EventType.MouseMoved : + if (MouseMoved != null) + MouseMoved(this, new MouseMoveEventArgs(e.MouseMove)); + break; + + case EventType.MouseWheelMoved : + if (MouseWheelMoved != null) + MouseWheelMoved(this, new MouseWheelEventArgs(e.MouseWheel)); + break; + + case EventType.Resized : + if (Resized != null) + Resized(this, new SizeEventArgs(e.Size)); + break; + + case EventType.TextEntered : + if (TextEntered != null) + TextEntered(this, new TextEventArgs(e.Text)); + break; + } + } + /// Event handler for the Closed event public event EventHandler Closed = null; @@ -541,6 +575,9 @@ namespace SFML [DllImport("csfml-window"), SuppressUnmanagedCodeSecurity] static extern bool sfWindow_GetEvent(IntPtr This, out Event Evt); + [DllImport("csfml-window"), SuppressUnmanagedCodeSecurity] + static extern bool sfWindow_WaitEvent(IntPtr This, out Event Evt); + [DllImport("csfml-window"), SuppressUnmanagedCodeSecurity] static extern void sfWindow_Display(IntPtr This); diff --git a/include/SFML/Window/Window.hpp b/include/SFML/Window/Window.hpp index 7b347292..9f2713a0 100644 --- a/include/SFML/Window/Window.hpp +++ b/include/SFML/Window/Window.hpp @@ -192,7 +192,7 @@ public : //////////////////////////////////////////////////////////// /// \brief Pop the event on top of events stack, if any, and return it /// - /// This function is not blocking, if there's no pending event then + /// This function is not blocking: if there's no pending event then /// it will return false and leave \a event unmodified. /// Note that more than event may be present in the events stack, /// thus you should always call this function in a loop @@ -212,6 +212,31 @@ public : //////////////////////////////////////////////////////////// bool GetEvent(Event& event); + //////////////////////////////////////////////////////////// + /// \brief Wait for an event and return it + /// + /// This function is blocking: if there's no pending event then + /// it will wait until an event is received. + /// After this function returns (and no error occured), + /// the \a event object is always valid and filled properly. + /// This function is typically used when you have a thread that + /// is dedicated to events handling: you want to make this thread + /// sleep as long as no new event is received. + /// \code + /// sf::Event event; + /// if (window.WaitEvent(event)) + /// { + /// // process event... + /// } + /// \endcode + /// + /// \param event Event to be returned + /// + /// \return False if any error occured + /// + //////////////////////////////////////////////////////////// + bool WaitEvent(Event& event); + //////////////////////////////////////////////////////////// /// \brief Enable or disable vertical synchronization /// diff --git a/src/SFML/Window/Linux/WindowImplX11.cpp b/src/SFML/Window/Linux/WindowImplX11.cpp index f0eaeffa..1f9610a3 100644 --- a/src/SFML/Window/Linux/WindowImplX11.cpp +++ b/src/SFML/Window/Linux/WindowImplX11.cpp @@ -280,7 +280,7 @@ WindowHandle WindowImplX11::GetHandle() const //////////////////////////////////////////////////////////// -void WindowImplX11::ProcessEvents() +void WindowImplX11::ProcessEvents(bool block) { // This function implements a workaround to properly discard // repeated key events when necessary. The problem is that the diff --git a/src/SFML/Window/Linux/WindowImplX11.hpp b/src/SFML/Window/Linux/WindowImplX11.hpp index 0c8a4ca9..768e2da6 100644 --- a/src/SFML/Window/Linux/WindowImplX11.hpp +++ b/src/SFML/Window/Linux/WindowImplX11.hpp @@ -82,81 +82,83 @@ public : ::Display* GetDisplay() const; private : - - //////////////////////////////////////////////////////////// - /// \brief Get the OS-specific handle of the window - /// - /// \return Handle of the window - /// - //////////////////////////////////////////////////////////// - virtual WindowHandle GetHandle() const; - - //////////////////////////////////////////////////////////// - /// \brief Process incoming events from operating system - /// - //////////////////////////////////////////////////////////// - virtual void ProcessEvents(); - - //////////////////////////////////////////////////////////// - /// \brief Show or hide the mouse cursor - /// - /// \param show True to show, false to hide - /// - //////////////////////////////////////////////////////////// - virtual void ShowMouseCursor(bool show); - - //////////////////////////////////////////////////////////// - /// \brief Change the position of the mouse cursor - /// - /// \param left Left coordinate of the cursor, relative to the window - /// \param top Top coordinate of the cursor, relative to the window - /// - //////////////////////////////////////////////////////////// - virtual void SetCursorPosition(unsigned int left, unsigned int top); - - //////////////////////////////////////////////////////////// - /// \brief Change the position of the window on screen - /// - /// \param left Left position - /// \param top Top position - /// - //////////////////////////////////////////////////////////// - virtual void SetPosition(int left, int top); - - //////////////////////////////////////////////////////////// - /// \brief Change the size of the rendering region of the window - /// - /// \param width New width - /// \param height New height - /// - //////////////////////////////////////////////////////////// - virtual void SetSize(unsigned int width, unsigned int height); - - //////////////////////////////////////////////////////////// - /// \brief Show or hide the window - /// - /// \param show True to show, false to hide - /// - //////////////////////////////////////////////////////////// - virtual void Show(bool show); - - //////////////////////////////////////////////////////////// - /// \brief Enable or disable automatic key-repeat - /// - /// \param enabled True to enable, false to disable - /// - //////////////////////////////////////////////////////////// - virtual void EnableKeyRepeat(bool enabled); - - //////////////////////////////////////////////////////////// - /// \brief Change the window's icon - /// - /// \param width Icon's width, in pixels - /// \param height Icon's height, in pixels - /// \param pixels Pointer to the pixels in memory, format must be RGBA 32 bits - /// - //////////////////////////////////////////////////////////// - virtual void SetIcon(unsigned int width, unsigned int height, const Uint8* pixels); + + //////////////////////////////////////////////////////////// + /// \brief Get the OS-specific handle of the window + /// + /// \return Handle of the window + /// + //////////////////////////////////////////////////////////// + virtual WindowHandle GetHandle() const; + + //////////////////////////////////////////////////////////// + /// \brief Process incoming events from the operating system + /// + /// \param block Use true to block the thread until an event arrives + /// + //////////////////////////////////////////////////////////// + virtual void ProcessEvents(bool block); + + //////////////////////////////////////////////////////////// + /// \brief Show or hide the mouse cursor + /// + /// \param show True to show, false to hide + /// + //////////////////////////////////////////////////////////// + virtual void ShowMouseCursor(bool show); + + //////////////////////////////////////////////////////////// + /// \brief Change the position of the mouse cursor + /// + /// \param left Left coordinate of the cursor, relative to the window + /// \param top Top coordinate of the cursor, relative to the window + /// + //////////////////////////////////////////////////////////// + virtual void SetCursorPosition(unsigned int left, unsigned int top); + + //////////////////////////////////////////////////////////// + /// \brief Change the position of the window on screen + /// + /// \param left Left position + /// \param top Top position + /// + //////////////////////////////////////////////////////////// + virtual void SetPosition(int left, int top); + + //////////////////////////////////////////////////////////// + /// \brief Change the size of the rendering region of the window + /// + /// \param width New width + /// \param height New height + /// + //////////////////////////////////////////////////////////// + virtual void SetSize(unsigned int width, unsigned int height); + + //////////////////////////////////////////////////////////// + /// \brief Show or hide the window + /// + /// \param show True to show, false to hide + /// + //////////////////////////////////////////////////////////// + virtual void Show(bool show); + + //////////////////////////////////////////////////////////// + /// \brief Enable or disable automatic key-repeat + /// + /// \param enabled True to enable, false to disable + /// + //////////////////////////////////////////////////////////// + virtual void EnableKeyRepeat(bool enabled); + + //////////////////////////////////////////////////////////// + /// \brief Change the window's icon + /// + /// \param width Icon's width, in pixels + /// \param height Icon's height, in pixels + /// \param pixels Pointer to the pixels in memory, format must be RGBA 32 bits + /// + //////////////////////////////////////////////////////////// + virtual void SetIcon(unsigned int width, unsigned int height, const Uint8* pixels); //////////////////////////////////////////////////////////// /// \brief Switch to fullscreen mode diff --git a/src/SFML/Window/Win32/WindowImplWin32.cpp b/src/SFML/Window/Win32/WindowImplWin32.cpp index 34a6f19a..01a0bf48 100644 --- a/src/SFML/Window/Win32/WindowImplWin32.cpp +++ b/src/SFML/Window/Win32/WindowImplWin32.cpp @@ -202,11 +202,14 @@ WindowHandle WindowImplWin32::GetHandle() const //////////////////////////////////////////////////////////// -void WindowImplWin32::ProcessEvents() +void WindowImplWin32::ProcessEvents(bool block) { - // We update the window only if we own it + // We process the window events only if we own it if (!myCallback) { + if (block) + WaitMessage(); + MSG message; while (PeekMessage(&message, myHandle, 0, 0, PM_REMOVE)) { diff --git a/src/SFML/Window/Win32/WindowImplWin32.hpp b/src/SFML/Window/Win32/WindowImplWin32.hpp index f5fcb9e3..232b93e4 100644 --- a/src/SFML/Window/Win32/WindowImplWin32.hpp +++ b/src/SFML/Window/Win32/WindowImplWin32.hpp @@ -81,10 +81,12 @@ private : virtual WindowHandle GetHandle() const; //////////////////////////////////////////////////////////// - /// \brief Process incoming events from operating system + /// \brief Process incoming events from the operating system + /// + /// \param block Use true to block the thread until an event arrives /// //////////////////////////////////////////////////////////// - virtual void ProcessEvents(); + virtual void ProcessEvents(bool block); //////////////////////////////////////////////////////////// /// \brief Show or hide the mouse cursor diff --git a/src/SFML/Window/Window.cpp b/src/SFML/Window/Window.cpp index ac92c142..e8eedaa7 100644 --- a/src/SFML/Window/Window.cpp +++ b/src/SFML/Window/Window.cpp @@ -211,9 +211,29 @@ bool Window::GetEvent(Event& event) { // Let the window implementation process incoming events if the events queue is empty if (myWindow && myEvents.empty()) - myWindow->DoEvents(); + myWindow->DoEvents(false); - // Pop first event of queue, if not empty + // Pop the first event of the queue, if it is not empty + if (!myEvents.empty()) + { + event = myEvents.front(); + myEvents.pop(); + + return true; + } + + return false; +} + + +//////////////////////////////////////////////////////////// +bool Window::WaitEvent(Event& event) +{ + // Let the window implementation process incoming events if the events queue is empty + if (myWindow && myEvents.empty()) + myWindow->DoEvents(true); + + // Pop the first event of the queue, if it is not empty if (!myEvents.empty()) { event = myEvents.front(); diff --git a/src/SFML/Window/WindowImpl.cpp b/src/SFML/Window/WindowImpl.cpp index cc754199..7e411905 100644 --- a/src/SFML/Window/WindowImpl.cpp +++ b/src/SFML/Window/WindowImpl.cpp @@ -126,13 +126,13 @@ void WindowImpl::SetJoystickThreshold(float threshold) //////////////////////////////////////////////////////////// -void WindowImpl::DoEvents() +void WindowImpl::DoEvents(bool block) { // Read the joysticks state and generate the appropriate events ProcessJoystickEvents(); // Let the derived class process other events - ProcessEvents(); + ProcessEvents(block); } diff --git a/src/SFML/Window/WindowImpl.hpp b/src/SFML/Window/WindowImpl.hpp index 06cfe0b5..82272bd1 100644 --- a/src/SFML/Window/WindowImpl.hpp +++ b/src/SFML/Window/WindowImpl.hpp @@ -124,10 +124,12 @@ public : void SetJoystickThreshold(float threshold); //////////////////////////////////////////////////////////// - /// \brief Process incoming events from operating system + /// \brief Process incoming events from the operating system + /// + /// \param block Use true to block the thread until an event arrives /// //////////////////////////////////////////////////////////// - void DoEvents(); + void DoEvents(bool block); //////////////////////////////////////////////////////////// /// \brief Get the OS-specific handle of the window @@ -229,10 +231,12 @@ private : void ProcessJoystickEvents(); //////////////////////////////////////////////////////////// - /// \brief Process incoming events from operating system + /// \brief Process incoming events from the operating system + /// + /// \param block Use true to block the thread until an event arrives /// //////////////////////////////////////////////////////////// - virtual void ProcessEvents() = 0; + virtual void ProcessEvents(bool block) = 0; //////////////////////////////////////////////////////////// // Total number of joysticks supported