From 2e47e5dbda2fa83b6c8023f6ba7bbcc231799a45 Mon Sep 17 00:00:00 2001 From: Mika Attila Date: Mon, 31 Oct 2016 13:32:34 +0100 Subject: [PATCH] Deduplicate code for sfContextSettings (#116) Deduplicate code for sfContextSettings --- src/SFML/Graphics/RenderWindow.cpp | 31 ++-------- src/SFML/Window/CMakeLists.txt | 2 + src/SFML/Window/Context.cpp | 11 +--- src/SFML/Window/ContextSettingsInternal.cpp | 65 +++++++++++++++++++++ src/SFML/Window/ContextSettingsInternal.h | 52 +++++++++++++++++ src/SFML/Window/Window.cpp | 31 ++-------- 6 files changed, 134 insertions(+), 58 deletions(-) create mode 100644 src/SFML/Window/ContextSettingsInternal.cpp create mode 100644 src/SFML/Window/ContextSettingsInternal.h diff --git a/src/SFML/Graphics/RenderWindow.cpp b/src/SFML/Graphics/RenderWindow.cpp index d586ab4..b1fa344 100644 --- a/src/SFML/Graphics/RenderWindow.cpp +++ b/src/SFML/Graphics/RenderWindow.cpp @@ -38,6 +38,7 @@ #include #include #include +#include #include @@ -51,12 +52,7 @@ sfRenderWindow* sfRenderWindow_create(sfVideoMode mode, const char* title, sfUin sf::ContextSettings params; if (settings) { - params.depthBits = settings->depthBits; - params.stencilBits = settings->stencilBits; - params.antialiasingLevel = settings->antialiasingLevel; - params.majorVersion = settings->majorVersion; - params.minorVersion = settings->minorVersion; - params.attributeFlags = settings->attributeFlags; + priv::sfContextSettings_writeToCpp(*settings, params); } // Create the window @@ -78,12 +74,7 @@ sfRenderWindow* sfRenderWindow_createUnicode(sfVideoMode mode, const sfUint32* t sf::ContextSettings params; if (settings) { - params.depthBits = settings->depthBits; - params.stencilBits = settings->stencilBits; - params.antialiasingLevel = settings->antialiasingLevel; - params.majorVersion = settings->majorVersion; - params.minorVersion = settings->minorVersion; - params.attributeFlags = settings->attributeFlags; + priv::sfContextSettings_writeToCpp(*settings, params); } // Create the window @@ -103,12 +94,7 @@ sfRenderWindow* sfRenderWindow_createFromHandle(sfWindowHandle handle, const sfC sf::ContextSettings params; if (settings) { - params.depthBits = settings->depthBits; - params.stencilBits = settings->stencilBits; - params.antialiasingLevel = settings->antialiasingLevel; - params.majorVersion = settings->majorVersion; - params.minorVersion = settings->minorVersion; - params.attributeFlags = settings->attributeFlags; + priv::sfContextSettings_writeToCpp(*settings, params); } // Create the window @@ -145,16 +131,11 @@ sfBool sfRenderWindow_isOpen(const sfRenderWindow* renderWindow) //////////////////////////////////////////////////////////// sfContextSettings sfRenderWindow_getSettings(const sfRenderWindow* renderWindow) { - sfContextSettings settings = {0, 0, 0, 2, 0}; + sfContextSettings settings = priv::sfContextSettings_null(); CSFML_CHECK_RETURN(renderWindow, settings); const sf::ContextSettings& params = renderWindow->This.getSettings(); - settings.depthBits = params.depthBits; - settings.stencilBits = params.stencilBits; - settings.antialiasingLevel = params.antialiasingLevel; - settings.majorVersion = params.majorVersion; - settings.minorVersion = params.minorVersion; - settings.attributeFlags = params.attributeFlags; + priv::sfContextSettings_readFromCpp(params, settings); return settings; } diff --git a/src/SFML/Window/CMakeLists.txt b/src/SFML/Window/CMakeLists.txt index 521b71d..dff5163 100644 --- a/src/SFML/Window/CMakeLists.txt +++ b/src/SFML/Window/CMakeLists.txt @@ -6,6 +6,8 @@ set(SRCROOT ${CMAKE_SOURCE_DIR}/src/SFML/Window) set(SRC ${INCROOT}/Export.h ${SRCROOT}/Context.cpp + ${SRCROOT}/ContextSettingsInternal.cpp + ${SRCROOT}/ContextSettingsInternal.h ${SRCROOT}/ContextStruct.h ${INCROOT}/Context.h ${INCROOT}/Event.h diff --git a/src/SFML/Window/Context.cpp b/src/SFML/Window/Context.cpp index 2c41826..c420665 100644 --- a/src/SFML/Window/Context.cpp +++ b/src/SFML/Window/Context.cpp @@ -28,6 +28,7 @@ #include #include #include +#include //////////////////////////////////////////////////////////// @@ -53,17 +54,11 @@ sfBool sfContext_setActive(sfContext* context, sfBool active) //////////////////////////////////////////////////////////// sfContextSettings sfContext_getSettings(const sfContext* context) { - sfContextSettings settings = {0, 0, 0, 0, 0}; + sfContextSettings settings = priv::sfContextSettings_null(); CSFML_CHECK_RETURN(context, settings); const sf::ContextSettings& params = context->This.getSettings(); - - settings.depthBits = params.depthBits; - settings.stencilBits = params.stencilBits; - settings.antialiasingLevel = params.antialiasingLevel; - settings.majorVersion = params.majorVersion; - settings.minorVersion = params.minorVersion; - settings.attributeFlags = params.attributeFlags; + priv::sfContextSettings_readFromCpp(params, settings); return settings; } diff --git a/src/SFML/Window/ContextSettingsInternal.cpp b/src/SFML/Window/ContextSettingsInternal.cpp new file mode 100644 index 0000000..9f557fa --- /dev/null +++ b/src/SFML/Window/ContextSettingsInternal.cpp @@ -0,0 +1,65 @@ +//////////////////////////////////////////////////////////// +// +// SFML - Simple and Fast Multimedia Library +// Copyright (C) 2007-2016 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. +// +//////////////////////////////////////////////////////////// + +//////////////////////////////////////////////////////////// +// Headers +//////////////////////////////////////////////////////////// +#include + + +namespace priv +{ +//////////////////////////////////////////////////////////// +sfContextSettings sfContextSettings_null() +{ + sfContextSettings settings = {0, 0, 0, 0, 0, 0}; + + return settings; +} + + +//////////////////////////////////////////////////////////// +void sfContextSettings_readFromCpp(const sf::ContextSettings& from, sfContextSettings& to) +{ + to.depthBits = from.depthBits; + to.stencilBits = from.stencilBits; + to.antialiasingLevel = from.antialiasingLevel; + to.majorVersion = from.majorVersion; + to.minorVersion = from.minorVersion; + to.attributeFlags = from.attributeFlags; +} + + +//////////////////////////////////////////////////////////// +void sfContextSettings_writeToCpp(const sfContextSettings& from, sf::ContextSettings& to) +{ + to.depthBits = from.depthBits; + to.stencilBits = from.stencilBits; + to.antialiasingLevel = from.antialiasingLevel; + to.majorVersion = from.majorVersion; + to.minorVersion = from.minorVersion; + to.attributeFlags = from.attributeFlags; +} + +} // namespace priv diff --git a/src/SFML/Window/ContextSettingsInternal.h b/src/SFML/Window/ContextSettingsInternal.h new file mode 100644 index 0000000..ad0969a --- /dev/null +++ b/src/SFML/Window/ContextSettingsInternal.h @@ -0,0 +1,52 @@ +//////////////////////////////////////////////////////////// +// +// SFML - Simple and Fast Multimedia Library +// Copyright (C) 2007-2016 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_CONTEXTSETTINGSINTERNAL_H +#define SFML_CONTEXTSETTINGSINTERNAL_H + +//////////////////////////////////////////////////////////// +// Headers +//////////////////////////////////////////////////////////// +#include +#include + +namespace priv +{ + //////////////////////////////////////////////////////////// + // Create a "null" sfContextSettings that's returned in case of an error. + //////////////////////////////////////////////////////////// + sfContextSettings sfContextSettings_null(); + + //////////////////////////////////////////////////////////// + // Read the data of an sf::ContextSettings into an sfContextSettings + //////////////////////////////////////////////////////////// + void sfContextSettings_readFromCpp(const sf::ContextSettings& from, sfContextSettings& to); + + //////////////////////////////////////////////////////////// + // Write the data of an sfContextSettings into an sf::ContextSettings + //////////////////////////////////////////////////////////// + void sfContextSettings_writeToCpp(const sfContextSettings& from, sf::ContextSettings& to); +} + +#endif // SFML_CONTEXTSETTINGSINTERNAL_H diff --git a/src/SFML/Window/Window.cpp b/src/SFML/Window/Window.cpp index 996871b..421010d 100644 --- a/src/SFML/Window/Window.cpp +++ b/src/SFML/Window/Window.cpp @@ -28,6 +28,7 @@ #include #include #include +#include #include @@ -41,12 +42,7 @@ sfWindow* sfWindow_create(sfVideoMode mode, const char* title, sfUint32 style, c sf::ContextSettings params; if (settings) { - params.depthBits = settings->depthBits; - params.stencilBits = settings->stencilBits; - params.antialiasingLevel = settings->antialiasingLevel; - params.majorVersion = settings->majorVersion; - params.minorVersion = settings->minorVersion; - params.attributeFlags = settings->attributeFlags; + priv::sfContextSettings_writeToCpp(*settings, params); } // Create the window @@ -66,12 +62,7 @@ sfWindow* sfWindow_createUnicode(sfVideoMode mode, const sfUint32* title, sfUint sf::ContextSettings params; if (settings) { - params.depthBits = settings->depthBits; - params.stencilBits = settings->stencilBits; - params.antialiasingLevel = settings->antialiasingLevel; - params.majorVersion = settings->majorVersion; - params.minorVersion = settings->minorVersion; - params.attributeFlags = settings->attributeFlags; + priv::sfContextSettings_writeToCpp(*settings, params); } // Create the window @@ -89,12 +80,7 @@ sfWindow* sfWindow_createFromHandle(sfWindowHandle handle, const sfContextSettin sf::ContextSettings params; if (settings) { - params.depthBits = settings->depthBits; - params.stencilBits = settings->stencilBits; - params.antialiasingLevel = settings->antialiasingLevel; - params.majorVersion = settings->majorVersion; - params.minorVersion = settings->minorVersion; - params.attributeFlags = settings->attributeFlags; + priv::sfContextSettings_writeToCpp(*settings, params); } // Create the window @@ -128,16 +114,11 @@ sfBool sfWindow_isOpen(const sfWindow* window) //////////////////////////////////////////////////////////// sfContextSettings sfWindow_getSettings(const sfWindow* window) { - sfContextSettings settings = {0, 0, 0, 0, 0}; + sfContextSettings settings = priv::sfContextSettings_null(); CSFML_CHECK_RETURN(window, settings); const sf::ContextSettings& params = window->This.getSettings(); - settings.depthBits = params.depthBits; - settings.stencilBits = params.stencilBits; - settings.antialiasingLevel = params.antialiasingLevel; - settings.majorVersion = params.majorVersion; - settings.minorVersion = params.minorVersion; - settings.attributeFlags = params.attributeFlags; + priv::sfContextSettings_readFromCpp(params, settings); return settings; }