Skip to content

Commit f162b3a

Browse files
vittorioromeoeXpl0it3r
authored andcommitted
Enable support for unity builds
1 parent 47a4e88 commit f162b3a

16 files changed

Lines changed: 813 additions & 666 deletions

.github/workflows/ci.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ jobs:
1717
- { name: Linux Clang, os: ubuntu-latest, flags: -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ }
1818
- { name: MacOS XCode, os: macos-latest }
1919
config:
20-
- { name: Shared, flags: -DBUILD_SHARED_LIBS=TRUE }
21-
- { name: Static, flags: -DBUILD_SHARED_LIBS=FALSE }
20+
- { name: Shared, flags: -DBUILD_SHARED_LIBS=TRUE }
21+
- { name: Static, flags: -DBUILD_SHARED_LIBS=FALSE }
2222

2323
include:
24+
- platform: { name: Windows VS2019, os: windows-latest }
25+
config: { name: Unity, flags: -DBUILD_SHARED_LIBS=TRUE -DCMAKE_UNITY_BUILD=ON }
2426
- platform: { name: MacOS XCode, os: macos-latest }
2527
config: { name: Frameworks, flags: -DSFML_BUILD_FRAMEWORKS=TRUE }
2628
- platform: { name: MacOS XCode, os: macos-latest }

examples/voip/Client.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
#include <iostream>
88

99

10-
const sf::Uint8 audioData = 1;
11-
const sf::Uint8 endOfStream = 2;
10+
const sf::Uint8 clientAudioData = 1;
11+
const sf::Uint8 clientEndOfStream = 2;
1212

1313

