From 88e9f82beef1e6b87e12c466f8e77fb1d9b639c3 Mon Sep 17 00:00:00 2001 From: metaquarx Date: Thu, 17 Feb 2022 19:32:28 +0000 Subject: [PATCH] Add '.clang-format' file, CI integration, and formatting exceptions Co-authored-by: Vittorio Romeo Co-authored-by: Chris Thrasher Co-authored-by: kimci86 --- .clang-format | 167 ++++++++++++++++++ .github/workflows/ci.yml | 27 +++ CMakeLists.txt | 9 + cmake/Format.cmake | 30 ++++ examples/X11/X11.cpp | 2 + examples/cocoa/CocoaAppDelegate.h | 2 + examples/opengl/OpenGL.cpp | 2 + examples/vulkan/Vulkan.cpp | 17 ++ examples/window/Window.cpp | 2 + include/SFML/Graphics/Transform.inl | 28 ++- include/SFML/System/Utf.inl | 13 ++ src/SFML/Audio/AudioDevice.cpp | 3 + src/SFML/Graphics/RenderTarget.cpp | 2 + src/SFML/Graphics/Texture.cpp | 2 + src/SFML/Graphics/Transform.cpp | 4 + src/SFML/Graphics/Transformable.cpp | 2 + src/SFML/Graphics/View.cpp | 2 + src/SFML/Network/Packet.cpp | 8 + src/SFML/Network/Unix/SocketImpl.cpp | 2 + src/SFML/Network/Win32/SocketImpl.cpp | 2 + src/SFML/System/Android/Activity.hpp | 2 + src/SFML/Window/Android/WindowImplAndroid.cpp | 2 + src/SFML/Window/DRM/InputImplUDev.cpp | 2 + src/SFML/Window/OSX/CursorImpl.mm | 2 + src/SFML/Window/OSX/HIDInputManager.mm | 6 + src/SFML/Window/Unix/CursorImpl.cpp | 3 + src/SFML/Window/Unix/GlxContext.cpp | 4 + src/SFML/Window/Unix/InputImpl.cpp | 9 + src/SFML/Window/Unix/JoystickImpl.cpp | 4 +- src/SFML/Window/Unix/WindowImplX11.cpp | 10 ++ src/SFML/Window/Win32/CursorImpl.cpp | 3 + src/SFML/Window/Win32/InputImpl.cpp | 3 + src/SFML/Window/Win32/WindowImplWin32.cpp | 2 + test/System/FileInputStream.cpp | 1 + 34 files changed, 375 insertions(+), 4 deletions(-) create mode 100644 .clang-format create mode 100644 cmake/Format.cmake diff --git a/.clang-format b/.clang-format new file mode 100644 index 00000000..7e5a36bd --- /dev/null +++ b/.clang-format @@ -0,0 +1,167 @@ +--- +# Project +Standard: c++17 +ColumnLimit: 120 + +# Indentation +AccessModifierOffset: -4 +BitFieldColonSpacing: Both +ContinuationIndentWidth: 4 +IndentCaseLabels: true +IndentCaseBlocks: false +IndentExternBlock: Indent +IndentPPDirectives: None +IndentRequires: true +IndentWidth: 4 +IndentWrappedFunctionNames: true +TabWidth: 4 +UseTab: Never + +# Alignment +AlignAfterOpenBracket: Align +AlignConsecutiveAssignments: Consecutive +AlignConsecutiveBitFields: Consecutive +AlignConsecutiveDeclarations: Consecutive +AlignConsecutiveMacros: Consecutive +AlignEscapedNewlines: Left +AlignOperands: Align +AlignTrailingComments: true + +# Allow +AllowAllArgumentsOnNextLine: false +AllowAllConstructorInitializersOnNextLine: false +AllowAllParametersOfDeclarationOnNextLine: false +AllowShortBlocksOnASingleLine: Never +AllowShortCaseLabelsOnASingleLine: false +AllowShortEnumsOnASingleLine: false +AllowShortFunctionsOnASingleLine: None +AllowShortIfStatementsOnASingleLine: Never +AllowShortLoopsOnASingleLine: false +AllowShortLambdasOnASingleLine: All + +# Break +AlwaysBreakAfterReturnType: None +AlwaysBreakBeforeMultilineStrings: true +AlwaysBreakTemplateDeclarations: Yes +BreakBeforeConceptDeclarations: true +BreakBeforeTernaryOperators: true +BreakConstructorInitializers: AfterColon +BreakBeforeBinaryOperators: None +BreakInheritanceList: AfterColon +BreakStringLiterals: true + +# Initializers & arguments +BinPackArguments: false +BinPackParameters: false +ConstructorInitializerAllOnOneLineOrOnePerLine: true +ConstructorInitializerIndentWidth: 0 +Cpp11BracedListStyle: true + +# Braces +BreakBeforeBraces: Custom +BraceWrapping: + AfterCaseLabel: true + AfterClass: true + AfterControlStatement: Always + AfterEnum: true + AfterFunction: true + AfterNamespace: true + AfterStruct: true + AfterUnion: true + AfterExternBlock: true + BeforeCatch: false + BeforeElse: true + BeforeLambdaBody: true + BeforeWhile: false + IndentBraces: false + SplitEmptyFunction: true + SplitEmptyRecord: true + SplitEmptyNamespace: true + +# Namespaces +CompactNamespaces: false +FixNamespaceComments: true +NamespaceIndentation: None + +# Derive +DeriveLineEnding: false +DerivePointerAlignment: false + +# Empty lines +EmptyLineBeforeAccessModifier: Always +KeepEmptyLinesAtTheStartOfBlocks: true +MaxEmptyLinesToKeep: 2 + +# Sorting +IncludeBlocks: Regroup +IncludeCategories: + - Priority: 1 + Regex: '^$' + - Priority: 2 + Regex: '^$' + - Priority: 3 + Regex: '^$' +SortIncludes: true +SortUsingDeclarations: true + +# Penalties +PenaltyBreakAssignment: 1000 +PenaltyBreakBeforeFirstCallParameter: 1000 +PenaltyBreakComment: 200 +PenaltyBreakFirstLessLess: 100 +PenaltyBreakString: 1 +PenaltyBreakTemplateDeclaration: 0 +PenaltyExcessCharacter: 5 +PenaltyIndentedWhitespace: 1 +PenaltyReturnTypeOnItsOwnLine: 500 + +# Pointer alignment +PointerAlignment: Left + +# Comments +ReflowComments: false + +# Spaces +SpaceAfterCStyleCast: false +SpaceAfterLogicalNot: false +SpaceAfterTemplateKeyword: true +SpaceAroundPointerQualifiers: Default +SpaceBeforeAssignmentOperators: true +SpaceBeforeCaseColon: false +SpaceBeforeCpp11BracedList: false +SpaceBeforeCtorInitializerColon: true +SpaceBeforeInheritanceColon: true +SpaceBeforeParens: ControlStatements +SpaceBeforeRangeBasedForLoopColon: true +SpaceBeforeSquareBrackets: false +SpaceInEmptyBlock: false +SpaceInEmptyParentheses: false +SpacesBeforeTrailingComments: 1 +SpacesInAngles: false +SpacesInCStyleCastParentheses: false +SpacesInConditionalStatement: false +SpacesInContainerLiterals: false +SpacesInParentheses: false +SpacesInSquareBrackets: false + +# Line endings +UseCRLF: false + +--- +Language: ObjC +IndentWidth: 4 +BreakBeforeBraces: Custom +BraceWrapping: + AfterObjCDeclaration: true + +ObjCBinPackProtocolList: Never +ObjCBlockIndentWidth: 4 +ObjCBreakBeforeNestedBlockParam: true +ObjCSpaceAfterProperty: true +ObjCSpaceBeforeProtocolList: false + +... diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 03a2b6b8..7ea1098d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -94,3 +94,30 @@ jobs: directory: ./build files: ./build/coverage.out fail_ci_if_error: true + + format: + name: Formatting on ${{ matrix.platform.name }} + runs-on: ${{ matrix.platform.os }} + + strategy: + fail-fast: false + matrix: + platform: + - { name: Windows, os: windows-2022, executable: clang-format } + - { name: Linux, os: ubuntu-latest, executable: clang-format-12 } + - { name: macOS, os: macos-12, executable: clang-format } + + steps: + - name: Checkout Code + uses: actions/checkout@v2 + + - name: Install macOS Dependencies + if: runner.os == 'macOS' + run: brew install clang-format + + - name: Format Code + shell: bash + run: cmake -DCLANG_FORMAT_EXECUTABLE=${{ matrix.platform.executable }} -P $GITHUB_WORKSPACE/cmake/Format.cmake + + - name: Check Formatting + run: git diff --exit-code diff --git a/CMakeLists.txt b/CMakeLists.txt index dac4254b..409823b6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -536,3 +536,12 @@ string(REGEX REPLACE "/" "\\\\\\\\" NSIS_IMAGE_PATH ${NSIS_IMAGE_PATH}) set(CPACK_NSIS_INSTALLER_MUI_ICON_CODE "!define MUI_WELCOMEFINISHPAGE_BITMAP \\\"${NSIS_IMAGE_PATH}sidebar.bmp\\\"\n!define MUI_HEADERIMAGE_BITMAP \\\"${NSIS_IMAGE_PATH}header.bmp\\\"\n!define MUI_ICON \\\"${NSIS_IMAGE_PATH}sfml.ico\\\"") include(CPack) + +if(NOT CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME) + return() +endif() + +sfml_set_option(CLANG_FORMAT_EXECUTABLE clang-format STRING "Override clang-format executable, requires minimum version 12") +add_custom_target(format + COMMAND ${CMAKE_COMMAND} "-DCLANG_FORMAT_EXECUTABLE=${CLANG_FORMAT_EXECUTABLE}" -P ./cmake/Format.cmake + WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" VERBATIM) diff --git a/cmake/Format.cmake b/cmake/Format.cmake new file mode 100644 index 00000000..bd6609c6 --- /dev/null +++ b/cmake/Format.cmake @@ -0,0 +1,30 @@ +# Check executable exists +if(NOT EXISTS ${CLANG_FORMAT_EXECUTABLE}) + find_program(CLANG_FORMAT_EXEC_TEMP ${CLANG_FORMAT_EXECUTABLE}) + if(CLANG_FORMAT_EXEC_TEMP) + set(CLANG_FORMAT_EXECUTABLE ${CLANG_FORMAT_EXEC_TEMP}) + unset(CLANG_FORMAT_EXEC_TEMP) + else() + message(FATAL_ERROR "Unable to find clang-format executable: \"${CLANG_FORMAT_EXECUTABLE}\"") + endif() +endif() + +# Check executable version +execute_process(COMMAND ${CLANG_FORMAT_EXECUTABLE} --version OUTPUT_VARIABLE CLANG_FORMAT_VERSION) +string(REGEX MATCH "clang-format version ([0-9]+)" CLANG_FORMAT_VERSION ${CLANG_FORMAT_VERSION}) +unset(CLANG_FORMAT_VERSION) +if(CMAKE_MATCH_1 GREATER_EQUAL 12) + message(STATUS "Using clang-format version ${CMAKE_MATCH_1}") +else() + message(FATAL_ERROR "clang-format version ${CMAKE_MATCH_1} is too low") +endif() + +# Run +set(SOURCES "") +foreach(FOLDER IN ITEMS examples include src test tools) + file(GLOB_RECURSE folder_files "${FOLDER}/*.h" "${FOLDER}/*.hpp" "${FOLDER}/*.inl" "${FOLDER}/*.cpp" "${FOLDER}/*.mm") + list(FILTER folder_files EXCLUDE REGEX "gl.h|vulkan.h|stb_perlin.h") # 3rd party code to exclude from formatting + list(APPEND SOURCES ${folder_files}) +endforeach() + +execute_process(COMMAND ${CLANG_FORMAT_EXECUTABLE} -i ${SOURCES}) diff --git a/examples/X11/X11.cpp b/examples/X11/X11.cpp index 624df662..87711175 100644 --- a/examples/X11/X11.cpp +++ b/examples/X11/X11.cpp @@ -94,6 +94,7 @@ glRotatef(elapsedTime * 18.f, 0.f, 0.f, 1.f); // Define a 3D cube (6 faces made of 2 triangles composed by 3 vertices) + // clang-format off constexpr std::array cube = { // positions // colors @@ -139,6 +140,7 @@ 50, -50, 50, 0, 1, 1, 50, 50, 50, 0, 1, 1 }; + // clang-format on // Draw the cube glVertexPointer(3, GL_FLOAT, 6 * sizeof(GLfloat), cube.data()); diff --git a/examples/cocoa/CocoaAppDelegate.h b/examples/cocoa/CocoaAppDelegate.h index f0f39ecc..f1d1ce67 100644 --- a/examples/cocoa/CocoaAppDelegate.h +++ b/examples/cocoa/CocoaAppDelegate.h @@ -49,7 +49,9 @@ struct SFMLmainWindow; } @property (retain) IBOutlet NSWindow* window; + @property (assign) IBOutlet NSView* sfmlView; + @property (assign) IBOutlet NSTextField* textField; -(IBAction)colorChanged:(NSPopUpButton*)sender; diff --git a/examples/opengl/OpenGL.cpp b/examples/opengl/OpenGL.cpp index 944bf022..886aa4a8 100644 --- a/examples/opengl/OpenGL.cpp +++ b/examples/opengl/OpenGL.cpp @@ -125,6 +125,7 @@ int main() sf::Texture::bind(&texture); // Define a 3D cube (6 faces made of 2 triangles composed by 3 vertices) + // clang-format off constexpr std::array cube = { // positions // texture coordinates @@ -170,6 +171,7 @@ int main() 20, -20, 20, 1, 0, 20, 20, 20, 1, 1 }; + // clang-format on // Enable position and texture coordinates vertex components glEnableClientState(GL_VERTEX_ARRAY); diff --git a/examples/vulkan/Vulkan.cpp b/examples/vulkan/Vulkan.cpp index 3f286ed2..c04ec2a6 100644 --- a/examples/vulkan/Vulkan.cpp +++ b/examples/vulkan/Vulkan.cpp @@ -43,12 +43,15 @@ namespace void matrixRotateX(Matrix& result, sf::Angle angle) { float rad = angle.asRadians(); + + // clang-format off Matrix matrix = { {1.f, 0.f, 0.f, 0.f}, {0.f, std::cos(rad), std::sin(rad), 0.f}, {0.f, -std::sin(rad), std::cos(rad), 0.f}, {0.f, 0.f, 0.f, 1.f} }; + // clang-format on matrixMultiply(result, result, matrix); } @@ -57,12 +60,15 @@ namespace void matrixRotateY(Matrix& result, sf::Angle angle) { float rad = angle.asRadians(); + + // clang-format off Matrix matrix = { { std::cos(rad), 0.f, std::sin(rad), 0.f}, { 0.f, 1.f, 0.f, 0.f}, {-std::sin(rad), 0.f, std::cos(rad), 0.f}, { 0.f, 0.f, 0.f, 1.f} }; + // clang-format on matrixMultiply(result, result, matrix); } @@ -71,12 +77,15 @@ namespace void matrixRotateZ(Matrix& result, sf::Angle angle) { float rad = angle.asRadians(); + + // clang-format off Matrix matrix = { { std::cos(rad), std::sin(rad), 0.f, 0.f}, {-std::sin(rad), std::cos(rad), 0.f, 0.f}, { 0.f, 0.f, 1.f, 0.f}, { 0.f, 0.f, 0.f, 1.f} }; + // clang-format on matrixMultiply(result, result, matrix); } @@ -85,11 +94,13 @@ namespace void matrixLookAt(Matrix& result, const Vec3& eye, const Vec3& center, const Vec3& up) { // Forward-looking vector + // clang-format off Vec3 forward = { center[0] - eye[0], center[1] - eye[1], center[2] - eye[2] }; + // clang-format on // Normalize float factor = 1.0f / std::sqrt(forward[0] * forward[0] + forward[1] * forward[1] + forward[2] * forward[2]); @@ -98,11 +109,13 @@ namespace f *= factor; // Side vector (Forward cross product Up) + // clang-format off Vec3 side = { forward[1] * up[2] - forward[2] * up[1], forward[2] * up[0] - forward[0] * up[2], forward[0] * up[1] - forward[1] * up[0] }; + // clang-format on // Normalize factor = 1.0f / std::sqrt(side[0] * side[0] + side[1] * side[1] + side[2] * side[2]); @@ -1371,6 +1384,7 @@ public: // Create our vertex buffer and upload its data void setupVertexBuffer() { + // clang-format off constexpr std::array vertexData = { // X Y Z R G B A U V -0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, @@ -1403,6 +1417,7 @@ public: 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, -0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f }; + // clang-format on // Create a staging buffer that is writable by the CPU VkBuffer stagingBuffer = 0; @@ -1465,6 +1480,7 @@ public: // Create our index buffer and upload its data void setupIndexBuffer() { + // clang-format off constexpr std::array indexData = { 0, 1, 2, 2, 3, 0, @@ -1484,6 +1500,7 @@ public: 20, 21, 22, 22, 23, 20 }; + // clang-format on // Create a staging buffer that is writable by the CPU VkBuffer stagingBuffer = 0; diff --git a/examples/window/Window.cpp b/examples/window/Window.cpp index 4526f20e..e1a2a312 100644 --- a/examples/window/Window.cpp +++ b/examples/window/Window.cpp @@ -74,6 +74,7 @@ int main() #endif // Define a 3D cube (6 faces made of 2 triangles composed by 3 vertices) + // clang-format off constexpr std::array cube = { // positions // colors (r, g, b, a) @@ -119,6 +120,7 @@ int main() 50, -50, 50, 1, 1, 0, 1, 50, 50, 50, 1, 1, 0, 1, }; + // clang-format on // Enable position and color vertex components glEnableClientState(GL_VERTEX_ARRAY); diff --git a/include/SFML/Graphics/Transform.inl b/include/SFML/Graphics/Transform.inl index e0b68fe2..14b906f9 100644 --- a/include/SFML/Graphics/Transform.inl +++ b/include/SFML/Graphics/Transform.inl @@ -24,6 +24,7 @@ //////////////////////////////////////////////////////////// +// clang-format off constexpr Transform::Transform() // Identity matrix : m_matrix{1.f, 0.f, 0.f, 0.f, @@ -32,9 +33,11 @@ constexpr Transform::Transform() 0.f, 0.f, 0.f, 1.f} { } +// clang-format on //////////////////////////////////////////////////////////// +// clang-format off constexpr Transform::Transform(float a00, float a01, float a02, float a10, float a11, float a12, float a20, float a21, float a22) @@ -44,6 +47,7 @@ constexpr Transform::Transform(float a00, float a01, float a02, a02, a12, 0.f, a22} { } +// clang-format on //////////////////////////////////////////////////////////// @@ -56,15 +60,18 @@ constexpr const float* Transform::getMatrix() const //////////////////////////////////////////////////////////// constexpr Transform Transform::getInverse() const { + // clang-format off // Compute the determinant float det = m_matrix[0] * (m_matrix[15] * m_matrix[5] - m_matrix[7] * m_matrix[13]) - m_matrix[1] * (m_matrix[15] * m_matrix[4] - m_matrix[7] * m_matrix[12]) + m_matrix[3] * (m_matrix[13] * m_matrix[4] - m_matrix[5] * m_matrix[12]); + // clang-format on // Compute the inverse if the determinant is not zero // (don't use an epsilon because the determinant may *really* be tiny) if (det != 0.f) { + // clang-format off return Transform( (m_matrix[15] * m_matrix[5] - m_matrix[7] * m_matrix[13]) / det, -(m_matrix[15] * m_matrix[4] - m_matrix[7] * m_matrix[12]) / det, (m_matrix[13] * m_matrix[4] - m_matrix[5] * m_matrix[12]) / det, @@ -74,6 +81,7 @@ constexpr Transform Transform::getInverse() const (m_matrix[7] * m_matrix[1] - m_matrix[3] * m_matrix[5]) / det, -(m_matrix[7] * m_matrix[0] - m_matrix[3] * m_matrix[4]) / det, (m_matrix[5] * m_matrix[0] - m_matrix[1] * m_matrix[4]) / det); + // clang-format on } else { @@ -107,12 +115,16 @@ constexpr FloatRect Transform::transformRect(const FloatRect& rectangle) const float top = points[0].y; float right = points[0].x; float bottom = points[0].y; + for (int i = 1; i < 4; ++i) { - if (points[i].x < left) left = points[i].x; - else if (points[i].x > right) right = points[i].x; - if (points[i].y < top) top = points[i].y; + // clang-format off + if (points[i].x < left) left = points[i].x; + else if (points[i].x > right) right = points[i].x; + + if (points[i].y < top) top = points[i].y; else if (points[i].y > bottom) bottom = points[i].y; + // clang-format on } return FloatRect({left, top}, {right - left, bottom - top}); @@ -125,6 +137,7 @@ constexpr Transform& Transform::combine(const Transform& transform) const float* a = m_matrix; const float* b = transform.m_matrix; + // clang-format off *this = Transform(a[0] * b[0] + a[4] * b[1] + a[12] * b[3], a[0] * b[4] + a[4] * b[5] + a[12] * b[7], a[0] * b[12] + a[4] * b[13] + a[12] * b[15], @@ -134,6 +147,7 @@ constexpr Transform& Transform::combine(const Transform& transform) a[3] * b[0] + a[7] * b[1] + a[15] * b[3], a[3] * b[4] + a[7] * b[5] + a[15] * b[7], a[3] * b[12] + a[7] * b[13] + a[15] * b[15]); + // clang-format on return *this; } @@ -142,9 +156,11 @@ constexpr Transform& Transform::combine(const Transform& transform) //////////////////////////////////////////////////////////// constexpr Transform& Transform::translate(const Vector2f& offset) { + // clang-format off Transform translation(1, 0, offset.x, 0, 1, offset.y, 0, 0, 1); + // clang-format on return combine(translation); } @@ -153,9 +169,11 @@ constexpr Transform& Transform::translate(const Vector2f& offset) //////////////////////////////////////////////////////////// constexpr Transform& Transform::scale(const Vector2f& factors) { + // clang-format off Transform scaling(factors.x, 0, 0, 0, factors.y, 0, 0, 0, 1); + // clang-format on return combine(scaling); } @@ -164,9 +182,11 @@ constexpr Transform& Transform::scale(const Vector2f& factors) //////////////////////////////////////////////////////////// constexpr Transform& Transform::scale(const Vector2f& factors, const Vector2f& center) { + // clang-format off Transform scaling(factors.x, 0, center.x * (1 - factors.x), 0, factors.y, center.y * (1 - factors.y), 0, 0, 1); + // clang-format on return combine(scaling); } @@ -199,9 +219,11 @@ constexpr bool operator ==(const Transform& left, const Transform& right) const float* a = left.getMatrix(); const float* b = right.getMatrix(); + // clang-format off return ((a[0] == b[0]) && (a[1] == b[1]) && (a[3] == b[3]) && (a[4] == b[4]) && (a[5] == b[5]) && (a[7] == b[7]) && (a[12] == b[12]) && (a[13] == b[13]) && (a[15] == b[15])); + // clang-format on } diff --git a/include/SFML/System/Utf.inl b/include/SFML/System/Utf.inl index 3c7f30e8..da400c8d 100644 --- a/include/SFML/System/Utf.inl +++ b/include/SFML/System/Utf.inl @@ -47,6 +47,7 @@ OutputIt priv::copy(InputIt first, InputIt last, OutputIt d_first) template In Utf<8>::decode(In begin, In end, Uint32& output, Uint32 replacement) { + // clang-format off // Some useful precomputed data static constexpr int trailing[256] = { @@ -59,16 +60,20 @@ In Utf<8>::decode(In begin, In end, Uint32& output, Uint32 replacement) 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5 }; + static constexpr Uint32 offsets[6] = { 0x00000000, 0x00003080, 0x000E2080, 0x03C82080, 0xFA082080, 0x82082080 }; + // clang-format on // decode the character int trailingBytes = trailing[static_cast(*begin)]; if (begin + trailingBytes < end) { output = 0; + + // clang-format off switch (trailingBytes) { case 5: output += static_cast(*begin++); output <<= 6; [[fallthrough]]; @@ -78,6 +83,8 @@ In Utf<8>::decode(In begin, In end, Uint32& output, Uint32 replacement) case 1: output += static_cast(*begin++); output <<= 6; [[fallthrough]]; case 0: output += static_cast(*begin++); } + // clang-format on + output -= offsets[trailingBytes]; } else @@ -114,13 +121,18 @@ Out Utf<8>::encode(Uint32 input, Out output, Uint8 replacement) // Get the number of bytes to write std::size_t bytestoWrite = 1; + + // clang-format off if (input < 0x80) bytestoWrite = 1; else if (input < 0x800) bytestoWrite = 2; else if (input < 0x10000) bytestoWrite = 3; else if (input <= 0x0010FFFF) bytestoWrite = 4; + // clang-format on // Extract the bytes to write Uint8 bytes[4]; + + // clang-format off switch (bytestoWrite) { case 4: bytes[3] = static_cast((input | 0x80) & 0xBF); input >>= 6; [[fallthrough]]; @@ -128,6 +140,7 @@ Out Utf<8>::encode(Uint32 input, Out output, Uint8 replacement) case 2: bytes[1] = static_cast((input | 0x80) & 0xBF); input >>= 6; [[fallthrough]]; case 1: bytes[0] = static_cast (input | firstBytes[bytestoWrite]); } + // clang-format on // Add them to the output output = priv::copy(bytes, bytes + bytestoWrite, output); diff --git a/src/SFML/Audio/AudioDevice.cpp b/src/SFML/Audio/AudioDevice.cpp index feaf677e..edf78c9a 100644 --- a/src/SFML/Audio/AudioDevice.cpp +++ b/src/SFML/Audio/AudioDevice.cpp @@ -139,6 +139,8 @@ int AudioDevice::getFormatFromChannelCount(unsigned int channelCount) // Find the good format according to the number of channels int format = 0; + + // clang-format off switch (channelCount) { case 1: format = AL_FORMAT_MONO16; break; @@ -149,6 +151,7 @@ int AudioDevice::getFormatFromChannelCount(unsigned int channelCount) case 8: format = alGetEnumValue("AL_FORMAT_71CHN16"); break; default: format = 0; break; } + // clang-format on // Fixes a bug on OS X if (format == -1) diff --git a/src/SFML/Graphics/RenderTarget.cpp b/src/SFML/Graphics/RenderTarget.cpp index f2a6bc35..343079f0 100644 --- a/src/SFML/Graphics/RenderTarget.cpp +++ b/src/SFML/Graphics/RenderTarget.cpp @@ -80,6 +80,7 @@ namespace // Convert an sf::BlendMode::Factor constant to the corresponding OpenGL constant. sf::Uint32 factorToGlConstant(sf::BlendMode::Factor blendFactor) { + // clang-format off switch (blendFactor) { case sf::BlendMode::Zero: return GL_ZERO; @@ -93,6 +94,7 @@ namespace case sf::BlendMode::DstAlpha: return GL_DST_ALPHA; case sf::BlendMode::OneMinusDstAlpha: return GL_ONE_MINUS_DST_ALPHA; } + // clang-format on sf::err() << "Invalid value for sf::BlendMode::Factor! Fallback to sf::BlendMode::Zero." << std::endl; assert(false); diff --git a/src/SFML/Graphics/Texture.cpp b/src/SFML/Graphics/Texture.cpp index 699062ab..197a6c0e 100644 --- a/src/SFML/Graphics/Texture.cpp +++ b/src/SFML/Graphics/Texture.cpp @@ -748,10 +748,12 @@ void Texture::bind(const Texture* texture, CoordinateType coordinateType) // Check if we need to define a special texture matrix if ((coordinateType == Pixels) || texture->m_pixelsFlipped) { + // clang-format off GLfloat matrix[16] = {1.f, 0.f, 0.f, 0.f, 0.f, 1.f, 0.f, 0.f, 0.f, 0.f, 1.f, 0.f, 0.f, 0.f, 0.f, 1.f}; + // clang-format on // If non-normalized coordinates (= pixels) are requested, we need to // setup scale factors that convert the range [0 .. size] to [0 .. 1] diff --git a/src/SFML/Graphics/Transform.cpp b/src/SFML/Graphics/Transform.cpp index 3e00d905..3b55f6ad 100644 --- a/src/SFML/Graphics/Transform.cpp +++ b/src/SFML/Graphics/Transform.cpp @@ -39,9 +39,11 @@ Transform& Transform::rotate(Angle angle) float cos = std::cos(rad); float sin = std::sin(rad); + // clang-format off Transform rotation(cos, -sin, 0, sin, cos, 0, 0, 0, 1); + // clang-format on return combine(rotation); } @@ -54,9 +56,11 @@ Transform& Transform::rotate(Angle angle, const Vector2f& center) float cos = std::cos(rad); float sin = std::sin(rad); + // clang-format off Transform rotation(cos, -sin, center.x * (1 - cos) + center.y * sin, sin, cos, center.y * (1 - cos) - center.x * sin, 0, 0, 1); + // clang-format on return combine(rotation); } diff --git a/src/SFML/Graphics/Transformable.cpp b/src/SFML/Graphics/Transformable.cpp index 495f7c9e..2399cb86 100644 --- a/src/SFML/Graphics/Transformable.cpp +++ b/src/SFML/Graphics/Transformable.cpp @@ -151,9 +151,11 @@ const Transform& Transformable::getTransform() const float tx = -m_origin.x * sxc - m_origin.y * sys + m_position.x; float ty = m_origin.x * sxs - m_origin.y * syc + m_position.y; + // clang-format off m_transform = Transform( sxc, sys, tx, -sxs, syc, ty, 0.f, 0.f, 1.f); + // clang-format on m_transformNeedUpdate = false; } diff --git a/src/SFML/Graphics/View.cpp b/src/SFML/Graphics/View.cpp index 438e0c2b..d7c5866f 100644 --- a/src/SFML/Graphics/View.cpp +++ b/src/SFML/Graphics/View.cpp @@ -188,9 +188,11 @@ const Transform& View::getTransform() const float d = -b * m_center.y; // Rebuild the projection matrix + // clang-format off m_transform = Transform( a * cosine, a * sine, a * tx + c, -b * sine, b * cosine, b * ty + d, 0.f, 0.f, 1.f); + // clang-format on m_transformUpdated = true; } diff --git a/src/SFML/Network/Packet.cpp b/src/SFML/Network/Packet.cpp index 6cfb313a..51c602c1 100644 --- a/src/SFML/Network/Packet.cpp +++ b/src/SFML/Network/Packet.cpp @@ -222,6 +222,7 @@ Packet& Packet::operator >>(Int64& data) // to network byte order (big endian) manually Uint8 bytes[sizeof(data)]; std::memcpy(bytes, &m_data[m_readPos], sizeof(data)); + data = (static_cast(bytes[0]) << 56) | (static_cast(bytes[1]) << 48) | (static_cast(bytes[2]) << 40) | @@ -230,6 +231,7 @@ Packet& Packet::operator >>(Int64& data) (static_cast(bytes[5]) << 16) | (static_cast(bytes[6]) << 8) | (static_cast(bytes[7]) ); + m_readPos += sizeof(data); } @@ -246,6 +248,7 @@ Packet& Packet::operator >>(Uint64& data) // to network byte order (big endian) manually Uint8 bytes[sizeof(data)]; std::memcpy(bytes, &m_data[m_readPos], sizeof(data)); + data = (static_cast(bytes[0]) << 56) | (static_cast(bytes[1]) << 48) | (static_cast(bytes[2]) << 40) | @@ -254,6 +257,7 @@ Packet& Packet::operator >>(Uint64& data) (static_cast(bytes[5]) << 16) | (static_cast(bytes[6]) << 8) | (static_cast(bytes[7]) ); + m_readPos += sizeof(data); } @@ -463,6 +467,7 @@ Packet& Packet::operator <<(Int64 data) { // Since htonll is not available everywhere, we have to convert // to network byte order (big endian) manually + Uint8 toWrite[] = { static_cast((data >> 56) & 0xFF), @@ -474,6 +479,7 @@ Packet& Packet::operator <<(Int64 data) static_cast((data >> 8) & 0xFF), static_cast((data ) & 0xFF) }; + append(&toWrite, sizeof(toWrite)); return *this; } @@ -484,6 +490,7 @@ Packet& Packet::operator <<(Uint64 data) { // Since htonll is not available everywhere, we have to convert // to network byte order (big endian) manually + Uint8 toWrite[] = { static_cast((data >> 56) & 0xFF), @@ -495,6 +502,7 @@ Packet& Packet::operator <<(Uint64 data) static_cast((data >> 8) & 0xFF), static_cast((data ) & 0xFF) }; + append(&toWrite, sizeof(toWrite)); return *this; } diff --git a/src/SFML/Network/Unix/SocketImpl.cpp b/src/SFML/Network/Unix/SocketImpl.cpp index d4001602..06458f5d 100644 --- a/src/SFML/Network/Unix/SocketImpl.cpp +++ b/src/SFML/Network/Unix/SocketImpl.cpp @@ -95,6 +95,7 @@ Socket::Status SocketImpl::getErrorStatus() if ((errno == EAGAIN) || (errno == EINPROGRESS)) return Socket::NotReady; + // clang-format off switch (errno) { case EWOULDBLOCK: return Socket::NotReady; @@ -106,6 +107,7 @@ Socket::Status SocketImpl::getErrorStatus() case EPIPE: return Socket::Disconnected; default: return Socket::Error; } + // clang-format on } } // namespace priv diff --git a/src/SFML/Network/Win32/SocketImpl.cpp b/src/SFML/Network/Win32/SocketImpl.cpp index 71a6800a..0ecc475b 100644 --- a/src/SFML/Network/Win32/SocketImpl.cpp +++ b/src/SFML/Network/Win32/SocketImpl.cpp @@ -71,6 +71,7 @@ void SocketImpl::setBlocking(SocketHandle sock, bool block) //////////////////////////////////////////////////////////// Socket::Status SocketImpl::getErrorStatus() { + // clang-format off switch (WSAGetLastError()) { case WSAEWOULDBLOCK: return Socket::NotReady; @@ -83,6 +84,7 @@ Socket::Status SocketImpl::getErrorStatus() case WSAEISCONN: return Socket::Done; // when connecting a non-blocking socket default: return Socket::Error; } + // clang-format on } diff --git a/src/SFML/System/Android/Activity.hpp b/src/SFML/System/Android/Activity.hpp index 27bfba82..fd818cbe 100644 --- a/src/SFML/System/Android/Activity.hpp +++ b/src/SFML/System/Android/Activity.hpp @@ -92,7 +92,9 @@ struct ActivityStates }; SFML_SYSTEM_API ActivityStates*& getActivityStatesPtr(); + SFML_SYSTEM_API void resetActivity(ActivityStates* initializedStates); + SFML_SYSTEM_API ActivityStates& getActivity(); } // namespace priv diff --git a/src/SFML/Window/Android/WindowImplAndroid.cpp b/src/SFML/Window/Android/WindowImplAndroid.cpp index 2715646f..586a610b 100644 --- a/src/SFML/Window/Android/WindowImplAndroid.cpp +++ b/src/SFML/Window/Android/WindowImplAndroid.cpp @@ -564,6 +564,7 @@ int WindowImplAndroid::processPointerEvent(bool isDown, AInputEvent* _event, Act //////////////////////////////////////////////////////////// Keyboard::Key WindowImplAndroid::androidKeyToSF(int32_t key) { + // clang-format off switch (key) { case AKEYCODE_UNKNOWN: @@ -680,6 +681,7 @@ Keyboard::Key WindowImplAndroid::androidKeyToSF(int32_t key) case AKEYCODE_BUTTON_MODE: default: return Keyboard::Unknown; } + // clang-format on } diff --git a/src/SFML/Window/DRM/InputImplUDev.cpp b/src/SFML/Window/DRM/InputImplUDev.cpp index 2cf047f0..8b0435b5 100644 --- a/src/SFML/Window/DRM/InputImplUDev.cpp +++ b/src/SFML/Window/DRM/InputImplUDev.cpp @@ -176,6 +176,7 @@ namespace { switch (code) { + // clang-format off case KEY_ESC: return sf::Keyboard::Escape; case KEY_1: return sf::Keyboard::Num1; case KEY_2: return sf::Keyboard::Num2; @@ -286,6 +287,7 @@ namespace case KEY_SCROLLLOCK: default: return sf::Keyboard::Unknown; + // clang-format on } } diff --git a/src/SFML/Window/OSX/CursorImpl.mm b/src/SFML/Window/OSX/CursorImpl.mm index d20e94dc..3cee96a5 100644 --- a/src/SFML/Window/OSX/CursorImpl.mm +++ b/src/SFML/Window/OSX/CursorImpl.mm @@ -93,6 +93,7 @@ bool CursorImpl::loadFromSystem(Cursor::Type type) AutoreleasePool pool; NSCursor* newCursor = nil; + // clang-format off switch (type) { default: return false; @@ -129,6 +130,7 @@ bool CursorImpl::loadFromSystem(Cursor::Type type) break; #pragma clang diagnostic pop } + // clang-format on if (newCursor) { diff --git a/src/SFML/Window/OSX/HIDInputManager.mm b/src/SFML/Window/OSX/HIDInputManager.mm index 1fb0165c..c4cad28d 100644 --- a/src/SFML/Window/OSX/HIDInputManager.mm +++ b/src/SFML/Window/OSX/HIDInputManager.mm @@ -403,6 +403,7 @@ UInt8 HIDInputManager::usageToVirtualCode(UInt32 usage) { // Some usage key doesn't have any corresponding virtual // code or it was not found (return 0xff). + // clang-format off switch (usage) { case kHIDUsage_KeyboardErrorRollOver: return 0xff; @@ -602,12 +603,14 @@ UInt8 HIDInputManager::usageToVirtualCode(UInt32 usage) case kHIDUsage_Keyboard_Reserved: return 0xff; default: return 0xff; } + // clang-format on } //////////////////////////////////////////////////////// Keyboard::Key HIDInputManager::localizedKeys(UniChar ch) { + // clang-format off switch (ch) { case 'a': @@ -691,6 +694,7 @@ Keyboard::Key HIDInputManager::localizedKeys(UniChar ch) // The key is not 'localized'. default: return sf::Keyboard::Unknown; } + // clang-format on } @@ -699,6 +703,7 @@ Keyboard::Key HIDInputManager::nonLocalizedKeys(UniChar virtualKeycode) { // (Some) 0x code based on https://forums.macrumors.com/showthread.php?t=780577 // Some sf::Keyboard::Key are present twice. + // clang-format off switch (virtualKeycode) { // These cases should not be used but anyway... @@ -864,6 +869,7 @@ Keyboard::Key HIDInputManager::nonLocalizedKeys(UniChar virtualKeycode) // An unknown key. default: return sf::Keyboard::Unknown; } + // clang-format on } diff --git a/src/SFML/Window/Unix/CursorImpl.cpp b/src/SFML/Window/Unix/CursorImpl.cpp index f550b997..f3103111 100644 --- a/src/SFML/Window/Unix/CursorImpl.cpp +++ b/src/SFML/Window/Unix/CursorImpl.cpp @@ -165,6 +165,8 @@ bool CursorImpl::loadFromSystem(Cursor::Type type) release(); unsigned int shape; + + // clang-format off switch (type) { default: return false; @@ -188,6 +190,7 @@ bool CursorImpl::loadFromSystem(Cursor::Type type) case Cursor::Help: shape = XC_question_arrow; break; case Cursor::NotAllowed: shape = XC_X_cursor; break; } + // clang-format on m_cursor = XCreateFontCursor(m_display, shape); return true; diff --git a/src/SFML/Window/Unix/GlxContext.cpp b/src/SFML/Window/Unix/GlxContext.cpp index 8a5b5b9c..8da58bf6 100644 --- a/src/SFML/Window/Unix/GlxContext.cpp +++ b/src/SFML/Window/Unix/GlxContext.cpp @@ -25,7 +25,11 @@ //////////////////////////////////////////////////////////// // Headers //////////////////////////////////////////////////////////// + +// clang-format off #include // important to be included first (conflict with None) +// clang-format on + #include #include #include diff --git a/src/SFML/Window/Unix/InputImpl.cpp b/src/SFML/Window/Unix/InputImpl.cpp index aaf45041..2f6cad77 100644 --- a/src/SFML/Window/Unix/InputImpl.cpp +++ b/src/SFML/Window/Unix/InputImpl.cpp @@ -25,7 +25,11 @@ //////////////////////////////////////////////////////////// // Headers //////////////////////////////////////////////////////////// + +// clang-format off #include // important to be included first (conflict with None) +// clang-format on + #include #include #include @@ -41,6 +45,8 @@ bool InputImpl::isKeyPressed(Keyboard::Key key) { // Get the corresponding X11 keysym KeySym keysym = 0; + + // clang-format off switch (key) { case Keyboard::LShift: keysym = XK_Shift_L; break; @@ -146,6 +152,7 @@ bool InputImpl::isKeyPressed(Keyboard::Key key) case Keyboard::Num9: keysym = XK_9; break; default: keysym = 0; break; } + // clang-format on // Sanity checks if (key < 0 || key >= sf::Keyboard::KeyCount) @@ -202,6 +209,7 @@ bool InputImpl::isMouseButtonPressed(Mouse::Button button) // Close the connection with the X server CloseDisplay(display); + // clang-format off switch (button) { case Mouse::Left: return buttons & Button1Mask; @@ -211,6 +219,7 @@ bool InputImpl::isMouseButtonPressed(Mouse::Button button) case Mouse::XButton2: return false; // not supported by X default: return false; } + // clang-format on return false; } diff --git a/src/SFML/Window/Unix/JoystickImpl.cpp b/src/SFML/Window/Unix/JoystickImpl.cpp index c30426b2..89f1546e 100644 --- a/src/SFML/Window/Unix/JoystickImpl.cpp +++ b/src/SFML/Window/Unix/JoystickImpl.cpp @@ -611,6 +611,7 @@ JoystickCaps JoystickImpl::getCapabilities() const { switch (m_mapping[i]) { + // clang-format off case ABS_X: caps.axes[Joystick::X] = true; break; case ABS_Y: caps.axes[Joystick::Y] = true; break; case ABS_Z: @@ -621,7 +622,8 @@ JoystickCaps JoystickImpl::getCapabilities() const case ABS_RY: caps.axes[Joystick::V] = true; break; case ABS_HAT0X: caps.axes[Joystick::PovX] = true; break; case ABS_HAT0Y: caps.axes[Joystick::PovY] = true; break; - default: break; + default: break; + // clang-format on } } diff --git a/src/SFML/Window/Unix/WindowImplX11.cpp b/src/SFML/Window/Unix/WindowImplX11.cpp index 5cc32d9a..47391342 100644 --- a/src/SFML/Window/Unix/WindowImplX11.cpp +++ b/src/SFML/Window/Unix/WindowImplX11.cpp @@ -25,7 +25,11 @@ //////////////////////////////////////////////////////////// // Headers //////////////////////////////////////////////////////////// + +// clang-format off #include +// clang-format on + #include #include #include @@ -386,6 +390,7 @@ namespace sf::Keyboard::Key keysymToSF(KeySym symbol) { + // clang-format off switch (symbol) { case XK_Shift_L: return sf::Keyboard::LShift; @@ -491,6 +496,7 @@ namespace case XK_8: return sf::Keyboard::Num8; case XK_9: return sf::Keyboard::Num9; } + // clang-format on return sf::Keyboard::Unknown; } @@ -2015,6 +2021,8 @@ bool WindowImplX11::processEvent(XEvent& windowEvent) event.type = Event::MouseButtonPressed; event.mouseButton.x = windowEvent.xbutton.x; event.mouseButton.y = windowEvent.xbutton.y; + + // clang-format off switch(button) { case Button1: event.mouseButton.button = Mouse::Left; break; @@ -2023,6 +2031,8 @@ bool WindowImplX11::processEvent(XEvent& windowEvent) case 8: event.mouseButton.button = Mouse::XButton1; break; case 9: event.mouseButton.button = Mouse::XButton2; break; } + // clang-format on + pushEvent(event); } diff --git a/src/SFML/Window/Win32/CursorImpl.cpp b/src/SFML/Window/Win32/CursorImpl.cpp index b96e2aeb..55f053d2 100755 --- a/src/SFML/Window/Win32/CursorImpl.cpp +++ b/src/SFML/Window/Win32/CursorImpl.cpp @@ -146,6 +146,8 @@ bool CursorImpl::loadFromSystem(Cursor::Type type) release(); LPCTSTR shape = nullptr; + + // clang-format off switch (type) { case Cursor::Arrow: shape = IDC_ARROW; break; @@ -170,6 +172,7 @@ bool CursorImpl::loadFromSystem(Cursor::Type type) case Cursor::Help: shape = IDC_HELP; break; case Cursor::NotAllowed: shape = IDC_NO; break; } + // clang-format on // Get the shared system cursor and make sure not to destroy it m_cursor = LoadCursor(nullptr, shape); diff --git a/src/SFML/Window/Win32/InputImpl.cpp b/src/SFML/Window/Win32/InputImpl.cpp index 5c005833..c40b7b67 100644 --- a/src/SFML/Window/Win32/InputImpl.cpp +++ b/src/SFML/Window/Win32/InputImpl.cpp @@ -38,6 +38,8 @@ namespace priv bool InputImpl::isKeyPressed(Keyboard::Key key) { int vkey = 0; + + // clang-format off switch (key) { default: vkey = 0; break; @@ -143,6 +145,7 @@ bool InputImpl::isKeyPressed(Keyboard::Key key) case Keyboard::F15: vkey = VK_F15; break; case Keyboard::Pause: vkey = VK_PAUSE; break; } + // clang-format on return (GetAsyncKeyState(vkey) & 0x8000) != 0; } diff --git a/src/SFML/Window/Win32/WindowImplWin32.cpp b/src/SFML/Window/Win32/WindowImplWin32.cpp index b2ed27fd..bd9d12d4 100755 --- a/src/SFML/Window/Win32/WindowImplWin32.cpp +++ b/src/SFML/Window/Win32/WindowImplWin32.cpp @@ -992,6 +992,7 @@ void WindowImplWin32::processEvent(UINT message, WPARAM wParam, LPARAM lParam) //////////////////////////////////////////////////////////// Keyboard::Key WindowImplWin32::virtualKeyCodeToSF(WPARAM key, LPARAM flags) { + // clang-format off switch (key) { // Check the scancode to distinguish between left and right shift @@ -1105,6 +1106,7 @@ Keyboard::Key WindowImplWin32::virtualKeyCodeToSF(WPARAM key, LPARAM flags) case '8': return Keyboard::Num8; case '9': return Keyboard::Num9; } + // clang-format on return Keyboard::Unknown; } diff --git a/test/System/FileInputStream.cpp b/test/System/FileInputStream.cpp index c169656a..ee95f870 100644 --- a/test/System/FileInputStream.cpp +++ b/test/System/FileInputStream.cpp @@ -48,6 +48,7 @@ public: // Prevent copies. TemporaryFile(const TemporaryFile&) = delete; + TemporaryFile& operator=(const TemporaryFile&) = delete; // Return the randomly generated path.