From 3c7fba0f96538b253f95bbd5ad69a500886063d1 Mon Sep 17 00:00:00 2001 From: Vittorio Romeo Date: Thu, 16 Dec 2021 22:07:26 +0100 Subject: [PATCH] Reduce compile-time impact of 'windows.h' --- examples/win32/Win32.cpp | 3 ++ include/SFML/OpenGL.hpp | 5 ++- src/SFML/Main/MainWin32.cpp | 3 +- src/SFML/Network/Win32/SocketImpl.hpp | 9 +--- src/SFML/System/Win32/WindowsHeader.hpp | 50 +++++++++++++++++++++++ src/SFML/Window/Win32/ClipboardImpl.cpp | 2 +- src/SFML/Window/Win32/CursorImpl.cpp | 4 +- src/SFML/Window/Win32/CursorImpl.hpp | 6 +-- src/SFML/Window/Win32/InputImpl.cpp | 10 +---- src/SFML/Window/Win32/JoystickImpl.cpp | 2 +- src/SFML/Window/Win32/JoystickImpl.hpp | 11 +---- src/SFML/Window/Win32/VideoModeImpl.cpp | 2 +- src/SFML/Window/Win32/VulkanImplWin32.cpp | 5 +-- src/SFML/Window/Win32/WindowImplWin32.cpp | 15 +------ src/SFML/Window/Win32/WindowImplWin32.hpp | 2 +- src/SFML/Window/WindowImpl.cpp | 22 ++++++---- src/SFML/Window/WindowImpl.hpp | 12 +++--- 17 files changed, 95 insertions(+), 68 deletions(-) create mode 100644 src/SFML/System/Win32/WindowsHeader.hpp diff --git a/examples/win32/Win32.cpp b/examples/win32/Win32.cpp index 0c1fdee0c..bb705260b 100644 --- a/examples/win32/Win32.cpp +++ b/examples/win32/Win32.cpp @@ -3,6 +3,9 @@ // Headers //////////////////////////////////////////////////////////// #include +#ifndef WIN32_LEAN_AND_MEAN +#define WIN32_LEAN_AND_MEAN +#endif #include #include diff --git a/include/SFML/OpenGL.hpp b/include/SFML/OpenGL.hpp index 0cfa00161..0ba458d74 100644 --- a/include/SFML/OpenGL.hpp +++ b/include/SFML/OpenGL.hpp @@ -40,6 +40,9 @@ // The Visual C++ version of gl.h uses WINGDIAPI and APIENTRY but doesn't define them #ifdef _MSC_VER + #ifndef WIN32_LEAN_AND_MEAN + #define WIN32_LEAN_AND_MEAN + #endif #include #endif @@ -67,7 +70,7 @@ #include #include - + // We're not using OpenGL ES 2+ yet, but we can use the sRGB extension #include #include diff --git a/src/SFML/Main/MainWin32.cpp b/src/SFML/Main/MainWin32.cpp index 3e76a7ffa..9b6b12968 100644 --- a/src/SFML/Main/MainWin32.cpp +++ b/src/SFML/Main/MainWin32.cpp @@ -39,7 +39,8 @@ #ifdef SFML_SYSTEM_WINDOWS -#include +#include +#include // for `__argc` and `__argv` extern int main(int argc, char* argv[]); diff --git a/src/SFML/Network/Win32/SocketImpl.hpp b/src/SFML/Network/Win32/SocketImpl.hpp index 5d6b398d7..a2991c0af 100644 --- a/src/SFML/Network/Win32/SocketImpl.hpp +++ b/src/SFML/Network/Win32/SocketImpl.hpp @@ -28,15 +28,8 @@ //////////////////////////////////////////////////////////// // Headers //////////////////////////////////////////////////////////// -#ifdef _WIN32_WINDOWS - #undef _WIN32_WINDOWS -#endif -#ifdef _WIN32_WINNT - #undef _WIN32_WINNT -#endif -#define _WIN32_WINDOWS 0x0501 -#define _WIN32_WINNT 0x0501 #include +#include #include #include diff --git a/src/SFML/System/Win32/WindowsHeader.hpp b/src/SFML/System/Win32/WindowsHeader.hpp new file mode 100644 index 000000000..c0a87e1a1 --- /dev/null +++ b/src/SFML/System/Win32/WindowsHeader.hpp @@ -0,0 +1,50 @@ +//////////////////////////////////////////////////////////// +// +// SFML - Simple and Fast Multimedia Library +// Copyright (C) 2007-2021 Laurent Gomila (laurent@sfml-dev.org) +// +// 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_WINDOWSHEADER_HPP +#define SFML_WINDOWSHEADER_HPP + +#ifndef NOMINMAX +#define NOMINMAX +#endif + +#ifndef WIN32_LEAN_AND_MEAN +#define WIN32_LEAN_AND_MEAN +#endif + +#ifndef _WIN32_WINDOWS +#define _WIN32_WINDOWS 0x0501 +#endif + +#ifndef _WIN32_WINNT +#define _WIN32_WINNT 0x0501 +#endif + +#ifndef WINVER +#define WINVER 0x0501 +#endif + +#include + +#endif // SFML_WINDOWSHEADER_HPP diff --git a/src/SFML/Window/Win32/ClipboardImpl.cpp b/src/SFML/Window/Win32/ClipboardImpl.cpp index 4a678bf40..8f4a234dc 100644 --- a/src/SFML/Window/Win32/ClipboardImpl.cpp +++ b/src/SFML/Window/Win32/ClipboardImpl.cpp @@ -26,9 +26,9 @@ // Headers //////////////////////////////////////////////////////////// #include +#include #include #include -#include namespace sf diff --git a/src/SFML/Window/Win32/CursorImpl.cpp b/src/SFML/Window/Win32/CursorImpl.cpp index b2711e49e..5b1861a9d 100755 --- a/src/SFML/Window/Win32/CursorImpl.cpp +++ b/src/SFML/Window/Win32/CursorImpl.cpp @@ -26,9 +26,11 @@ // Headers //////////////////////////////////////////////////////////// #include +#include #include #include + namespace sf { namespace priv @@ -188,7 +190,7 @@ bool CursorImpl::loadFromSystem(Cursor::Type type) void CursorImpl::release() { if (m_cursor && !m_systemCursor) { - DestroyCursor(m_cursor); + DestroyCursor(static_cast(m_cursor)); m_cursor = nullptr; } } diff --git a/src/SFML/Window/Win32/CursorImpl.hpp b/src/SFML/Window/Win32/CursorImpl.hpp index 2318143ce..e545e7ce0 100755 --- a/src/SFML/Window/Win32/CursorImpl.hpp +++ b/src/SFML/Window/Win32/CursorImpl.hpp @@ -32,8 +32,6 @@ #include #include -#include - namespace sf { @@ -92,8 +90,8 @@ private: //////////////////////////////////////////////////////////// // Member data //////////////////////////////////////////////////////////// - HCURSOR m_cursor; - bool m_systemCursor; + void* m_cursor; // Type erasure via `void*` is used here to avoid depending on `windows.h` + bool m_systemCursor; }; } // namespace priv diff --git a/src/SFML/Window/Win32/InputImpl.cpp b/src/SFML/Window/Win32/InputImpl.cpp index dc37cf9f0..f0603ce19 100644 --- a/src/SFML/Window/Win32/InputImpl.cpp +++ b/src/SFML/Window/Win32/InputImpl.cpp @@ -25,17 +25,9 @@ //////////////////////////////////////////////////////////// // Headers //////////////////////////////////////////////////////////// -#ifdef _WIN32_WINDOWS - #undef _WIN32_WINDOWS -#endif -#ifdef _WIN32_WINNT - #undef _WIN32_WINNT -#endif -#define _WIN32_WINDOWS 0x0501 -#define _WIN32_WINNT 0x0501 #include #include -#include +#include namespace sf diff --git a/src/SFML/Window/Win32/JoystickImpl.cpp b/src/SFML/Window/Win32/JoystickImpl.cpp index 79f1babed..c447bf192 100644 --- a/src/SFML/Window/Win32/JoystickImpl.cpp +++ b/src/SFML/Window/Win32/JoystickImpl.cpp @@ -26,9 +26,9 @@ // Headers //////////////////////////////////////////////////////////// #include +#include #include #include -#include #include #include #include diff --git a/src/SFML/Window/Win32/JoystickImpl.hpp b/src/SFML/Window/Win32/JoystickImpl.hpp index 3377a9b91..d9d97b237 100644 --- a/src/SFML/Window/Win32/JoystickImpl.hpp +++ b/src/SFML/Window/Win32/JoystickImpl.hpp @@ -28,18 +28,9 @@ //////////////////////////////////////////////////////////// // Headers //////////////////////////////////////////////////////////// -#ifdef _WIN32_WINDOWS - #undef _WIN32_WINDOWS -#endif -#ifdef _WIN32_WINNT - #undef _WIN32_WINNT -#endif -#define _WIN32_WINDOWS 0x0501 -#define _WIN32_WINNT 0x0501 -#define DIRECTINPUT_VERSION 0x0800 #include #include -#include +#include #include #include diff --git a/src/SFML/Window/Win32/VideoModeImpl.cpp b/src/SFML/Window/Win32/VideoModeImpl.cpp index 1fd5bc000..5d58ef31e 100644 --- a/src/SFML/Window/Win32/VideoModeImpl.cpp +++ b/src/SFML/Window/Win32/VideoModeImpl.cpp @@ -26,7 +26,7 @@ // Headers //////////////////////////////////////////////////////////// #include -#include +#include #include diff --git a/src/SFML/Window/Win32/VulkanImplWin32.cpp b/src/SFML/Window/Win32/VulkanImplWin32.cpp index ff4a1dabb..5a4ce3436 100644 --- a/src/SFML/Window/Win32/VulkanImplWin32.cpp +++ b/src/SFML/Window/Win32/VulkanImplWin32.cpp @@ -26,10 +26,7 @@ // Headers //////////////////////////////////////////////////////////// #include -#ifndef WIN32_LEAN_AND_MEAN -#define WIN32_LEAN_AND_MEAN -#endif -#include +#include #define VK_USE_PLATFORM_WIN32_KHR #define VK_NO_PROTOTYPES #include diff --git a/src/SFML/Window/Win32/WindowImplWin32.cpp b/src/SFML/Window/Win32/WindowImplWin32.cpp index f7f559db8..573079945 100755 --- a/src/SFML/Window/Win32/WindowImplWin32.cpp +++ b/src/SFML/Window/Win32/WindowImplWin32.cpp @@ -25,20 +25,9 @@ //////////////////////////////////////////////////////////// // Headers //////////////////////////////////////////////////////////// -#ifdef _WIN32_WINDOWS - #undef _WIN32_WINDOWS -#endif -#ifdef _WIN32_WINNT - #undef _WIN32_WINNT -#endif -#ifdef WINVER - #undef WINVER -#endif -#define _WIN32_WINDOWS 0x0501 -#define _WIN32_WINNT 0x0501 -#define WINVER 0x0501 #include #include +#include #include #include // dbt.h is lowercase here, as a cross-compile on linux with mingw-w64 @@ -417,7 +406,7 @@ void WindowImplWin32::setMouseCursorGrabbed(bool grabbed) //////////////////////////////////////////////////////////// void WindowImplWin32::setMouseCursor(const CursorImpl& cursor) { - m_lastCursor = cursor.m_cursor; + m_lastCursor = static_cast(cursor.m_cursor); SetCursor(m_cursorVisible ? m_lastCursor : nullptr); } diff --git a/src/SFML/Window/Win32/WindowImplWin32.hpp b/src/SFML/Window/Win32/WindowImplWin32.hpp index 04fd3bd0b..978871a5c 100755 --- a/src/SFML/Window/Win32/WindowImplWin32.hpp +++ b/src/SFML/Window/Win32/WindowImplWin32.hpp @@ -30,8 +30,8 @@ //////////////////////////////////////////////////////////// #include #include +#include #include -#include namespace sf diff --git a/src/SFML/Window/WindowImpl.cpp b/src/SFML/Window/WindowImpl.cpp index 803e1e3ae..dd85d610c 100644 --- a/src/SFML/Window/WindowImpl.cpp +++ b/src/SFML/Window/WindowImpl.cpp @@ -27,6 +27,7 @@ //////////////////////////////////////////////////////////// #include #include +#include #include #include #include @@ -77,6 +78,12 @@ namespace sf { namespace priv { +//////////////////////////////////////////////////////////// +struct WindowImpl::JoystickStatesImpl +{ + JoystickState m_states[Joystick::Count]; //!< Previous state of the joysticks +}; + //////////////////////////////////////////////////////////// WindowImpl* WindowImpl::create(VideoMode mode, const String& title, Uint32 style, const ContextSettings& settings) { @@ -93,13 +100,14 @@ WindowImpl* WindowImpl::create(WindowHandle handle) //////////////////////////////////////////////////////////// WindowImpl::WindowImpl() : +m_joystickStatesImpl(new JoystickStatesImpl), m_joystickThreshold(0.1f) { // Get the initial joystick states JoystickManager::getInstance().update(); for (unsigned int i = 0; i < Joystick::Count; ++i) { - m_joystickStates[i] = JoystickManager::getInstance().getState(i); + m_joystickStatesImpl->m_states[i] = JoystickManager::getInstance().getState(i); std::fill_n(m_previousAxes[i], static_cast(Joystick::AxisCount), 0.f); } @@ -112,7 +120,7 @@ m_joystickThreshold(0.1f) //////////////////////////////////////////////////////////// WindowImpl::~WindowImpl() { - // Nothing to do + delete m_joystickStatesImpl; } @@ -179,11 +187,11 @@ void WindowImpl::processJoystickEvents() for (unsigned int i = 0; i < Joystick::Count; ++i) { // Copy the previous state of the joystick and get the new one - JoystickState previousState = m_joystickStates[i]; - m_joystickStates[i] = JoystickManager::getInstance().getState(i); + JoystickState previousState = m_joystickStatesImpl->m_states[i]; + m_joystickStatesImpl->m_states[i] = JoystickManager::getInstance().getState(i); // Connection state - bool connected = m_joystickStates[i].connected; + bool connected = m_joystickStatesImpl->m_states[i].connected; if (previousState.connected ^ connected) { Event event; @@ -207,7 +215,7 @@ void WindowImpl::processJoystickEvents() { auto axis = static_cast(j); float prevPos = m_previousAxes[i][axis]; - float currPos = m_joystickStates[i].axes[axis]; + float currPos = m_joystickStatesImpl->m_states[i].axes[axis]; if (std::abs(currPos - prevPos) >= m_joystickThreshold) { Event event; @@ -226,7 +234,7 @@ void WindowImpl::processJoystickEvents() for (unsigned int j = 0; j < caps.buttonCount; ++j) { bool prevPressed = previousState.buttons[j]; - bool currPressed = m_joystickStates[i].buttons[j]; + bool currPressed = m_joystickStatesImpl->m_states[i].buttons[j]; if (prevPressed ^ currPressed) { diff --git a/src/SFML/Window/WindowImpl.hpp b/src/SFML/Window/WindowImpl.hpp index 9557fe513..b81111302 100644 --- a/src/SFML/Window/WindowImpl.hpp +++ b/src/SFML/Window/WindowImpl.hpp @@ -35,7 +35,6 @@ #include #include #include -#include #include #include #include @@ -266,6 +265,7 @@ protected: virtual void processEvents() = 0; private: + struct JoystickStatesImpl; //////////////////////////////////////////////////////////// /// \brief Read the joysticks state and generate the appropriate events @@ -282,11 +282,11 @@ private: //////////////////////////////////////////////////////////// // Member data //////////////////////////////////////////////////////////// - std::queue m_events; //!< Queue of available events - JoystickState m_joystickStates[Joystick::Count]; //!< Previous state of the joysticks - 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] + std::queue m_events; //!< Queue of available events + JoystickStatesImpl* m_joystickStatesImpl; //!< Previous state of the joysticks (PImpl) + 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] }; } // namespace priv