1414
////////////////////////////////////////////////////////////
@@ -71,7 +71,7 @@ class NetworkRecorder : public sf::SoundRecorder
7171
{
7272
// Pack the audio samples into a network packet
7373
sf::Packet packet;
74-
packet << audioData;
74+
packet << clientAudioData;
7575
packet.append(samples, sampleCount * sizeof(sf::Int16));
7676

7777
// Send the audio packet to the server
@@ -86,7 +86,7 @@ class NetworkRecorder : public sf::SoundRecorder
8686
{
8787
// Send a "end-of-stream" packet
8888
sf::Packet packet;
89-
packet << endOfStream;
89+
packet << clientEndOfStream;
9090
m_socket.send(packet);
9191

9292
// Close the socket

examples/voip/Server.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
#include <iterator>
1010

1111

12-
const sf::Uint8 audioData = 1;
13-
const sf::Uint8 endOfStream = 2;
12+
const sf::Uint8 serverAudioData = 1;
13+
const sf::Uint8 serverEndOfStream = 2;
1414

1515

1616
////////////////////////////////////////////////////////////
@@ -123,7 +123,7 @@ class NetworkAudioStream : public sf::SoundStream
123123
sf::Uint8 id;
124124
packet >> id;
125125

126-
if (id == audioData)
126+
if (id == serverAudioData)
127127
{
128128
// Extract audio samples from the packet, and append it to our samples buffer
129129
const sf::Int16* samples = reinterpret_cast<const sf::Int16*>(static_cast<const char*>(packet.getData()) + 1);
@@ -136,7 +136,7 @@ class NetworkAudioStream : public sf::SoundStream
136136
std::copy(samples, samples + sampleCount, std::back_inserter(m_samples));
137137
}
138138
}
139-
else if (id == endOfStream)
139+
else if (id == serverEndOfStream)
140140
{
141141
// End of stream reached: we stop receiving audio data
142142
std::cout << "Audio data has been 100% received!" << std::endl;

src/SFML/Graphics/GLExtensions.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,18 @@
2525
////////////////////////////////////////////////////////////
2626
// Headers
2727
////////////////////////////////////////////////////////////
28-
#define SF_GLAD_GL_IMPLEMENTATION
2928
#include <SFML/Graphics/GLExtensions.hpp>
3029
#include <SFML/Window/Context.hpp>
3130
#include <SFML/System/Err.hpp>
3231

32+
// We check for this definition in order to avoid multiple definitions of GLAD
33+
// entities during unity builds of SFML.
34+
#ifndef SF_GLAD_GL_IMPLEMENTATION_INCLUDED
35+
#define SF_GLAD_GL_IMPLEMENTATION_INCLUDED
36+
#define SF_GLAD_GL_IMPLEMENTATION
37+
#include <glad/gl.h>
38+
#endif
39+
3340
#if !defined(GL_MAJOR_VERSION)
3441
#define GL_MAJOR_VERSION 0x821B
3542
#endif

src/SFML/Graphics/RenderTarget.cpp

Lines changed: 90 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -52,95 +52,99 @@
5252

5353
namespace
5454
{
55-
// Mutex to protect ID generation and our context-RenderTarget-map
56-
sf::Mutex mutex;
57-
58-
// Unique identifier, used for identifying RenderTargets when
59-
// tracking the currently active RenderTarget within a given context
60-
sf::Uint64 getUniqueId()
55+
// A nested named namespace is used here to allow unity builds of SFML.
56+
namespace RenderTargetImpl
6157
{
62-
sf::Lock lock(mutex);
63-
64-
static sf::Uint64 id = 1; // start at 1, zero is "no RenderTarget"
65-
66-
return id++;
67-
}
58+
// Mutex to protect ID generation and our context-RenderTarget-map
59+
sf::Mutex mutex;
6860

69-
// Map to help us detect whether a different RenderTarget
70-
// has been activated within a single context
71-
typedef std::map<sf::Uint64, sf::Uint64> ContextRenderTargetMap;
72-
ContextRenderTargetMap contextRenderTargetMap;
61+
// Unique identifier, used for identifying RenderTargets when
62+
// tracking the currently active RenderTarget within a given context
63+
sf::Uint64 getUniqueId()
64+
{
65+
sf::Lock lock(mutex);
7366

74-
// Check if a RenderTarget with the given ID is active in the current context
75-
bool isActive(sf::Uint64 id)
76-
{
77-
ContextRenderTargetMap::iterator iter = contextRenderTargetMap.find(sf::Context::getActiveContextId());
67+
static sf::Uint64 id = 1; // start at 1, zero is "no RenderTarget"
7868

79-
if ((iter == contextRenderTargetMap.end()) || (iter->second != id))
80-
return false;
69+
return id++;
70+
}
8171

82-
return true;
83-
}
72+
// Map to help us detect whether a different RenderTarget
73+
// has been activated within a single context
74+
typedef std::map<sf::Uint64, sf::Uint64> ContextRenderTargetMap;
75+
ContextRenderTargetMap contextRenderTargetMap;
8476

85-
// Convert an sf::BlendMode::Factor constant to the corresponding OpenGL constant.
86-
sf::Uint32 factorToGlConstant(sf::BlendMode::Factor blendFactor)
87-
{
88-
switch (blendFactor)
77+
// Check if a RenderTarget with the given ID is active in the current context
78+
bool isActive(sf::Uint64 id)
8979
{
90-
case sf::BlendMode::Zero: return GL_ZERO;
91-
case sf::BlendMode::One: return GL_ONE;
92-
case sf::BlendMode::SrcColor: return GL_SRC_COLOR;
93-
case sf::BlendMode::OneMinusSrcColor: return GL_ONE_MINUS_SRC_COLOR;
94-
case sf::BlendMode::DstColor: return GL_DST_COLOR;
95-
case sf::BlendMode::OneMinusDstColor: return GL_ONE_MINUS_DST_COLOR;
96-
case sf::BlendMode::SrcAlpha: return GL_SRC_ALPHA;
97-
case sf::BlendMode::OneMinusSrcAlpha: return GL_ONE_MINUS_SRC_ALPHA;
98-
case sf::BlendMode::DstAlpha: return GL_DST_ALPHA;
99-
case sf::BlendMode::OneMinusDstAlpha: return GL_ONE_MINUS_DST_ALPHA;
100-
}
80+
ContextRenderTargetMap::iterator iter = contextRenderTargetMap.find(sf::Context::getActiveContextId());
10181

102-
sf::err() << "Invalid value for sf::BlendMode::Factor! Fallback to sf::BlendMode::Zero." << std::endl;
103-
assert(false);
104-
return GL_ZERO;
105-
}
82+
if ((iter == contextRenderTargetMap.end()) || (iter->second != id))
83+
return false;
10684

85+
return true;
86+
}
10787

108-
// Convert an sf::BlendMode::BlendEquation constant to the corresponding OpenGL constant.
109-
sf::Uint32 equationToGlConstant(sf::BlendMode::Equation blendEquation)
110-
{
111-
switch (blendEquation)
88+
// Convert an sf::BlendMode::Factor constant to the corresponding OpenGL constant.
89+
sf::Uint32 factorToGlConstant(sf::BlendMode::Factor blendFactor)
11290
{
113-
case sf::BlendMode::Add:
114-
return GLEXT_GL_FUNC_ADD;
115-
case sf::BlendMode::Subtract:
116-
if (GLEXT_blend_subtract)
117-
return GLEXT_GL_FUNC_SUBTRACT;
118-
break;
119-
case sf::BlendMode::ReverseSubtract:
120-
if (GLEXT_blend_subtract)
121-
return GLEXT_GL_FUNC_REVERSE_SUBTRACT;
122-
break;
123-
case sf::BlendMode::Min:
124-
if (GLEXT_blend_minmax)
125-
return GLEXT_GL_MIN;
126-
break;
127-
case sf::BlendMode::Max:
128-
if (GLEXT_blend_minmax)
129-
return GLEXT_GL_MAX;
130-
break;
91+
switch (blendFactor)
92+
{
93+
case sf::BlendMode::Zero: return GL_ZERO;
94+
case sf::BlendMode::One: return GL_ONE;
95+
case sf::BlendMode::SrcColor: return GL_SRC_COLOR;
96+
case sf::BlendMode::OneMinusSrcColor: return GL_ONE_MINUS_SRC_COLOR;
97+
case sf::BlendMode::DstColor: return GL_DST_COLOR;
98+
case sf::BlendMode::OneMinusDstColor: return GL_ONE_MINUS_DST_COLOR;
99+
case sf::BlendMode::SrcAlpha: return GL_SRC_ALPHA;
100+
case sf::BlendMode::OneMinusSrcAlpha: return GL_ONE_MINUS_SRC_ALPHA;
101+
case sf::BlendMode::DstAlpha: return GL_DST_ALPHA;
102+
case sf::BlendMode::OneMinusDstAlpha: return GL_ONE_MINUS_DST_ALPHA;
103+
}
104+
105+
sf::err() << "Invalid value for sf::BlendMode::Factor! Fallback to sf::BlendMode::Zero." << std::endl;
106+
assert(false);
107+
return GL_ZERO;
131108
}
132109

133-
static bool warned = false;
134-
if (!warned)
110+
111+
// Convert an sf::BlendMode::BlendEquation constant to the corresponding OpenGL constant.
112+
sf::Uint32 equationToGlConstant(sf::BlendMode::Equation blendEquation)
135113
{
136-
sf::err() << "OpenGL extension EXT_blend_minmax or EXT_blend_subtract unavailable" << std::endl;
137-
sf::err() << "Some blending equations will fallback to sf::BlendMode::Add" << std::endl;
138-
sf::err() << "Ensure that hardware acceleration is enabled if available" << std::endl;
114+
switch (blendEquation)
115+
{
116+
case sf::BlendMode::Add:
117+
return GLEXT_GL_FUNC_ADD;
118+
case sf::BlendMode::Subtract:
119+
if (GLEXT_blend_subtract)
120+
return GLEXT_GL_FUNC_SUBTRACT;
121+
break;
122+
case sf::BlendMode::ReverseSubtract:
123+
if (GLEXT_blend_subtract)
124+
return GLEXT_GL_FUNC_REVERSE_SUBTRACT;
125+
break;
126+
case sf::BlendMode::Min:
127+
if (GLEXT_blend_minmax)
128+
return GLEXT_GL_MIN;
129+
break;
130+
case sf::BlendMode::Max:
131+
if (GLEXT_blend_minmax)
132+
return GLEXT_GL_MAX;
133+
break;
134+
}
139135

140-
warned = true;
141-
}
136+
static bool warned = false;
137+
if (!warned)
138+
{
139+
sf::err() << "OpenGL extension EXT_blend_minmax or EXT_blend_subtract unavailable" << std::endl;
140+
sf::err() << "Some blending equations will fallback to sf::BlendMode::Add" << std::endl;
141+
sf::err() << "Ensure that hardware acceleration is enabled if available" << std::endl;
142142

143-
return GLEXT_GL_FUNC_ADD;
143+
warned = true;
144+
}
145+
146+
return GLEXT_GL_FUNC_ADD;
147+
}
144148
}
145149
}
146150

