Skip to content

Commit 2e47e5d

Browse files
crumblingstatueLaurentGomila
authored andcommitted
Deduplicate code for sfContextSettings (#116)
Deduplicate code for sfContextSettings
1 parent 1226d9d commit 2e47e5d

File tree

6 files changed

+134
-58
lines changed

6 files changed

+134
-58
lines changed

src/SFML/Graphics/RenderWindow.cpp

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include <SFML/Graphics/ConvertRenderStates.hpp>
3939
#include <SFML/Window/Touch.hpp>
4040
#include <SFML/Internal.h>
41+
#include <SFML/Window/ContextSettingsInternal.h>
4142
#include <SFML/ConvertEvent.h>
4243

4344

@@ -51,12 +52,7 @@ sfRenderWindow* sfRenderWindow_create(sfVideoMode mode, const char* title, sfUin
5152
sf::ContextSettings params;
5253
if (settings)
5354
{
54-
params.depthBits = settings->depthBits;
55-
params.stencilBits = settings->stencilBits;
56-
params.antialiasingLevel = settings->antialiasingLevel;
57-
params.majorVersion = settings->majorVersion;
58-
params.minorVersion = settings->minorVersion;
59-
params.attributeFlags = settings->attributeFlags;
55+
priv::sfContextSettings_writeToCpp(*settings, params);
6056
}
6157

6258
// Create the window
@@ -78,12 +74,7 @@ sfRenderWindow* sfRenderWindow_createUnicode(sfVideoMode mode, const sfUint32* t
7874
sf::ContextSettings params;
7975
if (settings)
8076
{
81-
params.depthBits = settings->depthBits;
82-
params.stencilBits = settings->stencilBits;
83-
params.antialiasingLevel = settings->antialiasingLevel;
84-
params.majorVersion = settings->majorVersion;
85-
params.minorVersion = settings->minorVersion;
86-
params.attributeFlags = settings->attributeFlags;
77+
priv::sfContextSettings_writeToCpp(*settings, params);
8778
}
8879

8980
// Create the window
@@ -103,12 +94,7 @@ sfRenderWindow* sfRenderWindow_createFromHandle(sfWindowHandle handle, const sfC
10394
sf::ContextSettings params;
10495
if (settings)
10596
{
106-
params.depthBits = settings->depthBits;
107-
params.stencilBits = settings->stencilBits;
108-
params.antialiasingLevel = settings->antialiasingLevel;
109-
params.majorVersion = settings->majorVersion;
110-
params.minorVersion = settings->minorVersion;
111-
params.attributeFlags = settings->attributeFlags;
97+
priv::sfContextSettings_writeToCpp(*settings, params);
11298
}
11399

114100
// Create the window
@@ -145,16 +131,11 @@ sfBool sfRenderWindow_isOpen(const sfRenderWindow* renderWindow)
145131
////////////////////////////////////////////////////////////
146132
sfContextSettings sfRenderWindow_getSettings(const sfRenderWindow* renderWindow)
147133
{
148-
sfContextSettings settings = {0, 0, 0, 2, 0};
134+
sfContextSettings settings = priv::sfContextSettings_null();
149135
CSFML_CHECK_RETURN(renderWindow, settings);
150136

151137
const sf::ContextSettings& params = renderWindow->This.getSettings();
152-
settings.depthBits = params.depthBits;
153-
settings.stencilBits = params.stencilBits;
154-
settings.antialiasingLevel = params.antialiasingLevel;
155-
settings.majorVersion = params.majorVersion;
156-
settings.minorVersion = params.minorVersion;
157-
settings.attributeFlags = params.attributeFlags;
138+
priv::sfContextSettings_readFromCpp(params, settings);
158139

159140
return settings;
160141
}

src/SFML/Window/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ set(SRCROOT ${CMAKE_SOURCE_DIR}/src/SFML/Window)
66
set(SRC
77
${INCROOT}/Export.h
88
${SRCROOT}/Context.cpp
9+
${SRCROOT}/ContextSettingsInternal.cpp
10+
${SRCROOT}/ContextSettingsInternal.h
911
${SRCROOT}/ContextStruct.h
1012
${INCROOT}/Context.h
1113
${INCROOT}/Event.h

src/SFML/Window/Context.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <SFML/Window/Context.h>
2929
#include <SFML/Window/ContextStruct.h>
3030
#include <SFML/Internal.h>
31+
#include <SFML/Window/ContextSettingsInternal.h>
3132

3233

3334
////////////////////////////////////////////////////////////
@@ -53,17 +54,11 @@ sfBool sfContext_setActive(sfContext* context, sfBool active)
5354
////////////////////////////////////////////////////////////
5455
sfContextSettings sfContext_getSettings(const sfContext* context)
5556
{
56-
sfContextSettings settings = {0, 0, 0, 0, 0};
57+
sfContextSettings settings = priv::sfContextSettings_null();
5758
CSFML_CHECK_RETURN(context, settings);
5859

5960
const sf::ContextSettings& params = context->This.getSettings();
60-
61-
settings.depthBits = params.depthBits;
62-
settings.stencilBits = params.stencilBits;
63-
settings.antialiasingLevel = params.antialiasingLevel;
64-
settings.majorVersion = params.majorVersion;
65-
settings.minorVersion = params.minorVersion;
66-
settings.attributeFlags = params.attributeFlags;
61+
priv::sfContextSettings_readFromCpp(params, settings);
6762

6863
return settings;
6964
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
////////////////////////////////////////////////////////////
2+
//
3+
// SFML - Simple and Fast Multimedia Library
4+
// Copyright (C) 2007-2016 Laurent Gomila ([email protected])
5+
//
6+
// This software is provided 'as-is', without any express or implied warranty.
7+
// In no event will the authors be held liable for any damages arising from the use of this software.
8+
//
9+
// Permission is granted to anyone to use this software for any purpose,
10+
// including commercial applications, and to alter it and redistribute it freely,
11+
// subject to the following restrictions:
12+
//
13+
// 1. The origin of this software must not be misrepresented;
14+
// you must not claim that you wrote the original software.
15+
// If you use this software in a product, an acknowledgment
16+
// in the product documentation would be appreciated but is not required.
17+
//
18+
// 2. Altered source versions must be plainly marked as such,
19+
// and must not be misrepresented as being the original software.
20+
//
21+
// 3. This notice may not be removed or altered from any source distribution.
22+
//
23+
////////////////////////////////////////////////////////////
24+
25+
////////////////////////////////////////////////////////////
26+
// Headers
27+
////////////////////////////////////////////////////////////
28+
#include <SFML/Window/ContextSettingsInternal.h>
29+
30+
31+
namespace priv
32+
{
33+
////////////////////////////////////////////////////////////
34+
sfContextSettings sfContextSettings_null()
35+
{
36+
sfContextSettings settings = {0, 0, 0, 0, 0, 0};
37+
38+
return settings;
39+
}
40+
41+
42+
////////////////////////////////////////////////////////////
43+
void sfContextSettings_readFromCpp(const sf::ContextSettings& from, sfContextSettings& to)
44+
{
45+
to.depthBits = from.depthBits;
46+
to.stencilBits = from.stencilBits;
47+
to.antialiasingLevel = from.antialiasingLevel;
48+
to.majorVersion = from.majorVersion;
49+
to.minorVersion = from.minorVersion;
50+
to.attributeFlags = from.attributeFlags;
51+
}
52+
53+
54+
////////////////////////////////////////////////////////////
55+
void sfContextSettings_writeToCpp(const sfContextSettings& from, sf::ContextSettings& to)
56+
{
57+
to.depthBits = from.depthBits;
58+
to.stencilBits = from.stencilBits;
59+
to.antialiasingLevel = from.antialiasingLevel;
60+
to.majorVersion = from.majorVersion;
61+
to.minorVersion = from.minorVersion;
62+
to.attributeFlags = from.attributeFlags;
63+
}
64+
65+
} // namespace priv
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
////////////////////////////////////////////////////////////
2+
//
3+
// SFML - Simple and Fast Multimedia Library
4+
// Copyright (C) 2007-2016 Laurent Gomila ([email protected])
5+
//
6+
// This software is provided 'as-is', without any express or implied warranty.
7+
// In no event will the authors be held liable for any damages arising from the use of this software.
8+
//
9+
// Permission is granted to anyone to use this software for any purpose,
10+
// including commercial applications, and to alter it and redistribute it freely,
11+
// subject to the following restrictions:
12+
//
13+
// 1. The origin of this software must not be misrepresented;
14+
// you must not claim that you wrote the original software.
15+
// If you use this software in a product, an acknowledgment
16+
// in the product documentation would be appreciated but is not required.
17+
//
18+
// 2. Altered source versions must be plainly marked as such,
19+
// and must not be misrepresented as being the original software.
20+
//
21+
// 3. This notice may not be removed or altered from any source distribution.
22+
//
23+
////////////////////////////////////////////////////////////
24+
25+
#ifndef SFML_CONTEXTSETTINGSINTERNAL_H
26+
#define SFML_CONTEXTSETTINGSINTERNAL_H
27+
28+
////////////////////////////////////////////////////////////
29+
// Headers
30+
////////////////////////////////////////////////////////////
31+
#include <SFML/Window.h>
32+
#include <SFML/Window.hpp>
33+
34+
namespace priv
35+
{
36+
////////////////////////////////////////////////////////////
37+
// Create a "null" sfContextSettings that's returned in case of an error.
38+
////////////////////////////////////////////////////////////
39+
sfContextSettings sfContextSettings_null();
40+
41+
////////////////////////////////////////////////////////////
42+
// Read the data of an sf::ContextSettings into an sfContextSettings
43+
////////////////////////////////////////////////////////////
44+
void sfContextSettings_readFromCpp(const sf::ContextSettings& from, sfContextSettings& to);
45+
46+
////////////////////////////////////////////////////////////
47+
// Write the data of an sfContextSettings into an sf::ContextSettings
48+
////////////////////////////////////////////////////////////
49+
void sfContextSettings_writeToCpp(const sfContextSettings& from, sf::ContextSettings& to);
50+
}
51+
52+
#endif // SFML_CONTEXTSETTINGSINTERNAL_H

src/SFML/Window/Window.cpp

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <SFML/Window/Window.h>
2929
#include <SFML/Window/WindowStruct.h>
3030
#include <SFML/Internal.h>
31+
#include <SFML/Window/ContextSettingsInternal.h>
3132
#include <SFML/ConvertEvent.h>
3233

3334

@@ -41,12 +42,7 @@ sfWindow* sfWindow_create(sfVideoMode mode, const char* title, sfUint32 style, c
4142
sf::ContextSettings params;
4243
if (settings)
4344
{
44-
params.depthBits = settings->depthBits;
45-
params.stencilBits = settings->stencilBits;
46-
params.antialiasingLevel = settings->antialiasingLevel;
47-
params.majorVersion = settings->majorVersion;
48-
params.minorVersion = settings->minorVersion;
49-
params.attributeFlags = settings->attributeFlags;
45+
priv::sfContextSettings_writeToCpp(*settings, params);
5046
}
5147

5248
// Create the window
@@ -66,12 +62,7 @@ sfWindow* sfWindow_createUnicode(sfVideoMode mode, const sfUint32* title, sfUint
6662
sf::ContextSettings params;
6763
if (settings)
6864
{
69-
params.depthBits = settings->depthBits;
70-
params.stencilBits = settings->stencilBits;
71-
params.antialiasingLevel = settings->antialiasingLevel;
72-
params.majorVersion = settings->majorVersion;
73-
params.minorVersion = settings->minorVersion;
74-
params.attributeFlags = settings->attributeFlags;
65+
priv::sfContextSettings_writeToCpp(*settings, params);
7566
}
7667

7768
// Create the window
@@ -89,12 +80,7 @@ sfWindow* sfWindow_createFromHandle(sfWindowHandle handle, const sfContextSettin
8980
sf::ContextSettings params;
9081
if (settings)
9182
{
92-
params.depthBits = settings->depthBits;
93-
params.stencilBits = settings->stencilBits;
94-
params.antialiasingLevel = settings->antialiasingLevel;
95-
params.majorVersion = settings->majorVersion;
96-
params.minorVersion = settings->minorVersion;
97-
params.attributeFlags = settings->attributeFlags;
83+
priv::sfContextSettings_writeToCpp(*settings, params);
9884
}
9985

10086
// Create the window
@@ -128,16 +114,11 @@ sfBool sfWindow_isOpen(const sfWindow* window)
128114
////////////////////////////////////////////////////////////
129115
sfContextSettings sfWindow_getSettings(const sfWindow* window)
130116
{
131-
sfContextSettings settings = {0, 0, 0, 0, 0};
117+
sfContextSettings settings = priv::sfContextSettings_null();
132118
CSFML_CHECK_RETURN(window, settings);
133119

134120
const sf::ContextSettings& params = window->This.getSettings();
135-
settings.depthBits = params.depthBits;
136-
settings.stencilBits = params.stencilBits;
137-
settings.antialiasingLevel = params.antialiasingLevel;
138-
settings.majorVersion = params.majorVersion;
139-
settings.minorVersion = params.minorVersion;
140-
settings.attributeFlags = params.attributeFlags;
121+
priv::sfContextSettings_readFromCpp(params, settings);
141122

142123
return settings;
143124
}

0 commit comments

Comments
 (0)