@@ -167,7 +171,7 @@ RenderTarget::~RenderTarget()
167171
////////////////////////////////////////////////////////////
168172
void RenderTarget::clear(const Color& color)
169173
{
170-
if (isActive(m_id) || setActive(true))
174+
if (RenderTargetImpl::isActive(m_id) || setActive(true))
171175
{
172176
// Unbind texture to fix RenderTexture preventing clear
173177
applyTexture(NULL);
@@ -282,7 +286,7 @@ void RenderTarget::draw(const Vertex* vertices, std::size_t vertexCount,
282286
}
283287
#endif
284288

285-
if (isActive(m_id) || setActive(true))
289+
if (RenderTargetImpl::isActive(m_id) || setActive(true))
286290
{
287291
// Check if the vertex count is low enough so that we can pre-transform them
288292
bool useVertexCache = (vertexCount <= StatesCache::VertexCacheSize);
@@ -382,7 +386,7 @@ void RenderTarget::draw(const VertexBuffer& vertexBuffer, std::size_t firstVerte
382386
}
383387
#endif
384388

385-
if (isActive(m_id) || setActive(true))
389+
if (RenderTargetImpl::isActive(m_id) || setActive(true))
386390
{
387391
setupDraw(false, states);
388392

@@ -424,11 +428,12 @@ bool RenderTarget::setActive(bool active)
424428
{
425429
// Mark this RenderTarget as active or no longer active in the tracking map
426430
{
427-
sf::Lock lock(mutex);
431+
sf::Lock lock(RenderTargetImpl::mutex);
428432

429433
Uint64 contextId = Context::getActiveContextId();
430434

431-
ContextRenderTargetMap::iterator iter = contextRenderTargetMap.find(contextId);
435+
using RenderTargetImpl::contextRenderTargetMap;
436+
RenderTargetImpl::ContextRenderTargetMap::iterator iter = contextRenderTargetMap.find(contextId);
432437

433438
if (active)
434439
{
@@ -462,7 +467,7 @@ bool RenderTarget::setActive(bool active)
462467
////////////////////////////////////////////////////////////
463468
void RenderTarget::pushGLStates()
464469
{
465-
if (isActive(m_id) || setActive(true))
470+
if (RenderTargetImpl::isActive(m_id) || setActive(true))
466471
{
467472
#ifdef SFML_DEBUG
468473
// make sure that the user didn't leave an unchecked OpenGL error
@@ -494,7 +499,7 @@ void RenderTarget::pushGLStates()
494499
////////////////////////////////////////////////////////////
495500
void RenderTarget::popGLStates()
496501
{
497-
if (isActive(m_id) || setActive(true))
502+
if (RenderTargetImpl::isActive(m_id) || setActive(true))
498503
{
499504
glCheck(glMatrixMode(GL_PROJECTION));
500505
glCheck(glPopMatrix());
@@ -523,7 +528,7 @@ void RenderTarget::resetGLStates()
523528
setActive(false);
524529
#endif
525530

526-
if (isActive(m_id) || setActive(true))
531+
if (RenderTargetImpl::isActive(m_id) || setActive(true))
527532
{
528533
// Make sure that extensions are initialized
529534
priv::ensureExtensionsInit();
@@ -582,7 +587,7 @@ void RenderTarget::initialize()
582587

583588
// Generate a unique ID for this RenderTarget to track
584589
// whether it is active within a specific context
585-
m_id = getUniqueId();
590+
m_id = RenderTargetImpl::getUniqueId();
586591
}
587592

588593

@@ -608,6 +613,9 @@ void RenderTarget::applyCurrentView()
608613
////////////////////////////////////////////////////////////
609614
void RenderTarget::applyBlendMode(const BlendMode& mode)
610615
{
616+
using RenderTargetImpl::factorToGlConstant;
617+
using RenderTargetImpl::equationToGlConstant;
618+
611619
// Apply the blend mode, falling back to the non-separate versions if necessary
612620
if (GLEXT_blend_func_separate)
613621
{

0 commit comments

Comments
 (0)