Merge pull request #1805 from dsnopek/4.4-cherrypicks-2a

Cherry-picks for the godot-cpp 4.4 branch - 2nd batch
This commit is contained in:
David Snopek
2025-07-03 08:53:10 -05:00
committed by GitHub
124 changed files with 8095 additions and 2351 deletions

View File

@@ -1,80 +1,105 @@
# Commented out parameters are those with the same value as base LLVM style.
# We can uncomment them if we want to change their value, or enforce the
# chosen value in case the base style changes (last sync: Clang 14.0).
---
### General config, applies to all languages ###
BasedOnStyle: LLVM
# chosen value in case the base style changes (last sync: Clang 17.0.6).
BasedOnStyle: LLVM
AccessModifierOffset: -4
AlignAfterOpenBracket: DontAlign
# AlignArrayOfStructures: None
# AlignConsecutiveMacros: None
# AlignConsecutiveAssignments: None
# AlignConsecutiveBitFields: None
# AlignConsecutiveDeclarations: None
# AlignConsecutiveAssignments:
# Enabled: false
# AcrossEmptyLines: false
# AcrossComments: false
# AlignCompound: false
# PadOperators: true
# AlignConsecutiveBitFields:
# Enabled: false
# AcrossEmptyLines: false
# AcrossComments: false
# AlignCompound: false
# PadOperators: false
# AlignConsecutiveDeclarations:
# Enabled: false
# AcrossEmptyLines: false
# AcrossComments: false
# AlignCompound: false
# PadOperators: false
# AlignConsecutiveMacros:
# Enabled: false
# AcrossEmptyLines: false
# AcrossComments: false
# AlignCompound: false
# PadOperators: false
# AlignConsecutiveShortCaseStatements:
# Enabled: false
# AcrossEmptyLines: false
# AcrossComments: false
# AlignCaseColons: false
# AlignEscapedNewlines: Right
AlignOperands: DontAlign
AlignTrailingComments: false
AlignOperands: DontAlign
AlignTrailingComments:
Kind: Never
OverEmptyLines: 0
# AllowAllArgumentsOnNextLine: true
AllowAllParametersOfDeclarationOnNextLine: false
# AllowShortEnumsOnASingleLine: true
# AllowShortBlocksOnASingleLine: Never
# AllowShortCaseLabelsOnASingleLine: false
# AllowShortFunctionsOnASingleLine: All
# AllowShortLambdasOnASingleLine: All
# AllowShortEnumsOnASingleLine: true
AllowShortFunctionsOnASingleLine: Inline
# AllowShortIfStatementsOnASingleLine: Never
# AllowShortLambdasOnASingleLine: All
# AllowShortLoopsOnASingleLine: false
# AlwaysBreakAfterDefinitionReturnType: None
# AlwaysBreakAfterReturnType: None
# AlwaysBreakBeforeMultilineStrings: false
# AlwaysBreakTemplateDeclarations: MultiLine
# AttributeMacros:
# - __capability
AttributeMacros:
- _ALWAYS_INLINE_
- _FORCE_INLINE_
- _NO_INLINE_
# BinPackArguments: true
# BinPackParameters: true
# BitFieldColonSpacing: Both
# BraceWrapping:
# AfterCaseLabel: false
# AfterClass: false
# AfterCaseLabel: false
# AfterClass: false
# AfterControlStatement: Never
# AfterEnum: false
# AfterFunction: false
# AfterNamespace: false
# AfterEnum: false
# AfterFunction: false
# AfterNamespace: false
# AfterObjCDeclaration: false
# AfterStruct: false
# AfterUnion: false
# AfterStruct: false
# AfterUnion: false
# AfterExternBlock: false
# BeforeCatch: false
# BeforeElse: false
# BeforeCatch: false
# BeforeElse: false
# BeforeLambdaBody: false
# BeforeWhile: false
# IndentBraces: false
# BeforeWhile: false
# IndentBraces: false
# SplitEmptyFunction: true
# SplitEmptyRecord: true
# SplitEmptyNamespace: true
# BreakAfterAttributes: Never
# BreakAfterJavaFieldAnnotations: false
# BreakArrays: true
# BreakBeforeBinaryOperators: None
# BreakBeforeConceptDeclarations: true
# BreakBeforeBraces: Attach
# BreakBeforeInheritanceComma: false
# BreakInheritanceList: BeforeColon
# BreakBeforeConceptDeclarations: Always
# BreakBeforeInlineASMColon: OnlyMultiline
# BreakBeforeTernaryOperators: true
# BreakConstructorInitializersBeforeComma: false
BreakConstructorInitializers: AfterColon
# BreakInheritanceList: BeforeColon
# BreakStringLiterals: true
ColumnLimit: 0
# CommentPragmas: '^ IWYU pragma:'
# QualifierAlignment: Leave
ColumnLimit: 0
# CommentPragmas: "^ IWYU pragma:"
# CompactNamespaces: false
ConstructorInitializerIndentWidth: 8
ContinuationIndentWidth: 8
Cpp11BracedListStyle: false
# DeriveLineEnding: true
# DerivePointerAlignment: false
# DisableFormat: false
# DisableFormat: false
# EmptyLineAfterAccessModifier: Never
# EmptyLineBeforeAccessModifier: LogicalBlock
# ExperimentalAutoDetectBinPacking: false
# PackConstructorInitializers: BinPack
ConstructorInitializerAllOnOneLineOrOnePerLine: true
# AllowAllConstructorInitializersOnNextLine: true
# FixNamespaceComments: true
# ForEachMacros:
# - foreach
@@ -82,34 +107,61 @@ ConstructorInitializerAllOnOneLineOrOnePerLine: true
# - BOOST_FOREACH
# IfMacros:
# - KJ_IF_MAYBE
# IncludeBlocks: Preserve
# IncludeBlocks: Preserve
IncludeCategories:
- Regex: '".*"'
Priority: 1
- Regex: '^<.*\.h>'
Priority: 2
- Regex: '^<.*'
Priority: 3
# IncludeIsMainRegex: '(Test)?$'
# IncludeIsMainSourceRegex: ''
- Regex: ^".*"$
Priority: 1
- Regex: ^<.*\.h>$
Priority: 2
- Regex: ^<.*>$
Priority: 3
# IncludeIsMainRegex: (Test)?$
# IncludeIsMainSourceRegex: ""
# IndentAccessModifiers: false
IndentCaseLabels: true
# IndentCaseBlocks: false
IndentCaseLabels: true
# IndentExternBlock: AfterExternBlock
# IndentGotoLabels: true
# IndentPPDirectives: None
# IndentExternBlock: AfterExternBlock
# IndentRequires: false
IndentWidth: 4
# IndentRequiresClause: true
IndentWidth: 4
# IndentWrappedFunctionNames: false
InsertBraces: true
# InsertNewlineAtEOF: false
# InsertTrailingCommas: None
# IntegerLiteralSeparator:
# Binary: 0
# BinaryMinDigits: 0
# Decimal: 0
# DecimalMinDigits: 0
# Hex: 0
# HexMinDigits: 0
JavaImportGroups:
- org.godotengine
- android
- androidx
- com.android
- com.google
- java
- javax
# JavaScriptQuotes: Leave
# JavaScriptWrapImports: true
# KeepEmptyLinesAtEOF: false
KeepEmptyLinesAtTheStartOfBlocks: false
# LambdaBodyIndentation: Signature
# MacroBlockBegin: ''
# MacroBlockEnd: ''
# Language: Cpp
# LineEnding: DeriveLF
# MacroBlockBegin: ""
# MacroBlockEnd: ""
# MaxEmptyLinesToKeep: 1
# NamespaceIndentation: None
# ObjCBinPackProtocolList: Auto
ObjCBlockIndentWidth: 4
# ObjCBreakBeforeNestedBlockParam: true
# ObjCSpaceAfterProperty: false
# ObjCSpaceBeforeProtocolList: true
# PPIndentWidth: -1
PackConstructorInitializers: NextLine
# PenaltyBreakAssignment: 2
# PenaltyBreakBeforeFirstCallParameter: 19
# PenaltyBreakComment: 300
@@ -118,82 +170,71 @@ KeepEmptyLinesAtTheStartOfBlocks: false
# PenaltyBreakString: 1000
# PenaltyBreakTemplateDeclaration: 10
# PenaltyExcessCharacter: 1000000
# PenaltyReturnTypeOnItsOwnLine: 60
# PenaltyIndentedWhitespace: 0
# PenaltyReturnTypeOnItsOwnLine: 60
# PointerAlignment: Right
# PPIndentWidth: -1
# QualifierAlignment: Leave
# ReferenceAlignment: Pointer
# ReflowComments: true
# ReflowComments: true
# RemoveBracesLLVM: false
# RemoveParentheses: Leave
RemoveSemicolon: true
# RequiresClausePosition: OwnLine
# RequiresExpressionIndentation: OuterScope
# SeparateDefinitionBlocks: Leave
# ShortNamespaceLines: 1
# SortIncludes: CaseSensitive
# SortIncludes: CaseSensitive
# SortJavaStaticImport: Before
# SortUsingDeclarations: true
# SortUsingDeclarations: LexicographicNumeric
# SpaceAfterCStyleCast: false
# SpaceAfterLogicalNot: false
# SpaceAfterTemplateKeyword: true
# SpaceAroundPointerQualifiers: Default
# SpaceBeforeAssignmentOperators: true
# SpaceBeforeCaseColon: false
# SpaceBeforeCpp11BracedList: false
# SpaceBeforeCtorInitializerColon: true
# SpaceBeforeInheritanceColon: true
# SpaceBeforeJsonColon: false
# SpaceBeforeParens: ControlStatements
# SpaceBeforeParensOptions:
# AfterControlStatements: true
# AfterForeachMacros: true
# AfterFunctionDefinitionName: false
# AfterFunctionDeclarationName: false
# AfterIfMacros: true
# AfterFunctionDefinitionName: false
# AfterIfMacros: true
# AfterOverloadedOperator: false
# AfterRequiresInClause: false
# AfterRequiresInExpression: false
# BeforeNonEmptyParentheses: false
# SpaceAroundPointerQualifiers: Default
# SpaceBeforeRangeBasedForLoopColon: true
# SpaceInEmptyBlock: false
# SpaceInEmptyParentheses: false
# SpacesBeforeTrailingComments: 1
# SpacesInAngles: Never
# SpacesInConditionalStatement: false
# SpacesInContainerLiterals: true
# SpacesInCStyleCastParentheses: false
## Godot TODO: We'll want to use a min of 1, but we need to see how to fix
## our comment capitalization at the same time.
SpacesInLineCommentPrefix:
Minimum: 0
Maximum: -1
# SpacesInParentheses: false
# SpacesInSquareBrackets: false
# SpaceBeforeSquareBrackets: false
# BitFieldColonSpacing: Both
# SpaceInEmptyBlock: false
# SpacesBeforeTrailingComments: 1
# SpacesInAngles: Never
# SpacesInContainerLiterals: true
SpacesInLineCommentPrefix:
Minimum: 0 # We want a minimum of 1 for comments, but allow 0 for disabled code.
Maximum: -1
# SpacesInParens: Never
# SpacesInParensOptions:
# InConditionalStatements: false
# InCStyleCasts: false
# InEmptyParentheses: false
# Other: false
# SpacesInSquareBrackets: false
Standard: c++20
# StatementAttributeLikeMacros:
# - Q_EMIT
# StatementMacros:
# - Q_UNUSED
# - QT_REQUIRE_VERSION
TabWidth: 4
# UseCRLF: false
UseTab: Always
TabWidth: 4
UseTab: Always
# VerilogBreakBetweenInstancePorts: true
# WhitespaceSensitiveMacros:
# - STRINGIZE
# - PP_STRINGIZE
# - BOOST_PP_STRINGIZE
# - NS_SWIFT_NAME
# - CF_SWIFT_NAME
---
### C++ specific config ###
Language: Cpp
Standard: c++17
---
### ObjC specific config ###
Language: ObjC
# ObjCBinPackProtocolList: Auto
ObjCBlockIndentWidth: 4
# ObjCBreakBeforeNestedBlockParam: true
# ObjCSpaceAfterProperty: false
# ObjCSpaceBeforeProtocolList: true
---
### Java specific config ###
Language: Java
# BreakAfterJavaFieldAnnotations: false
JavaImportGroups: ['org.godotengine', 'android', 'androidx', 'com.android', 'com.google', 'java', 'javax']
...
# - NS_SWIFT_NAME
# - PP_STRINGIZE
# - STRINGIZE

View File

@@ -6,11 +6,12 @@ end_of_line = lf
indent_size = 4
indent_style = tab
insert_final_newline = true
max_line_length = 120
trim_trailing_whitespace = true
[{*.py,SConstruct}]
indent_style = space
[*.{yml,yaml}]
[{*.{yml,yaml},.clang-format}]
indent_size = 2
indent_style = space

11
.git-blame-ignore-revs Normal file
View File

@@ -0,0 +1,11 @@
# This file contains a list of Git commit hashes that should be hidden from the
# regular Git history. Typically, this includes commits involving mass auto-formatting
# or other normalizations. Commit hashes *must* use the full 40-character notation.
# To apply the ignore list in your local Git client, you must run:
#
# git config blame.ignoreRevsFile .git-blame-ignore-revs
#
# This file is automatically used by GitHub.com's blame view.
# Style: Replace header guards with `#pragma once`
7056c996dd43ae1aa466c94d95cc2fe63853d8a9

View File

@@ -22,10 +22,14 @@ inputs:
ndk-version:
default: r23c
description: Android NDK version.
buildtool:
default: scons
description: scons or cmake
scons-version:
default: 4.4.0
description: SCons version.
runs:
using: composite
steps:
@@ -55,8 +59,13 @@ runs:
version: ${{ inputs.mingw-version }}
- name: Setup SCons
if: ${{ inputs.buildtool == 'scons' }}
shell: bash
run: |
python -c "import sys; print(sys.version)"
python -m pip install scons==${{ inputs.scons-version }}
scons --version
- name: Install Ninja
if: ${{ inputs.buildtool == 'cmake' }}
uses: ashutoshvarma/setup-ninja@master

186
.github/workflows/ci-cmake.yml vendored Normal file
View File

@@ -0,0 +1,186 @@
name: Continuous integration
on:
workflow_call:
env:
# Only used for the cache key. Increment version to force clean build.
GODOT_BASE_BRANCH: master
# Used to select the version of Godot to run the tests with.
GODOT_TEST_VERSION: master
# Use UTF-8 on Linux.
LANG: en_US.UTF-8
LC_ALL: en_US.UTF-8
concurrency:
group: ci-cmake-${{ github.actor }}-${{ github.head_ref || github.run_number }}-${{ github.ref }}
cancel-in-progress: true
jobs:
build-cmake:
name: ${{ matrix.name }}
runs-on: ${{ matrix.os }}
env:
EM_VERSION: 3.1.39
config-flags:
-DCMAKE_C_COMPILER_LAUNCHER=sccache
-DCMAKE_CXX_COMPILER_LAUNCHER=sccache
-DGODOTCPP_ENABLE_TESTING=ON
-DGODOTCPP_BUILD_PROFILE="test/build_profile.json"
SCCACHE_GHA_ENABLED: "true"
strategy:
fail-fast: false
matrix:
include:
- name: 🐧 Linux (GCC, Makefiles)
os: ubuntu-22.04
platform: linux
config-flags: -DCMAKE_BUILD_TYPE=Release
artifact-name: godot-cpp-linux-glibc2.27-x86_64-release.cmake
artifact-path: cmake-build/bin/libgodot-cpp.linux.template_release.x86_64.a
run-tests: true
- name: 🏁 Windows (x86_64, MSVC)
os: windows-2022
platform: windows
compiler: msvc
build-flags: --config Release
artifact-name: godot-cpp-windows-msvc2019-x86_64-release.cmake
artifact-path: cmake-build/bin/libgodot-cpp.windows.template_release.x86_64.lib
run-tests: false
- name: 🏁 Windows (x86_64, MinGW, Ninja)
os: windows-2022
platform: windows
compiler: mingw
config-flags:
-GNinja -DCMAKE_BUILD_TYPE=Release
-DCMAKE_CXX_COMPILER=cc -DCMAKE_CXX_COMPILER=c++
artifact-name: godot-cpp-linux-mingw-x86_64-release.cmake
artifact-path: cmake-build/bin/libgodot-cpp.windows.template_release.x86_64.a
run-tests: false
- name: 🍎 macOS (universal, Makefiles)
os: macos-latest
platform: macos
config-flags: -DCMAKE_OSX_ARCHITECTURES="arm64;x86_64"
artifact-name: godot-cpp-macos-universal-release.cmake
artifact-path: cmake-build/bin/libgodot-cpp.macos.template_release.universal.a
run-tests: false
- name: 🤖 Android (arm64, Ninja)
os: ubuntu-22.04
platform: android
config-flags:
-G Ninja -DCMAKE_BUILD_TYPE=Release
--toolchain ${ANDROID_HOME}/ndk/23.2.8568313/build/cmake/android.toolchain.cmake
-DANDROID_PLATFORM=21 -DANDROID_ABI=arm64-v8a
artifact-name: godot-cpp-android-arm64-release.cmake
artifact-path: cmake-build/bin/libgodot-cpp.android.template_release.arm64.a
flags: arch=arm64
run-tests: false
- name: 🍏 iOS (arm64, XCode)
os: macos-latest
platform: ios
config-flags:
-G Xcode
--toolchain cmake/ios.toolchain.cmake
-DPLATFORM=OS64
build-flags: --config Release
artifact-name: godot-cpp-ios-arm64-release.cmake
artifact-path: cmake-build/bin/libgodot-cpp.ios.template_release.arm64.a
flags: arch=arm64
run-tests: false
- name: 🌐 Web (wasm32, Ninja)
os: ubuntu-22.04
platform: web
config-flags:
-G Ninja -DCMAKE_BUILD_TYPE=Release
--toolchain ${EMSDK}/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake
artifact-name: godot-cpp-web-wasm32-release.cmake
artifact-path: cmake-build/bin/libgodot-cpp.web.template_release.wasm32.a
run-tests: false
steps:
- name: Checkout
uses: actions/checkout@v4
with:
submodules: recursive
- name: Run sccache-cache
uses: mozilla-actions/sccache-action@v0.0.9
- name: Setup godot-cpp
uses: ./.github/actions/setup-godot-cpp
with:
platform: ${{ matrix.platform }}
windows-compiler: ${{ matrix.compiler }}
buildtool: cmake
- name: Configure godot-cpp-test with template_debug
run: >
cmake --log-level=VERBOSE -S . -B cmake-build ${{ env.config-flags }} ${{ matrix.config-flags }}
- name: Build godot-cpp-test (template_debug)
run: >
cmake --build cmake-build --verbose --target godot-cpp-test ${{ matrix.build-flags }}
- name: Configure godot-cpp-test with template_release
run: >
cmake --fresh --log-level=VERBOSE -S . -B cmake-build
-DGODOTCPP_TARGET=template_release ${{ env.config-flags }} ${{ matrix.config-flags }}
- name: Build godot-cpp-test (template_release)
run: >
cmake --build cmake-build --verbose --target godot-cpp-test ${{ matrix.build-flags }}
- name: Run sccache stat for check
shell: bash
run: ${SCCACHE_PATH} --show-stats
- name: Download latest Godot artifacts
uses: dsnopek/action-download-artifact@1322f74e2dac9feed2ee76a32d9ae1ca3b4cf4e9
if: matrix.run-tests && env.GODOT_TEST_VERSION == 'master'
with:
repo: godotengine/godot
branch: master
event: push
workflow: linux_builds.yml
workflow_conclusion: success
name: linux-editor-mono
search_artifacts: true
check_artifacts: true
ensure_latest: true
path: godot-artifacts
- name: Prepare Godot artifacts for testing
if: matrix.run-tests && env.GODOT_TEST_VERSION == 'master'
run: |
chmod +x ./godot-artifacts/godot.linuxbsd.editor.x86_64.mono
echo "GODOT=$(pwd)/godot-artifacts/godot.linuxbsd.editor.x86_64.mono" >> $GITHUB_ENV
- name: Download requested Godot version for testing
if: matrix.run-tests && env.GODOT_TEST_VERSION != 'master'
run: |
wget "https://github.com/godotengine/godot-builds/releases/download/${GODOT_TEST_VERSION}/Godot_v${GODOT_TEST_VERSION}_linux.x86_64.zip" -O Godot.zip
unzip -a Godot.zip
chmod +x "Godot_v${GODOT_TEST_VERSION}_linux.x86_64"
echo "GODOT=$(pwd)/Godot_v${GODOT_TEST_VERSION}_linux.x86_64" >> $GITHUB_ENV
- name: Run tests
if: matrix.run-tests
run: |
$GODOT --headless --version
cd test
# Need to run the editor so .godot is generated... but it crashes! Ignore that :-)
(cd project && (timeout 30 $GODOT --import --headless >/dev/null 2>&1 || true))
./run-tests.sh
- name: Upload artifact
uses: actions/upload-artifact@v4
with:
name: ${{ matrix.artifact-name }}
path: ${{ matrix.artifact-path }}
if-no-files-found: error

View File

@@ -10,13 +10,15 @@ env:
# Use UTF-8 on Linux.
LANG: en_US.UTF-8
LC_ALL: en_US.UTF-8
# Use UTF-8 on Windows.
PYTHONIOENCODING: utf8
concurrency:
group: ci-${{ github.actor }}-${{ github.head_ref || github.run_number }}-${{ github.ref }}
group: ci-scons-${{ github.actor }}-${{ github.head_ref || github.run_number }}-${{ github.ref }}
cancel-in-progress: true
jobs:
build:
build-scons:
name: ${{ matrix.name }}
runs-on: ${{ matrix.os }}
strategy:
@@ -31,17 +33,8 @@ jobs:
run-tests: true
cache-name: linux-x86_64
- name: 🐧 Linux (GCC, Double Precision)
os: ubuntu-22.04
platform: linux
artifact-name: godot-cpp-linux-glibc2.27-x86_64-double-release
artifact-path: bin/libgodot-cpp.linux.template_release.double.x86_64.a
flags: precision=double
run-tests: false
cache-name: linux-x86_64-f64
- name: 🏁 Windows (x86_64, MSVC)
os: windows-2019
os: windows-2022
platform: windows
artifact-name: godot-cpp-windows-msvc2019-x86_64-release
artifact-path: bin/libgodot-cpp.windows.template_release.x86_64.lib
@@ -49,7 +42,7 @@ jobs:
cache-name: windows-x86_64-msvc
- name: 🏁 Windows (x86_64, MinGW)
os: windows-2019
os: windows-2022
platform: windows
artifact-name: godot-cpp-linux-mingw-x86_64-release
artifact-path: bin/libgodot-cpp.windows.template_release.x86_64.a
@@ -113,6 +106,7 @@ jobs:
with:
platform: ${{ matrix.platform }}
windows-compiler: ${{ contains(matrix.flags, 'use_mingw=yes') && 'mingw' || 'msvc' }}
buildtool: scons
- name: Generate godot-cpp sources only
run: |
@@ -183,40 +177,3 @@ jobs:
name: ${{ matrix.artifact-name }}
path: ${{ matrix.artifact-path }}
if-no-files-found: error
linux-cmake-ninja:
name: 🐧 Build (Linux, GCC, CMake Ninja)
runs-on: ubuntu-22.04
steps:
- name: Checkout
uses: actions/checkout@v4
with:
submodules: recursive
- name: Install dependencies
run: |
sudo apt-get update -qq
sudo apt-get install -qqq build-essential pkg-config cmake ninja-build
- name: Build test GDExtension library
run: |
mkdir cmake-build
cd cmake-build
cmake ../ -DGODOTCPP_ENABLE_TESTING=YES -DGODOTCPP_TARGET=template_release
cmake --build . --verbose -j $(nproc) --target godot-cpp-test --config Release
windows-msvc-cmake:
name: 🏁 Build (Windows, MSVC, CMake)
runs-on: windows-2019
steps:
- name: Checkout
uses: actions/checkout@v4
with:
submodules: recursive
- name: Build test GDExtension library
run: |
mkdir cmake-build
cd cmake-build
cmake ../ -DGODOTCPP_ENABLE_TESTING=YES -DGODOTCPP_TARGET=template_release
cmake --build . --verbose --target godot-cpp-test --config Release

View File

@@ -9,13 +9,58 @@ jobs:
# First stage: Only static checks, fast and prevent expensive builds from running.
static-checks:
if: '!vars.DISABLE_GODOT_CI'
name: 📊 Static Checks
if: '!vars.DISABLE_GODOT_CI'
uses: ./.github/workflows/static_checks.yml
# Second stage: Run all the builds and some of the tests.
ci:
name: 🛠️ Continuous Integration
# Second stage: Review code changes
changes:
name: Analyze Changes
needs: static-checks
uses: ./.github/workflows/ci.yml
runs-on: ubuntu-latest
outputs:
sources: ${{ steps.filter.outputs.sources_any_changed }}
scons: ${{ steps.filter.outputs.scons_any_changed }}
cmake: ${{ steps.filter.outputs.cmake_any_changed }}
steps:
- uses: actions/checkout@v4
with:
submodules: recursive
- uses: tj-actions/changed-files@v45
id: filter
with:
files_yaml: |
sources:
- '.github/workflows/*.yml'
- '**/*.py'
- '**/*.cpp'
- '**/*.hpp'
- '**/*.h'
- '**/*.inc'
- 'test/build_profile.json'
- 'gdextension/extension_api.json'
scons:
- '**/SConstruct'
- '**/SCsub'
cmake:
- '**/CMakeLists.txt'
- '**/*.cmake'
- name: echo sources changed
run: |
echo sources ${{ steps.filter.outputs.sources_any_modified }}
echo scons ${{ steps.filter.outputs.scons_any_modified }}
echo cmake ${{ steps.filter.outputs.cmake_any_modified }}
# Third stage: Run all the builds and some of the tests.
ci-scons:
name: 🛠️ SCons CI
needs: changes
if: ${{ needs.changes.outputs.scons == 'true' || needs.changes.outputs.sources == 'true' }}
uses: ./.github/workflows/ci-scons.yml
ci-cmake:
name: 🛠️ CMake CI
needs: changes
if: ${{ needs.changes.outputs.cmake == 'true' || needs.changes.outputs.sources == 'true' }}
uses: ./.github/workflows/ci-cmake.yml

View File

@@ -17,21 +17,13 @@ jobs:
fetch-depth: 2
- name: Get changed files
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
if [ "${{ github.event_name }}" == "pull_request" ]; then
files=$(git diff-tree --no-commit-id --name-only -r HEAD^1..HEAD 2> /dev/null || true)
elif [ "${{ github.event_name }}" == "push" -a "${{ github.event.forced }}" == "false" -a "${{ github.event.created }}" == "false" ]; then
files=$(git diff-tree --no-commit-id --name-only -r ${{ github.event.before }}..${{ github.event.after }} 2> /dev/null || true)
fi
files=$(echo "$files" | grep -v 'thirdparty' | xargs -I {} sh -c 'echo "\"./{}\""' | tr '\n' ' ')
echo "CHANGED_FILES=$files" >> $GITHUB_ENV
id: changed-files
uses: tj-actions/changed-files@v45
- name: Style checks via pre-commit
uses: pre-commit/action@v3.0.1
with:
extra_args: --verbose --hook-stage manual --files ${{ env.CHANGED_FILES }}
extra_args: --verbose --hook-stage manual --files ${{ steps.changed-files.outputs.all_changed_files }}
- name: Check generated files consistency
run:

View File

@@ -9,32 +9,36 @@ exclude: |
repos:
- repo: https://github.com/pre-commit/mirrors-clang-format
rev: v17.0.6
rev: v20.1.0
hooks:
- id: clang-format
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.4.4
rev: v0.11.4
hooks:
- id: ruff
args: [--fix]
files: (\.py|SConstruct)$
types_or: [text]
- id: ruff-format
files: (\.py|SConstruct)$
types_or: [text]
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.971
rev: v1.14.1 # Latest version that supports Python 3.8
hooks:
- id: mypy
files: \.py$
types_or: [text]
- repo: https://github.com/codespell-project/codespell
rev: v2.3.0
rev: v2.4.1
hooks:
- id: codespell
additional_dependencies: [tomli]
- repo: https://github.com/BlankSpruce/gersemi
rev: 0.18.2
rev: 0.19.2
hooks:
- id: gersemi
args: ["-i", "--no-warn-about-unknown-commands", "-l", "120"]

View File

@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.17)
cmake_minimum_required(VERSION 3.10...3.17)
#[=======================================================================[.rst:

View File

@@ -7,6 +7,8 @@
> from Godot's `master` branch.
>
> For users of stable branches, switch to the branch matching your target Godot version:
> - [`4.4`](https://github.com/godotengine/godot-cpp/tree/4.4)
> - [`4.3`](https://github.com/godotengine/godot-cpp/tree/4.3)
> - [`4.2`](https://github.com/godotengine/godot-cpp/tree/4.2)
> - [`4.1`](https://github.com/godotengine/godot-cpp/tree/4.1)
> - [`4.0`](https://github.com/godotengine/godot-cpp/tree/4.0)
@@ -49,20 +51,13 @@ Godot version.**
## Compatibility
> [!WARNING]
>
> The GDExtension API is brand new in Godot 4.0, and is still
considered in **beta** stage, despite Godot 4.0 itself being released.
>
> This applies to both the GDExtension interface header, the API JSON, and this
first-party `godot-cpp` extension.
>
> Some compatibility breakage is to be expected as GDExtension and `godot-cpp`
> get more used, documented, and critical issues get resolved. See the
> [Godot issue tracker](https://github.com/godotengine/godot/issues?q=is%3Aissue+is%3Aopen+label%3Atopic%3Agdextension)
> and the [godot-cpp issue tracker](https://github.com/godotengine/godot-cpp/issues)
> for a list of known issues, and be sure to provide feedback on issues and PRs
> which affect your use of this extension.
GDExtensions targeting an earlier version of Godot should work in later minor versions,
but not vice-versa. For example, a GDExtension targeting Godot 4.2 should work just fine
in Godot 4.3, but one targeting Godot 4.3 won't work in Godot 4.2.
There is one exception to this: extensions targeting Godot 4.0 will _not_ work with
Godot 4.1 and later.
See [Updating your GDExtension for 4.1](https://docs.godotengine.org/en/latest/tutorials/migrating/upgrading_to_godot_4.1.html#updating-your-gdextension-for-godot-4-1).
## Contributing
@@ -145,4 +140,4 @@ See the [godot-cpp-template](https://github.com/godotengine/godot-cpp-template)
generic reusable template.
Or checkout the code for the [Summator example](https://github.com/paddy-exe/GDExtensionSummator)
as shown in the [official documentation](https://docs.godotengine.org/en/latest/tutorials/scripting/gdextension/gdextension_cpp_example.html).
as shown in the [official documentation](https://docs.godotengine.org/en/latest/tutorials/scripting/cpp/gdextension_cpp_example.html).

View File

@@ -3,7 +3,7 @@
import os
EnsureSConsVersion(4, 0)
EnsurePythonVersion(3, 8)
try:
Import("env")

View File

@@ -51,11 +51,7 @@ virtual $RETVAL _##m_name($FUNCARGS) $CONST override; \\
def generate_wrappers(target):
max_versions = 12
txt = """
#ifndef GDEXTENSION_WRAPPERS_GEN_H
#define GDEXTENSION_WRAPPERS_GEN_H
"""
txt = "#pragma once"
for i in range(max_versions + 1):
txt += "\n/* Module Wrapper " + str(i) + " Arguments */\n"
@@ -64,8 +60,6 @@ def generate_wrappers(target):
txt += generate_mod_version(i, True, False)
txt += generate_mod_version(i, True, True)
txt += "\n#endif\n"
with open(target, "w", encoding="utf-8") as f:
f.write(txt)
@@ -187,8 +181,7 @@ def generate_virtuals(target):
max_versions = 12
txt = """/* THIS FILE IS GENERATED DO NOT EDIT */
#ifndef GDEXTENSION_GDVIRTUAL_GEN_H
#define GDEXTENSION_GDVIRTUAL_GEN_H
#pragma once
"""
@@ -203,8 +196,6 @@ def generate_virtuals(target):
txt += generate_virtual_version(i, True, False, True)
txt += generate_virtual_version(i, True, True, True)
txt += "#endif // GDEXTENSION_GDVIRTUAL_GEN_H\n"
with open(target, "w", encoding="utf-8") as f:
f.write(txt)
@@ -364,11 +355,8 @@ def generate_builtin_bindings(api, output_dir, build_config):
variant_size_source = []
add_header("variant_size.hpp", variant_size_source)
header_guard = "GODOT_CPP_VARIANT_SIZE_HPP"
variant_size_source.append(f"#ifndef {header_guard}")
variant_size_source.append(f"#define {header_guard}")
variant_size_source.append(f'#define GODOT_CPP_VARIANT_SIZE {builtin_sizes["Variant"]}')
variant_size_source.append(f"#endif // ! {header_guard}")
variant_size_source.append("#pragma once")
variant_size_source.append(f"#define GODOT_CPP_VARIANT_SIZE {builtin_sizes['Variant']}")
variant_size_file.write("\n".join(variant_size_source))
@@ -448,8 +436,7 @@ def generate_builtin_bindings(api, output_dir, build_config):
builtin_header = []
add_header("builtin_types.hpp", builtin_header)
builtin_header.append("#ifndef GODOT_CPP_BUILTIN_TYPES_HPP")
builtin_header.append("#define GODOT_CPP_BUILTIN_TYPES_HPP")
builtin_header.append("#pragma once")
builtin_header.append("")
@@ -464,8 +451,6 @@ def generate_builtin_bindings(api, output_dir, build_config):
builtin_header.append("")
builtin_header.append("#endif // ! GODOT_CPP_BUILTIN_TYPES_HPP")
builtin_header_file.write("\n".join(builtin_header))
# Create a header with bindings for builtin types.
@@ -474,8 +459,7 @@ def generate_builtin_bindings(api, output_dir, build_config):
builtin_binds = []
add_header("builtin_binds.hpp", builtin_binds)
builtin_binds.append("#ifndef GODOT_CPP_BUILTIN_BINDS_HPP")
builtin_binds.append("#define GODOT_CPP_BUILTIN_BINDS_HPP")
builtin_binds.append("#pragma once")
builtin_binds.append("")
builtin_binds.append("#include <godot_cpp/variant/builtin_types.hpp>")
builtin_binds.append("")
@@ -487,7 +471,6 @@ def generate_builtin_bindings(api, output_dir, build_config):
builtin_binds.append(f"VARIANT_ENUM_CAST({builtin_api['name']}::{enum_api['name']});")
builtin_binds.append("")
builtin_binds.append("#endif // ! GODOT_CPP_BUILTIN_BINDS_HPP")
builtin_binds_file.write("\n".join(builtin_binds))
@@ -503,9 +486,7 @@ def generate_builtin_class_vararg_method_implements_header(builtin_classes):
add_header("builtin_vararg_methods.hpp", result)
header_guard = "GODOT_CPP_BUILTIN_VARARG_METHODS_HPP"
result.append(f"#ifndef {header_guard}")
result.append(f"#define {header_guard}")
result.append("#pragma once")
result.append("")
for builtin_api in builtin_classes:
if "methods" not in builtin_api:
@@ -520,8 +501,6 @@ def generate_builtin_class_vararg_method_implements_header(builtin_classes):
)
result.append("")
result.append(f"#endif // ! {header_guard}")
return "\n".join(result)
@@ -531,12 +510,9 @@ def generate_builtin_class_header(builtin_api, size, used_classes, fully_used_cl
class_name = builtin_api["name"]
snake_class_name = camel_to_snake(class_name).upper()
header_guard = f"GODOT_CPP_{snake_class_name}_HPP"
add_header(f"{snake_class_name.lower()}.hpp", result)
result.append(f"#ifndef {header_guard}")
result.append(f"#define {header_guard}")
result.append("#pragma once")
result.append("")
result.append("#include <godot_cpp/core/defs.hpp>")
@@ -565,7 +541,7 @@ def generate_builtin_class_header(builtin_api, size, used_classes, fully_used_cl
result.append("#include <godot_cpp/variant/vector4.hpp>")
result.append("")
if is_packed_array(class_name):
if is_packed_array(class_name) or class_name == "Array":
result.append("#include <godot_cpp/core/error_macros.hpp>")
result.append("#include <initializer_list>")
result.append("")
@@ -625,19 +601,19 @@ def generate_builtin_class_header(builtin_api, size, used_classes, fully_used_cl
if "constructors" in builtin_api:
for constructor in builtin_api["constructors"]:
result.append(f'\t\tGDExtensionPtrConstructor constructor_{constructor["index"]};')
result.append(f"\t\tGDExtensionPtrConstructor constructor_{constructor['index']};")
if builtin_api["has_destructor"]:
result.append("\t\tGDExtensionPtrDestructor destructor;")
if "methods" in builtin_api:
for method in builtin_api["methods"]:
result.append(f'\t\tGDExtensionPtrBuiltInMethod method_{method["name"]};')
result.append(f"\t\tGDExtensionPtrBuiltInMethod method_{method['name']};")
if "members" in builtin_api:
for member in builtin_api["members"]:
result.append(f'\t\tGDExtensionPtrSetter member_{member["name"]}_setter;')
result.append(f'\t\tGDExtensionPtrGetter member_{member["name"]}_getter;')
result.append(f"\t\tGDExtensionPtrSetter member_{member['name']}_setter;")
result.append(f"\t\tGDExtensionPtrGetter member_{member['name']}_getter;")
if "indexing_return_type" in builtin_api:
result.append("\t\tGDExtensionPtrIndexedSetter indexed_setter;")
@@ -652,10 +628,10 @@ def generate_builtin_class_header(builtin_api, size, used_classes, fully_used_cl
for operator in builtin_api["operators"]:
if "right_type" in operator:
result.append(
f'\t\tGDExtensionPtrOperatorEvaluator operator_{get_operator_id_name(operator["name"])}_{operator["right_type"]};'
f"\t\tGDExtensionPtrOperatorEvaluator operator_{get_operator_id_name(operator['name'])}_{operator['right_type']};"
)
else:
result.append(f'\t\tGDExtensionPtrOperatorEvaluator operator_{get_operator_id_name(operator["name"])};')
result.append(f"\t\tGDExtensionPtrOperatorEvaluator operator_{get_operator_id_name(operator['name'])};")
result.append("\t} _method_bindings;")
@@ -666,6 +642,11 @@ def generate_builtin_class_header(builtin_api, size, used_classes, fully_used_cl
result.append("")
result.append(f"\t{class_name}(const Variant *p_variant);")
if class_name == "Array":
result.append("")
result.append("\tconst Variant *ptr() const;")
result.append("\tVariant *ptrw();")
result.append("")
result.append("public:")
@@ -712,12 +693,12 @@ def generate_builtin_class_header(builtin_api, size, used_classes, fully_used_cl
if class_name == "Vector3" and constant["name"].startswith("AXIS"):
if axis_constants_count == 0:
result.append("\tenum Axis {")
result.append(f'\t\t{constant["name"]} = {constant["value"]},')
result.append(f"\t\t{constant['name']} = {constant['value']},")
axis_constants_count += 1
if axis_constants_count == 3:
result.append("\t};")
else:
result.append(f'\tstatic const {correct_type(constant["type"])} {constant["name"]};')
result.append(f"\tstatic const {correct_type(constant['type'])} {constant['name']};")
if builtin_api["has_destructor"]:
result.append(f"\t~{class_name}();")
@@ -737,13 +718,13 @@ def generate_builtin_class_header(builtin_api, size, used_classes, fully_used_cl
method_signature += "static "
if "return_type" in method:
method_signature += f'{correct_type(method["return_type"])}'
method_signature += f"{correct_type(method['return_type'])}"
if not method_signature.endswith("*"):
method_signature += " "
else:
method_signature += "void "
method_signature += f'{method["name"]}('
method_signature += f"{method['name']}("
method_arguments = []
if "arguments" in method:
@@ -778,21 +759,21 @@ def generate_builtin_class_header(builtin_api, size, used_classes, fully_used_cl
if "members" in builtin_api:
for member in builtin_api["members"]:
if f'get_{member["name"]}' not in method_list:
result.append(f'\t{correct_type(member["type"])} get_{member["name"]}() const;')
if f'set_{member["name"]}' not in method_list:
result.append(f'\tvoid set_{member["name"]}({type_for_parameter(member["type"])}value);')
if f"get_{member['name']}" not in method_list:
result.append(f"\t{correct_type(member['type'])} get_{member['name']}() const;")
if f"set_{member['name']}" not in method_list:
result.append(f"\tvoid set_{member['name']}({type_for_parameter(member['type'])}value);")
if "operators" in builtin_api:
for operator in builtin_api["operators"]:
if is_valid_cpp_operator(operator["name"]):
if "right_type" in operator:
result.append(
f'\t{correct_type(operator["return_type"])} operator{get_operator_cpp_name(operator["name"])}({type_for_parameter(operator["right_type"])}p_other) const;'
f"\t{correct_type(operator['return_type'])} operator{get_operator_cpp_name(operator['name'])}({type_for_parameter(operator['right_type'])}p_other) const;"
)
else:
result.append(
f'\t{correct_type(operator["return_type"])} operator{get_operator_cpp_name(operator["name"])}() const;'
f"\t{correct_type(operator['return_type'])} operator{get_operator_cpp_name(operator['name'])}() const;"
)
# Copy assignment.
@@ -931,6 +912,48 @@ def generate_builtin_class_header(builtin_api, size, used_classes, fully_used_cl
result.append("\tVariant &operator[](int64_t p_index);")
result.append("\tvoid set_typed(uint32_t p_type, const StringName &p_class_name, const Variant &p_script);")
result.append("\tvoid _ref(const Array &p_from) const;")
result.append("""
struct Iterator {
_FORCE_INLINE_ Variant &operator*() const;
_FORCE_INLINE_ Variant *operator->() const;
_FORCE_INLINE_ Iterator &operator++();
_FORCE_INLINE_ Iterator &operator--();
_FORCE_INLINE_ bool operator==(const Iterator &b) const { return elem_ptr == b.elem_ptr; }
_FORCE_INLINE_ bool operator!=(const Iterator &b) const { return elem_ptr != b.elem_ptr; }
Iterator(Variant *p_ptr) { elem_ptr = p_ptr; }
Iterator() {}
Iterator(const Iterator &p_it) { elem_ptr = p_it.elem_ptr; }
private:
Variant *elem_ptr = nullptr;
};
struct ConstIterator {
_FORCE_INLINE_ const Variant &operator*() const;
_FORCE_INLINE_ const Variant *operator->() const;
_FORCE_INLINE_ ConstIterator &operator++();
_FORCE_INLINE_ ConstIterator &operator--();
_FORCE_INLINE_ bool operator==(const ConstIterator &b) const { return elem_ptr == b.elem_ptr; }
_FORCE_INLINE_ bool operator!=(const ConstIterator &b) const { return elem_ptr != b.elem_ptr; }
ConstIterator(const Variant *p_ptr) { elem_ptr = p_ptr; }
ConstIterator() {}
ConstIterator(const ConstIterator &p_it) { elem_ptr = p_it.elem_ptr; }
private:
const Variant *elem_ptr = nullptr;
};
_FORCE_INLINE_ Iterator begin();
_FORCE_INLINE_ Iterator end();
_FORCE_INLINE_ ConstIterator begin() const;
_FORCE_INLINE_ ConstIterator end() const;
""")
result.append("\t_FORCE_INLINE_ Array(std::initializer_list<Variant> p_init);")
if class_name == "Dictionary":
result.append("\tconst Variant &operator[](const Variant &p_key) const;")
@@ -965,8 +988,6 @@ def generate_builtin_class_header(builtin_api, size, used_classes, fully_used_cl
result.append("")
result.append("} // namespace godot")
result.append("")
result.append(f"#endif // ! {header_guard}")
result.append("")
return "\n".join(result)
@@ -1020,7 +1041,7 @@ def generate_builtin_class_source(builtin_api, size, used_classes, fully_used_cl
if "constructors" in builtin_api:
for constructor in builtin_api["constructors"]:
result.append(
f'\t_method_bindings.constructor_{constructor["index"]} = internal::gdextension_interface_variant_get_ptr_constructor({enum_type_name}, {constructor["index"]});'
f"\t_method_bindings.constructor_{constructor['index']} = internal::gdextension_interface_variant_get_ptr_constructor({enum_type_name}, {constructor['index']});"
)
if builtin_api["has_destructor"]:
@@ -1044,17 +1065,17 @@ def generate_builtin_class_source(builtin_api, size, used_classes, fully_used_cl
# TODO: Add error check for hash mismatch.
result.append(f'\t_gde_name = StringName("{method["name"]}");')
result.append(
f'\t_method_bindings.method_{method["name"]} = internal::gdextension_interface_variant_get_ptr_builtin_method({enum_type_name}, _gde_name._native_ptr(), {method["hash"]});'
f"\t_method_bindings.method_{method['name']} = internal::gdextension_interface_variant_get_ptr_builtin_method({enum_type_name}, _gde_name._native_ptr(), {method['hash']});"
)
if "members" in builtin_api:
for member in builtin_api["members"]:
result.append(f'\t_gde_name = StringName("{member["name"]}");')
result.append(
f'\t_method_bindings.member_{member["name"]}_setter = internal::gdextension_interface_variant_get_ptr_setter({enum_type_name}, _gde_name._native_ptr());'
f"\t_method_bindings.member_{member['name']}_setter = internal::gdextension_interface_variant_get_ptr_setter({enum_type_name}, _gde_name._native_ptr());"
)
result.append(
f'\t_method_bindings.member_{member["name"]}_getter = internal::gdextension_interface_variant_get_ptr_getter({enum_type_name}, _gde_name._native_ptr());'
f"\t_method_bindings.member_{member['name']}_getter = internal::gdextension_interface_variant_get_ptr_getter({enum_type_name}, _gde_name._native_ptr());"
)
if "indexing_return_type" in builtin_api:
@@ -1086,11 +1107,11 @@ def generate_builtin_class_source(builtin_api, size, used_classes, fully_used_cl
f"GDEXTENSION_VARIANT_TYPE_{camel_to_snake(operator['right_type']).upper()}"
)
result.append(
f'\t_method_bindings.operator_{get_operator_id_name(operator["name"])}_{operator["right_type"]} = internal::gdextension_interface_variant_get_ptr_operator_evaluator(GDEXTENSION_VARIANT_OP_{get_operator_id_name(operator["name"]).upper()}, {enum_type_name}, {right_type_variant_type});'
f"\t_method_bindings.operator_{get_operator_id_name(operator['name'])}_{operator['right_type']} = internal::gdextension_interface_variant_get_ptr_operator_evaluator(GDEXTENSION_VARIANT_OP_{get_operator_id_name(operator['name']).upper()}, {enum_type_name}, {right_type_variant_type});"
)
else:
result.append(
f'\t_method_bindings.operator_{get_operator_id_name(operator["name"])} = internal::gdextension_interface_variant_get_ptr_operator_evaluator(GDEXTENSION_VARIANT_OP_{get_operator_id_name(operator["name"]).upper()}, {enum_type_name}, GDEXTENSION_VARIANT_TYPE_NIL);'
f"\t_method_bindings.operator_{get_operator_id_name(operator['name'])} = internal::gdextension_interface_variant_get_ptr_operator_evaluator(GDEXTENSION_VARIANT_OP_{get_operator_id_name(operator['name']).upper()}, {enum_type_name}, GDEXTENSION_VARIANT_TYPE_NIL);"
)
result.append("}")
@@ -1115,7 +1136,7 @@ def generate_builtin_class_source(builtin_api, size, used_classes, fully_used_cl
result.append(method_signature)
method_call = (
f'\tinternal::_call_builtin_constructor(_method_bindings.constructor_{constructor["index"]}, &opaque'
f"\tinternal::_call_builtin_constructor(_method_bindings.constructor_{constructor['index']}, &opaque"
)
if "arguments" in constructor:
if len(constructor["arguments"]) == 1 and constructor["arguments"][0]["type"] == class_name:
@@ -1183,7 +1204,7 @@ def generate_builtin_class_source(builtin_api, size, used_classes, fully_used_cl
method_call += f"return internal::_call_builtin_method_ptr_ret_obj<{return_type}>("
else:
method_call += "internal::_call_builtin_method_ptr_no_ret("
method_call += f'_method_bindings.method_{method["name"]}, '
method_call += f"_method_bindings.method_{method['name']}, "
if "is_static" in method and method["is_static"]:
method_call += "nullptr"
else:
@@ -1212,19 +1233,19 @@ def generate_builtin_class_source(builtin_api, size, used_classes, fully_used_cl
if "members" in builtin_api:
for member in builtin_api["members"]:
if f'get_{member["name"]}' not in method_list:
result.append(f'{correct_type(member["type"])} {class_name}::get_{member["name"]}() const {{')
if f"get_{member['name']}" not in method_list:
result.append(f"{correct_type(member['type'])} {class_name}::get_{member['name']}() const {{")
result.append(
f'\treturn internal::_call_builtin_ptr_getter<{correct_type(member["type"])}>(_method_bindings.member_{member["name"]}_getter, (GDExtensionConstTypePtr)&opaque);'
f"\treturn internal::_call_builtin_ptr_getter<{correct_type(member['type'])}>(_method_bindings.member_{member['name']}_getter, (GDExtensionConstTypePtr)&opaque);"
)
result.append("}")
if f'set_{member["name"]}' not in method_list:
result.append(f'void {class_name}::set_{member["name"]}({type_for_parameter(member["type"])}value) {{')
if f"set_{member['name']}" not in method_list:
result.append(f"void {class_name}::set_{member['name']}({type_for_parameter(member['type'])}value) {{")
(encode, arg_name) = get_encoded_arg("value", member["type"], None)
result += encode
result.append(
f'\t_method_bindings.member_{member["name"]}_setter((GDExtensionConstTypePtr)&opaque, (GDExtensionConstTypePtr){arg_name});'
f"\t_method_bindings.member_{member['name']}_setter((GDExtensionConstTypePtr)&opaque, (GDExtensionConstTypePtr){arg_name});"
)
result.append("}")
@@ -1235,20 +1256,20 @@ def generate_builtin_class_source(builtin_api, size, used_classes, fully_used_cl
if is_valid_cpp_operator(operator["name"]):
if "right_type" in operator:
result.append(
f'{correct_type(operator["return_type"])} {class_name}::operator{get_operator_cpp_name(operator["name"])}({type_for_parameter(operator["right_type"])}p_other) const {{'
f"{correct_type(operator['return_type'])} {class_name}::operator{get_operator_cpp_name(operator['name'])}({type_for_parameter(operator['right_type'])}p_other) const {{"
)
(encode, arg_name) = get_encoded_arg("other", operator["right_type"], None)
result += encode
result.append(
f'\treturn internal::_call_builtin_operator_ptr<{get_gdextension_type(correct_type(operator["return_type"]))}>(_method_bindings.operator_{get_operator_id_name(operator["name"])}_{operator["right_type"]}, (GDExtensionConstTypePtr)&opaque, (GDExtensionConstTypePtr){arg_name});'
f"\treturn internal::_call_builtin_operator_ptr<{get_gdextension_type(correct_type(operator['return_type']))}>(_method_bindings.operator_{get_operator_id_name(operator['name'])}_{operator['right_type']}, (GDExtensionConstTypePtr)&opaque, (GDExtensionConstTypePtr){arg_name});"
)
result.append("}")
else:
result.append(
f'{correct_type(operator["return_type"])} {class_name}::operator{get_operator_cpp_name(operator["name"])}() const {{'
f"{correct_type(operator['return_type'])} {class_name}::operator{get_operator_cpp_name(operator['name'])}() const {{"
)
result.append(
f'\treturn internal::_call_builtin_operator_ptr<{get_gdextension_type(correct_type(operator["return_type"]))}>(_method_bindings.operator_{get_operator_id_name(operator["name"])}, (GDExtensionConstTypePtr)&opaque, (GDExtensionConstTypePtr) nullptr);'
f"\treturn internal::_call_builtin_operator_ptr<{get_gdextension_type(correct_type(operator['return_type']))}>(_method_bindings.operator_{get_operator_id_name(operator['name'])}, (GDExtensionConstTypePtr)&opaque, (GDExtensionConstTypePtr) nullptr);"
)
result.append("}")
result.append("")
@@ -1370,7 +1391,7 @@ def generate_engine_classes_bindings(api, output_dir, use_template_get_node):
fully_used_classes.add(dict_type_name)
else:
used_classes.add(dict_type_name)
dict_type_name = dict_type_names[2]
dict_type_name = dict_type_names[1]
if dict_type_name.endswith("*"):
dict_type_name = dict_type_name[:-1]
if is_included(dict_type_name, class_name):
@@ -1425,7 +1446,7 @@ def generate_engine_classes_bindings(api, output_dir, use_template_get_node):
fully_used_classes.add(dict_type_name)
else:
used_classes.add(dict_type_name)
dict_type_name = dict_type_names[2]
dict_type_name = dict_type_names[1]
if dict_type_name.endswith("*"):
dict_type_name = dict_type_name[:-1]
if is_included(dict_type_name, class_name):
@@ -1498,9 +1519,7 @@ def generate_engine_classes_bindings(api, output_dir, use_template_get_node):
result = []
add_header(f"{snake_struct_name}.hpp", result)
header_guard = f"GODOT_CPP_{snake_struct_name.upper()}_HPP"
result.append(f"#ifndef {header_guard}")
result.append(f"#define {header_guard}")
result.append("#pragma once")
used_classes = []
expanded_format = native_struct["format"].replace("(", " ").replace(")", ";").replace(",", ";")
@@ -1540,7 +1559,6 @@ def generate_engine_classes_bindings(api, output_dir, use_template_get_node):
result.append("")
result.append("} // namespace godot")
result.append("")
result.append(f"#endif // ! {header_guard}")
with header_filename.open("w+", encoding="utf-8") as header_file:
header_file.write("\n".join(result))
@@ -1556,11 +1574,7 @@ def generate_engine_class_header(class_api, used_classes, fully_used_classes, us
add_header(f"{snake_class_name.lower()}.hpp", result)
header_guard = f"GODOT_CPP_{snake_class_name}_HPP"
result.append(f"#ifndef {header_guard}")
result.append(f"#define {header_guard}")
result.append("#pragma once")
result.append("")
if len(fully_used_classes) > 0:
@@ -1624,12 +1638,12 @@ def generate_engine_class_header(class_api, used_classes, fully_used_classes, us
if "enums" in class_api:
for enum_api in class_api["enums"]:
if enum_api["is_bitfield"]:
result.append(f'\tenum {enum_api["name"]} : uint64_t {{')
result.append(f"\tenum {enum_api['name']} : uint64_t {{")
else:
result.append(f'\tenum {enum_api["name"]} {{')
result.append(f"\tenum {enum_api['name']} {{")
for value in enum_api["values"]:
result.append(f'\t\t{value["name"]} = {value["value"]},')
result.append(f"\t\t{value['name']} = {value['value']},")
result.append("\t};")
result.append("")
@@ -1637,7 +1651,7 @@ def generate_engine_class_header(class_api, used_classes, fully_used_classes, us
for value in class_api["constants"]:
if "type" not in value:
value["type"] = "int"
result.append(f'\tstatic const {value["type"]} {value["name"]} = {value["value"]};')
result.append(f"\tstatic const {value['type']} {value['name']} = {value['value']};")
result.append("")
if is_singleton:
@@ -1709,6 +1723,16 @@ def generate_engine_class_header(class_api, used_classes, fully_used_classes, us
result.append(f"\t~{class_name}();")
result.append("")
if class_name == "Object":
result.append('\tString _to_string() const { return "<" + get_class() + "#" + itos(get_instance_id()) + ">"; }')
result.append("")
if class_name == "Node":
result.append(
'\tString _to_string() const { return (!get_name().is_empty() ? String(get_name()) + ":" : "") + Object::_to_string(); }'
)
result.append("")
result.append("public:")
# Special cases.
@@ -1760,9 +1784,9 @@ def generate_engine_class_header(class_api, used_classes, fully_used_classes, us
if "enums" in class_api and class_name != "Object":
for enum_api in class_api["enums"]:
if enum_api["is_bitfield"]:
result.append(f'VARIANT_BITFIELD_CAST({class_name}::{enum_api["name"]});')
result.append(f"VARIANT_BITFIELD_CAST({class_name}::{enum_api['name']});")
else:
result.append(f'VARIANT_ENUM_CAST({class_name}::{enum_api["name"]});')
result.append(f"VARIANT_ENUM_CAST({class_name}::{enum_api['name']});")
result.append("")
if class_name == "ClassDBSingleton":
@@ -1771,12 +1795,12 @@ def generate_engine_class_header(class_api, used_classes, fully_used_classes, us
if "enums" in class_api:
for enum_api in class_api["enums"]:
if enum_api["is_bitfield"]:
result.append(f'\tenum {enum_api["name"]} : uint64_t {{ \\')
result.append(f"\tenum {enum_api['name']} : uint64_t {{ \\")
else:
result.append(f'\tenum {enum_api["name"]} {{ \\')
result.append(f"\tenum {enum_api['name']} {{ \\")
for value in enum_api["values"]:
result.append(f'\t\t{value["name"]} = {value["value"]}, \\')
result.append(f"\t\t{value['name']} = {value['value']}, \\")
result.append("\t}; \\")
result.append("\t \\")
@@ -1807,7 +1831,7 @@ def generate_engine_class_header(class_api, used_classes, fully_used_classes, us
else:
method_signature += "void "
method_signature += f'{method["name"]}('
method_signature += f"{method['name']}("
method_arguments = []
if "arguments" in method:
@@ -1826,7 +1850,7 @@ def generate_engine_class_header(class_api, used_classes, fully_used_classes, us
method_body += "return "
if "alias_for" in class_api and return_type.startswith(class_api["alias_for"] + "::"):
method_body += f"({return_type})"
method_body += f'ClassDBSingleton::get_singleton()->{method["name"]}('
method_body += f"ClassDBSingleton::get_singleton()->{method['name']}("
method_body += ", ".join(map(lambda x: escape_argument(x["name"]), method_arguments))
if vararg:
method_body += ", p_args..."
@@ -1842,14 +1866,13 @@ def generate_engine_class_header(class_api, used_classes, fully_used_classes, us
if "enums" in class_api:
for enum_api in class_api["enums"]:
if enum_api["is_bitfield"]:
result.append(f'\tVARIANT_BITFIELD_CAST({class_api["alias_for"]}::{enum_api["name"]}); \\')
result.append(f"\tVARIANT_BITFIELD_CAST({class_api['alias_for']}::{enum_api['name']}); \\")
else:
result.append(f'\tVARIANT_ENUM_CAST({class_api["alias_for"]}::{enum_api["name"]}); \\')
result.append(f"\tVARIANT_ENUM_CAST({class_api['alias_for']}::{enum_api['name']}); \\")
result.append("\t")
result.append("")
result.append(f"#endif // ! {header_guard}")
result.append("")
return "\n".join(result)
@@ -1942,7 +1965,7 @@ def generate_engine_class_source(class_api, used_classes, fully_used_classes, us
if has_return:
result.append(
f'\tCHECK_METHOD_BIND_RET(_gde_method_bind, {get_default_value_for_type(method["return_value"]["type"])});'
f"\tCHECK_METHOD_BIND_RET(_gde_method_bind, ({get_default_value_for_type(method['return_value']['type'])}));"
)
else:
result.append("\tCHECK_METHOD_BIND(_gde_method_bind);")
@@ -2024,7 +2047,7 @@ def generate_engine_class_source(class_api, used_classes, fully_used_classes, us
method_signature += " {"
if "return_value" in method and correct_type(method["return_value"]["type"]) != "void":
result.append(method_signature)
result.append(f'\treturn {get_default_value_for_type(method["return_value"]["type"])};')
result.append(f"\treturn {get_default_value_for_type(method['return_value']['type'])};")
result.append("}")
else:
method_signature += "}"
@@ -2051,9 +2074,7 @@ def generate_global_constants(api, output_dir):
header_filename = include_gen_folder / "global_constants.hpp"
header_guard = "GODOT_CPP_GLOBAL_CONSTANTS_HPP"
header.append(f"#ifndef {header_guard}")
header.append(f"#define {header_guard}")
header.append("#pragma once")
header.append("")
header.append("#include <cstdint>")
header.append("")
@@ -2062,7 +2083,7 @@ def generate_global_constants(api, output_dir):
if len(api["global_constants"]) > 0:
for constant in api["global_constants"]:
header.append(f'const int64_t {escape_identifier(constant["name"])} = {constant["value"]};')
header.append(f"const int64_t {escape_identifier(constant['name'])} = {constant['value']};")
header.append("")
@@ -2071,19 +2092,18 @@ def generate_global_constants(api, output_dir):
continue
if enum_def["is_bitfield"]:
header.append(f'enum {enum_def["name"]} : uint64_t {{')
header.append(f"enum {enum_def['name']} : uint64_t {{")
else:
header.append(f'enum {enum_def["name"]} {{')
header.append(f"enum {enum_def['name']} {{")
for value in enum_def["values"]:
header.append(f'\t{value["name"]} = {value["value"]},')
header.append(f"\t{value['name']} = {value['value']},")
header.append("};")
header.append("")
header.append("} // namespace godot")
header.append("")
header.append(f"#endif // ! {header_guard}")
with header_filename.open("w+", encoding="utf-8") as header_file:
header_file.write("\n".join(header))
@@ -2099,19 +2119,15 @@ def generate_version_header(api, output_dir):
header_file_path = include_gen_folder / header_filename
header_guard = "GODOT_CPP_VERSION_HPP"
header.append(f"#ifndef {header_guard}")
header.append(f"#define {header_guard}")
header.append("#pragma once")
header.append("")
header.append(f"#define GODOT_VERSION_MAJOR {api['header']['version_major']}")
header.append(f"#define GODOT_VERSION_MINOR {api['header']['version_minor']}")
header.append(f"#define GODOT_VERSION_PATCH {api['header']['version_patch']}")
header.append(f"#define GODOT_VERSION_STATUS \"{api['header']['version_status']}\"")
header.append(f"#define GODOT_VERSION_BUILD \"{api['header']['version_build']}\"")
header.append(f'#define GODOT_VERSION_STATUS "{api["header"]["version_status"]}"')
header.append(f'#define GODOT_VERSION_BUILD "{api["header"]["version_build"]}"')
header.append("")
header.append(f"#endif // {header_guard}")
header.append("")
with header_file_path.open("w+", encoding="utf-8") as header_file:
@@ -2132,9 +2148,7 @@ def generate_global_constant_binds(api, output_dir):
header_filename = include_gen_folder / "global_constants_binds.hpp"
header_guard = "GODOT_CPP_GLOBAL_CONSTANTS_BINDS_HPP"
header.append(f"#ifndef {header_guard}")
header.append(f"#define {header_guard}")
header.append("#pragma once")
header.append("")
header.append("#include <godot_cpp/classes/global_constants.hpp>")
header.append("")
@@ -2144,17 +2158,15 @@ def generate_global_constant_binds(api, output_dir):
continue
if enum_def["is_bitfield"]:
header.append(f'VARIANT_BITFIELD_CAST({enum_def["name"]});')
header.append(f"VARIANT_BITFIELD_CAST({enum_def['name']});")
else:
header.append(f'VARIANT_ENUM_CAST({enum_def["name"]});')
header.append(f"VARIANT_ENUM_CAST({enum_def['name']});")
# Variant::Type is not a global enum, but only one line, it is worth to place in this file instead of creating new file.
header.append("VARIANT_ENUM_CAST(godot::Variant::Type);")
header.append("")
header.append(f"#endif // ! {header_guard}")
with header_filename.open("w+", encoding="utf-8") as header_file:
header_file.write("\n".join(header))
@@ -2173,9 +2185,7 @@ def generate_utility_functions(api, output_dir):
header_filename = include_gen_folder / "utility_functions.hpp"
header_guard = "GODOT_CPP_UTILITY_FUNCTIONS_HPP"
header.append(f"#ifndef {header_guard}")
header.append(f"#define {header_guard}")
header.append("#pragma once")
header.append("")
header.append("#include <godot_cpp/variant/builtin_types.hpp>")
header.append("#include <godot_cpp/variant/variant.hpp>")
@@ -2214,7 +2224,6 @@ def generate_utility_functions(api, output_dir):
header.append("")
header.append("} // namespace godot")
header.append("")
header.append(f"#endif // ! {header_guard}")
with header_filename.open("w+", encoding="utf-8") as header_file:
header_file.write("\n".join(header))
@@ -2250,7 +2259,7 @@ def generate_utility_functions(api, output_dir):
has_return = "return_type" in function and function["return_type"] != "void"
if has_return:
source.append(
f'\tCHECK_METHOD_BIND_RET(_gde_function, {get_default_value_for_type(function["return_type"])});'
f"\tCHECK_METHOD_BIND_RET(_gde_function, ({get_default_value_for_type(function['return_type'])}));"
)
else:
source.append("\tCHECK_METHOD_BIND(_gde_function);")
@@ -2262,7 +2271,7 @@ def generate_utility_functions(api, output_dir):
if function["return_type"] == "Object":
function_call += "internal::_call_utility_ret_obj(_gde_function"
else:
function_call += f'internal::_call_utility_ret<{get_gdextension_type(correct_type(function["return_type"]))}>(_gde_function'
function_call += f"internal::_call_utility_ret<{get_gdextension_type(correct_type(function['return_type']))}>(_gde_function"
else:
function_call += "internal::_call_utility_no_ret(_gde_function"
@@ -2280,7 +2289,7 @@ def generate_utility_functions(api, output_dir):
function_call += ", ".join(arguments)
else:
if has_return:
source.append(f'\t{get_gdextension_type(correct_type(function["return_type"]))} ret;')
source.append(f"\t{get_gdextension_type(correct_type(function['return_type']))} ret;")
else:
source.append("\tVariant ret;")
function_call += "_gde_function(&ret, reinterpret_cast<GDExtensionConstVariantPtr *>(p_args), p_arg_count"
@@ -2474,7 +2483,7 @@ def make_varargs_template(
if len(class_befor_signature) > 0:
function_signature += class_befor_signature + "::"
function_signature += f'{escape_identifier(function_data["name"])}'
function_signature += f"{escape_identifier(function_data['name'])}"
method_arguments = []
if "arguments" in function_data:
@@ -2499,7 +2508,7 @@ def make_varargs_template(
if argument["type"] == "Variant":
args_array += escape_argument(argument["name"])
else:
args_array += f'Variant({escape_argument(argument["name"])})'
args_array += f"Variant({escape_argument(argument['name'])})"
args_array += ", "
args_array += "Variant(p_args)... };"
@@ -2515,7 +2524,7 @@ def make_varargs_template(
if return_type != "void":
call_line += "return "
call_line += f'{escape_identifier(function_data["name"])}_internal(call_args.data(), variant_args.size());'
call_line += f"{escape_identifier(function_data['name'])}_internal(call_args.data(), variant_args.size());"
result.append(call_line)
else:
base = "(GDExtensionTypePtr)&opaque"
@@ -2525,7 +2534,7 @@ def make_varargs_template(
ret = "nullptr"
if return_type != "void":
ret = "&ret"
result.append(f'\t{correct_type(function_data["return_type"])} ret;')
result.append(f"\t{correct_type(function_data['return_type'])} ret;")
function_name = function_data["name"]
result.append(
@@ -2746,9 +2755,22 @@ def correct_type(type_name, meta=None, use_alias=True):
if type_name in type_conversion:
return type_conversion[type_name]
if type_name.startswith("typedarray::"):
return type_name.replace("typedarray::", "TypedArray<") + ">"
arr_type_name = type_name.replace("typedarray::", "")
if is_refcounted(arr_type_name):
arr_type_name = "Ref<" + arr_type_name + ">"
return "TypedArray<" + arr_type_name + ">"
if type_name.startswith("typeddictionary::"):
return type_name.replace("typeddictionary::", "TypedDictionary<").replace(";", ", ") + ">"
dict_type_name = type_name.replace("typeddictionary::", "")
dict_type_names = dict_type_name.split(";")
if is_refcounted(dict_type_names[0]):
key_name = "Ref<" + dict_type_names[0] + ">"
else:
key_name = dict_type_names[0]
if is_refcounted(dict_type_names[1]):
val_name = "Ref<" + dict_type_names[1] + ">"
else:
val_name = dict_type_names[1]
return "TypedDictionary<" + key_name + ", " + val_name + ">"
if is_enum(type_name):
if is_bitfield(type_name):
base_class = get_enum_class(type_name)

View File

@@ -86,9 +86,9 @@ missing. ]]
function(
binding_generator_generate_bindings
API_FILE
USE_TEMPLATE_GET_NODE,
BITS,
PRECISION,
USE_TEMPLATE_GET_NODE
BITS
PRECISION
OUTPUT_DIR
)
# This code snippet will be squashed into a single line

View File

@@ -47,7 +47,7 @@ by default, we need to test for it. ]]
function(compiler_detection)
if(${CMAKE_CXX_COMPILER_ID} STREQUAL Clang)
if(${CMAKE_CXX_COMPILER_FRONTEND_VARIANT} STREQUAL MSVC)
message("Using clang-cl")
message(STATUS "Using clang-cl")
set(IS_CLANG "0" PARENT_SCOPE)
set(IS_MSVC "1" PARENT_SCOPE)
set(NOT_MSVC "0" PARENT_SCOPE)
@@ -159,7 +159,7 @@ function(common_compiler_flags)
GDEXTENSION
# features
$<${DEBUG_FEATURES}:DEBUG_ENABLED DEBUG_METHODS_ENABLED>
$<${DEBUG_FEATURES}:DEBUG_ENABLED>
$<${IS_DEV_BUILD}:DEV_ENABLED>

View File

@@ -29,7 +29,7 @@ include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/windows.cmake)
# Detect number of processors
include(ProcessorCount)
ProcessorCount(PROC_MAX)
message("Auto-detected ${PROC_MAX} CPU cores available for build parallelism.")
message(STATUS "Auto-detected ${PROC_MAX} CPU cores available for build parallelism.")
# List of known platforms
set(PLATFORM_LIST
@@ -197,15 +197,21 @@ function(godotcpp_generate)
another compiler simulating the Visual C++ cl command-line syntax. ]]
if(MSVC)
math(EXPR PROC_N "(${PROC_MAX}-1) | (${X}-2)>>31 & 1")
message("Using ${PROC_N} cores for multi-threaded compilation.")
message(STATUS "Using ${PROC_N} cores for multi-threaded compilation.")
# TODO You can override it at configure time with ...." )
else()
if(CMAKE_BUILD_PARALLEL_LEVEL)
set(_cores "${CMAKE_BUILD_PARALLEL_LEVEL}")
else()
set(_cores "all")
endif()
message(
"Using ${CMAKE_BUILD_PARALLEL_LEVEL} cores, You can override"
" it at configure time by using -j <n> or --parallel <n> on the build"
STATUS
"Using ${_cores} cores. You can override"
" this at configure time by using -j <n> or --parallel <n> in the build"
" command."
)
message(" eg. cmake --build . -j 7 ...")
message(STATUS " eg. cmake --build . -j 7 ...")
endif()
#[[ GODOTCPP_SYMBOL_VISIBLITY
@@ -245,8 +251,8 @@ function(godotcpp_generate)
# Build Profile
if(GODOTCPP_BUILD_PROFILE)
message(STATUS "Using build profile to trim api file")
message("\tBUILD_PROFILE = '${GODOTCPP_BUILD_PROFILE}'")
message("\tAPI_SOURCE = '${GODOTCPP_GDEXTENSION_API_FILE}'")
message(STATUS "\tBUILD_PROFILE = '${GODOTCPP_BUILD_PROFILE}'")
message(STATUS "\tAPI_SOURCE = '${GODOTCPP_GDEXTENSION_API_FILE}'")
build_profile_generate_trimmed_api(
"${GODOTCPP_BUILD_PROFILE}"
"${GODOTCPP_GDEXTENSION_API_FILE}"
@@ -276,7 +282,7 @@ function(godotcpp_generate)
string(
CONCAT
SYSTEM_NAME
"$<$<PLATFORM_ID:Android>:android.${ANDROID_ABI}>"
"$<$<PLATFORM_ID:Android>:android>"
"$<$<PLATFORM_ID:iOS>:ios>"
"$<$<PLATFORM_ID:Linux>:linux>"
"$<$<PLATFORM_ID:Darwin>:macos>"

1147
cmake/ios.toolchain.cmake Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -14,11 +14,25 @@ function(linux_options)
Not implemented as compiler selection is managed by CMake. Look to
doc/cmake.rst for examples.
]]
option(GODOTCPP_USE_STATIC_CPP "Link libgcc and libstdc++ statically for better portability" ON)
endfunction()
#[===========================[ Target Generation ]===========================]
function(linux_generate)
set(STATIC_CPP "$<BOOL:${GODOTCPP_USE_STATIC_CPP}>")
target_compile_definitions(godot-cpp PUBLIC LINUX_ENABLED UNIX_ENABLED)
# gersemi: off
target_link_options(
godot-cpp
PUBLIC
$<${STATIC_CPP}:
-static-libgcc
-static-libstdc++
>
)
# gersemi: on
common_compiler_flags()
endfunction()

View File

@@ -17,14 +17,6 @@ https://cmake.org/cmake/help/latest/variable/CMAKE_OSX_ARCHITECTURES.html
# Find Requirements
if(APPLE)
set(CMAKE_OSX_SYSROOT $ENV{SDKROOT})
find_library(
COCOA_LIBRARY
REQUIRED
NAMES Cocoa
PATHS ${CMAKE_OSX_SYSROOT}/System/Library
PATH_SUFFIXES Frameworks
NO_DEFAULT_PATH
)
endif(APPLE)
#[=============================[ MacOS Options ]=============================]
@@ -45,9 +37,5 @@ endfunction()
function(macos_generate)
target_compile_definitions(godot-cpp PUBLIC MACOS_ENABLED UNIX_ENABLED)
target_link_options(godot-cpp PUBLIC -Wl,-undefined,dynamic_lookup)
target_link_libraries(godot-cpp INTERFACE ${COCOA_LIBRARY})
common_compiler_flags()
endfunction()

View File

@@ -28,8 +28,7 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef GODOT_EDITOR_PLUGIN_REGISTRATION_HPP
#define GODOT_EDITOR_PLUGIN_REGISTRATION_HPP
#pragma once
#include <godot_cpp/templates/vector.hpp>
@@ -58,5 +57,3 @@ public:
};
} // namespace godot
#endif // GODOT_EDITOR_PLUGIN_REGISTRATION_HPP

View File

@@ -28,8 +28,7 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef GODOT_REF_HPP
#define GODOT_REF_HPP
#pragma once
#include <godot_cpp/core/defs.hpp>
@@ -71,6 +70,10 @@ class Ref {
}
public:
static _FORCE_INLINE_ String get_class_static() {
return T::get_class_static();
}
_FORCE_INLINE_ bool operator==(const T *p_ptr) const {
return reference == p_ptr;
}
@@ -284,5 +287,3 @@ struct GetTypeInfo<const Ref<T> &, typename EnableIf<TypeInherits<RefCounted, T>
};
} // namespace godot
#endif // GODOT_REF_HPP

View File

@@ -28,8 +28,7 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef GODOT_WRAPPED_HPP
#define GODOT_WRAPPED_HPP
#pragma once
#include <godot_cpp/core/memory.hpp>
@@ -94,7 +93,7 @@ protected:
bool _property_can_revert(const StringName &p_name) const { return false; }
bool _property_get_revert(const StringName &p_name, Variant &r_property) const { return false; }
void _validate_property(PropertyInfo &p_property) const {}
String _to_string() const { return "[" + String(get_class_static()) + ":" + itos(get_instance_id()) + "]"; }
String _to_string() const { return "<Wrapped#0>"; }
static void notification_bind(GDExtensionClassInstancePtr p_instance, int32_t p_what, GDExtensionBool p_reversed) {}
static GDExtensionBool set_bind(GDExtensionClassInstancePtr p_instance, GDExtensionConstStringNamePtr p_name, GDExtensionConstVariantPtr p_value) { return false; }
@@ -122,10 +121,6 @@ public:
return string_name;
}
uint64_t get_instance_id() const {
return 0;
}
// Must be public but you should not touch this.
GodotObject *_owner = nullptr;
};
@@ -193,7 +188,9 @@ private:
friend class ::godot::Wrapped; \
\
protected: \
virtual bool _is_extension_class() const override { return true; } \
virtual bool _is_extension_class() const override { \
return true; \
} \
\
static const ::godot::StringName *_get_extension_class_name() { \
const ::godot::StringName &string_name = get_class_static(); \
@@ -205,35 +202,35 @@ protected:
} \
\
static void (::godot::Wrapped::*_get_notification())(int) { \
return (void(::godot::Wrapped::*)(int)) & m_class::_notification; \
return (void (::godot::Wrapped::*)(int)) & m_class::_notification; \
} \
\
static bool (::godot::Wrapped::*_get_set())(const ::godot::StringName &p_name, const ::godot::Variant &p_property) { \
return (bool(::godot::Wrapped::*)(const ::godot::StringName &p_name, const ::godot::Variant &p_property)) & m_class::_set; \
return (bool (::godot::Wrapped::*)(const ::godot::StringName &p_name, const ::godot::Variant &p_property)) & m_class::_set; \
} \
\
static bool (::godot::Wrapped::*_get_get())(const ::godot::StringName &p_name, ::godot::Variant &r_ret) const { \
return (bool(::godot::Wrapped::*)(const ::godot::StringName &p_name, ::godot::Variant &r_ret) const) & m_class::_get; \
return (bool (::godot::Wrapped::*)(const ::godot::StringName &p_name, ::godot::Variant &r_ret) const) & m_class::_get; \
} \
\
static void (::godot::Wrapped::*_get_get_property_list())(::godot::List<::godot::PropertyInfo> * p_list) const { \
return (void(::godot::Wrapped::*)(::godot::List<::godot::PropertyInfo> * p_list) const) & m_class::_get_property_list; \
return (void (::godot::Wrapped::*)(::godot::List<::godot::PropertyInfo> * p_list) const) & m_class::_get_property_list; \
} \
\
static bool (::godot::Wrapped::*_get_property_can_revert())(const ::godot::StringName &p_name) const { \
return (bool(::godot::Wrapped::*)(const ::godot::StringName &p_name) const) & m_class::_property_can_revert; \
return (bool (::godot::Wrapped::*)(const ::godot::StringName &p_name) const) & m_class::_property_can_revert; \
} \
\
static bool (::godot::Wrapped::*_get_property_get_revert())(const ::godot::StringName &p_name, ::godot::Variant &) const { \
return (bool(::godot::Wrapped::*)(const ::godot::StringName &p_name, ::godot::Variant &) const) & m_class::_property_get_revert; \
return (bool (::godot::Wrapped::*)(const ::godot::StringName &p_name, ::godot::Variant &) const) & m_class::_property_get_revert; \
} \
\
static void (::godot::Wrapped::*_get_validate_property())(::godot::PropertyInfo & p_property) const { \
return (void(::godot::Wrapped::*)(::godot::PropertyInfo & p_property) const) & m_class::_validate_property; \
return (void (::godot::Wrapped::*)(::godot::PropertyInfo & p_property) const) & m_class::_validate_property; \
} \
\
static ::godot::String (::godot::Wrapped::*_get_to_string())() const { \
return (::godot::String(::godot::Wrapped::*)() const) & m_class::_to_string; \
return (::godot::String (::godot::Wrapped::*)() const) & m_class::_to_string; \
} \
\
template <typename T, typename B> \
@@ -511,5 +508,3 @@ private:
#define GDVIRTUAL_BIND(m_name, ...) ::godot::ClassDB::add_virtual_method(get_class_static(), _gdvirtual_##m_name##_get_method_info(), ::godot::snarray(__VA_ARGS__));
#define GDVIRTUAL_IS_OVERRIDDEN(m_name) _gdvirtual_##m_name##_overridden()
#define GDVIRTUAL_IS_OVERRIDDEN_PTR(m_obj, m_name) m_obj->_gdvirtual_##m_name##_overridden()
#endif // GODOT_WRAPPED_HPP

View File

@@ -28,11 +28,11 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef GODOT_BINDER_COMMON_HPP
#define GODOT_BINDER_COMMON_HPP
#pragma once
#include <gdextension_interface.h>
#include <godot_cpp/core/defs.hpp>
#include <godot_cpp/core/method_ptrcall.hpp>
#include <godot_cpp/core/type_info.hpp>
@@ -692,5 +692,3 @@ void call_with_ptr_args_static_method_ret(R (*p_method)(P...), const GDExtension
#include <godot_cpp/classes/global_constants_binds.hpp>
#include <godot_cpp/variant/builtin_binds.hpp>
#endif // GODOT_BINDER_COMMON_HPP

View File

@@ -28,8 +28,7 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef GODOT_BUILTIN_PTRCALL_HPP
#define GODOT_BUILTIN_PTRCALL_HPP
#pragma once
#include <gdextension_interface.h>
#include <godot_cpp/core/object.hpp>
@@ -88,5 +87,3 @@ T _call_builtin_ptr_getter(const GDExtensionPtrGetter getter, GDExtensionConstTy
} // namespace internal
} // namespace godot
#endif // GODOT_BUILTIN_PTRCALL_HPP

View File

@@ -28,8 +28,7 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef GODOT_CLASS_DB_HPP
#define GODOT_CLASS_DB_HPP
#pragma once
#include <gdextension_interface.h>
@@ -370,5 +369,3 @@ MethodBind *ClassDB::bind_vararg_method(uint32_t p_flags, StringName p_name, M p
} // namespace godot
CLASSDB_SINGLETON_VARIANT_CAST;
#endif // GODOT_CLASS_DB_HPP

View File

@@ -28,12 +28,11 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef GODOT_DEFS_HPP
#define GODOT_DEFS_HPP
#pragma once
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <utility>
namespace godot {
@@ -65,15 +64,33 @@ namespace godot {
#endif
#endif
// Should always inline, except in debug builds because it makes debugging harder.
// Should always inline, except in dev builds because it makes debugging harder,
// or `size_enabled` builds where inlining is actively avoided.
#ifndef _FORCE_INLINE_
#ifdef DISABLE_FORCED_INLINE
#if defined(DEV_ENABLED) || defined(SIZE_EXTRA)
#define _FORCE_INLINE_ inline
#else
#define _FORCE_INLINE_ _ALWAYS_INLINE_
#endif
#endif
// Should never inline.
#ifndef _NO_INLINE_
#if defined(__GNUC__)
#define _NO_INLINE_ __attribute__((noinline))
#elif defined(_MSC_VER)
#define _NO_INLINE_ __declspec(noinline)
#else
#define _NO_INLINE_
#endif
#endif
// In some cases [[nodiscard]] will get false positives,
// we can prevent the warning in specific cases by preceding the call with a cast.
#ifndef _ALLOW_DISCARD_
#define _ALLOW_DISCARD_ (void)
#endif
// Windows badly defines a lot of stuff we'll never use. Undefine it.
#ifdef _WIN32
#undef min // override standard definition
@@ -81,14 +98,182 @@ namespace godot {
#undef ERROR // override (really stupid) wingdi.h standard definition
#undef DELETE // override (another really stupid) winnt.h standard definition
#undef MessageBox // override winuser.h standard definition
#undef MIN // override standard definition
#undef MAX // override standard definition
#undef CLAMP // override standard definition
#undef Error
#undef OK
#undef CONNECT_DEFERRED // override from Windows SDK, clashes with Object enum
#undef MemoryBarrier
#undef MONO_FONT
#endif
// Make room for our constexpr's below by overriding potential system-specific macros.
#undef SIGN
#undef MIN
#undef MAX
#undef CLAMP
template <typename T>
constexpr const T SIGN(const T m_v) {
return m_v > 0 ? +1.0f : (m_v < 0 ? -1.0f : 0.0f);
}
template <typename T, typename T2>
constexpr auto MIN(const T m_a, const T2 m_b) {
return m_a < m_b ? m_a : m_b;
}
template <typename T, typename T2>
constexpr auto MAX(const T m_a, const T2 m_b) {
return m_a > m_b ? m_a : m_b;
}
template <typename T, typename T2, typename T3>
constexpr auto CLAMP(const T m_a, const T2 m_min, const T3 m_max) {
return m_a < m_min ? m_min : (m_a > m_max ? m_max : m_a);
}
// Generic swap template.
#ifndef SWAP
#define SWAP(m_x, m_y) std::swap((m_x), (m_y))
#endif // SWAP
/* Functions to handle powers of 2 and shifting. */
// Returns `true` if a positive integer is a power of 2, `false` otherwise.
template <typename T>
inline bool is_power_of_2(const T x) {
return x && ((x & (x - 1)) == 0);
}
// Function to find the next power of 2 to an integer.
static _FORCE_INLINE_ unsigned int next_power_of_2(unsigned int x) {
if (x == 0) {
return 0;
}
--x;
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
return ++x;
}
// Function to find the previous power of 2 to an integer.
static _FORCE_INLINE_ unsigned int previous_power_of_2(unsigned int x) {
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
return x - (x >> 1);
}
// Function to find the closest power of 2 to an integer.
static _FORCE_INLINE_ unsigned int closest_power_of_2(unsigned int x) {
unsigned int nx = next_power_of_2(x);
unsigned int px = previous_power_of_2(x);
return (nx - x) > (x - px) ? px : nx;
}
// Get a shift value from a power of 2.
static inline int get_shift_from_power_of_2(unsigned int p_bits) {
for (unsigned int i = 0; i < 32; i++) {
if (p_bits == (unsigned int)(1 << i)) {
return i;
}
}
return -1;
}
template <typename T>
static _FORCE_INLINE_ T nearest_power_of_2_templated(T x) {
--x;
// The number of operations on x is the base two logarithm
// of the number of bits in the type. Add three to account
// for sizeof(T) being in bytes.
size_t num = get_shift_from_power_of_2(sizeof(T)) + 3;
// If the compiler is smart, it unrolls this loop.
// If it's dumb, this is a bit slow.
for (size_t i = 0; i < num; i++) {
x |= x >> (1 << i);
}
return ++x;
}
// Function to find the nearest (bigger) power of 2 to an integer.
static inline unsigned int nearest_shift(unsigned int p_number) {
for (int i = 30; i >= 0; i--) {
if (p_number & (1 << i)) {
return i + 1;
}
}
return 0;
}
// constexpr function to find the floored log2 of a number
template <typename T>
constexpr T floor_log2(T x) {
return x < 2 ? x : 1 + floor_log2(x >> 1);
}
// Get the number of bits needed to represent the number.
// IE, if you pass in 8, you will get 4.
// If you want to know how many bits are needed to store 8 values however, pass in (8 - 1).
template <typename T>
constexpr T get_num_bits(T x) {
return floor_log2(x);
}
// Swap 16, 32 and 64 bits value for endianness.
#if defined(__GNUC__)
#define BSWAP16(x) __builtin_bswap16(x)
#define BSWAP32(x) __builtin_bswap32(x)
#define BSWAP64(x) __builtin_bswap64(x)
#elif defined(_MSC_VER)
#define BSWAP16(x) _byteswap_ushort(x)
#define BSWAP32(x) _byteswap_ulong(x)
#define BSWAP64(x) _byteswap_uint64(x)
#else
static inline uint16_t BSWAP16(uint16_t x) {
return (x >> 8) | (x << 8);
}
static inline uint32_t BSWAP32(uint32_t x) {
return ((x << 24) | ((x << 8) & 0x00FF0000) | ((x >> 8) & 0x0000FF00) | (x >> 24));
}
static inline uint64_t BSWAP64(uint64_t x) {
x = (x & 0x00000000FFFFFFFF) << 32 | (x & 0xFFFFFFFF00000000) >> 32;
x = (x & 0x0000FFFF0000FFFF) << 16 | (x & 0xFFFF0000FFFF0000) >> 16;
x = (x & 0x00FF00FF00FF00FF) << 8 | (x & 0xFF00FF00FF00FF00) >> 8;
return x;
}
#endif
// Generic comparator used in Map, List, etc.
template <typename T>
struct Comparator {
_ALWAYS_INLINE_ bool operator()(const T &p_a, const T &p_b) const { return (p_a < p_b); }
};
// Global lock macro, relies on the static Mutex::_global_mutex.
void _global_lock();
void _global_unlock();
struct _GlobalLock {
_GlobalLock() { _global_lock(); }
~_GlobalLock() { _global_unlock(); }
};
#define GLOBAL_LOCK_FUNCTION _GlobalLock _global_lock_;
#if defined(__GNUC__)
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
@@ -97,22 +282,17 @@ namespace godot {
#define unlikely(x) x
#endif
#ifdef REAL_T_IS_DOUBLE
typedef double real_t;
#if defined(__GNUC__)
#define _PRINTF_FORMAT_ATTRIBUTE_2_0 __attribute__((format(printf, 2, 0)))
#define _PRINTF_FORMAT_ATTRIBUTE_2_3 __attribute__((format(printf, 2, 3)))
#else
typedef float real_t;
#define _PRINTF_FORMAT_ATTRIBUTE_2_0
#define _PRINTF_FORMAT_ATTRIBUTE_2_3
#endif
// Generic swap template.
#ifndef SWAP
#define SWAP(m_x, m_y) __swap_tmpl((m_x), (m_y))
template <typename T>
inline void __swap_tmpl(T &x, T &y) {
T aux = x;
x = y;
y = aux;
}
#endif // SWAP
// This is needed due to a strange OpenGL API that expects a pointer
// type for an argument that is actually an offset.
#define CAST_INT_TO_UCHAR_PTR(ptr) ((uint8_t *)(uintptr_t)(ptr))
// Home-made index sequence trick, so it can be used everywhere without the costly include of std::tuple.
// https://stackoverflow.com/questions/15014096/c-index-of-type-during-variadic-template-expansion
@@ -125,10 +305,36 @@ struct BuildIndexSequence : BuildIndexSequence<N - 1, N - 1, Is...> {};
template <size_t... Is>
struct BuildIndexSequence<0, Is...> : IndexSequence<Is...> {};
// Limit the depth of recursive algorithms when dealing with Array/Dictionary
#define MAX_RECURSION 100
#ifdef DEBUG_ENABLED
#define DEBUG_METHODS_ENABLED
#endif
// Macro GD_IS_DEFINED() allows to check if a macro is defined. It needs to be defined to anything (say 1) to work.
#define __GDARG_PLACEHOLDER_1 false,
#define __gd_take_second_arg(__ignored, val, ...) val
#define ____gd_is_defined(arg1_or_junk) __gd_take_second_arg(arg1_or_junk true, false)
#define ___gd_is_defined(val) ____gd_is_defined(__GDARG_PLACEHOLDER_##val)
#define GD_IS_DEFINED(x) ___gd_is_defined(x)
// Whether the default value of a type is just all-0 bytes.
// This can most commonly be exploited by using memset for these types instead of loop-construct.
// Trivially constructible types are also zero-constructible.
template <typename T>
struct is_zero_constructible : std::is_trivially_constructible<T> {};
template <typename T>
struct is_zero_constructible<const T> : is_zero_constructible<T> {};
template <typename T>
struct is_zero_constructible<volatile T> : is_zero_constructible<T> {};
template <typename T>
struct is_zero_constructible<const volatile T> : is_zero_constructible<T> {};
template <typename T>
inline constexpr bool is_zero_constructible_v = is_zero_constructible<T>::value;
} //namespace godot
// To maintain compatibility an alias is defined outside the namespace.
// Consider it deprecated.
using real_t = godot::real_t;
#endif // GODOT_DEFS_HPP

View File

@@ -28,8 +28,7 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef GODOT_ENGINE_PTRCALL_HPP
#define GODOT_ENGINE_PTRCALL_HPP
#pragma once
#include <gdextension_interface.h>
@@ -56,10 +55,10 @@ O *_call_native_mb_ret_obj(const GDExtensionMethodBindPtr mb, void *instance, co
template <typename R, typename... Args>
R _call_native_mb_ret(const GDExtensionMethodBindPtr mb, void *instance, const Args &...args) {
R ret;
typename PtrToArg<R>::EncodeT ret;
std::array<GDExtensionConstTypePtr, sizeof...(Args)> mb_args = { { (GDExtensionConstTypePtr)args... } };
internal::gdextension_interface_object_method_bind_ptrcall(mb, instance, mb_args.data(), &ret);
return ret;
return static_cast<R>(ret);
}
template <typename... Args>
@@ -70,10 +69,10 @@ void _call_native_mb_no_ret(const GDExtensionMethodBindPtr mb, void *instance, c
template <typename R, typename... Args>
R _call_utility_ret(GDExtensionPtrUtilityFunction func, const Args &...args) {
R ret;
typename PtrToArg<R>::EncodeT ret;
std::array<GDExtensionConstTypePtr, sizeof...(Args)> mb_args = { { (GDExtensionConstTypePtr)args... } };
func(&ret, mb_args.data(), mb_args.size());
return ret;
return static_cast<R>(ret);
}
template <typename... Args>
@@ -93,5 +92,3 @@ void _call_utility_no_ret(const GDExtensionPtrUtilityFunction func, const Args &
} // namespace internal
} // namespace godot
#endif // GODOT_ENGINE_PTRCALL_HPP

View File

@@ -28,8 +28,7 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef GODOT_ERROR_MACROS_HPP
#define GODOT_ERROR_MACROS_HPP
#pragma once
#include <godot_cpp/core/defs.hpp>
@@ -802,5 +801,3 @@ void _err_flush_stdout();
#define CHECK_METHOD_BIND_RET(m_mb, m_ret)
#define CHECK_METHOD_BIND(m_mb)
#endif
#endif // GODOT_ERROR_MACROS_HPP

View File

@@ -0,0 +1,51 @@
/**************************************************************************/
/* math.compat.inc */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef DISABLE_DEPRECATED
namespace godot {
#undef ABS
// Generic ABS function, for math uses please use Math::abs.
template <typename T>
[[deprecated("Use Math::abs instead")]]
constexpr T ABS(T m_v) {
return m_v < 0 ? -m_v : m_v;
}
}
// To maintain compatibility an alias is defined outside the namespace.
// Consider it deprecated.
using real_t = godot::real_t;
#endif

View File

@@ -28,10 +28,10 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef GODOT_MATH_HPP
#define GODOT_MATH_HPP
#pragma once
#include <godot_cpp/core/defs.hpp>
#include <godot_cpp/core/math_defs.hpp>
#include <gdextension_interface.h>
@@ -39,185 +39,8 @@
namespace godot {
#define Math_SQRT12 0.7071067811865475244008443621048490
#define Math_SQRT2 1.4142135623730950488016887242
#define Math_LN2 0.6931471805599453094172321215
#define Math_PI 3.1415926535897932384626433833
#define Math_TAU 6.2831853071795864769252867666
#define Math_E 2.7182818284590452353602874714
#define Math_INF INFINITY
#define Math_NAN NAN
// Make room for our constexpr's below by overriding potential system-specific macros.
#undef ABS
#undef SIGN
#undef MIN
#undef MAX
#undef CLAMP
// Generic ABS function, for math uses please use Math::abs.
template <typename T>
constexpr T ABS(T m_v) {
return m_v < 0 ? -m_v : m_v;
}
template <typename T>
constexpr const T SIGN(const T m_v) {
return m_v == 0 ? 0.0f : (m_v < 0 ? -1.0f : +1.0f);
}
template <typename T, typename T2>
constexpr auto MIN(const T m_a, const T2 m_b) {
return m_a < m_b ? m_a : m_b;
}
template <typename T, typename T2>
constexpr auto MAX(const T m_a, const T2 m_b) {
return m_a > m_b ? m_a : m_b;
}
template <typename T, typename T2, typename T3>
constexpr auto CLAMP(const T m_a, const T2 m_min, const T3 m_max) {
return m_a < m_min ? m_min : (m_a > m_max ? m_max : m_a);
}
// Generic swap template.
#ifndef SWAP
#define SWAP(m_x, m_y) __swap_tmpl((m_x), (m_y))
template <typename T>
inline void __swap_tmpl(T &x, T &y) {
T aux = x;
x = y;
y = aux;
}
#endif // SWAP
/* Functions to handle powers of 2 and shifting. */
// Function to find the next power of 2 to an integer.
static _FORCE_INLINE_ unsigned int next_power_of_2(unsigned int x) {
if (x == 0) {
return 0;
}
--x;
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
return ++x;
}
// Function to find the previous power of 2 to an integer.
static _FORCE_INLINE_ unsigned int previous_power_of_2(unsigned int x) {
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
return x - (x >> 1);
}
// Function to find the closest power of 2 to an integer.
static _FORCE_INLINE_ unsigned int closest_power_of_2(unsigned int x) {
unsigned int nx = next_power_of_2(x);
unsigned int px = previous_power_of_2(x);
return (nx - x) > (x - px) ? px : nx;
}
// Get a shift value from a power of 2.
static inline int get_shift_from_power_of_2(unsigned int p_bits) {
for (unsigned int i = 0; i < 32; i++) {
if (p_bits == (unsigned int)(1 << i)) {
return i;
}
}
return -1;
}
template <typename T>
static _FORCE_INLINE_ T nearest_power_of_2_templated(T x) {
--x;
// The number of operations on x is the base two logarithm
// of the number of bits in the type. Add three to account
// for sizeof(T) being in bytes.
size_t num = get_shift_from_power_of_2(sizeof(T)) + 3;
// If the compiler is smart, it unrolls this loop.
// If it's dumb, this is a bit slow.
for (size_t i = 0; i < num; i++) {
x |= x >> (1 << i);
}
return ++x;
}
// Function to find the nearest (bigger) power of 2 to an integer.
static inline unsigned int nearest_shift(unsigned int p_number) {
for (int i = 30; i >= 0; i--) {
if (p_number & (1 << i)) {
return i + 1;
}
}
return 0;
}
// constexpr function to find the floored log2 of a number
template <typename T>
constexpr T floor_log2(T x) {
return x < 2 ? x : 1 + floor_log2(x >> 1);
}
// Get the number of bits needed to represent the number.
// IE, if you pass in 8, you will get 4.
// If you want to know how many bits are needed to store 8 values however, pass in (8 - 1).
template <typename T>
constexpr T get_num_bits(T x) {
return floor_log2(x);
}
// Swap 16, 32 and 64 bits value for endianness.
#if defined(__GNUC__)
#define BSWAP16(x) __builtin_bswap16(x)
#define BSWAP32(x) __builtin_bswap32(x)
#define BSWAP64(x) __builtin_bswap64(x)
#else
static inline uint16_t BSWAP16(uint16_t x) {
return (x >> 8) | (x << 8);
}
static inline uint32_t BSWAP32(uint32_t x) {
return ((x << 24) | ((x << 8) & 0x00FF0000) | ((x >> 8) & 0x0000FF00) | (x >> 24));
}
static inline uint64_t BSWAP64(uint64_t x) {
x = (x & 0x00000000FFFFFFFF) << 32 | (x & 0xFFFFFFFF00000000) >> 32;
x = (x & 0x0000FFFF0000FFFF) << 16 | (x & 0xFFFF0000FFFF0000) >> 16;
x = (x & 0x00FF00FF00FF00FF) << 8 | (x & 0xFF00FF00FF00FF00) >> 8;
return x;
}
#endif
namespace Math {
// This epsilon should match the one used by Godot for consistency.
// Using `f` when `real_t` is float.
#define CMP_EPSILON 0.00001f
#define CMP_EPSILON2 (CMP_EPSILON * CMP_EPSILON)
// This epsilon is for values related to a unit size (scalar or vector len).
#ifdef PRECISE_MATH_CHECKS
#define UNIT_EPSILON 0.00001
#else
// Tolerate some more floating point error normally.
#define UNIT_EPSILON 0.001
#endif
// Functions reproduced as in Godot's source code `math_funcs.h`.
// Some are overloads to automatically support changing real_t into either double or float in the way Godot does.
@@ -538,6 +361,26 @@ inline float bezier_interpolate(float p_start, float p_control_1, float p_contro
return p_start * omt3 + p_control_1 * omt2 * p_t * 3.0f + p_control_2 * omt * t2 * 3.0f + p_end * t3;
}
inline double bezier_derivative(double p_start, double p_control_1, double p_control_2, double p_end, double p_t) {
/* Formula from Wikipedia article on Bezier curves. */
double omt = (1.0 - p_t);
double omt2 = omt * omt;
double t2 = p_t * p_t;
double d = (p_control_1 - p_start) * 3.0 * omt2 + (p_control_2 - p_control_1) * 6.0 * omt * p_t + (p_end - p_control_2) * 3.0 * t2;
return d;
}
inline float bezier_derivative(float p_start, float p_control_1, float p_control_2, float p_end, float p_t) {
/* Formula from Wikipedia article on Bezier curves. */
float omt = (1.0f - p_t);
float omt2 = omt * omt;
float t2 = p_t * p_t;
float d = (p_control_1 - p_start) * 3.0f * omt2 + (p_control_2 - p_control_1) * 6.0f * omt * p_t + (p_end - p_control_2) * 3.0f * t2;
return d;
}
template <typename T>
inline T clamp(T x, T minv, T maxv) {
if (x < minv) {
@@ -816,4 +659,4 @@ inline float snap_scalar_separation(float p_offset, float p_step, float p_target
} // namespace Math
} // namespace godot
#endif // GODOT_MATH_HPP
#include "math.compat.inc"

View File

@@ -0,0 +1,72 @@
/**************************************************************************/
/* math_defs.hpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#pragma once
namespace godot {
#define CMP_EPSILON 0.00001
#define CMP_EPSILON2 (CMP_EPSILON * CMP_EPSILON)
#define CMP_NORMALIZE_TOLERANCE 0.000001
#define CMP_POINT_IN_PLANE_EPSILON 0.00001
#define Math_SQRT12 0.7071067811865475244008443621048490
#define Math_SQRT2 1.4142135623730950488016887242
#define Math_LN2 0.6931471805599453094172321215
#define Math_TAU 6.2831853071795864769252867666
#define Math_PI 3.1415926535897932384626433833
#define Math_E 2.7182818284590452353602874714
#ifdef DEBUG_ENABLED
#define MATH_CHECKS
#endif
//this epsilon is for values related to a unit size (scalar or vector len)
#ifdef PRECISE_MATH_CHECKS
#define UNIT_EPSILON 0.00001
#else
//tolerate some more floating point error normally
#define UNIT_EPSILON 0.001
#endif
#define USEC_TO_SEC(m_usec) ((m_usec) / 1000000.0)
/**
* The "Real" type is an abstract type used for real numbers, such as 1.5,
* in contrast to integer numbers. Precision can be controlled with the
* presence or absence of the REAL_T_IS_DOUBLE define.
*/
#ifdef REAL_T_IS_DOUBLE
typedef double real_t;
#else
typedef float real_t;
#endif
} // namespace godot

View File

@@ -28,8 +28,7 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef GODOT_MEMORY_HPP
#define GODOT_MEMORY_HPP
#pragma once
#include <cstddef>
#include <cstdint>
@@ -102,12 +101,6 @@ _ALWAYS_INLINE_ T *_post_initialize(T *p_obj) {
#define memnew_allocator(m_class, m_allocator) (::godot::_pre_initialize<std::remove_pointer_t<decltype(new ("", "") m_class)>>(), ::godot::_post_initialize(new ("", m_allocator::alloc) m_class))
#define memnew_placement(m_placement, m_class) (::godot::_pre_initialize<std::remove_pointer_t<decltype(new ("", "") m_class)>>(), ::godot::_post_initialize(new ("", m_placement, sizeof(m_class), "") m_class))
// Generic comparator used in Map, List, etc.
template <typename T>
struct Comparator {
_ALWAYS_INLINE_ bool operator()(const T &p_a, const T &p_b) const { return (p_a < p_b); }
};
template <typename T>
void memdelete(T *p_class, typename std::enable_if<!std::is_base_of_v<godot::Wrapped, T>>::type * = nullptr) {
if constexpr (!std::is_trivially_destructible_v<T>) {
@@ -216,5 +209,3 @@ struct _GlobalNilClass {
};
} // namespace godot
#endif // GODOT_MEMORY_HPP

View File

@@ -28,8 +28,7 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef GODOT_METHOD_BIND_HPP
#define GODOT_METHOD_BIND_HPP
#pragma once
#include <godot_cpp/core/binder_common.hpp>
#include <godot_cpp/core/type_info.hpp>
@@ -48,14 +47,14 @@
namespace godot {
class MethodBind {
uint32_t hint_flags = METHOD_FLAGS_DEFAULT;
StringName name;
StringName instance_class;
int argument_count = 0;
uint32_t hint_flags = METHOD_FLAGS_DEFAULT;
bool _static = false;
bool _is_const = false;
bool _has_return = false;
bool _const = false;
bool _returns = false;
bool _vararg = false;
std::vector<StringName> argument_names;
@@ -63,20 +62,20 @@ class MethodBind {
std::vector<Variant> default_arguments;
protected:
void _set_const(bool p_const);
void _set_static(bool p_static);
void _set_returns(bool p_returns);
void _set_vararg(bool p_vararg);
virtual GDExtensionVariantType gen_argument_type(int p_arg) const = 0;
virtual PropertyInfo gen_argument_type_info(int p_arg) const = 0;
void generate_argument_types(int p_count);
void set_const(bool p_const);
void set_return(bool p_return);
void set_static(bool p_static);
void set_vararg(bool p_vararg);
void set_argument_count(int p_count);
void _generate_argument_types(int p_count);
void set_argument_count(int p_count) { argument_count = p_count; }
public:
StringName get_name() const;
void set_name(const StringName &p_name);
_FORCE_INLINE_ int get_default_argument_count() const { return (int)default_arguments.size(); }
_FORCE_INLINE_ const std::vector<Variant> &get_default_arguments() const { return default_arguments; }
_FORCE_INLINE_ int get_default_argument_count() const { return (int)default_arguments.size(); }
_FORCE_INLINE_ Variant has_default_argument(int p_arg) const {
const int num_default_args = (int)(default_arguments.size());
const int idx = p_arg - (argument_count - num_default_args);
@@ -97,19 +96,6 @@ public:
return default_arguments[idx];
}
}
_FORCE_INLINE_ StringName get_instance_class() const { return instance_class; }
_FORCE_INLINE_ void set_instance_class(StringName p_class) { instance_class = p_class; }
_FORCE_INLINE_ int get_argument_count() const { return argument_count; }
_FORCE_INLINE_ bool is_const() const { return _is_const; }
_FORCE_INLINE_ bool is_static() const { return _static; }
_FORCE_INLINE_ bool is_vararg() const { return _vararg; }
_FORCE_INLINE_ bool has_return() const { return _has_return; }
_FORCE_INLINE_ uint32_t get_hint_flags() const { return hint_flags | (is_const() ? GDEXTENSION_METHOD_FLAG_CONST : 0) | (is_vararg() ? GDEXTENSION_METHOD_FLAG_VARARG : 0) | (is_static() ? GDEXTENSION_METHOD_FLAG_STATIC : 0); }
_FORCE_INLINE_ void set_hint_flags(uint32_t p_hint_flags) { hint_flags = p_hint_flags; }
void set_argument_names(const std::vector<StringName> &p_names);
std::vector<StringName> get_argument_names() const;
void set_default_arguments(const std::vector<Variant> &p_default_arguments) { default_arguments = p_default_arguments; }
_FORCE_INLINE_ GDExtensionVariantType get_argument_type(int p_argument) const {
ERR_FAIL_COND_V(p_argument < -1 || p_argument > argument_count, GDEXTENSION_VARIANT_TYPE_NIL);
@@ -117,7 +103,6 @@ public:
}
PropertyInfo get_argument_info(int p_argument) const;
virtual GDExtensionClassMethodArgumentMetadata get_argument_metadata(int p_argument) const = 0;
std::vector<PropertyInfo> get_arguments_info_list() const {
std::vector<PropertyInfo> vec;
@@ -128,6 +113,31 @@ public:
}
return vec;
}
void set_argument_names(const std::vector<StringName> &p_names);
std::vector<StringName> get_argument_names() const;
virtual GDExtensionClassMethodArgumentMetadata get_argument_metadata(int p_argument) const = 0;
_FORCE_INLINE_ void set_hint_flags(uint32_t p_hint_flags) { hint_flags = p_hint_flags; }
_FORCE_INLINE_ uint32_t get_hint_flags() const { return hint_flags | (is_const() ? GDEXTENSION_METHOD_FLAG_CONST : 0) | (is_vararg() ? GDEXTENSION_METHOD_FLAG_VARARG : 0) | (is_static() ? GDEXTENSION_METHOD_FLAG_STATIC : 0); }
_FORCE_INLINE_ StringName get_instance_class() const { return instance_class; }
_FORCE_INLINE_ void set_instance_class(StringName p_class) { instance_class = p_class; }
_FORCE_INLINE_ int get_argument_count() const { return argument_count; }
virtual Variant call(GDExtensionClassInstancePtr p_instance, const GDExtensionConstVariantPtr *p_args, GDExtensionInt p_argument_count, GDExtensionCallError &r_error) const = 0;
virtual void ptrcall(GDExtensionClassInstancePtr p_instance, const GDExtensionConstTypePtr *p_args, GDExtensionTypePtr r_return) const = 0;
StringName get_name() const;
void set_name(const StringName &p_name);
_FORCE_INLINE_ bool is_const() const { return _const; }
_FORCE_INLINE_ bool is_static() const { return _static; }
_FORCE_INLINE_ bool is_vararg() const { return _vararg; }
_FORCE_INLINE_ bool has_return() const { return _returns; }
void set_default_arguments(const std::vector<Variant> &p_default_arguments) { default_arguments = p_default_arguments; }
std::vector<GDExtensionClassMethodArgumentMetadata> get_arguments_metadata_list() const {
std::vector<GDExtensionClassMethodArgumentMetadata> vec;
// First element is return value
@@ -138,9 +148,6 @@ public:
return vec;
}
virtual Variant call(GDExtensionClassInstancePtr p_instance, const GDExtensionConstVariantPtr *p_args, GDExtensionInt p_argument_count, GDExtensionCallError &r_error) const = 0;
virtual void ptrcall(GDExtensionClassInstancePtr p_instance, const GDExtensionConstTypePtr *p_args, GDExtensionTypePtr r_return) const = 0;
static void bind_call(void *p_method_userdata, GDExtensionClassInstancePtr p_instance, const GDExtensionConstVariantPtr *p_args, GDExtensionInt p_argument_count, GDExtensionVariantPtr r_return, GDExtensionCallError *r_error);
static void bind_ptrcall(void *p_method_userdata, GDExtensionClassInstancePtr p_instance, const GDExtensionConstTypePtr *p_args, GDExtensionTypePtr r_return);
@@ -150,8 +157,7 @@ public:
template <typename Derived, typename T, typename R, bool should_returns>
class MethodBindVarArgBase : public MethodBind {
protected:
R(T::*method)
(const Variant **, GDExtensionInt, GDExtensionCallError &);
R (T::*method)(const Variant **, GDExtensionInt, GDExtensionCallError &);
std::vector<PropertyInfo> arguments;
public:
@@ -182,8 +188,8 @@ public:
const MethodInfo &p_method_info,
bool p_return_nil_is_variant) :
method(p_method) {
set_vararg(true);
set_const(true);
_set_vararg(true);
_set_const(true);
set_argument_count(p_method_info.arguments.size());
if (p_method_info.arguments.size()) {
arguments = p_method_info.arguments;
@@ -196,8 +202,8 @@ public:
set_argument_names(names);
}
generate_argument_types((int)p_method_info.arguments.size());
set_return(should_returns);
_generate_argument_types((int)p_method_info.arguments.size());
_set_returns(should_returns);
}
~MethodBindVarArgBase() {}
@@ -334,7 +340,7 @@ public:
MethodBindT(void (MB_T::*p_method)(P...)) {
method = p_method;
generate_argument_types(sizeof...(P));
_generate_argument_types(sizeof...(P));
set_argument_count(sizeof...(P));
}
};
@@ -410,9 +416,9 @@ public:
MethodBindTC(void (MB_T::*p_method)(P...) const) {
method = p_method;
generate_argument_types(sizeof...(P));
_generate_argument_types(sizeof...(P));
set_argument_count(sizeof...(P));
set_const(true);
_set_const(true);
}
};
@@ -435,8 +441,7 @@ template <typename T, typename R, typename... P>
template <typename R, typename... P>
#endif // TYPED_METHOD_BIND
class MethodBindTR : public MethodBind {
R(MB_T::*method)
(P...);
R (MB_T::*method)(P...);
protected:
// GCC raises warnings in the case P = {} as the comparison is always false...
@@ -493,9 +498,9 @@ public:
MethodBindTR(R (MB_T::*p_method)(P...)) {
method = p_method;
generate_argument_types(sizeof...(P));
_generate_argument_types(sizeof...(P));
set_argument_count(sizeof...(P));
set_return(true);
_set_returns(true);
}
};
@@ -518,8 +523,7 @@ template <typename T, typename R, typename... P>
template <typename R, typename... P>
#endif // TYPED_METHOD_BIND
class MethodBindTRC : public MethodBind {
R(MB_T::*method)
(P...) const;
R (MB_T::*method)(P...) const;
protected:
// GCC raises warnings in the case P = {} as the comparison is always false...
@@ -576,10 +580,10 @@ public:
MethodBindTRC(R (MB_T::*p_method)(P...) const) {
method = p_method;
generate_argument_types(sizeof...(P));
_generate_argument_types(sizeof...(P));
set_argument_count(sizeof...(P));
set_return(true);
set_const(true);
_set_returns(true);
_set_const(true);
}
};
@@ -648,9 +652,9 @@ public:
MethodBindTS(void (*p_function)(P...)) {
function = p_function;
generate_argument_types(sizeof...(P));
_generate_argument_types(sizeof...(P));
set_argument_count(sizeof...(P));
set_static(true);
_set_static(true);
}
};
@@ -664,8 +668,7 @@ MethodBind *create_static_method_bind(void (*p_method)(P...)) {
template <typename R, typename... P>
class MethodBindTRS : public MethodBind {
R(*function)
(P...);
R (*function)(P...);
protected:
// GCC raises warnings in the case P = {} as the comparison is always false...
@@ -717,10 +720,10 @@ public:
MethodBindTRS(R (*p_function)(P...)) {
function = p_function;
generate_argument_types(sizeof...(P));
_generate_argument_types(sizeof...(P));
set_argument_count(sizeof...(P));
set_static(true);
set_return(true);
_set_static(true);
_set_returns(true);
}
};
@@ -731,5 +734,3 @@ MethodBind *create_static_method_bind(R (*p_method)(P...)) {
}
} // namespace godot
#endif // GODOT_METHOD_BIND_HPP

View File

@@ -28,8 +28,7 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef GODOT_METHOD_PTRCALL_HPP
#define GODOT_METHOD_PTRCALL_HPP
#pragma once
#include <godot_cpp/core/defs.hpp>
@@ -122,6 +121,9 @@ MAKE_PTRARGCONV(uint16_t, int64_t);
MAKE_PTRARGCONV(int16_t, int64_t);
MAKE_PTRARGCONV(uint32_t, int64_t);
MAKE_PTRARGCONV(int32_t, int64_t);
MAKE_PTRARGCONV(char16_t, int64_t);
MAKE_PTRARGCONV(char32_t, int64_t);
MAKE_PTRARGCONV(wchar_t, int64_t);
MAKE_PTRARG(int64_t);
MAKE_PTRARG(uint64_t);
// Float types
@@ -234,5 +236,3 @@ GDVIRTUAL_NATIVE_PTR(float);
GDVIRTUAL_NATIVE_PTR(double);
} // namespace godot
#endif // GODOT_METHOD_PTRCALL_HPP

View File

@@ -28,8 +28,7 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef GODOT_MUTEX_LOCK_HPP
#define GODOT_MUTEX_LOCK_HPP
#pragma once
#include <godot_cpp/classes/mutex.hpp>
@@ -55,5 +54,3 @@ public:
#define _THREAD_SAFE_UNLOCK_ _thread_safe_.unlock();
} // namespace godot
#endif // GODOT_MUTEX_LOCK_HPP

View File

@@ -28,8 +28,7 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef GODOT_OBJECT_HPP
#define GODOT_OBJECT_HPP
#pragma once
#include <godot_cpp/core/defs.hpp>
@@ -148,5 +147,3 @@ const T *Object::cast_to(const Object *p_object) {
}
} // namespace godot
#endif // GODOT_OBJECT_HPP

View File

@@ -28,8 +28,7 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef GODOT_OBJECT_ID_HPP
#define GODOT_OBJECT_ID_HPP
#pragma once
#include <godot_cpp/core/defs.hpp>
@@ -58,5 +57,3 @@ public:
};
} // namespace godot
#endif // GODOT_OBJECT_ID_HPP

View File

@@ -28,8 +28,7 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef GODOT_PRINT_STRING_HPP
#define GODOT_PRINT_STRING_HPP
#pragma once
#include <godot_cpp/variant/utility_functions.hpp>
@@ -69,5 +68,3 @@ void print_verbose(const Variant &p_variant, Args... p_args) {
bool is_print_verbose_enabled();
} // namespace godot
#endif // GODOT_PRINT_STRING_HPP

View File

@@ -28,8 +28,7 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef GODOT_PROPERTY_INFO_HPP
#define GODOT_PROPERTY_INFO_HPP
#pragma once
#include <godot_cpp/core/defs.hpp>
@@ -128,5 +127,3 @@ struct PropertyInfo {
};
} // namespace godot
#endif // GODOT_PROPERTY_INFO_HPP

View File

@@ -28,9 +28,9 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef GODOT_TYPE_INFO_HPP
#define GODOT_TYPE_INFO_HPP
#pragma once
#include <godot_cpp/core/method_ptrcall.hpp>
#include <godot_cpp/core/object.hpp>
#include <godot_cpp/variant/typed_array.hpp>
#include <godot_cpp/variant/variant.hpp>
@@ -416,5 +416,3 @@ MAKE_TYPED_ARRAY_INFO(IPAddress, Variant::STRING)
#define CLASS_INFO(m_type) (GetTypeInfo<m_type *>::get_class_info())
} // namespace godot
#endif // GODOT_TYPE_INFO_HPP

View File

@@ -28,8 +28,7 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef GODOT_GODOT_HPP
#define GODOT_GODOT_HPP
#pragma once
#include <gdextension_interface.h>
@@ -265,5 +264,3 @@ public:
};
} // namespace godot
#endif // GODOT_GODOT_HPP

View File

@@ -28,8 +28,7 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef GODOT_COWDATA_HPP
#define GODOT_COWDATA_HPP
#pragma once
#include <godot_cpp/classes/global_constants.hpp>
#include <godot_cpp/core/error_macros.hpp>
@@ -38,8 +37,10 @@
#include <godot_cpp/templates/safe_refcount.hpp>
#include <cstring>
#include <initializer_list>
#include <new>
#include <type_traits>
#include <utility>
namespace godot {
@@ -167,13 +168,25 @@ private:
return *out;
}
void _unref(void *p_data);
// Decrements the reference count. Deallocates the backing buffer if needed.
// After this function, _ptr is guaranteed to be NULL.
void _unref();
void _ref(const CowData *p_from);
void _ref(const CowData &p_from);
USize _copy_on_write();
Error _realloc(Size p_alloc_size);
public:
void operator=(const CowData<T> &p_from) { _ref(p_from); }
void operator=(CowData<T> &&p_from) {
if (_ptr == p_from._ptr) {
return;
}
_unref();
_ptr = p_from._ptr;
p_from._ptr = nullptr;
}
_FORCE_INLINE_ T *ptrw() {
_copy_on_write();
@@ -222,19 +235,22 @@ public:
T *p = ptrw();
Size len = size();
for (Size i = p_index; i < len - 1; i++) {
p[i] = p[i + 1];
p[i] = std::move(p[i + 1]);
}
resize(len - 1);
}
Error insert(Size p_pos, const T &p_val) {
ERR_FAIL_INDEX_V(p_pos, size() + 1, ERR_INVALID_PARAMETER);
resize(size() + 1);
for (Size i = (size() - 1); i > p_pos; i--) {
set(i, get(i - 1));
Size new_size = size() + 1;
ERR_FAIL_INDEX_V(p_pos, new_size, ERR_INVALID_PARAMETER);
Error err = resize(new_size);
ERR_FAIL_COND_V(err, err);
T *p = ptrw();
for (Size i = new_size - 1; i > p_pos; i--) {
p[i] = std::move(p[i - 1]);
}
set(p_pos, p_val);
p[p_pos] = p_val;
return OK;
}
@@ -244,35 +260,47 @@ public:
Size count(const T &p_val) const;
_FORCE_INLINE_ CowData() {}
_FORCE_INLINE_ ~CowData();
_FORCE_INLINE_ CowData(CowData<T> &p_from) { _ref(p_from); };
_FORCE_INLINE_ ~CowData() { _unref(); }
_FORCE_INLINE_ CowData(std::initializer_list<T> p_init);
_FORCE_INLINE_ CowData(const CowData<T> &p_from) { _ref(p_from); }
_FORCE_INLINE_ CowData(CowData<T> &&p_from) {
_ptr = p_from._ptr;
p_from._ptr = nullptr;
}
};
template <typename T>
void CowData<T>::_unref(void *p_data) {
if (!p_data) {
void CowData<T>::_unref() {
if (!_ptr) {
return;
}
SafeNumeric<USize> *refc = _get_refcount();
if (refc->decrement() > 0) {
return; // still in use
// Data is still in use elsewhere.
_ptr = nullptr;
return;
}
// clean up
// Clean up.
// First, invalidate our own reference.
// NOTE: It is required to do so immediately because it must not be observable outside of this
// function after refcount has already been reduced to 0.
// WARNING: It must be done before calling the destructors, because one of them may otherwise
// observe it through a reference to us. In this case, it may try to access the buffer,
// which is illegal after some of the elements in it have already been destructed, and
// may lead to a segmentation fault.
USize current_size = *_get_size();
T *prev_ptr = _ptr;
_ptr = nullptr;
if constexpr (!std::is_trivially_destructible_v<T>) {
USize *count = _get_size();
T *data = (T *)(count + 1);
for (USize i = 0; i < *count; ++i) {
// call destructors
data[i].~T();
for (USize i = 0; i < current_size; ++i) {
prev_ptr[i].~T();
}
}
// free mem
Memory::free_static(((uint8_t *)p_data) - DATA_OFFSET, false);
Memory::free_static((uint8_t *)prev_ptr - DATA_OFFSET, false);
}
template <typename T>
@@ -307,7 +335,7 @@ typename CowData<T>::USize CowData<T>::_copy_on_write() {
}
}
_unref(_ptr);
_unref();
_ptr = _data_ptr;
rc = 1;
@@ -327,14 +355,13 @@ Error CowData<T>::resize(Size p_size) {
}
if (p_size == 0) {
// wants to clean up
_unref(_ptr);
_ptr = nullptr;
// Wants to clean up.
_unref(); // Resets _ptr to nullptr.
return OK;
}
// possibly changing size, copy on write
USize rc = _copy_on_write();
_copy_on_write();
USize current_alloc_size = _get_alloc_size(current_size);
USize alloc_size;
@@ -355,16 +382,12 @@ Error CowData<T>::resize(Size p_size) {
*(_size_ptr) = 0; //size, currently none
_ptr = _data_ptr;
} else {
uint8_t *mem_new = (uint8_t *)Memory::realloc_static(((uint8_t *)_ptr) - DATA_OFFSET, alloc_size + DATA_OFFSET, false);
ERR_FAIL_NULL_V(mem_new, ERR_OUT_OF_MEMORY);
SafeNumeric<USize> *_refc_ptr = _get_refcount_ptr(mem_new);
T *_data_ptr = _get_data_ptr(mem_new);
new (_refc_ptr) SafeNumeric<USize>(rc); //refcount
_ptr = _data_ptr;
const Error error = _realloc(alloc_size);
if (error) {
return error;
}
}
}
@@ -390,15 +413,10 @@ Error CowData<T>::resize(Size p_size) {
}
if (alloc_size != current_alloc_size) {
uint8_t *mem_new = (uint8_t *)Memory::realloc_static(((uint8_t *)_ptr) - DATA_OFFSET, alloc_size + DATA_OFFSET, false);
ERR_FAIL_NULL_V(mem_new, ERR_OUT_OF_MEMORY);
SafeNumeric<USize> *_refc_ptr = _get_refcount_ptr(mem_new);
T *_data_ptr = _get_data_ptr(mem_new);
new (_refc_ptr) SafeNumeric<USize>(rc); //refcount
_ptr = _data_ptr;
const Error error = _realloc(alloc_size);
if (error) {
return error;
}
}
*_get_size() = p_size;
@@ -407,6 +425,21 @@ Error CowData<T>::resize(Size p_size) {
return OK;
}
template <typename T>
Error CowData<T>::_realloc(Size p_alloc_size) {
uint8_t *mem_new = (uint8_t *)Memory::realloc_static(((uint8_t *)_ptr) - DATA_OFFSET, p_alloc_size + DATA_OFFSET, false);
ERR_FAIL_NULL_V(mem_new, ERR_OUT_OF_MEMORY);
SafeNumeric<USize> *_refc_ptr = _get_refcount_ptr(mem_new);
T *_data_ptr = _get_data_ptr(mem_new);
// If we realloc, we're guaranteed to be the only reference.
new (_refc_ptr) SafeNumeric<USize>(1);
_ptr = _data_ptr;
return OK;
}
template <typename T>
typename CowData<T>::Size CowData<T>::find(const T &p_val, Size p_from) const {
Size ret = -1;
@@ -466,11 +499,10 @@ void CowData<T>::_ref(const CowData &p_from) {
return; // self assign, do nothing.
}
_unref(_ptr);
_ptr = nullptr;
_unref(); // Resets _ptr to nullptr.
if (!p_from._ptr) {
return; // nothing to do
return; //nothing to do
}
if (p_from._get_refcount()->conditional_increment() > 0) { // could reference
@@ -479,8 +511,16 @@ void CowData<T>::_ref(const CowData &p_from) {
}
template <typename T>
CowData<T>::~CowData() {
_unref(_ptr);
CowData<T>::CowData(std::initializer_list<T> p_init) {
Error err = resize(p_init.size());
if (err != OK) {
return;
}
Size i = 0;
for (const T &element : p_init) {
set(i++, element);
}
}
#if defined(__GNUC__) && !defined(__clang__)
@@ -488,5 +528,3 @@ CowData<T>::~CowData() {
#endif
} // namespace godot
#endif // GODOT_COWDATA_HPP

View File

@@ -28,8 +28,7 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef GODOT_HASH_MAP_HPP
#define GODOT_HASH_MAP_HPP
#pragma once
#include <godot_cpp/core/error_macros.hpp>
#include <godot_cpp/core/memory.hpp>
@@ -62,15 +61,17 @@ struct HashMapElement {
data(p_key, p_value) {}
};
bool _hashmap_variant_less_than(const Variant &p_left, const Variant &p_right);
template <typename TKey, typename TValue,
typename Hasher = HashMapHasherDefault,
typename Comparator = HashMapComparatorDefault<TKey>,
typename Allocator = DefaultTypedAllocator<HashMapElement<TKey, TValue>>>
class HashMap {
public:
const uint32_t MIN_CAPACITY_INDEX = 2; // Use a prime.
const float MAX_OCCUPANCY = 0.75;
const uint32_t EMPTY_HASH = 0;
static constexpr uint32_t MIN_CAPACITY_INDEX = 2; // Use a prime.
static constexpr float MAX_OCCUPANCY = 0.75;
static constexpr uint32_t EMPTY_HASH = 0;
private:
Allocator element_alloc;
@@ -92,19 +93,20 @@ private:
return hash;
}
_FORCE_INLINE_ uint32_t _get_probe_length(uint32_t p_pos, uint32_t p_hash, uint32_t p_capacity) const {
uint32_t original_pos = p_hash % p_capacity;
return (p_pos - original_pos + p_capacity) % p_capacity;
static _FORCE_INLINE_ uint32_t _get_probe_length(const uint32_t p_pos, const uint32_t p_hash, const uint32_t p_capacity, const uint64_t p_capacity_inv) {
const uint32_t original_pos = fastmod(p_hash, p_capacity_inv, p_capacity);
return fastmod(p_pos - original_pos + p_capacity, p_capacity_inv, p_capacity);
}
bool _lookup_pos(const TKey &p_key, uint32_t &r_pos) const {
if (elements == nullptr) {
if (elements == nullptr || num_elements == 0) {
return false; // Failed lookups, no elements
}
uint32_t capacity = hash_table_size_primes[capacity_index];
const uint32_t capacity = hash_table_size_primes[capacity_index];
const uint64_t capacity_inv = hash_table_size_primes_inv[capacity_index];
uint32_t hash = _hash(p_key);
uint32_t pos = hash % capacity;
uint32_t pos = fastmod(hash, capacity_inv, capacity);
uint32_t distance = 0;
while (true) {
@@ -112,7 +114,7 @@ private:
return false;
}
if (distance > _get_probe_length(pos, hashes[pos], capacity)) {
if (distance > _get_probe_length(pos, hashes[pos], capacity, capacity_inv)) {
return false;
}
@@ -121,17 +123,18 @@ private:
return true;
}
pos = (pos + 1) % capacity;
pos = fastmod((pos + 1), capacity_inv, capacity);
distance++;
}
}
void _insert_with_hash(uint32_t p_hash, HashMapElement<TKey, TValue> *p_value) {
uint32_t capacity = hash_table_size_primes[capacity_index];
const uint32_t capacity = hash_table_size_primes[capacity_index];
const uint64_t capacity_inv = hash_table_size_primes_inv[capacity_index];
uint32_t hash = p_hash;
HashMapElement<TKey, TValue> *value = p_value;
uint32_t distance = 0;
uint32_t pos = hash % capacity;
uint32_t pos = fastmod(hash, capacity_inv, capacity);
while (true) {
if (hashes[pos] == EMPTY_HASH) {
@@ -144,14 +147,14 @@ private:
}
// Not an empty slot, let's check the probing length of the existing one.
uint32_t existing_probe_len = _get_probe_length(pos, hashes[pos], capacity);
uint32_t existing_probe_len = _get_probe_length(pos, hashes[pos], capacity, capacity_inv);
if (existing_probe_len < distance) {
SWAP(hash, hashes[pos]);
SWAP(value, elements[pos]);
distance = existing_probe_len;
}
pos = (pos + 1) % capacity;
pos = fastmod((pos + 1), capacity_inv, capacity);
distance++;
}
}
@@ -251,7 +254,7 @@ public:
}
void clear() {
if (elements == nullptr) {
if (elements == nullptr || num_elements == 0) {
return;
}
uint32_t capacity = hash_table_size_primes[capacity_index];
@@ -270,6 +273,47 @@ public:
num_elements = 0;
}
void sort() {
if (elements == nullptr || num_elements < 2) {
return; // An empty or single element HashMap is already sorted.
}
// Use insertion sort because we want this operation to be fast for the
// common case where the input is already sorted or nearly sorted.
HashMapElement<TKey, TValue> *inserting = head_element->next;
while (inserting != nullptr) {
HashMapElement<TKey, TValue> *after = nullptr;
for (HashMapElement<TKey, TValue> *current = inserting->prev; current != nullptr; current = current->prev) {
if (_hashmap_variant_less_than(inserting->data.key, current->data.key)) {
after = current;
} else {
break;
}
}
HashMapElement<TKey, TValue> *next = inserting->next;
if (after != nullptr) {
// Modify the elements around `inserting` to remove it from its current position.
inserting->prev->next = next;
if (next == nullptr) {
tail_element = inserting->prev;
} else {
next->prev = inserting->prev;
}
// Modify `before` and `after` to insert `inserting` between them.
HashMapElement<TKey, TValue> *before = after->prev;
if (before == nullptr) {
head_element = inserting;
} else {
before->next = inserting;
}
after->prev = inserting;
// Point `inserting` to its new surroundings.
inserting->prev = before;
inserting->next = after;
}
inserting = next;
}
}
TValue &get(const TKey &p_key) {
uint32_t pos = 0;
bool exists = _lookup_pos(p_key, pos);
@@ -317,13 +361,14 @@ public:
return false;
}
uint32_t capacity = hash_table_size_primes[capacity_index];
uint32_t next_pos = (pos + 1) % capacity;
while (hashes[next_pos] != EMPTY_HASH && _get_probe_length(next_pos, hashes[next_pos], capacity) != 0) {
const uint32_t capacity = hash_table_size_primes[capacity_index];
const uint64_t capacity_inv = hash_table_size_primes_inv[capacity_index];
uint32_t next_pos = fastmod((pos + 1), capacity_inv, capacity);
while (hashes[next_pos] != EMPTY_HASH && _get_probe_length(next_pos, hashes[next_pos], capacity, capacity_inv) != 0) {
SWAP(hashes[next_pos], hashes[pos]);
SWAP(elements[next_pos], elements[pos]);
pos = next_pos;
next_pos = (pos + 1) % capacity;
next_pos = fastmod((pos + 1), capacity_inv, capacity);
}
hashes[pos] = EMPTY_HASH;
@@ -351,6 +396,40 @@ public:
return true;
}
// Replace the key of an entry in-place, without invalidating iterators or changing the entries position during iteration.
// p_old_key must exist in the map and p_new_key must not, unless it is equal to p_old_key.
bool replace_key(const TKey &p_old_key, const TKey &p_new_key) {
if (p_old_key == p_new_key) {
return true;
}
uint32_t pos = 0;
ERR_FAIL_COND_V(_lookup_pos(p_new_key, pos), false);
ERR_FAIL_COND_V(!_lookup_pos(p_old_key, pos), false);
HashMapElement<TKey, TValue> *element = elements[pos];
// Delete the old entries in hashes and elements.
const uint32_t capacity = hash_table_size_primes[capacity_index];
const uint64_t capacity_inv = hash_table_size_primes_inv[capacity_index];
uint32_t next_pos = fastmod((pos + 1), capacity_inv, capacity);
while (hashes[next_pos] != EMPTY_HASH && _get_probe_length(next_pos, hashes[next_pos], capacity, capacity_inv) != 0) {
SWAP(hashes[next_pos], hashes[pos]);
SWAP(elements[next_pos], elements[pos]);
pos = next_pos;
next_pos = fastmod((pos + 1), capacity_inv, capacity);
}
hashes[pos] = EMPTY_HASH;
elements[pos] = nullptr;
// _insert_with_hash will increment this again.
num_elements--;
// Update the HashMapElement with the new key and reinsert it.
const_cast<TKey &>(element->data.key) = p_new_key;
uint32_t hash = _hash(p_new_key);
_insert_with_hash(hash, element);
return true;
}
// Reserves space for a number of elements, useful to avoid many resizes and rehashes.
// If adding a known (possibly large) number of elements at once, must be larger than old capacity.
void reserve(uint32_t p_new_capacity) {
@@ -561,6 +640,13 @@ public:
capacity_index = MIN_CAPACITY_INDEX;
}
HashMap(std::initializer_list<KeyValue<TKey, TValue>> p_init) {
reserve(p_init.size());
for (const KeyValue<TKey, TValue> &E : p_init) {
insert(E.key, E.value);
}
}
uint32_t debug_get_hash(uint32_t p_index) {
if (num_elements == 0) {
return 0;
@@ -587,5 +673,3 @@ public:
};
} // namespace godot
#endif // GODOT_HASH_MAP_HPP

View File

@@ -28,8 +28,7 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef GODOT_HASH_SET_HPP
#define GODOT_HASH_SET_HPP
#pragma once
#include <godot_cpp/core/error_macros.hpp>
#include <godot_cpp/core/memory.hpp>
@@ -76,19 +75,20 @@ private:
return hash;
}
_FORCE_INLINE_ uint32_t _get_probe_length(uint32_t p_pos, uint32_t p_hash, uint32_t p_capacity) const {
uint32_t original_pos = p_hash % p_capacity;
return (p_pos - original_pos + p_capacity) % p_capacity;
static _FORCE_INLINE_ uint32_t _get_probe_length(const uint32_t p_pos, const uint32_t p_hash, const uint32_t p_capacity, const uint64_t p_capacity_inv) {
const uint32_t original_pos = fastmod(p_hash, p_capacity_inv, p_capacity);
return fastmod(p_pos - original_pos + p_capacity, p_capacity_inv, p_capacity);
}
bool _lookup_pos(const TKey &p_key, uint32_t &r_pos) const {
if (keys == nullptr) {
if (keys == nullptr || num_elements == 0) {
return false; // Failed lookups, no elements
}
uint32_t capacity = hash_table_size_primes[capacity_index];
const uint32_t capacity = hash_table_size_primes[capacity_index];
const uint64_t capacity_inv = hash_table_size_primes_inv[capacity_index];
uint32_t hash = _hash(p_key);
uint32_t pos = hash % capacity;
uint32_t pos = fastmod(hash, capacity_inv, capacity);
uint32_t distance = 0;
while (true) {
@@ -96,7 +96,7 @@ private:
return false;
}
if (distance > _get_probe_length(pos, hashes[pos], capacity)) {
if (distance > _get_probe_length(pos, hashes[pos], capacity, capacity_inv)) {
return false;
}
@@ -105,17 +105,18 @@ private:
return true;
}
pos = (pos + 1) % capacity;
pos = fastmod(pos + 1, capacity_inv, capacity);
distance++;
}
}
uint32_t _insert_with_hash(uint32_t p_hash, uint32_t p_index) {
uint32_t capacity = hash_table_size_primes[capacity_index];
const uint32_t capacity = hash_table_size_primes[capacity_index];
const uint64_t capacity_inv = hash_table_size_primes_inv[capacity_index];
uint32_t hash = p_hash;
uint32_t index = p_index;
uint32_t distance = 0;
uint32_t pos = hash % capacity;
uint32_t pos = fastmod(hash, capacity_inv, capacity);
while (true) {
if (hashes[pos] == EMPTY_HASH) {
@@ -126,7 +127,7 @@ private:
}
// Not an empty slot, let's check the probing length of the existing one.
uint32_t existing_probe_len = _get_probe_length(pos, hashes[pos], capacity);
uint32_t existing_probe_len = _get_probe_length(pos, hashes[pos], capacity, capacity_inv);
if (existing_probe_len < distance) {
key_to_hash[index] = pos;
SWAP(hash, hashes[pos]);
@@ -134,7 +135,7 @@ private:
distance = existing_probe_len;
}
pos = (pos + 1) % capacity;
pos = fastmod(pos + 1, capacity_inv, capacity);
distance++;
}
}
@@ -237,7 +238,7 @@ public:
}
void clear() {
if (keys == nullptr) {
if (keys == nullptr || num_elements == 0) {
return;
}
uint32_t capacity = hash_table_size_primes[capacity_index];
@@ -265,11 +266,12 @@ public:
}
uint32_t key_pos = pos;
pos = key_to_hash[pos]; // make hash pos
pos = key_to_hash[pos]; //make hash pos
uint32_t capacity = hash_table_size_primes[capacity_index];
uint32_t next_pos = (pos + 1) % capacity;
while (hashes[next_pos] != EMPTY_HASH && _get_probe_length(next_pos, hashes[next_pos], capacity) != 0) {
const uint32_t capacity = hash_table_size_primes[capacity_index];
const uint64_t capacity_inv = hash_table_size_primes_inv[capacity_index];
uint32_t next_pos = fastmod(pos + 1, capacity_inv, capacity);
while (hashes[next_pos] != EMPTY_HASH && _get_probe_length(next_pos, hashes[next_pos], capacity, capacity_inv) != 0) {
uint32_t kpos = hash_to_key[pos];
uint32_t kpos_next = hash_to_key[next_pos];
SWAP(key_to_hash[kpos], key_to_hash[kpos_next]);
@@ -277,7 +279,7 @@ public:
SWAP(hash_to_key[next_pos], hash_to_key[pos]);
pos = next_pos;
next_pos = (pos + 1) % capacity;
next_pos = fastmod(pos + 1, capacity_inv, capacity);
}
hashes[pos] = EMPTY_HASH;
@@ -444,6 +446,13 @@ public:
capacity_index = MIN_CAPACITY_INDEX;
}
HashSet(std::initializer_list<TKey> p_init) {
reserve(p_init.size());
for (const TKey &E : p_init) {
insert(E);
}
}
void reset() {
clear();
@@ -473,5 +482,3 @@ public:
};
} // namespace godot
#endif // GODOT_HASH_SET_HPP

View File

@@ -28,8 +28,7 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef GODOT_HASHFUNCS_HPP
#define GODOT_HASHFUNCS_HPP
#pragma once
// Needed for fastmod.
#if defined(_MSC_VER)
@@ -67,10 +66,11 @@ namespace godot {
static _FORCE_INLINE_ uint32_t hash_djb2(const char *p_cstr) {
const unsigned char *chr = (const unsigned char *)p_cstr;
uint32_t hash = 5381;
uint32_t c;
uint32_t c = *chr++;
while ((c = *chr++)) {
while (c) {
hash = ((hash << 5) + hash) ^ c; /* hash * 33 ^ c */
c = *chr++;
}
return hash;
@@ -108,6 +108,16 @@ static _FORCE_INLINE_ uint32_t hash_one_uint64(const uint64_t p_int) {
return uint32_t(v);
}
static _FORCE_INLINE_ uint64_t hash64_murmur3_64(uint64_t key, uint64_t seed) {
key ^= seed;
key ^= key >> 33;
key *= 0xff51afd7ed558ccd;
key ^= key >> 33;
key *= 0xc4ceb9fe1a85ec53;
key ^= key >> 33;
return key;
}
#define HASH_MURMUR3_SEED 0x7F07C65
// Murmurhash3 32-bit version.
// All MurmurHash versions are public domain software, and the author disclaims all copyright to their code.
@@ -228,7 +238,7 @@ static _FORCE_INLINE_ uint32_t hash_murmur3_buffer(const void *key, int length,
k1 = hash_rotl32(k1, 15);
k1 *= c2;
h1 ^= k1;
}
};
// Finalize with additional bit mixing.
h1 ^= length;
@@ -311,40 +321,41 @@ struct HashMapHasherDefault {
static _FORCE_INLINE_ uint32_t hash(const String &p_string) { return p_string.hash(); }
static _FORCE_INLINE_ uint32_t hash(const char *p_cstr) { return hash_djb2(p_cstr); }
static _FORCE_INLINE_ uint32_t hash(const wchar_t p_wchar) { return hash_fmix32(p_wchar); }
static _FORCE_INLINE_ uint32_t hash(const char16_t p_uchar) { return hash_fmix32(p_uchar); }
static _FORCE_INLINE_ uint32_t hash(const char32_t p_uchar) { return hash_fmix32(p_uchar); }
static _FORCE_INLINE_ uint32_t hash(const wchar_t p_wchar) { return hash_fmix32(uint32_t(p_wchar)); }
static _FORCE_INLINE_ uint32_t hash(const char16_t p_uchar) { return hash_fmix32(uint32_t(p_uchar)); }
static _FORCE_INLINE_ uint32_t hash(const char32_t p_uchar) { return hash_fmix32(uint32_t(p_uchar)); }
static _FORCE_INLINE_ uint32_t hash(const RID &p_rid) { return hash_one_uint64(p_rid.get_id()); }
static _FORCE_INLINE_ uint32_t hash(const CharString &p_char_string) { return hash_djb2(p_char_string.get_data()); }
static _FORCE_INLINE_ uint32_t hash(const StringName &p_string_name) { return p_string_name.hash(); }
static _FORCE_INLINE_ uint32_t hash(const NodePath &p_path) { return p_path.hash(); }
static _FORCE_INLINE_ uint32_t hash(const ObjectID &p_id) { return hash_one_uint64(p_id); }
static _FORCE_INLINE_ uint32_t hash(const uint64_t p_int) { return hash_one_uint64(p_int); }
static _FORCE_INLINE_ uint32_t hash(const int64_t p_int) { return hash_one_uint64(p_int); }
static _FORCE_INLINE_ uint32_t hash(const int64_t p_int) { return hash_one_uint64(uint64_t(p_int)); }
static _FORCE_INLINE_ uint32_t hash(const float p_float) { return hash_murmur3_one_float(p_float); }
static _FORCE_INLINE_ uint32_t hash(const double p_double) { return hash_murmur3_one_double(p_double); }
static _FORCE_INLINE_ uint32_t hash(const uint32_t p_int) { return hash_fmix32(p_int); }
static _FORCE_INLINE_ uint32_t hash(const int32_t p_int) { return hash_fmix32(p_int); }
static _FORCE_INLINE_ uint32_t hash(const uint16_t p_int) { return hash_fmix32(p_int); }
static _FORCE_INLINE_ uint32_t hash(const int16_t p_int) { return hash_fmix32(p_int); }
static _FORCE_INLINE_ uint32_t hash(const uint8_t p_int) { return hash_fmix32(p_int); }
static _FORCE_INLINE_ uint32_t hash(const int8_t p_int) { return hash_fmix32(p_int); }
static _FORCE_INLINE_ uint32_t hash(const int32_t p_int) { return hash_fmix32(uint32_t(p_int)); }
static _FORCE_INLINE_ uint32_t hash(const uint16_t p_int) { return hash_fmix32(uint32_t(p_int)); }
static _FORCE_INLINE_ uint32_t hash(const int16_t p_int) { return hash_fmix32(uint32_t(p_int)); }
static _FORCE_INLINE_ uint32_t hash(const uint8_t p_int) { return hash_fmix32(uint32_t(p_int)); }
static _FORCE_INLINE_ uint32_t hash(const int8_t p_int) { return hash_fmix32(uint32_t(p_int)); }
static _FORCE_INLINE_ uint32_t hash(const Vector2i &p_vec) {
uint32_t h = hash_murmur3_one_32(p_vec.x);
h = hash_murmur3_one_32(p_vec.y, h);
uint32_t h = hash_murmur3_one_32(uint32_t(p_vec.x));
h = hash_murmur3_one_32(uint32_t(p_vec.y), h);
return hash_fmix32(h);
}
static _FORCE_INLINE_ uint32_t hash(const Vector3i &p_vec) {
uint32_t h = hash_murmur3_one_32(p_vec.x);
h = hash_murmur3_one_32(p_vec.y, h);
h = hash_murmur3_one_32(p_vec.z, h);
uint32_t h = hash_murmur3_one_32(uint32_t(p_vec.x));
h = hash_murmur3_one_32(uint32_t(p_vec.y), h);
h = hash_murmur3_one_32(uint32_t(p_vec.z), h);
return hash_fmix32(h);
}
static _FORCE_INLINE_ uint32_t hash(const Vector4i &p_vec) {
uint32_t h = hash_murmur3_one_32(p_vec.x);
h = hash_murmur3_one_32(p_vec.y, h);
h = hash_murmur3_one_32(p_vec.z, h);
h = hash_murmur3_one_32(p_vec.w, h);
uint32_t h = hash_murmur3_one_32(uint32_t(p_vec.x));
h = hash_murmur3_one_32(uint32_t(p_vec.y), h);
h = hash_murmur3_one_32(uint32_t(p_vec.z), h);
h = hash_murmur3_one_32(uint32_t(p_vec.w), h);
return hash_fmix32(h);
}
static _FORCE_INLINE_ uint32_t hash(const Vector2 &p_vec) {
@@ -366,10 +377,10 @@ struct HashMapHasherDefault {
return hash_fmix32(h);
}
static _FORCE_INLINE_ uint32_t hash(const Rect2i &p_rect) {
uint32_t h = hash_murmur3_one_32(p_rect.position.x);
h = hash_murmur3_one_32(p_rect.position.y, h);
h = hash_murmur3_one_32(p_rect.size.x, h);
h = hash_murmur3_one_32(p_rect.size.y, h);
uint32_t h = hash_murmur3_one_32(uint32_t(p_rect.position.x));
h = hash_murmur3_one_32(uint32_t(p_rect.position.y), h);
h = hash_murmur3_one_32(uint32_t(p_rect.size.x), h);
h = hash_murmur3_one_32(uint32_t(p_rect.size.y), h);
return hash_fmix32(h);
}
static _FORCE_INLINE_ uint32_t hash(const Rect2 &p_rect) {
@@ -390,6 +401,19 @@ struct HashMapHasherDefault {
}
};
struct HashHasher {
static _FORCE_INLINE_ uint32_t hash(const int32_t hash) { return hash; }
static _FORCE_INLINE_ uint32_t hash(const uint32_t hash) { return hash; }
static _FORCE_INLINE_ uint64_t hash(const int64_t hash) { return hash; }
static _FORCE_INLINE_ uint64_t hash(const uint64_t hash) { return hash; }
};
// TODO: Fold this into HashMapHasherDefault once C++20 concepts are allowed
template <typename T>
struct HashableHasher {
static _FORCE_INLINE_ uint32_t hash(const T &hashable) { return hashable.hash(); }
};
template <typename T>
struct HashMapComparatorDefault {
static bool compare(const T &p_lhs, const T &p_rhs) {
@@ -411,6 +435,13 @@ struct HashMapComparatorDefault<double> {
}
};
template <>
struct HashMapComparatorDefault<Color> {
static bool compare(const Color &p_lhs, const Color &p_rhs) {
return ((p_lhs.r == p_rhs.r) || (Math::is_nan(p_lhs.r) && Math::is_nan(p_rhs.r))) && ((p_lhs.g == p_rhs.g) || (Math::is_nan(p_lhs.g) && Math::is_nan(p_rhs.g))) && ((p_lhs.b == p_rhs.b) || (Math::is_nan(p_lhs.b) && Math::is_nan(p_rhs.b))) && ((p_lhs.a == p_rhs.a) || (Math::is_nan(p_lhs.a) && Math::is_nan(p_rhs.a)));
}
};
template <>
struct HashMapComparatorDefault<Vector2> {
static bool compare(const Vector2 &p_lhs, const Vector2 &p_rhs) {
@@ -425,9 +456,90 @@ struct HashMapComparatorDefault<Vector3> {
}
};
template <>
struct HashMapComparatorDefault<Vector4> {
static bool compare(const Vector4 &p_lhs, const Vector4 &p_rhs) {
return ((p_lhs.x == p_rhs.x) || (Math::is_nan(p_lhs.x) && Math::is_nan(p_rhs.x))) && ((p_lhs.y == p_rhs.y) || (Math::is_nan(p_lhs.y) && Math::is_nan(p_rhs.y))) && ((p_lhs.z == p_rhs.z) || (Math::is_nan(p_lhs.z) && Math::is_nan(p_rhs.z))) && ((p_lhs.w == p_rhs.w) || (Math::is_nan(p_lhs.w) && Math::is_nan(p_rhs.w)));
}
};
template <>
struct HashMapComparatorDefault<Rect2> {
static bool compare(const Rect2 &p_lhs, const Rect2 &p_rhs) {
return HashMapComparatorDefault<Vector2>().compare(p_lhs.position, p_rhs.position) && HashMapComparatorDefault<Vector2>().compare(p_lhs.size, p_rhs.size);
}
};
template <>
struct HashMapComparatorDefault<AABB> {
static bool compare(const AABB &p_lhs, const AABB &p_rhs) {
return HashMapComparatorDefault<Vector3>().compare(p_lhs.position, p_rhs.position) && HashMapComparatorDefault<Vector3>().compare(p_lhs.size, p_rhs.size);
}
};
template <>
struct HashMapComparatorDefault<Plane> {
static bool compare(const Plane &p_lhs, const Plane &p_rhs) {
return HashMapComparatorDefault<Vector3>().compare(p_lhs.normal, p_rhs.normal) && ((p_lhs.d == p_rhs.d) || (Math::is_nan(p_lhs.d) && Math::is_nan(p_rhs.d)));
}
};
template <>
struct HashMapComparatorDefault<Transform2D> {
static bool compare(const Transform2D &p_lhs, const Transform2D &p_rhs) {
for (int i = 0; i < 3; ++i) {
if (!HashMapComparatorDefault<Vector2>().compare(p_lhs.columns[i], p_rhs.columns[i])) {
return false;
}
}
return true;
}
};
template <>
struct HashMapComparatorDefault<Basis> {
static bool compare(const Basis &p_lhs, const Basis &p_rhs) {
for (int i = 0; i < 3; ++i) {
if (!HashMapComparatorDefault<Vector3>().compare(p_lhs.rows[i], p_rhs.rows[i])) {
return false;
}
}
return true;
}
};
template <>
struct HashMapComparatorDefault<Transform3D> {
static bool compare(const Transform3D &p_lhs, const Transform3D &p_rhs) {
return HashMapComparatorDefault<Basis>().compare(p_lhs.basis, p_rhs.basis) && HashMapComparatorDefault<Vector3>().compare(p_lhs.origin, p_rhs.origin);
}
};
template <>
struct HashMapComparatorDefault<Projection> {
static bool compare(const Projection &p_lhs, const Projection &p_rhs) {
for (int i = 0; i < 4; ++i) {
if (!HashMapComparatorDefault<Vector4>().compare(p_lhs.columns[i], p_rhs.columns[i])) {
return false;
}
}
return true;
}
};
template <>
struct HashMapComparatorDefault<Quaternion> {
static bool compare(const Quaternion &p_lhs, const Quaternion &p_rhs) {
return ((p_lhs.x == p_rhs.x) || (Math::is_nan(p_lhs.x) && Math::is_nan(p_rhs.x))) && ((p_lhs.y == p_rhs.y) || (Math::is_nan(p_lhs.y) && Math::is_nan(p_rhs.y))) && ((p_lhs.z == p_rhs.z) || (Math::is_nan(p_lhs.z) && Math::is_nan(p_rhs.z))) && ((p_lhs.w == p_rhs.w) || (Math::is_nan(p_lhs.w) && Math::is_nan(p_rhs.w)));
}
};
constexpr uint32_t HASH_TABLE_SIZE_MAX = 29;
const uint32_t hash_table_size_primes[HASH_TABLE_SIZE_MAX] = {
inline constexpr uint32_t hash_table_size_primes[HASH_TABLE_SIZE_MAX] = {
5,
13,
23,
@@ -460,7 +572,7 @@ const uint32_t hash_table_size_primes[HASH_TABLE_SIZE_MAX] = {
};
// Computed with elem_i = UINT64_C (0 x FFFFFFFF FFFFFFFF ) / d_i + 1, where d_i is the i-th element of the above array.
const uint64_t hash_table_size_primes_inv[HASH_TABLE_SIZE_MAX] = {
inline constexpr uint64_t hash_table_size_primes_inv[HASH_TABLE_SIZE_MAX] = {
3689348814741910324,
1418980313362273202,
802032351030850071,
@@ -522,5 +634,3 @@ static _FORCE_INLINE_ uint32_t fastmod(const uint32_t n, const uint64_t c, const
}
} // namespace godot
#endif // GODOT_HASHFUNCS_HPP

View File

@@ -28,13 +28,14 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef GODOT_LIST_HPP
#define GODOT_LIST_HPP
#pragma once
#include <godot_cpp/core/error_macros.hpp>
#include <godot_cpp/core/memory.hpp>
#include <godot_cpp/templates/sort_array.hpp>
#include <initializer_list>
/**
* Generic Templatized Linked List Implementation.
* The implementation differs from the STL one because
@@ -134,6 +135,8 @@ public:
data->erase(this);
}
void transfer_to_back(List<T, A> *p_dst_list);
_FORCE_INLINE_ Element() {}
};
@@ -224,7 +227,7 @@ private:
Element *last = nullptr;
int size_cache = 0;
bool erase(const Element *p_I) {
bool erase(Element *p_I) {
ERR_FAIL_NULL_V(p_I, false);
ERR_FAIL_COND_V(p_I->data != this, false);
@@ -244,7 +247,7 @@ private:
p_I->next_ptr->prev_ptr = p_I->prev_ptr;
}
memdelete_allocator<Element, A>(const_cast<Element *>(p_I));
memdelete_allocator<Element, A>(p_I);
size_cache--;
return true;
@@ -430,7 +433,7 @@ public:
/**
* erase an element in the list, by iterator pointing to it. Return true if it was found/erased.
*/
bool erase(const Element *p_I) {
bool erase(Element *p_I) {
if (_data && p_I) {
bool ret = _data->erase(p_I);
@@ -522,10 +525,14 @@ public:
it = it->next();
}
}
void operator=(List &&p_list) {
if (unlikely(this == &p_list)) {
return;
}
// Index operator, kept for compatibility.
_FORCE_INLINE_ T &operator[](int p_index) {
return get(p_index);
clear();
_data = p_list._data;
p_list._data = nullptr;
}
// Random access to elements, use with care,
@@ -543,11 +550,6 @@ public:
return I->get();
}
// Index operator, kept for compatibility.
_FORCE_INLINE_ const T &operator[](int p_index) const {
return get(p_index);
}
// Random access to elements, use with care,
// do not use for iteration.
const T &get(int p_index) const {
@@ -721,8 +723,8 @@ public:
template <typename C>
void sort_custom() {
// this version uses auxiliary memory for speed.
// if you don't want to use auxiliary memory, use the in_place version
//this version uses auxiliary memory for speed.
//if you don't want to use auxiliary memory, use the in_place version
int s = size();
if (s < 2) {
@@ -770,9 +772,19 @@ public:
it = it->next();
}
}
List(List &&p_list) {
_data = p_list._data;
p_list._data = nullptr;
}
List() {}
List(std::initializer_list<T> p_init) {
for (const T &E : p_init) {
push_back(E);
}
}
~List() {
clear();
if (_data) {
@@ -782,6 +794,41 @@ public:
}
};
} // namespace godot
template <typename T, typename A>
void List<T, A>::Element::transfer_to_back(List<T, A> *p_dst_list) {
// Detach from current.
#endif // GODOT_LIST_HPP
if (data->first == this) {
data->first = data->first->next_ptr;
}
if (data->last == this) {
data->last = data->last->prev_ptr;
}
if (prev_ptr) {
prev_ptr->next_ptr = next_ptr;
}
if (next_ptr) {
next_ptr->prev_ptr = prev_ptr;
}
data->size_cache--;
// Attach to the back of the new one.
if (!p_dst_list->_data) {
p_dst_list->_data = memnew_allocator(_Data, A);
p_dst_list->_data->first = this;
p_dst_list->_data->last = nullptr;
p_dst_list->_data->size_cache = 0;
prev_ptr = nullptr;
} else {
p_dst_list->_data->last->next_ptr = this;
prev_ptr = p_dst_list->_data->last;
}
p_dst_list->_data->last = this;
next_ptr = nullptr;
data = p_dst_list->_data;
p_dst_list->_data->size_cache++;
}
} // namespace godot

View File

@@ -28,8 +28,7 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef GODOT_LOCAL_VECTOR_HPP
#define GODOT_LOCAL_VECTOR_HPP
#pragma once
#include <godot_cpp/core/error_macros.hpp>
#include <godot_cpp/core/memory.hpp>
@@ -59,21 +58,18 @@ public:
return data;
}
// Must take a copy instead of a reference (see GH-31736).
_FORCE_INLINE_ void push_back(T p_elem) {
if (unlikely(count == capacity)) {
if (capacity == 0) {
capacity = 1;
} else {
capacity <<= 1;
}
capacity = tight ? (capacity + 1) : MAX((U)1, capacity << 1);
data = (T *)memrealloc(data, capacity * sizeof(T));
CRASH_COND_MSG(!data, "Out of memory");
}
if constexpr (!std::is_trivially_constructible<T>::value && !force_trivial) {
if constexpr (!std::is_trivially_constructible_v<T> && !force_trivial) {
memnew_placement(&data[count++], T(p_elem));
} else {
data[count++] = p_elem;
data[count++] = std::move(p_elem);
}
}
@@ -81,31 +77,49 @@ public:
ERR_FAIL_UNSIGNED_INDEX(p_index, count);
count--;
for (U i = p_index; i < count; i++) {
data[i] = data[i + 1];
data[i] = std::move(data[i + 1]);
}
if constexpr (!std::is_trivially_destructible<T>::value && !force_trivial) {
if constexpr (!std::is_trivially_destructible_v<T> && !force_trivial) {
data[count].~T();
}
}
/// Removes the item copying the last value into the position of the one to
/// remove. It's generally faster than `remove`.
/// remove. It's generally faster than `remove_at`.
void remove_at_unordered(U p_index) {
ERR_FAIL_INDEX(p_index, count);
count--;
if (count > p_index) {
data[p_index] = data[count];
data[p_index] = std::move(data[count]);
}
if constexpr (!std::is_trivially_destructible<T>::value && !force_trivial) {
if constexpr (!std::is_trivially_destructible_v<T> && !force_trivial) {
data[count].~T();
}
}
void erase(const T &p_val) {
_FORCE_INLINE_ bool erase(const T &p_val) {
int64_t idx = find(p_val);
if (idx >= 0) {
remove_at(idx);
return true;
}
return false;
}
U erase_multiple_unordered(const T &p_val) {
U from = 0;
U occurrences = 0;
while (true) {
int64_t idx = find(p_val, from);
if (idx == -1) {
break;
}
remove_at_unordered(idx);
from = idx;
occurrences++;
}
return occurrences;
}
void invert() {
@@ -137,7 +151,7 @@ public:
_FORCE_INLINE_ U size() const { return count; }
void resize(U p_size) {
if (p_size < count) {
if constexpr (!std::is_trivially_destructible<T>::value && !force_trivial) {
if constexpr (!std::is_trivially_destructible_v<T> && !force_trivial) {
for (U i = p_size; i < count; i++) {
data[i].~T();
}
@@ -145,16 +159,11 @@ public:
count = p_size;
} else if (p_size > count) {
if (unlikely(p_size > capacity)) {
if (capacity == 0) {
capacity = 1;
}
while (capacity < p_size) {
capacity <<= 1;
}
capacity = tight ? p_size : nearest_power_of_2_templated(p_size);
data = (T *)memrealloc(data, capacity * sizeof(T));
CRASH_COND_MSG(!data, "Out of memory");
}
if constexpr (!std::is_trivially_constructible<T>::value && !force_trivial) {
if constexpr (!std::is_trivially_constructible_v<T> && !force_trivial) {
for (U i = count; i < p_size; i++) {
memnew_placement(&data[i], T);
}
@@ -238,13 +247,13 @@ public:
void insert(U p_pos, T p_val) {
ERR_FAIL_UNSIGNED_INDEX(p_pos, count + 1);
if (p_pos == count) {
push_back(p_val);
push_back(std::move(p_val));
} else {
resize(count + 1);
for (U i = count - 1; i > p_pos; i--) {
data[i] = data[i - 1];
data[i] = std::move(data[i - 1]);
}
data[p_pos] = p_val;
data[p_pos] = std::move(p_val);
}
}
@@ -288,9 +297,17 @@ public:
operator Vector<T>() const {
Vector<T> ret;
ret.resize(size());
ret.resize(count);
T *w = ret.ptrw();
memcpy(w, data, sizeof(T) * count);
if (w) {
if constexpr (std::is_trivially_copyable_v<T>) {
memcpy(w, data, sizeof(T) * count);
} else {
for (U i = 0; i < count; i++) {
w[i] = data[i];
}
}
}
return ret;
}
@@ -298,7 +315,9 @@ public:
Vector<uint8_t> ret;
ret.resize(count * sizeof(T));
uint8_t *w = ret.ptrw();
memcpy(w, data, sizeof(T) * count);
if (w) {
memcpy(w, data, sizeof(T) * count);
}
return ret;
}
@@ -315,6 +334,16 @@ public:
data[i] = p_from.data[i];
}
}
_FORCE_INLINE_ LocalVector(LocalVector &&p_from) {
data = p_from.data;
count = p_from.count;
capacity = p_from.capacity;
p_from.data = nullptr;
p_from.count = 0;
p_from.capacity = 0;
}
inline void operator=(const LocalVector &p_from) {
resize(p_from.size());
for (U i = 0; i < p_from.count; i++) {
@@ -327,6 +356,26 @@ public:
data[i] = p_from[i];
}
}
inline void operator=(LocalVector &&p_from) {
if (unlikely(this == &p_from)) {
return;
}
reset();
data = p_from.data;
count = p_from.count;
capacity = p_from.capacity;
p_from.data = nullptr;
p_from.count = 0;
p_from.capacity = 0;
}
inline void operator=(Vector<T> &&p_from) {
resize(p_from.size());
for (U i = 0; i < count; i++) {
data[i] = std::move(p_from[i]);
}
}
_FORCE_INLINE_ ~LocalVector() {
if (data) {
@@ -339,5 +388,3 @@ template <typename T, typename U = uint32_t, bool force_trivial = false>
using TightLocalVector = LocalVector<T, U, force_trivial, true>;
} // namespace godot
#endif // GODOT_LOCAL_VECTOR_HPP

View File

@@ -28,8 +28,9 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef GODOT_PAIR_HPP
#define GODOT_PAIR_HPP
#pragma once
#include <godot_cpp/templates/hashfuncs.hpp>
namespace godot {
@@ -69,6 +70,15 @@ struct PairSort {
}
};
template <typename F, typename S>
struct PairHash {
static uint32_t hash(const Pair<F, S> &P) {
uint64_t h1 = HashMapHasherDefault::hash(P.first);
uint64_t h2 = HashMapHasherDefault::hash(P.second);
return hash_one_uint64((h1 << 32) | h2);
}
};
template <typename K, typename V>
struct KeyValue {
const K key;
@@ -103,5 +113,3 @@ struct KeyValueSort {
};
} // namespace godot
#endif // GODOT_PAIR_HPP

View File

@@ -28,13 +28,14 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef GODOT_RB_MAP_HPP
#define GODOT_RB_MAP_HPP
#pragma once
#include <godot_cpp/core/error_macros.hpp>
#include <godot_cpp/core/memory.hpp>
#include <godot_cpp/templates/pair.hpp>
#include <initializer_list>
namespace godot {
// based on the very nice implementation of rb-trees by:
@@ -98,6 +99,8 @@ public:
typedef KeyValue<K, V> ValueType;
struct Iterator {
friend class RBMap<K, V, C, A>;
_FORCE_INLINE_ KeyValue<K, V> &operator*() const {
return E->key_value();
}
@@ -111,11 +114,16 @@ public:
return *this;
}
_FORCE_INLINE_ bool operator==(const Iterator &b) const { return E == b.E; }
_FORCE_INLINE_ bool operator!=(const Iterator &b) const { return E != b.E; }
_FORCE_INLINE_ bool operator==(const Iterator &p_it) const { return E == p_it.E; }
_FORCE_INLINE_ bool operator!=(const Iterator &p_it) const { return E != p_it.E; }
explicit operator bool() const {
return E != nullptr;
}
Iterator &operator=(const Iterator &p_it) {
E = p_it.E;
return *this;
}
Iterator(Element *p_E) { E = p_E; }
Iterator() {}
Iterator(const Iterator &p_it) { E = p_it.E; }
@@ -138,11 +146,16 @@ public:
return *this;
}
_FORCE_INLINE_ bool operator==(const ConstIterator &b) const { return E == b.E; }
_FORCE_INLINE_ bool operator!=(const ConstIterator &b) const { return E != b.E; }
_FORCE_INLINE_ bool operator==(const ConstIterator &p_it) const { return E == p_it.E; }
_FORCE_INLINE_ bool operator!=(const ConstIterator &p_it) const { return E != p_it.E; }
explicit operator bool() const {
return E != nullptr;
}
ConstIterator &operator=(const ConstIterator &p_it) {
E = p_it.E;
return *this;
}
ConstIterator(const Element *p_E) { E = p_E; }
ConstIterator() {}
ConstIterator(const ConstIterator &p_it) { E = p_it.E; }
@@ -419,7 +432,7 @@ private:
new_node->right = _data._nil;
new_node->left = _data._nil;
// new_node->data=_data;
//new_node->data=_data;
if (new_parent == _data._root || less(p_key, new_parent->_data.key)) {
new_parent->left = new_node;
@@ -753,6 +766,12 @@ public:
_copy_from(p_map);
}
RBMap(std::initializer_list<KeyValue<K, V>> p_init) {
for (const KeyValue<K, V> &E : p_init) {
insert(E.key, E.value);
}
}
_FORCE_INLINE_ RBMap() {}
~RBMap() {
@@ -761,5 +780,3 @@ public:
};
} // namespace godot
#endif // GODOT_RB_MAP_HPP

View File

@@ -28,11 +28,12 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef GODOT_RB_SET_HPP
#define GODOT_RB_SET_HPP
#pragma once
#include <godot_cpp/core/memory.hpp>
#include <initializer_list>
// based on the very nice implementation of rb-trees by:
// https://web.archive.org/web/20120507164830/https://web.mit.edu/~emin/www/source_code/red_black_tree/index.html
@@ -399,7 +400,7 @@ private:
new_node->right = _data._nil;
new_node->left = _data._nil;
new_node->value = p_value;
// new_node->data=_data;
//new_node->data=_data;
if (new_parent == _data._root || less(p_value, new_parent->value)) {
new_parent->left = new_node;
@@ -702,6 +703,12 @@ public:
_copy_from(p_set);
}
RBSet(std::initializer_list<T> p_init) {
for (const T &E : p_init) {
insert(E);
}
}
_FORCE_INLINE_ RBSet() {}
~RBSet() {
@@ -710,5 +717,3 @@ public:
};
} // namespace godot
#endif // GODOT_RB_SET_HPP

View File

@@ -28,8 +28,7 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef GODOT_RID_OWNER_HPP
#define GODOT_RID_OWNER_HPP
#pragma once
#include <godot_cpp/core/memory.hpp>
#include <godot_cpp/godot.hpp>
@@ -461,5 +460,3 @@ public:
};
} // namespace godot
#endif // GODOT_RID_OWNER_HPP

View File

@@ -28,8 +28,7 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef GODOT_SAFE_REFCOUNT_HPP
#define GODOT_SAFE_REFCOUNT_HPP
#pragma once
#if !defined(NO_THREADS)
@@ -52,7 +51,7 @@ namespace godot {
#define SAFE_NUMERIC_TYPE_PUN_GUARANTEES(m_type) \
static_assert(sizeof(SafeNumeric<m_type>) == sizeof(m_type)); \
static_assert(alignof(SafeNumeric<m_type>) == alignof(m_type)); \
static_assert(std::is_trivially_destructible<std::atomic<m_type>>::value);
static_assert(std::is_trivially_destructible_v<std::atomic<m_type>>);
#define SAFE_FLAG_TYPE_PUN_GUARANTEES \
static_assert(sizeof(SafeFlag) == sizeof(bool)); \
static_assert(alignof(SafeFlag) == alignof(bool));
@@ -103,6 +102,17 @@ public:
return value.fetch_sub(p_value, std::memory_order_acq_rel) - p_value;
}
_ALWAYS_INLINE_ T bit_or(T p_value) {
return value.fetch_or(p_value, std::memory_order_acq_rel);
}
_ALWAYS_INLINE_ T bit_and(T p_value) {
return value.fetch_and(p_value, std::memory_order_acq_rel);
}
_ALWAYS_INLINE_ T bit_xor(T p_value) {
return value.fetch_xor(p_value, std::memory_order_acq_rel);
}
// Returns the original value instead of the new one
_ALWAYS_INLINE_ T postsub(T p_value) {
return value.fetch_sub(p_value, std::memory_order_acq_rel);
@@ -114,7 +124,8 @@ public:
if (tmp >= p_value) {
return tmp; // already greater, or equal
}
if (value.compare_exchange_weak(tmp, p_value, std::memory_order_release)) {
if (value.compare_exchange_weak(tmp, p_value, std::memory_order_acq_rel)) {
return p_value;
}
}
@@ -126,7 +137,7 @@ public:
if (c == 0) {
return 0;
}
if (value.compare_exchange_weak(c, c + 1, std::memory_order_release)) {
if (value.compare_exchange_weak(c, c + 1, std::memory_order_acq_rel)) {
return c + 1;
}
}
@@ -167,6 +178,16 @@ public:
class SafeRefCount {
SafeNumeric<uint32_t> count;
#ifdef DEV_ENABLED
_ALWAYS_INLINE_ void _check_unref_safety() {
// This won't catch every misuse, but it's better than nothing.
CRASH_COND_MSG(count.get() == 0,
"Trying to unreference a SafeRefCount which is already zero is wrong and a symptom of it being misused.\n"
"Upon a SafeRefCount reaching zero any object whose lifetime is tied to it, as well as the ref count itself, must be destroyed.\n"
"Moreover, to guarantee that, no multiple threads should be racing to do the final unreferencing to zero.");
}
#endif
public:
_ALWAYS_INLINE_ bool ref() { // true on success
return count.conditional_increment() != 0;
@@ -177,10 +198,16 @@ public:
}
_ALWAYS_INLINE_ bool unref() { // true if must be disposed of
#ifdef DEV_ENABLED
_check_unref_safety();
#endif
return count.decrement() == 0;
}
_ALWAYS_INLINE_ uint32_t unrefval() { // 0 if must be disposed of
#ifdef DEV_ENABLED
_check_unref_safety();
#endif
return count.decrement();
}
@@ -193,143 +220,6 @@ public:
}
};
#else
template <typename T>
class SafeNumeric {
protected:
T value;
public:
_ALWAYS_INLINE_ void set(T p_value) {
value = p_value;
}
_ALWAYS_INLINE_ T get() const {
return value;
}
_ALWAYS_INLINE_ T increment() {
return ++value;
}
_ALWAYS_INLINE_ T postincrement() {
return value++;
}
_ALWAYS_INLINE_ T decrement() {
return --value;
}
_ALWAYS_INLINE_ T postdecrement() {
return value--;
}
_ALWAYS_INLINE_ T add(T p_value) {
return value += p_value;
}
_ALWAYS_INLINE_ T postadd(T p_value) {
T old = value;
value += p_value;
return old;
}
_ALWAYS_INLINE_ T sub(T p_value) {
return value -= p_value;
}
_ALWAYS_INLINE_ T postsub(T p_value) {
T old = value;
value -= p_value;
return old;
}
_ALWAYS_INLINE_ T exchange_if_greater(T p_value) {
if (value < p_value) {
value = p_value;
}
return value;
}
_ALWAYS_INLINE_ T conditional_increment() {
if (value == 0) {
return 0;
} else {
return ++value;
}
}
_ALWAYS_INLINE_ explicit SafeNumeric<T>(T p_value = static_cast<T>(0)) :
value(p_value) {
}
};
class SafeFlag {
protected:
bool flag;
public:
_ALWAYS_INLINE_ bool is_set() const {
return flag;
}
_ALWAYS_INLINE_ void set() {
flag = true;
}
_ALWAYS_INLINE_ void clear() {
flag = false;
}
_ALWAYS_INLINE_ void set_to(bool p_value) {
flag = p_value;
}
_ALWAYS_INLINE_ explicit SafeFlag(bool p_value = false) :
flag(p_value) {}
};
class SafeRefCount {
uint32_t count = 0;
public:
_ALWAYS_INLINE_ bool ref() { // true on success
if (count != 0) {
++count;
return true;
} else {
return false;
}
}
_ALWAYS_INLINE_ uint32_t refval() { // none-zero on success
if (count != 0) {
return ++count;
} else {
return 0;
}
}
_ALWAYS_INLINE_ bool unref() { // true if must be disposed of
return --count == 0;
}
_ALWAYS_INLINE_ uint32_t unrefval() { // 0 if must be disposed of
return --count;
}
_ALWAYS_INLINE_ uint32_t get() const {
return count;
}
_ALWAYS_INLINE_ void init(uint32_t p_value = 1) {
count = p_value;
}
};
#endif
} // namespace godot
#endif // GODOT_SAFE_REFCOUNT_HPP
#endif // !defined(NO_THREADS)

View File

@@ -28,8 +28,7 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef GODOT_SEARCH_ARRAY_HPP
#define GODOT_SEARCH_ARRAY_HPP
#pragma once
#include <godot_cpp/templates/sort_array.hpp>
@@ -40,12 +39,12 @@ class SearchArray {
public:
Comparator compare;
inline int bisect(const T *p_array, int p_len, const T &p_value, bool p_before) const {
int lo = 0;
int hi = p_len;
inline int64_t bisect(const T *p_array, int64_t p_len, const T &p_value, bool p_before) const {
int64_t lo = 0;
int64_t hi = p_len;
if (p_before) {
while (lo < hi) {
const int mid = (lo + hi) / 2;
const int64_t mid = (lo + hi) / 2;
if (compare(p_array[mid], p_value)) {
lo = mid + 1;
} else {
@@ -54,7 +53,7 @@ public:
}
} else {
while (lo < hi) {
const int mid = (lo + hi) / 2;
const int64_t mid = (lo + hi) / 2;
if (compare(p_value, p_array[mid])) {
hi = mid;
} else {
@@ -67,5 +66,3 @@ public:
};
} // namespace godot
#endif // GODOT_SEARCH_ARRAY_HPP

View File

@@ -28,8 +28,7 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef GODOT_SELF_LIST_HPP
#define GODOT_SELF_LIST_HPP
#pragma once
#include <godot_cpp/core/defs.hpp>
#include <godot_cpp/core/error_macros.hpp>
@@ -101,11 +100,74 @@ public:
p_elem->_root = nullptr;
}
void clear() {
while (_first) {
remove(_first);
}
}
void sort() {
sort_custom<Comparator<T>>();
}
template <typename C>
void sort_custom() {
if (_first == _last) {
return;
}
SelfList<T> *from = _first;
SelfList<T> *current = from;
SelfList<T> *to = from;
while (current) {
SelfList<T> *next = current->_next;
if (from != current) {
current->_prev = nullptr;
current->_next = from;
SelfList<T> *find = from;
C less;
while (find && less(*find->_self, *current->_self)) {
current->_prev = find;
current->_next = find->_next;
find = find->_next;
}
if (current->_prev) {
current->_prev->_next = current;
} else {
from = current;
}
if (current->_next) {
current->_next->_prev = current;
} else {
to = current;
}
} else {
current->_prev = nullptr;
current->_next = nullptr;
}
current = next;
}
_first = from;
_last = to;
}
_FORCE_INLINE_ SelfList<T> *first() { return _first; }
_FORCE_INLINE_ const SelfList<T> *first() const { return _first; }
// Forbid copying, which has broken behavior.
void operator=(const List &) = delete;
_FORCE_INLINE_ List() {}
_FORCE_INLINE_ ~List() { ERR_FAIL_COND(_first != nullptr); }
_FORCE_INLINE_ ~List() {
// A self list must be empty on destruction.
DEV_ASSERT(_first == nullptr);
}
};
private:
@@ -127,6 +189,9 @@ public:
_FORCE_INLINE_ const SelfList<T> *prev() const { return _prev; }
_FORCE_INLINE_ T *self() const { return _self; }
// Forbid copying, which has broken behavior.
void operator=(const SelfList<T> &) = delete;
_FORCE_INLINE_ SelfList(T *p_self) {
_self = p_self;
}
@@ -139,5 +204,3 @@ public:
};
} // namespace godot
#endif // GODOT_SELF_LIST_HPP

View File

@@ -28,8 +28,7 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef GODOT_SORT_ARRAY_HPP
#define GODOT_SORT_ARRAY_HPP
#pragma once
#include <godot_cpp/core/error_macros.hpp>
@@ -79,8 +78,8 @@ public:
}
}
inline int bitlog(int n) const {
int k;
inline int64_t bitlog(int64_t n) const {
int64_t k;
for (k = 0; n != 1; n >>= 1) {
++k;
}
@@ -89,8 +88,8 @@ public:
/* Heap / Heapsort functions */
inline void push_heap(int p_first, int p_hole_idx, int p_top_index, T p_value, T *p_array) const {
int parent = (p_hole_idx - 1) / 2;
inline void push_heap(int64_t p_first, int64_t p_hole_idx, int64_t p_top_index, T p_value, T *p_array) const {
int64_t parent = (p_hole_idx - 1) / 2;
while (p_hole_idx > p_top_index && compare(p_array[p_first + parent], p_value)) {
p_array[p_first + p_hole_idx] = p_array[p_first + parent];
p_hole_idx = parent;
@@ -99,17 +98,17 @@ public:
p_array[p_first + p_hole_idx] = p_value;
}
inline void pop_heap(int p_first, int p_last, int p_result, T p_value, T *p_array) const {
inline void pop_heap(int64_t p_first, int64_t p_last, int64_t p_result, T p_value, T *p_array) const {
p_array[p_result] = p_array[p_first];
adjust_heap(p_first, 0, p_last - p_first, p_value, p_array);
}
inline void pop_heap(int p_first, int p_last, T *p_array) const {
inline void pop_heap(int64_t p_first, int64_t p_last, T *p_array) const {
pop_heap(p_first, p_last - 1, p_last - 1, p_array[p_last - 1], p_array);
}
inline void adjust_heap(int p_first, int p_hole_idx, int p_len, T p_value, T *p_array) const {
int top_index = p_hole_idx;
int second_child = 2 * p_hole_idx + 2;
inline void adjust_heap(int64_t p_first, int64_t p_hole_idx, int64_t p_len, T p_value, T *p_array) const {
int64_t top_index = p_hole_idx;
int64_t second_child = 2 * p_hole_idx + 2;
while (second_child < p_len) {
if (compare(p_array[p_first + second_child], p_array[p_first + (second_child - 1)])) {
@@ -128,18 +127,18 @@ public:
push_heap(p_first, p_hole_idx, top_index, p_value, p_array);
}
inline void sort_heap(int p_first, int p_last, T *p_array) const {
inline void sort_heap(int64_t p_first, int64_t p_last, T *p_array) const {
while (p_last - p_first > 1) {
pop_heap(p_first, p_last--, p_array);
}
}
inline void make_heap(int p_first, int p_last, T *p_array) const {
inline void make_heap(int64_t p_first, int64_t p_last, T *p_array) const {
if (p_last - p_first < 2) {
return;
}
int len = p_last - p_first;
int parent = (len - 2) / 2;
int64_t len = p_last - p_first;
int64_t parent = (len - 2) / 2;
while (true) {
adjust_heap(p_first, parent, len, p_array[p_first + parent], p_array);
@@ -150,9 +149,9 @@ public:
}
}
inline void partial_sort(int p_first, int p_last, int p_middle, T *p_array) const {
inline void partial_sort(int64_t p_first, int64_t p_last, int64_t p_middle, T *p_array) const {
make_heap(p_first, p_middle, p_array);
for (int i = p_middle; i < p_last; i++) {
for (int64_t i = p_middle; i < p_last; i++) {
if (compare(p_array[i], p_array[p_first])) {
pop_heap(p_first, p_middle, i, p_array[i], p_array);
}
@@ -160,29 +159,29 @@ public:
sort_heap(p_first, p_middle, p_array);
}
inline void partial_select(int p_first, int p_last, int p_middle, T *p_array) const {
inline void partial_select(int64_t p_first, int64_t p_last, int64_t p_middle, T *p_array) const {
make_heap(p_first, p_middle, p_array);
for (int i = p_middle; i < p_last; i++) {
for (int64_t i = p_middle; i < p_last; i++) {
if (compare(p_array[i], p_array[p_first])) {
pop_heap(p_first, p_middle, i, p_array[i], p_array);
}
}
}
inline int partitioner(int p_first, int p_last, T p_pivot, T *p_array) const {
const int unmodified_first = p_first;
const int unmodified_last = p_last;
inline int64_t partitioner(int64_t p_first, int64_t p_last, T p_pivot, T *p_array) const {
const int64_t unmodified_first = p_first;
const int64_t unmodified_last = p_last;
while (true) {
while (compare(p_array[p_first], p_pivot)) {
if (Validate) {
if constexpr (Validate) {
ERR_BAD_COMPARE(p_first == unmodified_last - 1);
}
p_first++;
}
p_last--;
while (compare(p_pivot, p_array[p_last])) {
if (Validate) {
if constexpr (Validate) {
ERR_BAD_COMPARE(p_last == unmodified_first);
}
p_last--;
@@ -197,7 +196,7 @@ public:
}
}
inline void introsort(int p_first, int p_last, T *p_array, int p_max_depth) const {
inline void introsort(int64_t p_first, int64_t p_last, T *p_array, int64_t p_max_depth) const {
while (p_last - p_first > INTROSORT_THRESHOLD) {
if (p_max_depth == 0) {
partial_sort(p_first, p_last, p_last, p_array);
@@ -206,7 +205,7 @@ public:
p_max_depth--;
int cut = partitioner(
int64_t cut = partitioner(
p_first,
p_last,
median_of_3(
@@ -220,7 +219,7 @@ public:
}
}
inline void introselect(int p_first, int p_nth, int p_last, T *p_array, int p_max_depth) const {
inline void introselect(int64_t p_first, int64_t p_nth, int64_t p_last, T *p_array, int64_t p_max_depth) const {
while (p_last - p_first > 3) {
if (p_max_depth == 0) {
partial_select(p_first, p_nth + 1, p_last, p_array);
@@ -230,7 +229,7 @@ public:
p_max_depth--;
int cut = partitioner(
int64_t cut = partitioner(
p_first,
p_last,
median_of_3(
@@ -249,10 +248,10 @@ public:
insertion_sort(p_first, p_last, p_array);
}
inline void unguarded_linear_insert(int p_last, T p_value, T *p_array) const {
int next = p_last - 1;
inline void unguarded_linear_insert(int64_t p_last, T p_value, T *p_array) const {
int64_t next = p_last - 1;
while (compare(p_value, p_array[next])) {
if (Validate) {
if constexpr (Validate) {
ERR_BAD_COMPARE(next == 0);
}
p_array[p_last] = p_array[next];
@@ -262,10 +261,10 @@ public:
p_array[p_last] = p_value;
}
inline void linear_insert(int p_first, int p_last, T *p_array) const {
inline void linear_insert(int64_t p_first, int64_t p_last, T *p_array) const {
T val = p_array[p_last];
if (compare(val, p_array[p_first])) {
for (int i = p_last; i > p_first; i--) {
for (int64_t i = p_last; i > p_first; i--) {
p_array[i] = p_array[i - 1];
}
@@ -275,22 +274,22 @@ public:
}
}
inline void insertion_sort(int p_first, int p_last, T *p_array) const {
inline void insertion_sort(int64_t p_first, int64_t p_last, T *p_array) const {
if (p_first == p_last) {
return;
}
for (int i = p_first + 1; i != p_last; i++) {
for (int64_t i = p_first + 1; i != p_last; i++) {
linear_insert(p_first, i, p_array);
}
}
inline void unguarded_insertion_sort(int p_first, int p_last, T *p_array) const {
for (int i = p_first; i != p_last; i++) {
inline void unguarded_insertion_sort(int64_t p_first, int64_t p_last, T *p_array) const {
for (int64_t i = p_first; i != p_last; i++) {
unguarded_linear_insert(i, p_array[i], p_array);
}
}
inline void final_insertion_sort(int p_first, int p_last, T *p_array) const {
inline void final_insertion_sort(int64_t p_first, int64_t p_last, T *p_array) const {
if (p_last - p_first > INTROSORT_THRESHOLD) {
insertion_sort(p_first, p_first + INTROSORT_THRESHOLD, p_array);
unguarded_insertion_sort(p_first + INTROSORT_THRESHOLD, p_last, p_array);
@@ -299,18 +298,18 @@ public:
}
}
inline void sort_range(int p_first, int p_last, T *p_array) const {
inline void sort_range(int64_t p_first, int64_t p_last, T *p_array) const {
if (p_first != p_last) {
introsort(p_first, p_last, p_array, bitlog(p_last - p_first) * 2);
final_insertion_sort(p_first, p_last, p_array);
}
}
inline void sort(T *p_array, int p_len) const {
inline void sort(T *p_array, int64_t p_len) const {
sort_range(0, p_len, p_array);
}
inline void nth_element(int p_first, int p_last, int p_nth, T *p_array) const {
inline void nth_element(int64_t p_first, int64_t p_last, int64_t p_nth, T *p_array) const {
if (p_first == p_last || p_nth == p_last) {
return;
}
@@ -319,5 +318,3 @@ public:
};
} // namespace godot
#endif // GODOT_SORT_ARRAY_HPP

View File

@@ -28,8 +28,7 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef GODOT_SPIN_LOCK_HPP
#define GODOT_SPIN_LOCK_HPP
#pragma once
#include <atomic>
@@ -50,5 +49,3 @@ public:
};
} // namespace godot
#endif // GODOT_SPIN_LOCK_HPP

View File

@@ -28,8 +28,7 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef GODOT_THREAD_WORK_POOL_HPP
#define GODOT_THREAD_WORK_POOL_HPP
#pragma once
#include <godot_cpp/classes/os.hpp>
#include <godot_cpp/classes/semaphore.hpp>
@@ -201,5 +200,3 @@ public:
};
} // namespace godot
#endif // GODOT_THREAD_WORK_POOL_HPP

View File

@@ -28,8 +28,7 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef GODOT_VECTOR_HPP
#define GODOT_VECTOR_HPP
#pragma once
/**
* @class Vector
@@ -69,6 +68,7 @@ private:
CowData<T> _cowdata;
public:
// Must take a copy instead of a reference (see GH-31736).
bool push_back(T p_elem);
_FORCE_INLINE_ bool append(const T &p_elem) { return push_back(p_elem); } //alias
void fill(T p_elem);
@@ -97,11 +97,13 @@ public:
Error resize(Size p_size) { return _cowdata.resize(p_size); }
Error resize_zeroed(Size p_size) { return _cowdata.template resize<true>(p_size); }
_FORCE_INLINE_ const T &operator[](Size p_index) const { return _cowdata.get(p_index); }
// Must take a copy instead of a reference (see GH-31736).
Error insert(Size p_pos, T p_val) { return _cowdata.insert(p_pos, p_val); }
Size find(const T &p_val, Size p_from = 0) const { return _cowdata.find(p_val, p_from); }
Size rfind(const T &p_val, Size p_from = -1) const { return _cowdata.rfind(p_val, p_from); }
Size count(const T &p_val) const { return _cowdata.count(p_val); }
// Must take a copy instead of a reference (see GH-31736).
void append_array(Vector<T> p_other);
_FORCE_INLINE_ bool has(const T &p_val) const { return find(p_val) != -1; }
@@ -146,17 +148,19 @@ public:
insert(i, p_val);
}
inline void operator=(const Vector &p_from) {
_cowdata._ref(p_from._cowdata);
}
void operator=(const Vector &p_from) { _cowdata._ref(p_from._cowdata); }
void operator=(Vector &&p_from) { _cowdata = std::move(p_from._cowdata); }
Vector<uint8_t> to_byte_array() const {
Vector<uint8_t> ret;
if (is_empty()) {
return ret;
}
ret.resize(size() * sizeof(T));
memcpy(ret.ptrw(), ptr(), sizeof(T) * size());
size_t alloc_size = size() * sizeof(T);
ret.resize(alloc_size);
if (alloc_size) {
memcpy(ret.ptrw(), ptr(), alloc_size);
}
return ret;
}
@@ -279,16 +283,11 @@ public:
}
_FORCE_INLINE_ Vector() {}
_FORCE_INLINE_ Vector(std::initializer_list<T> p_init) {
Error err = _cowdata.resize(p_init.size());
ERR_FAIL_COND(err);
Size i = 0;
for (const T &element : p_init) {
_cowdata.set(i++, element);
}
}
_FORCE_INLINE_ Vector(std::initializer_list<T> p_init) :
_cowdata(p_init) {}
_FORCE_INLINE_ Vector(const Vector &p_from) { _cowdata._ref(p_from._cowdata); }
_FORCE_INLINE_ Vector(Vector &&p_from) :
_cowdata(std::move(p_from._cowdata)) {}
_FORCE_INLINE_ ~Vector() {}
};
@@ -332,5 +331,3 @@ void Vector<T>::fill(T p_elem) {
}
} // namespace godot
#endif // GODOT_VECTOR_HPP

View File

@@ -28,8 +28,7 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef GODOT_VMAP_HPP
#define GODOT_VMAP_HPP
#pragma once
#include <godot_cpp/templates/cowdata.hpp>
@@ -73,16 +72,16 @@ private:
middle = (low + high) / 2;
if (p_val < a[middle].key) {
high = middle - 1; // search low end of array
high = middle - 1; //search low end of array
} else if (a[middle].key < p_val) {
low = middle + 1; // search high end of array
low = middle + 1; //search high end of array
} else {
r_exact = true;
return middle;
}
}
// return the position where this would be inserted
//return the position where this would be inserted
if (a[middle].key < p_val) {
middle++;
}
@@ -103,9 +102,9 @@ private:
middle = (low + high) / 2;
if (p_val < a[middle].key) {
high = middle - 1; // search low end of array
high = middle - 1; //search low end of array
} else if (a[middle].key < p_val) {
low = middle + 1; // search high end of array
low = middle + 1; //search high end of array
} else {
return middle;
}
@@ -143,6 +142,9 @@ public:
}
int find_nearest(const T &p_val) const {
if (_cowdata.is_empty()) {
return -1;
}
bool exact;
return _find(p_val, exact);
}
@@ -192,6 +194,8 @@ public:
}
_FORCE_INLINE_ VMap() {}
_FORCE_INLINE_ VMap(std::initializer_list<T> p_init) :
_cowdata(p_init) {}
_FORCE_INLINE_ VMap(const VMap &p_from) { _cowdata._ref(p_from._cowdata); }
inline void operator=(const VMap &p_from) {
@@ -200,5 +204,3 @@ public:
};
} // namespace godot
#endif // GODOT_VMAP_HPP

View File

@@ -28,8 +28,7 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef GODOT_VSET_HPP
#define GODOT_VSET_HPP
#pragma once
#include <godot_cpp/templates/vector.hpp>
@@ -60,16 +59,16 @@ class VSet {
middle = (low + high) / 2;
if (p_val < a[middle]) {
high = middle - 1; // search low end of array
high = middle - 1; //search low end of array
} else if (a[middle] < p_val) {
low = middle + 1; // search high end of array
low = middle + 1; //search high end of array
} else {
r_exact = true;
return middle;
}
}
// return the position where this would be inserted
//return the position where this would be inserted
if (a[middle] < p_val) {
middle++;
}
@@ -90,9 +89,9 @@ class VSet {
middle = (low + high) / 2;
if (p_val < a[middle]) {
high = middle - 1; // search low end of array
high = middle - 1; //search low end of array
} else if (a[middle] < p_val) {
low = middle + 1; // search high end of array
low = middle + 1; //search high end of array
} else {
return middle;
}
@@ -138,8 +137,10 @@ public:
inline const T &operator[](int p_index) const {
return _data[p_index];
}
_FORCE_INLINE_ VSet() {}
_FORCE_INLINE_ VSet(std::initializer_list<T> p_init) :
_data(p_init) {}
};
} // namespace godot
#endif // GODOT_VSET_HPP

View File

@@ -28,8 +28,7 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef GODOT_AABB_HPP
#define GODOT_AABB_HPP
#pragma once
#include <godot_cpp/variant/plane.hpp>
#include <godot_cpp/variant/vector3.hpp>
@@ -73,16 +72,21 @@ struct [[nodiscard]] AABB {
AABB merge(const AABB &p_with) const;
void merge_with(const AABB &p_aabb); ///merge with another AABB
AABB intersection(const AABB &p_aabb) const; ///get box where two intersect, empty if no intersection occurs
bool intersects_segment(const Vector3 &p_from, const Vector3 &p_to, Vector3 *r_clip = nullptr, Vector3 *r_normal = nullptr) const;
bool intersects_ray(const Vector3 &p_from, const Vector3 &p_dir, Vector3 *r_clip = nullptr, Vector3 *r_normal = nullptr) const;
_FORCE_INLINE_ bool smits_intersect_ray(const Vector3 &p_from, const Vector3 &p_dir, real_t t0, real_t t1) const;
_FORCE_INLINE_ bool smits_intersect_ray(const Vector3 &p_from, const Vector3 &p_dir, real_t p_t0, real_t p_t1) const;
bool intersects_segment(const Vector3 &p_from, const Vector3 &p_to, Vector3 *r_intersection_point = nullptr, Vector3 *r_normal = nullptr) const;
bool intersects_ray(const Vector3 &p_from, const Vector3 &p_dir) const {
bool inside;
return find_intersects_ray(p_from, p_dir, inside);
}
bool find_intersects_ray(const Vector3 &p_from, const Vector3 &p_dir, bool &r_inside, Vector3 *r_intersection_point = nullptr, Vector3 *r_normal = nullptr) const;
_FORCE_INLINE_ bool intersects_convex_shape(const Plane *p_planes, int p_plane_count, const Vector3 *p_points, int p_point_count) const;
_FORCE_INLINE_ bool inside_convex_shape(const Plane *p_planes, int p_plane_count) const;
bool intersects_plane(const Plane &p_plane) const;
_FORCE_INLINE_ bool has_point(const Vector3 &p_point) const;
_FORCE_INLINE_ Vector3 get_support(const Vector3 &p_normal) const;
_FORCE_INLINE_ Vector3 get_support(const Vector3 &p_direction) const;
Vector3 get_longest_axis() const;
int get_longest_axis_index() const;
@@ -209,15 +213,18 @@ inline bool AABB::encloses(const AABB &p_aabb) const {
(src_max.z >= dst_max.z));
}
Vector3 AABB::get_support(const Vector3 &p_normal) const {
Vector3 half_extents = size * 0.5f;
Vector3 ofs = position + half_extents;
return Vector3(
(p_normal.x > 0) ? half_extents.x : -half_extents.x,
(p_normal.y > 0) ? half_extents.y : -half_extents.y,
(p_normal.z > 0) ? half_extents.z : -half_extents.z) +
ofs;
Vector3 AABB::get_support(const Vector3 &p_direction) const {
Vector3 support = position;
if (p_direction.x > 0.0f) {
support.x += size.x;
}
if (p_direction.y > 0.0f) {
support.y += size.y;
}
if (p_direction.z > 0.0f) {
support.z += size.z;
}
return support;
}
Vector3 AABB::get_endpoint(int p_point) const {
@@ -403,7 +410,7 @@ inline real_t AABB::get_shortest_axis_size() const {
return max_size;
}
bool AABB::smits_intersect_ray(const Vector3 &p_from, const Vector3 &p_dir, real_t t0, real_t t1) const {
bool AABB::smits_intersect_ray(const Vector3 &p_from, const Vector3 &p_dir, real_t p_t0, real_t p_t1) const {
#ifdef MATH_CHECKS
if (unlikely(size.x < 0 || size.y < 0 || size.z < 0)) {
ERR_PRINT("AABB size is negative, this is not supported. Use AABB.abs() to get an AABB with a positive size.");
@@ -454,7 +461,7 @@ bool AABB::smits_intersect_ray(const Vector3 &p_from, const Vector3 &p_dir, real
if (tzmax < tmax) {
tmax = tzmax;
}
return ((tmin < t1) && (tmax > t0));
return ((tmin < p_t1) && (tmax > p_t0));
}
void AABB::grow_by(real_t p_amount) {
@@ -491,5 +498,3 @@ AABB AABB::quantized(real_t p_unit) const {
}
} // namespace godot
#endif // GODOT_AABB_HPP

View File

@@ -28,8 +28,7 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef GODOT_ARRAY_HELPERS_HPP
#define GODOT_ARRAY_HELPERS_HPP
#pragma once
namespace godot {
namespace helpers {
@@ -51,5 +50,3 @@ T append_all(T appendable) {
}
} // namespace helpers
} // namespace godot
#endif // GODOT_ARRAY_HELPERS_HPP

View File

@@ -28,8 +28,7 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef GODOT_BASIS_HPP
#define GODOT_BASIS_HPP
#pragma once
#include <godot_cpp/classes/global_constants.hpp>
#include <godot_cpp/variant/quaternion.hpp>
@@ -44,11 +43,11 @@ struct [[nodiscard]] Basis {
Vector3(0, 0, 1)
};
_FORCE_INLINE_ const Vector3 &operator[](int axis) const {
return rows[axis];
_FORCE_INLINE_ const Vector3 &operator[](int p_row) const {
return rows[p_row];
}
_FORCE_INLINE_ Vector3 &operator[](int axis) {
return rows[axis];
_FORCE_INLINE_ Vector3 &operator[](int p_row) {
return rows[p_row];
}
void invert();
@@ -59,21 +58,19 @@ struct [[nodiscard]] Basis {
_FORCE_INLINE_ real_t determinant() const;
void from_z(const Vector3 &p_z);
void rotate(const Vector3 &p_axis, real_t p_angle);
Basis rotated(const Vector3 &p_axis, real_t p_angle) const;
void rotate_local(const Vector3 &p_axis, real_t p_angle);
Basis rotated_local(const Vector3 &p_axis, real_t p_angle) const;
void rotate(const Vector3 &p_euler, EulerOrder p_order = EULER_ORDER_YXZ);
Basis rotated(const Vector3 &p_euler, EulerOrder p_order = EULER_ORDER_YXZ) const;
void rotate(const Vector3 &p_euler, EulerOrder p_order = EulerOrder::EULER_ORDER_YXZ);
Basis rotated(const Vector3 &p_euler, EulerOrder p_order = EulerOrder::EULER_ORDER_YXZ) const;
void rotate(const Quaternion &p_quaternion);
Basis rotated(const Quaternion &p_quaternion) const;
Vector3 get_euler_normalized(EulerOrder p_order = EULER_ORDER_YXZ) const;
Vector3 get_euler_normalized(EulerOrder p_order = EulerOrder::EULER_ORDER_YXZ) const;
void get_rotation_axis_angle(Vector3 &p_axis, real_t &p_angle) const;
void get_rotation_axis_angle_local(Vector3 &p_axis, real_t &p_angle) const;
Quaternion get_rotation_quaternion() const;
@@ -82,9 +79,9 @@ struct [[nodiscard]] Basis {
Vector3 rotref_posscale_decomposition(Basis &rotref) const;
Vector3 get_euler(EulerOrder p_order = EULER_ORDER_YXZ) const;
void set_euler(const Vector3 &p_euler, EulerOrder p_order = EULER_ORDER_YXZ);
static Basis from_euler(const Vector3 &p_euler, EulerOrder p_order = EULER_ORDER_YXZ) {
Vector3 get_euler(EulerOrder p_order = EulerOrder::EULER_ORDER_YXZ) const;
void set_euler(const Vector3 &p_euler, EulerOrder p_order = EulerOrder::EULER_ORDER_YXZ);
static Basis from_euler(const Vector3 &p_euler, EulerOrder p_order = EulerOrder::EULER_ORDER_YXZ) {
Basis b;
b.set_euler(p_euler, p_order);
return b;
@@ -104,27 +101,25 @@ struct [[nodiscard]] Basis {
void scale_orthogonal(const Vector3 &p_scale);
Basis scaled_orthogonal(const Vector3 &p_scale) const;
void make_scale_uniform();
float get_uniform_scale() const;
real_t get_uniform_scale() const;
Vector3 get_scale() const;
Vector3 get_scale_abs() const;
Vector3 get_scale_local() const;
Vector3 get_scale_global() const;
void set_axis_angle_scale(const Vector3 &p_axis, real_t p_angle, const Vector3 &p_scale);
void set_euler_scale(const Vector3 &p_euler, const Vector3 &p_scale, EulerOrder p_order = EULER_ORDER_YXZ);
void set_euler_scale(const Vector3 &p_euler, const Vector3 &p_scale, EulerOrder p_order = EulerOrder::EULER_ORDER_YXZ);
void set_quaternion_scale(const Quaternion &p_quaternion, const Vector3 &p_scale);
// transposed dot products
_FORCE_INLINE_ real_t tdotx(const Vector3 &v) const {
return rows[0][0] * v[0] + rows[1][0] * v[1] + rows[2][0] * v[2];
_FORCE_INLINE_ real_t tdotx(const Vector3 &p_v) const {
return rows[0][0] * p_v[0] + rows[1][0] * p_v[1] + rows[2][0] * p_v[2];
}
_FORCE_INLINE_ real_t tdoty(const Vector3 &v) const {
return rows[0][1] * v[0] + rows[1][1] * v[1] + rows[2][1] * v[2];
_FORCE_INLINE_ real_t tdoty(const Vector3 &p_v) const {
return rows[0][1] * p_v[0] + rows[1][1] * p_v[1] + rows[2][1] * p_v[2];
}
_FORCE_INLINE_ real_t tdotz(const Vector3 &v) const {
return rows[0][2] * v[0] + rows[1][2] * v[1] + rows[2][2] * v[2];
_FORCE_INLINE_ real_t tdotz(const Vector3 &p_v) const {
return rows[0][2] * p_v[0] + rows[1][2] * p_v[1] + rows[2][2] * p_v[2];
}
bool is_equal_approx(const Basis &p_basis) const;
@@ -141,31 +136,35 @@ struct [[nodiscard]] Basis {
_FORCE_INLINE_ Basis operator+(const Basis &p_matrix) const;
_FORCE_INLINE_ void operator-=(const Basis &p_matrix);
_FORCE_INLINE_ Basis operator-(const Basis &p_matrix) const;
_FORCE_INLINE_ void operator*=(const real_t p_val);
_FORCE_INLINE_ Basis operator*(const real_t p_val) const;
_FORCE_INLINE_ void operator*=(real_t p_val);
_FORCE_INLINE_ Basis operator*(real_t p_val) const;
_FORCE_INLINE_ void operator/=(real_t p_val);
_FORCE_INLINE_ Basis operator/(real_t p_val) const;
bool is_orthogonal() const;
bool is_orthonormal() const;
bool is_conformal() const;
bool is_diagonal() const;
bool is_rotation() const;
Basis lerp(const Basis &p_to, const real_t &p_weight) const;
Basis slerp(const Basis &p_to, const real_t &p_weight) const;
Basis lerp(const Basis &p_to, real_t p_weight) const;
Basis slerp(const Basis &p_to, real_t p_weight) const;
void rotate_sh(real_t *p_values);
operator String() const;
/* create / set */
_FORCE_INLINE_ void set(real_t xx, real_t xy, real_t xz, real_t yx, real_t yy, real_t yz, real_t zx, real_t zy, real_t zz) {
rows[0][0] = xx;
rows[0][1] = xy;
rows[0][2] = xz;
rows[1][0] = yx;
rows[1][1] = yy;
rows[1][2] = yz;
rows[2][0] = zx;
rows[2][1] = zy;
rows[2][2] = zz;
_FORCE_INLINE_ void set(real_t p_xx, real_t p_xy, real_t p_xz, real_t p_yx, real_t p_yy, real_t p_yz, real_t p_zx, real_t p_zy, real_t p_zz) {
rows[0][0] = p_xx;
rows[0][1] = p_xy;
rows[0][2] = p_xz;
rows[1][0] = p_yx;
rows[1][1] = p_yy;
rows[1][2] = p_yz;
rows[2][0] = p_zx;
rows[2][1] = p_zy;
rows[2][2] = p_zz;
}
_FORCE_INLINE_ void set_columns(const Vector3 &p_x, const Vector3 &p_y, const Vector3 &p_z) {
set_column(0, p_x);
@@ -195,20 +194,20 @@ struct [[nodiscard]] Basis {
rows[2].zero();
}
_FORCE_INLINE_ Basis transpose_xform(const Basis &m) const {
_FORCE_INLINE_ Basis transpose_xform(const Basis &p_m) const {
return Basis(
rows[0].x * m[0].x + rows[1].x * m[1].x + rows[2].x * m[2].x,
rows[0].x * m[0].y + rows[1].x * m[1].y + rows[2].x * m[2].y,
rows[0].x * m[0].z + rows[1].x * m[1].z + rows[2].x * m[2].z,
rows[0].y * m[0].x + rows[1].y * m[1].x + rows[2].y * m[2].x,
rows[0].y * m[0].y + rows[1].y * m[1].y + rows[2].y * m[2].y,
rows[0].y * m[0].z + rows[1].y * m[1].z + rows[2].y * m[2].z,
rows[0].z * m[0].x + rows[1].z * m[1].x + rows[2].z * m[2].x,
rows[0].z * m[0].y + rows[1].z * m[1].y + rows[2].z * m[2].y,
rows[0].z * m[0].z + rows[1].z * m[1].z + rows[2].z * m[2].z);
rows[0].x * p_m[0].x + rows[1].x * p_m[1].x + rows[2].x * p_m[2].x,
rows[0].x * p_m[0].y + rows[1].x * p_m[1].y + rows[2].x * p_m[2].y,
rows[0].x * p_m[0].z + rows[1].x * p_m[1].z + rows[2].x * p_m[2].z,
rows[0].y * p_m[0].x + rows[1].y * p_m[1].x + rows[2].y * p_m[2].x,
rows[0].y * p_m[0].y + rows[1].y * p_m[1].y + rows[2].y * p_m[2].y,
rows[0].y * p_m[0].z + rows[1].y * p_m[1].z + rows[2].y * p_m[2].z,
rows[0].z * p_m[0].x + rows[1].z * p_m[1].x + rows[2].z * p_m[2].x,
rows[0].z * p_m[0].y + rows[1].z * p_m[1].y + rows[2].z * p_m[2].y,
rows[0].z * p_m[0].z + rows[1].z * p_m[1].z + rows[2].z * p_m[2].z);
}
Basis(real_t xx, real_t xy, real_t xz, real_t yx, real_t yy, real_t yz, real_t zx, real_t zy, real_t zz) {
set(xx, xy, xz, yx, yy, yz, zx, zy, zz);
Basis(real_t p_xx, real_t p_xy, real_t p_xz, real_t p_yx, real_t p_yy, real_t p_yz, real_t p_zx, real_t p_zy, real_t p_zz) {
set(p_xx, p_xy, p_xz, p_yx, p_yy, p_yz, p_zx, p_zy, p_zz);
}
void orthonormalize();
@@ -282,18 +281,30 @@ _FORCE_INLINE_ Basis Basis::operator-(const Basis &p_matrix) const {
return ret;
}
_FORCE_INLINE_ void Basis::operator*=(const real_t p_val) {
_FORCE_INLINE_ void Basis::operator*=(real_t p_val) {
rows[0] *= p_val;
rows[1] *= p_val;
rows[2] *= p_val;
}
_FORCE_INLINE_ Basis Basis::operator*(const real_t p_val) const {
_FORCE_INLINE_ Basis Basis::operator*(real_t p_val) const {
Basis ret(*this);
ret *= p_val;
return ret;
}
_FORCE_INLINE_ void Basis::operator/=(real_t p_val) {
rows[0] /= p_val;
rows[1] /= p_val;
rows[2] /= p_val;
}
_FORCE_INLINE_ Basis Basis::operator/(real_t p_val) const {
Basis ret(*this);
ret /= p_val;
return ret;
}
Vector3 Basis::xform(const Vector3 &p_vector) const {
return Vector3(
rows[0].dot(p_vector),
@@ -315,5 +326,3 @@ real_t Basis::determinant() const {
}
} // namespace godot
#endif // GODOT_BASIS_HPP

View File

@@ -28,8 +28,7 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef GODOT_CALLABLE_CUSTOM_HPP
#define GODOT_CALLABLE_CUSTOM_HPP
#pragma once
#include <godot_cpp/core/object_id.hpp>
#include <godot_cpp/variant/string_name.hpp>
@@ -61,5 +60,3 @@ public:
};
} // namespace godot
#endif // GODOT_CALLABLE_CUSTOM_HPP

View File

@@ -28,8 +28,7 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef GODOT_CALLABLE_METHOD_POINTER_HPP
#define GODOT_CALLABLE_METHOD_POINTER_HPP
#pragma once
#include <godot_cpp/core/binder_common.hpp>
#include <godot_cpp/variant/variant.hpp>
@@ -105,8 +104,7 @@ template <typename T, typename R, typename... P>
class CallableCustomMethodPointerRet : public CallableCustomMethodPointerBase {
struct Data {
T *instance;
R(T::*method)
(P...);
R (T::*method)(P...);
} data;
static_assert(sizeof(Data) % 4 == 0);
@@ -147,8 +145,7 @@ template <typename T, typename R, typename... P>
class CallableCustomMethodPointerRetC : public CallableCustomMethodPointerBase {
struct Data {
T *instance;
R(T::*method)
(P...) const;
R (T::*method)(P...) const;
} data;
static_assert(sizeof(Data) % 4 == 0);
@@ -228,8 +225,7 @@ Callable create_custom_callable_static_function_pointer(void (*p_method)(P...))
template <typename R, typename... P>
class CallableCustomStaticMethodPointerRet : public CallableCustomMethodPointerBase {
struct Data {
R(*method)
(P...);
R (*method)(P...);
} data;
static_assert(sizeof(Data) % 4 == 0);
@@ -269,5 +265,3 @@ Callable create_custom_callable_static_function_pointer(R (*p_method)(P...)) {
#define callable_mp_static(M) ::godot::create_custom_callable_static_function_pointer(M)
} // namespace godot
#endif // GODOT_CALLABLE_METHOD_POINTER_HPP

File diff suppressed because it is too large Load Diff

View File

@@ -28,8 +28,7 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef GODOT_CHAR_STRING_HPP
#define GODOT_CHAR_STRING_HPP
#pragma once
#include <godot_cpp/templates/cowdata.hpp>
@@ -114,7 +113,7 @@ public:
CharStringT<T> &operator+=(T p_char);
int64_t length() const { return size() ? size() - 1 : 0; }
const T *get_data() const;
operator const T *() const { return get_data(); };
operator const T *() const { return get_data(); }
protected:
void copy_from(const T *p_cstr);
@@ -138,5 +137,3 @@ typedef CharStringT<char32_t> Char32String;
typedef CharStringT<wchar_t> CharWideString;
} // namespace godot
#endif // GODOT_CHAR_STRING_HPP

View File

@@ -28,63 +28,107 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef GODOT_CHAR_UTILS_HPP
#define GODOT_CHAR_UTILS_HPP
#pragma once
static _FORCE_INLINE_ bool is_ascii_upper_case(char32_t c) {
return (c >= 'A' && c <= 'Z');
#include "char_range.inc.hpp"
namespace godot {
#define BSEARCH_CHAR_RANGE(m_array) \
int low = 0; \
int high = sizeof(m_array) / sizeof(m_array[0]) - 1; \
int middle = (low + high) / 2; \
\
while (low <= high) { \
if (p_char < m_array[middle].start) { \
high = middle - 1; \
} else if (p_char > m_array[middle].end) { \
low = middle + 1; \
} else { \
return true; \
} \
\
middle = (low + high) / 2; \
} \
\
return false
constexpr bool is_unicode_identifier_start(char32_t p_char) {
BSEARCH_CHAR_RANGE(xid_start);
}
static _FORCE_INLINE_ bool is_ascii_lower_case(char32_t c) {
return (c >= 'a' && c <= 'z');
constexpr bool is_unicode_identifier_continue(char32_t p_char) {
BSEARCH_CHAR_RANGE(xid_continue);
}
static _FORCE_INLINE_ bool is_digit(char32_t c) {
return (c >= '0' && c <= '9');
constexpr bool is_unicode_upper_case(char32_t p_char) {
BSEARCH_CHAR_RANGE(uppercase_letter);
}
static _FORCE_INLINE_ bool is_hex_digit(char32_t c) {
return (is_digit(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'));
constexpr bool is_unicode_lower_case(char32_t p_char) {
BSEARCH_CHAR_RANGE(lowercase_letter);
}
static _FORCE_INLINE_ bool is_binary_digit(char32_t c) {
return (c == '0' || c == '1');
constexpr bool is_unicode_letter(char32_t p_char) {
BSEARCH_CHAR_RANGE(unicode_letter);
}
static _FORCE_INLINE_ bool is_ascii_char(char32_t c) {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
#undef BSEARCH_CHAR_RANGE
constexpr bool is_ascii_upper_case(char32_t p_char) {
return (p_char >= 'A' && p_char <= 'Z');
}
static _FORCE_INLINE_ bool is_ascii_alphanumeric_char(char32_t c) {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9');
constexpr bool is_ascii_lower_case(char32_t p_char) {
return (p_char >= 'a' && p_char <= 'z');
}
static _FORCE_INLINE_ bool is_ascii_identifier_char(char32_t c) {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_';
constexpr bool is_digit(char32_t p_char) {
return (p_char >= '0' && p_char <= '9');
}
static _FORCE_INLINE_ bool is_symbol(char32_t c) {
return c != '_' && ((c >= '!' && c <= '/') || (c >= ':' && c <= '@') || (c >= '[' && c <= '`') || (c >= '{' && c <= '~') || c == '\t' || c == ' ');
constexpr bool is_hex_digit(char32_t p_char) {
return (is_digit(p_char) || (p_char >= 'a' && p_char <= 'f') || (p_char >= 'A' && p_char <= 'F'));
}
static _FORCE_INLINE_ bool is_control(char32_t p_char) {
constexpr bool is_binary_digit(char32_t p_char) {
return (p_char == '0' || p_char == '1');
}
constexpr bool is_ascii_alphabet_char(char32_t p_char) {
return (p_char >= 'a' && p_char <= 'z') || (p_char >= 'A' && p_char <= 'Z');
}
constexpr bool is_ascii_alphanumeric_char(char32_t p_char) {
return (p_char >= 'a' && p_char <= 'z') || (p_char >= 'A' && p_char <= 'Z') || (p_char >= '0' && p_char <= '9');
}
constexpr bool is_ascii_identifier_char(char32_t p_char) {
return (p_char >= 'a' && p_char <= 'z') || (p_char >= 'A' && p_char <= 'Z') || (p_char >= '0' && p_char <= '9') || p_char == '_';
}
constexpr bool is_symbol(char32_t p_char) {
return p_char != '_' && ((p_char >= '!' && p_char <= '/') || (p_char >= ':' && p_char <= '@') || (p_char >= '[' && p_char <= '`') || (p_char >= '{' && p_char <= '~') || p_char == '\t' || p_char == ' ');
}
constexpr bool is_control(char32_t p_char) {
return (p_char <= 0x001f) || (p_char >= 0x007f && p_char <= 0x009f);
}
static _FORCE_INLINE_ bool is_whitespace(char32_t p_char) {
return (p_char == ' ') || (p_char == 0x00a0) || (p_char == 0x1680) || (p_char >= 0x2000 && p_char <= 0x200a) || (p_char == 0x202f) || (p_char == 0x205f) || (p_char == 0x3000) || (p_char == 0x2028) || (p_char == 0x2029) || (p_char >= 0x0009 && p_char <= 0x000d) || (p_char == 0x0085);
constexpr bool is_whitespace(char32_t p_char) {
return (p_char == ' ') || (p_char == 0x00a0) || (p_char == 0x1680) || (p_char >= 0x2000 && p_char <= 0x200b) || (p_char == 0x202f) || (p_char == 0x205f) || (p_char == 0x3000) || (p_char == 0x2028) || (p_char == 0x2029) || (p_char >= 0x0009 && p_char <= 0x000d) || (p_char == 0x0085);
}
static _FORCE_INLINE_ bool is_linebreak(char32_t p_char) {
constexpr bool is_linebreak(char32_t p_char) {
return (p_char >= 0x000a && p_char <= 0x000d) || (p_char == 0x0085) || (p_char == 0x2028) || (p_char == 0x2029);
}
static _FORCE_INLINE_ bool is_punct(char32_t p_char) {
constexpr bool is_punct(char32_t p_char) {
return (p_char >= ' ' && p_char <= '/') || (p_char >= ':' && p_char <= '@') || (p_char >= '[' && p_char <= '^') || (p_char == '`') || (p_char >= '{' && p_char <= '~') || (p_char >= 0x2000 && p_char <= 0x206f) || (p_char >= 0x3000 && p_char <= 0x303f);
}
static _FORCE_INLINE_ bool is_underscore(char32_t p_char) {
constexpr bool is_underscore(char32_t p_char) {
return (p_char == '_');
}
#endif // GODOT_CHAR_UTILS_HPP
} // namespace godot

View File

@@ -28,8 +28,7 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef GODOT_COLOR_HPP
#define GODOT_COLOR_HPP
#pragma once
#include <godot_cpp/core/math.hpp>
@@ -103,12 +102,10 @@ struct [[nodiscard]] Color {
_FORCE_INLINE_ Color lerp(const Color &p_to, float p_weight) const {
Color res = *this;
res.r += (p_weight * (p_to.r - r));
res.g += (p_weight * (p_to.g - g));
res.b += (p_weight * (p_to.b - b));
res.a += (p_weight * (p_to.a - a));
res.r = Math::lerp(res.r, p_to.r, p_weight);
res.g = Math::lerp(res.g, p_to.g, p_weight);
res.b = Math::lerp(res.b, p_to.b, p_weight);
res.a = Math::lerp(res.a, p_to.a, p_weight);
return res;
}
@@ -129,33 +126,46 @@ struct [[nodiscard]] Color {
}
_FORCE_INLINE_ uint32_t to_rgbe9995() const {
const float pow2to9 = 512.0f;
const float B = 15.0f;
const float N = 9.0f;
// https://github.com/microsoft/DirectX-Graphics-Samples/blob/v10.0.19041.0/MiniEngine/Core/Color.cpp
static const float kMaxVal = float(0x1FF << 7);
static const float kMinVal = float(1.f / (1 << 16));
float sharedexp = 65408.000f; // Result of: ((pow2to9 - 1.0f) / pow2to9) * powf(2.0f, 31.0f - 15.0f)
// Clamp RGB to [0, 1.FF*2^16]
const float _r = CLAMP(r, 0.0f, kMaxVal);
const float _g = CLAMP(g, 0.0f, kMaxVal);
const float _b = CLAMP(b, 0.0f, kMaxVal);
float cRed = MAX(0.0f, MIN(sharedexp, r));
float cGreen = MAX(0.0f, MIN(sharedexp, g));
float cBlue = MAX(0.0f, MIN(sharedexp, b));
// Compute the maximum channel, no less than 1.0*2^-15
const float MaxChannel = MAX(MAX(_r, _g), MAX(_b, kMinVal));
float cMax = MAX(cRed, MAX(cGreen, cBlue));
// Take the exponent of the maximum channel (rounding up the 9th bit) and
// add 15 to it. When added to the channels, it causes the implicit '1.0'
// bit and the first 8 mantissa bits to be shifted down to the low 9 bits
// of the mantissa, rounding the truncated bits.
union {
float f;
uint32_t i;
} R, G, B, E;
float expp = MAX(-B - 1.0f, floor(Math::log(cMax) / (real_t)Math_LN2)) + 1.0f + B;
E.f = MaxChannel;
E.i += 0x07804000; // Add 15 to the exponent and 0x4000 to the mantissa
E.i &= 0x7F800000; // Zero the mantissa
float sMax = (float)floor((cMax / Math::pow(2.0f, expp - B - N)) + 0.5f);
// This shifts the 9-bit values we need into the lowest bits, rounding as
// needed. Note that if the channel has a smaller exponent than the max
// channel, it will shift even more. This is intentional.
R.f = _r + E.f;
G.f = _g + E.f;
B.f = _b + E.f;
float exps = expp + 1.0f;
// Convert the Bias to the correct exponent in the upper 5 bits.
E.i <<= 4;
E.i += 0x10000000;
if (0.0f <= sMax && sMax < pow2to9) {
exps = expp;
}
float sRed = Math::floor((cRed / pow(2.0f, exps - B - N)) + 0.5f);
float sGreen = Math::floor((cGreen / pow(2.0f, exps - B - N)) + 0.5f);
float sBlue = Math::floor((cBlue / pow(2.0f, exps - B - N)) + 0.5f);
return (uint32_t(Math::fast_ftoi(sRed)) & 0x1FF) | ((uint32_t(Math::fast_ftoi(sGreen)) & 0x1FF) << 9) | ((uint32_t(Math::fast_ftoi(sBlue)) & 0x1FF) << 18) | ((uint32_t(Math::fast_ftoi(exps)) & 0x1F) << 27);
// Combine the fields. RGB floats have unwanted data in the upper 9
// bits. Only red needs to mask them off because green and blue shift
// it out to the left.
return E.i | (B.i << 18U) | (G.i << 9U) | (R.i & 511U);
}
_FORCE_INLINE_ Color blend(const Color &p_over) const {
@@ -174,16 +184,16 @@ struct [[nodiscard]] Color {
_FORCE_INLINE_ Color srgb_to_linear() const {
return Color(
r < 0.04045f ? r * (1.0f / 12.92f) : Math::pow((r + 0.055f) * (float)(1.0 / (1.0 + 0.055)), 2.4f),
g < 0.04045f ? g * (1.0f / 12.92f) : Math::pow((g + 0.055f) * (float)(1.0 / (1.0 + 0.055)), 2.4f),
b < 0.04045f ? b * (1.0f / 12.92f) : Math::pow((b + 0.055f) * (float)(1.0 / (1.0 + 0.055)), 2.4f),
r < 0.04045f ? r * (1.0f / 12.92f) : Math::pow(float((r + 0.055) * (1.0 / (1.0 + 0.055))), 2.4f),
g < 0.04045f ? g * (1.0f / 12.92f) : Math::pow(float((g + 0.055) * (1.0 / (1.0 + 0.055))), 2.4f),
b < 0.04045f ? b * (1.0f / 12.92f) : Math::pow(float((b + 0.055) * (1.0 / (1.0 + 0.055))), 2.4f),
a);
}
_FORCE_INLINE_ Color linear_to_srgb() const {
return Color(
r < 0.0031308f ? 12.92f * r : (1.0f + 0.055f) * Math::pow(r, 1.0f / 2.4f) - 0.055f,
g < 0.0031308f ? 12.92f * g : (1.0f + 0.055f) * Math::pow(g, 1.0f / 2.4f) - 0.055f,
b < 0.0031308f ? 12.92f * b : (1.0f + 0.055f) * Math::pow(b, 1.0f / 2.4f) - 0.055f, a);
r < 0.0031308f ? 12.92f * r : (1.0 + 0.055) * Math::pow(r, 1.0f / 2.4f) - 0.055,
g < 0.0031308f ? 12.92f * g : (1.0 + 0.055) * Math::pow(g, 1.0f / 2.4f) - 0.055,
b < 0.0031308f ? 12.92f * b : (1.0 + 0.055) * Math::pow(b, 1.0f / 2.4f) - 0.055, a);
}
static Color hex(uint32_t p_hex);
@@ -199,6 +209,7 @@ struct [[nodiscard]] Color {
static Color from_string(const String &p_string, const Color &p_default);
static Color from_hsv(float p_h, float p_s, float p_v, float p_alpha = 1.0f);
static Color from_rgbe9995(uint32_t p_rgbe);
static Color from_rgba8(int64_t p_r8, int64_t p_g8, int64_t p_b8, int64_t p_a8 = 255);
_FORCE_INLINE_ bool operator<(const Color &p_color) const; // Used in set keys.
operator String() const;
@@ -285,5 +296,3 @@ _FORCE_INLINE_ Color operator*(float p_scalar, const Color &p_color) {
}
} // namespace godot
#endif // GODOT_COLOR_HPP

View File

@@ -28,8 +28,7 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef GODOT_COLOR_NAMES_INC_HPP
#define GODOT_COLOR_NAMES_INC_HPP
#pragma once
namespace godot {
@@ -188,9 +187,6 @@ static NamedColor named_colors[] = {
{ "WHITE_SMOKE", Color::hex(0xF5F5F5FF) },
{ "YELLOW", Color::hex(0xFFFF00FF) },
{ "YELLOW_GREEN", Color::hex(0x9ACD32FF) },
{ nullptr, Color() },
};
} // namespace godot
#endif // GODOT_COLOR_NAMES_INC_HPP

View File

@@ -28,8 +28,7 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef GODOT_PLANE_HPP
#define GODOT_PLANE_HPP
#pragma once
#include <godot_cpp/classes/global_constants.hpp>
#include <godot_cpp/variant/vector3.hpp>
@@ -50,7 +49,7 @@ struct [[nodiscard]] Plane {
/* Plane-Point operations */
_FORCE_INLINE_ Vector3 center() const { return normal * d; }
_FORCE_INLINE_ Vector3 get_center() const { return normal * d; }
Vector3 get_any_perpendicular_normal() const;
_FORCE_INLINE_ bool is_point_over(const Vector3 &p_point) const; ///< Point is over plane
@@ -137,5 +136,3 @@ bool Plane::operator!=(const Plane &p_plane) const {
}
} // namespace godot
#endif // GODOT_PLANE_HPP

View File

@@ -28,10 +28,10 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef GODOT_PROJECTION_HPP
#define GODOT_PROJECTION_HPP
#pragma once
#include <godot_cpp/core/math.hpp>
#include <godot_cpp/templates/vector.hpp>
#include <godot_cpp/variant/vector3.hpp>
#include <godot_cpp/variant/vector4.hpp>
@@ -56,21 +56,21 @@ struct [[nodiscard]] Projection {
Vector4 columns[4];
_FORCE_INLINE_ const Vector4 &operator[](const int p_axis) const {
_FORCE_INLINE_ const Vector4 &operator[](int p_axis) const {
DEV_ASSERT((unsigned int)p_axis < 4);
return columns[p_axis];
}
_FORCE_INLINE_ Vector4 &operator[](const int p_axis) {
_FORCE_INLINE_ Vector4 &operator[](int p_axis) {
DEV_ASSERT((unsigned int)p_axis < 4);
return columns[p_axis];
}
float determinant() const;
real_t determinant() const;
void set_identity();
void set_zero();
void set_light_bias();
void set_depth_correction(bool p_flip_y = true);
void set_depth_correction(bool p_flip_y = true, bool p_reverse_z = true, bool p_remap_z = true);
void set_light_atlas_rect(const Rect2 &p_rect);
void set_perspective(real_t p_fovy_degrees, real_t p_aspect, real_t p_z_near, real_t p_z_far, bool p_flip_fov = false);
@@ -107,7 +107,7 @@ struct [[nodiscard]] Projection {
real_t get_fov() const;
bool is_orthogonal() const;
Array get_projection_planes(const Transform3D &p_transform) const;
Vector<Plane> get_projection_planes(const Transform3D &p_transform) const;
bool get_endpoints(const Transform3D &p_transform, Vector3 *p_8points) const;
Vector2 get_viewport_half_extents() const;
@@ -149,10 +149,11 @@ struct [[nodiscard]] Projection {
return !(*this == p_cam);
}
float get_lod_multiplier() const;
real_t get_lod_multiplier() const;
Projection();
Projection(const Vector4 &p_x, const Vector4 &p_y, const Vector4 &p_z, const Vector4 &p_w);
Projection(real_t p_xx, real_t p_xy, real_t p_xz, real_t p_xw, real_t p_yx, real_t p_yy, real_t p_yz, real_t p_yw, real_t p_zx, real_t p_zy, real_t p_zz, real_t p_zw, real_t p_wx, real_t p_wy, real_t p_wz, real_t p_ww);
Projection(const Transform3D &p_transform);
~Projection();
};
@@ -167,5 +168,3 @@ Vector3 Projection::xform(const Vector3 &p_vec3) const {
}
} // namespace godot
#endif // GODOT_PROJECTION_HPP

View File

@@ -28,8 +28,7 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef GODOT_QUATERNION_HPP
#define GODOT_QUATERNION_HPP
#pragma once
#include <godot_cpp/classes/global_constants.hpp>
#include <godot_cpp/core/math.hpp>
@@ -143,15 +142,24 @@ struct [[nodiscard]] Quaternion {
}
Quaternion(const Vector3 &p_v0, const Vector3 &p_v1) { // Shortest arc.
Vector3 c = p_v0.cross(p_v1);
real_t d = p_v0.dot(p_v1);
if (d < -1.0f + (real_t)CMP_EPSILON) {
x = 0;
y = 1;
z = 0;
#ifdef MATH_CHECKS
ERR_FAIL_COND_MSG(p_v0.is_zero_approx() || p_v1.is_zero_approx(), "The vectors must not be zero.");
#endif
constexpr real_t ALMOST_ONE = 1.0f - (real_t)CMP_EPSILON;
Vector3 n0 = p_v0.normalized();
Vector3 n1 = p_v1.normalized();
real_t d = n0.dot(n1);
if (Math::abs(d) > ALMOST_ONE) {
if (d >= 0) {
return; // Vectors are same.
}
Vector3 axis = n0.get_any_perpendicular();
x = axis.x;
y = axis.y;
z = axis.z;
w = 0;
} else {
Vector3 c = n0.cross(n1);
real_t s = Math::sqrt((1.0f + d) * 2.0f);
real_t rs = 1.0f / s;
@@ -232,5 +240,3 @@ _FORCE_INLINE_ Quaternion operator*(real_t p_real, const Quaternion &p_quaternio
}
} // namespace godot
#endif // GODOT_QUATERNION_HPP

View File

@@ -28,8 +28,7 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef GODOT_RECT2_HPP
#define GODOT_RECT2_HPP
#pragma once
#include <godot_cpp/classes/global_constants.hpp>
#include <godot_cpp/variant/vector2.hpp>
@@ -53,7 +52,7 @@ struct [[nodiscard]] Rect2 {
_FORCE_INLINE_ Vector2 get_center() const { return position + (size * 0.5f); }
inline bool intersects(const Rect2 &p_rect, const bool p_include_borders = false) const {
inline bool intersects(const Rect2 &p_rect, bool p_include_borders = false) const {
#ifdef MATH_CHECKS
if (unlikely(size.x < 0 || size.y < 0 || p_rect.size.x < 0 || p_rect.size.y < 0)) {
ERR_PRINT("Rect2 size is negative, this is not supported. Use Rect2.abs() to get a Rect2 with a positive size.");
@@ -106,17 +105,17 @@ struct [[nodiscard]] Rect2 {
}
if (p_point.y < position.y) {
real_t d = position.y - p_point.y;
dist = inside ? d : Math::min(dist, d);
dist = inside ? d : MIN(dist, d);
inside = false;
}
if (p_point.x >= (position.x + size.x)) {
real_t d = p_point.x - (position.x + size.x);
dist = inside ? d : Math::min(dist, d);
dist = inside ? d : MIN(dist, d);
inside = false;
}
if (p_point.y >= (position.y + size.y)) {
real_t d = p_point.y - (position.y + size.y);
dist = inside ? d : Math::min(dist, d);
dist = inside ? d : MIN(dist, d);
inside = false;
}
@@ -146,7 +145,7 @@ struct [[nodiscard]] Rect2 {
return size.x > 0.0f && size.y > 0.0f;
}
// Returns the intersection between two Rect2s or an empty Rect2 if there is no intersection
// Returns the intersection between two Rect2s or an empty Rect2 if there is no intersection.
inline Rect2 intersection(const Rect2 &p_rect) const {
Rect2 new_rect = p_rect;
@@ -283,13 +282,19 @@ struct [[nodiscard]] Rect2 {
return Rect2(position + size.minf(0), size.abs());
}
Vector2 get_support(const Vector2 &p_normal) const {
Vector2 half_extents = size * 0.5f;
Vector2 ofs = position + half_extents;
return Vector2(
(p_normal.x > 0) ? -half_extents.x : half_extents.x,
(p_normal.y > 0) ? -half_extents.y : half_extents.y) +
ofs;
_FORCE_INLINE_ Rect2 round() const {
return Rect2(position.round(), size.round());
}
Vector2 get_support(const Vector2 &p_direction) const {
Vector2 support = position;
if (p_direction.x > 0.0f) {
support.x += size.x;
}
if (p_direction.y > 0.0f) {
support.y += size.y;
}
return support;
}
_FORCE_INLINE_ bool intersects_filled_polygon(const Vector2 *p_points, int p_point_count) const {
@@ -305,14 +310,14 @@ struct [[nodiscard]] Rect2 {
i_f = i;
Vector2 r = (b - a);
float l = r.length();
const real_t l = r.length();
if (l == 0.0f) {
continue;
}
// Check inside.
Vector2 tg = r.orthogonal();
float s = tg.dot(center) - tg.dot(a);
const real_t s = tg.dot(center) - tg.dot(a);
if (s < 0.0f) {
side_plus++;
} else {
@@ -328,8 +333,8 @@ struct [[nodiscard]] Rect2 {
Vector2 t13 = (position - a) * ir;
Vector2 t24 = (end - a) * ir;
float tmin = Math::max(Math::min(t13.x, t24.x), Math::min(t13.y, t24.y));
float tmax = Math::min(Math::max(t13.x, t24.x), Math::max(t13.y, t24.y));
const real_t tmin = MAX(MIN(t13.x, t24.x), MIN(t13.y, t24.y));
const real_t tmax = MIN(MAX(t13.x, t24.x), MAX(t13.y, t24.y));
// if tmax < 0, ray (line) is intersecting AABB, but the whole AABB is behind us
if (tmax < 0 || tmin > tmax || tmin >= l) {
@@ -369,5 +374,3 @@ struct [[nodiscard]] Rect2 {
};
} // namespace godot
#endif // GODOT_RECT2_HPP

View File

@@ -28,8 +28,7 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef GODOT_RECT2I_HPP
#define GODOT_RECT2I_HPP
#pragma once
#include <godot_cpp/classes/global_constants.hpp>
#include <godot_cpp/variant/vector2i.hpp>
@@ -89,7 +88,7 @@ struct [[nodiscard]] Rect2i {
return size.x > 0 && size.y > 0;
}
// Returns the intersection between two Rect2is or an empty Rect2i if there is no intersection
// Returns the intersection between two Rect2is or an empty Rect2i if there is no intersection.
inline Rect2i intersection(const Rect2i &p_rect) const {
Rect2i new_rect = p_rect;
@@ -241,5 +240,3 @@ struct [[nodiscard]] Rect2i {
};
} // namespace godot
#endif // GODOT_RECT2I_HPP

View File

@@ -28,8 +28,7 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef GODOT_TRANSFORM2D_HPP
#define GODOT_TRANSFORM2D_HPP
#pragma once
#include <godot_cpp/variant/packed_vector2_array.hpp>
#include <godot_cpp/variant/rect2.hpp>
@@ -40,21 +39,24 @@ namespace godot {
class String;
struct [[nodiscard]] Transform2D {
// Warning #1: basis of Transform2D is stored differently from Basis. In terms of columns array, the basis matrix looks like "on paper":
// WARNING: The basis of Transform2D is stored differently from Basis.
// In terms of columns array, the basis matrix looks like "on paper":
// M = (columns[0][0] columns[1][0])
// (columns[0][1] columns[1][1])
// This is such that the columns, which can be interpreted as basis vectors of the coordinate system "painted" on the object, can be accessed as columns[i].
// Note that this is the opposite of the indices in mathematical texts, meaning: $M_{12}$ in a math book corresponds to columns[1][0] here.
// This is such that the columns, which can be interpreted as basis vectors
// of the coordinate system "painted" on the object, can be accessed as columns[i].
// NOTE: This is the opposite of the indices in mathematical texts,
// meaning: $M_{12}$ in a math book corresponds to columns[1][0] here.
// This requires additional care when working with explicit indices.
// See https://en.wikipedia.org/wiki/Row-_and_column-major_order for further reading.
// Warning #2: 2D be aware that unlike 3D code, 2D code uses a left-handed coordinate system: Y-axis points down,
// and angle is measure from +X to +Y in a clockwise-fashion.
// WARNING: Be aware that unlike 3D code, 2D code uses a left-handed coordinate system:
// Y-axis points down, and angle is measure from +X to +Y in a clockwise-fashion.
Vector2 columns[3];
_FORCE_INLINE_ real_t tdotx(const Vector2 &v) const { return columns[0][0] * v.x + columns[1][0] * v.y; }
_FORCE_INLINE_ real_t tdoty(const Vector2 &v) const { return columns[0][1] * v.x + columns[1][1] * v.y; }
_FORCE_INLINE_ real_t tdotx(const Vector2 &p_v) const { return columns[0][0] * p_v.x + columns[1][0] * p_v.y; }
_FORCE_INLINE_ real_t tdoty(const Vector2 &p_v) const { return columns[0][1] * p_v.x + columns[1][1] * p_v.y; }
const Vector2 &operator[](int p_idx) const { return columns[p_idx]; }
Vector2 &operator[](int p_idx) { return columns[p_idx]; }
@@ -65,20 +67,20 @@ struct [[nodiscard]] Transform2D {
void affine_invert();
Transform2D affine_inverse() const;
void set_rotation(const real_t p_rot);
void set_rotation(real_t p_rot);
real_t get_rotation() const;
real_t get_skew() const;
void set_skew(const real_t p_angle);
_FORCE_INLINE_ void set_rotation_and_scale(const real_t p_rot, const Size2 &p_scale);
_FORCE_INLINE_ void set_rotation_scale_and_skew(const real_t p_rot, const Size2 &p_scale, const real_t p_skew);
void rotate(const real_t p_angle);
void set_skew(real_t p_angle);
_FORCE_INLINE_ void set_rotation_and_scale(real_t p_rot, const Size2 &p_scale);
_FORCE_INLINE_ void set_rotation_scale_and_skew(real_t p_rot, const Size2 &p_scale, real_t p_skew);
void rotate(real_t p_angle);
void scale(const Size2 &p_scale);
void scale_basis(const Size2 &p_scale);
void translate_local(const real_t p_tx, const real_t p_ty);
void translate_local(real_t p_tx, real_t p_ty);
void translate_local(const Vector2 &p_translation);
real_t basis_determinant() const;
real_t determinant() const;
Size2 get_scale() const;
void set_scale(const Size2 &p_scale);
@@ -86,18 +88,18 @@ struct [[nodiscard]] Transform2D {
_FORCE_INLINE_ const Vector2 &get_origin() const { return columns[2]; }
_FORCE_INLINE_ void set_origin(const Vector2 &p_origin) { columns[2] = p_origin; }
Transform2D basis_scaled(const Size2 &p_scale) const;
Transform2D scaled(const Size2 &p_scale) const;
Transform2D scaled_local(const Size2 &p_scale) const;
Transform2D translated(const Vector2 &p_offset) const;
Transform2D translated_local(const Vector2 &p_offset) const;
Transform2D rotated(const real_t p_angle) const;
Transform2D rotated_local(const real_t p_angle) const;
Transform2D rotated(real_t p_angle) const;
Transform2D rotated_local(real_t p_angle) const;
Transform2D untranslated() const;
void orthonormalize();
Transform2D orthonormalized() const;
bool is_conformal() const;
bool is_equal_approx(const Transform2D &p_transform) const;
bool is_finite() const;
@@ -108,10 +110,12 @@ struct [[nodiscard]] Transform2D {
void operator*=(const Transform2D &p_transform);
Transform2D operator*(const Transform2D &p_transform) const;
void operator*=(const real_t p_val);
Transform2D operator*(const real_t p_val) const;
void operator*=(real_t p_val);
Transform2D operator*(real_t p_val) const;
void operator/=(real_t p_val);
Transform2D operator/(real_t p_val) const;
Transform2D interpolate_with(const Transform2D &p_transform, const real_t p_c) const;
Transform2D interpolate_with(const Transform2D &p_transform, real_t p_c) const;
_FORCE_INLINE_ Vector2 basis_xform(const Vector2 &p_vec) const;
_FORCE_INLINE_ Vector2 basis_xform_inv(const Vector2 &p_vec) const;
@@ -124,13 +128,13 @@ struct [[nodiscard]] Transform2D {
operator String() const;
Transform2D(const real_t xx, const real_t xy, const real_t yx, const real_t yy, const real_t ox, const real_t oy) {
columns[0][0] = xx;
columns[0][1] = xy;
columns[1][0] = yx;
columns[1][1] = yy;
columns[2][0] = ox;
columns[2][1] = oy;
Transform2D(real_t p_xx, real_t p_xy, real_t p_yx, real_t p_yy, real_t p_ox, real_t p_oy) {
columns[0][0] = p_xx;
columns[0][1] = p_xy;
columns[1][0] = p_yx;
columns[1][1] = p_yy;
columns[2][0] = p_ox;
columns[2][1] = p_oy;
}
Transform2D(const Vector2 &p_x, const Vector2 &p_y, const Vector2 &p_origin) {
@@ -139,9 +143,9 @@ struct [[nodiscard]] Transform2D {
columns[2] = p_origin;
}
Transform2D(const real_t p_rot, const Vector2 &p_pos);
Transform2D(real_t p_rot, const Vector2 &p_pos);
Transform2D(const real_t p_rot, const Size2 &p_scale, const real_t p_skew, const Vector2 &p_pos);
Transform2D(real_t p_rot, const Size2 &p_scale, real_t p_skew, const Vector2 &p_pos);
Transform2D() {
columns[0][0] = 1.0;
@@ -189,14 +193,14 @@ Rect2 Transform2D::xform(const Rect2 &p_rect) const {
return new_rect;
}
void Transform2D::set_rotation_and_scale(const real_t p_rot, const Size2 &p_scale) {
void Transform2D::set_rotation_and_scale(real_t p_rot, const Size2 &p_scale) {
columns[0][0] = Math::cos(p_rot) * p_scale.x;
columns[1][1] = Math::cos(p_rot) * p_scale.y;
columns[1][0] = -Math::sin(p_rot) * p_scale.y;
columns[0][1] = Math::sin(p_rot) * p_scale.x;
}
void Transform2D::set_rotation_scale_and_skew(const real_t p_rot, const Size2 &p_scale, const real_t p_skew) {
void Transform2D::set_rotation_scale_and_skew(real_t p_rot, const Size2 &p_scale, real_t p_skew) {
columns[0][0] = Math::cos(p_rot) * p_scale.x;
columns[1][1] = Math::cos(p_rot + p_skew) * p_scale.y;
columns[1][0] = -Math::sin(p_rot + p_skew) * p_scale.y;
@@ -247,5 +251,3 @@ PackedVector2Array Transform2D::xform_inv(const PackedVector2Array &p_array) con
}
} // namespace godot
#endif // GODOT_TRANSFORM2D_HPP

View File

@@ -28,8 +28,7 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef GODOT_TRANSFORM3D_HPP
#define GODOT_TRANSFORM3D_HPP
#pragma once
#include <godot_cpp/core/math.hpp>
#include <godot_cpp/variant/aabb.hpp>
@@ -55,8 +54,8 @@ struct [[nodiscard]] Transform3D {
void rotate(const Vector3 &p_axis, real_t p_angle);
void rotate_basis(const Vector3 &p_axis, real_t p_angle);
void set_look_at(const Vector3 &p_eye, const Vector3 &p_target, const Vector3 &p_up = Vector3(0, 1, 0));
Transform3D looking_at(const Vector3 &p_target, const Vector3 &p_up = Vector3(0, 1, 0)) const;
void set_look_at(const Vector3 &p_eye, const Vector3 &p_target, const Vector3 &p_up = Vector3(0, 1, 0), bool p_use_model_front = false);
Transform3D looking_at(const Vector3 &p_target, const Vector3 &p_up = Vector3(0, 1, 0), bool p_use_model_front = false) const;
void scale(const Vector3 &p_scale);
Transform3D scaled(const Vector3 &p_scale) const;
@@ -105,8 +104,10 @@ struct [[nodiscard]] Transform3D {
void operator*=(const Transform3D &p_transform);
Transform3D operator*(const Transform3D &p_transform) const;
void operator*=(const real_t p_val);
Transform3D operator*(const real_t p_val) const;
void operator*=(real_t p_val);
Transform3D operator*(real_t p_val) const;
void operator/=(real_t p_val);
Transform3D operator/(real_t p_val) const;
Transform3D interpolate_with(const Transform3D &p_transform, real_t p_c) const;
@@ -116,11 +117,11 @@ struct [[nodiscard]] Transform3D {
basis.xform(v));
}
void set(real_t xx, real_t xy, real_t xz, real_t yx, real_t yy, real_t yz, real_t zx, real_t zy, real_t zz, real_t tx, real_t ty, real_t tz) {
basis.set(xx, xy, xz, yx, yy, yz, zx, zy, zz);
origin.x = tx;
origin.y = ty;
origin.z = tz;
void set(real_t p_xx, real_t p_xy, real_t p_xz, real_t p_yx, real_t p_yy, real_t p_yz, real_t p_zx, real_t p_zy, real_t p_zz, real_t p_tx, real_t p_ty, real_t p_tz) {
basis.set(p_xx, p_xy, p_xz, p_yx, p_yy, p_yz, p_zx, p_zy, p_zz);
origin.x = p_tx;
origin.y = p_ty;
origin.z = p_tz;
}
operator String() const;
@@ -128,7 +129,7 @@ struct [[nodiscard]] Transform3D {
Transform3D() {}
Transform3D(const Basis &p_basis, const Vector3 &p_origin = Vector3());
Transform3D(const Vector3 &p_x, const Vector3 &p_y, const Vector3 &p_z, const Vector3 &p_origin);
Transform3D(real_t xx, real_t xy, real_t xz, real_t yx, real_t yy, real_t yz, real_t zx, real_t zy, real_t zz, real_t ox, real_t oy, real_t oz);
Transform3D(real_t p_xx, real_t p_xy, real_t p_xz, real_t p_yx, real_t p_yy, real_t p_yz, real_t p_zx, real_t p_zy, real_t p_zz, real_t p_ox, real_t p_oy, real_t p_oz);
};
_FORCE_INLINE_ Vector3 Transform3D::xform(const Vector3 &p_vector) const {
@@ -272,5 +273,3 @@ _FORCE_INLINE_ Plane Transform3D::xform_inv_fast(const Plane &p_plane, const Tra
}
} // namespace godot
#endif // GODOT_TRANSFORM3D_HPP

View File

@@ -28,8 +28,7 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef GODOT_TYPED_ARRAY_HPP
#define GODOT_TYPED_ARRAY_HPP
#pragma once
#include <godot_cpp/variant/array.hpp>
#include <godot_cpp/variant/variant.hpp>
@@ -54,6 +53,8 @@ public:
assign(p_array);
}
}
_FORCE_INLINE_ TypedArray(std::initializer_list<Variant> p_init) :
TypedArray(Array(p_init)) {}
_FORCE_INLINE_ TypedArray() {
set_typed(Variant::OBJECT, T::get_class_static(), Variant());
}
@@ -69,6 +70,9 @@ public:
ERR_FAIL_COND_MSG(!is_same_typed(p_array), "Cannot assign an array with a different element type."); \
_ref(p_array); \
} \
_FORCE_INLINE_ TypedArray(std::initializer_list<Variant> p_init) : \
Array(Array(p_init), m_variant_type, StringName(), Variant()) { \
} \
_FORCE_INLINE_ TypedArray(const Variant &p_variant) : \
TypedArray(Array(p_variant)) { \
} \
@@ -138,5 +142,3 @@ MAKE_TYPED_ARRAY(PackedColorArray, Variant::PACKED_COLOR_ARRAY)
#undef MAKE_TYPED_ARRAY
} // namespace godot
#endif // GODOT_TYPED_ARRAY_HPP

View File

@@ -28,10 +28,10 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef GODOT_TYPED_DICTIONARY_HPP
#define GODOT_TYPED_DICTIONARY_HPP
#pragma once
#include <godot_cpp/core/type_info.hpp>
#include <godot_cpp/templates/pair.hpp>
#include <godot_cpp/variant/dictionary.hpp>
#include <godot_cpp/variant/variant.hpp>
@@ -58,54 +58,75 @@ public:
_FORCE_INLINE_ TypedDictionary() {
set_typed(Variant::OBJECT, K::get_class_static(), Variant(), Variant::OBJECT, V::get_class_static(), Variant());
}
_FORCE_INLINE_ TypedDictionary(std::initializer_list<KeyValue<K, V>> p_init) :
Dictionary() {
set_typed(Variant::OBJECT, K::get_class_static(), Variant(), Variant::OBJECT, V::get_class_static(), Variant());
for (const KeyValue<K, V> &E : p_init) {
operator[](E.key) = E.value;
}
}
};
//specialization for the rest of variant types
#define MAKE_TYPED_DICTIONARY_WITH_OBJECT(m_type, m_variant_type) \
template <typename T> \
class TypedDictionary<T, m_type> : public Dictionary { \
public: \
_FORCE_INLINE_ void operator=(const Dictionary &p_dictionary) { \
ERR_FAIL_COND_MSG(!is_same_typed(p_dictionary), "Cannot assign an dictionary with a different element type."); \
Dictionary::operator=(p_dictionary); \
} \
_FORCE_INLINE_ TypedDictionary(const Variant &p_variant) : \
TypedDictionary(Dictionary(p_variant)) { \
} \
_FORCE_INLINE_ TypedDictionary(const Dictionary &p_dictionary) { \
set_typed(Variant::OBJECT, T::get_class_static(), Variant(), m_variant_type, StringName(), Variant()); \
if (is_same_typed(p_dictionary)) { \
Dictionary::operator=(p_dictionary); \
} else { \
assign(p_dictionary); \
} \
} \
_FORCE_INLINE_ TypedDictionary() { \
set_typed(Variant::OBJECT, T::get_class_static(), Variant(), m_variant_type, StringName(), Variant()); \
} \
}; \
template <typename T> \
class TypedDictionary<m_type, T> : public Dictionary { \
public: \
_FORCE_INLINE_ void operator=(const Dictionary &p_dictionary) { \
ERR_FAIL_COND_MSG(!is_same_typed(p_dictionary), "Cannot assign an dictionary with a different element type."); \
Dictionary::operator=(p_dictionary); \
} \
_FORCE_INLINE_ TypedDictionary(const Variant &p_variant) : \
TypedDictionary(Dictionary(p_variant)) { \
} \
_FORCE_INLINE_ TypedDictionary(const Dictionary &p_dictionary) { \
set_typed(m_variant_type, StringName(), Variant(), Variant::OBJECT, T::get_class_static(), Variant()); \
if (is_same_typed(p_dictionary)) { \
Dictionary::operator=(p_dictionary); \
} else { \
assign(p_dictionary); \
} \
} \
_FORCE_INLINE_ TypedDictionary() { \
set_typed(m_variant_type, StringName(), Variant(), Variant::OBJECT, T::get_class_static(), Variant()); \
} \
#define MAKE_TYPED_DICTIONARY_WITH_OBJECT(m_type, m_variant_type) \
template <typename T> \
class TypedDictionary<T, m_type> : public Dictionary { \
public: \
_FORCE_INLINE_ void operator=(const Dictionary &p_dictionary) { \
ERR_FAIL_COND_MSG(!is_same_typed(p_dictionary), "Cannot assign an dictionary with a different element type."); \
Dictionary::operator=(p_dictionary); \
} \
_FORCE_INLINE_ TypedDictionary(const Variant &p_variant) : \
TypedDictionary(Dictionary(p_variant)) { \
} \
_FORCE_INLINE_ TypedDictionary(const Dictionary &p_dictionary) { \
set_typed(Variant::OBJECT, T::get_class_static(), Variant(), m_variant_type, StringName(), Variant()); \
if (is_same_typed(p_dictionary)) { \
Dictionary::operator=(p_dictionary); \
} else { \
assign(p_dictionary); \
} \
} \
_FORCE_INLINE_ TypedDictionary() { \
set_typed(Variant::OBJECT, T::get_class_static(), Variant(), m_variant_type, StringName(), Variant()); \
} \
_FORCE_INLINE_ TypedDictionary(std::initializer_list<KeyValue<T, m_type>> p_init) : \
Dictionary() { \
set_typed(Variant::OBJECT, T::get_class_static(), Variant(), m_variant_type, StringName(), Variant()); \
for (const KeyValue<T, m_type> &E : p_init) { \
operator[](E.key) = E.value; \
} \
} \
}; \
template <typename T> \
class TypedDictionary<m_type, T> : public Dictionary { \
public: \
_FORCE_INLINE_ void operator=(const Dictionary &p_dictionary) { \
ERR_FAIL_COND_MSG(!is_same_typed(p_dictionary), "Cannot assign an dictionary with a different element type."); \
Dictionary::operator=(p_dictionary); \
} \
_FORCE_INLINE_ TypedDictionary(const Variant &p_variant) : \
TypedDictionary(Dictionary(p_variant)) { \
} \
_FORCE_INLINE_ TypedDictionary(const Dictionary &p_dictionary) { \
set_typed(m_variant_type, StringName(), Variant(), Variant::OBJECT, T::get_class_static(), Variant()); \
if (is_same_typed(p_dictionary)) { \
Dictionary::operator=(p_dictionary); \
} else { \
assign(p_dictionary); \
} \
} \
_FORCE_INLINE_ TypedDictionary() { \
set_typed(m_variant_type, StringName(), Variant(), Variant::OBJECT, T::get_class_static(), Variant()); \
} \
_FORCE_INLINE_ TypedDictionary(std::initializer_list<KeyValue<m_type, T>> p_init) : \
Dictionary() { \
set_typed(m_variant_type, StringName(), Variant(), Variant::OBJECT, std::remove_pointer<T>::type::get_class_static(), Variant()); \
for (const KeyValue<m_type, T> &E : p_init) { \
operator[](E.key) = E.value; \
} \
} \
};
#define MAKE_TYPED_DICTIONARY_EXPANDED(m_type_key, m_variant_type_key, m_type_value, m_variant_type_value) \
@@ -130,6 +151,13 @@ public:
_FORCE_INLINE_ TypedDictionary() { \
set_typed(m_variant_type_key, StringName(), Variant(), m_variant_type_value, StringName(), Variant()); \
} \
_FORCE_INLINE_ TypedDictionary(std::initializer_list<KeyValue<m_type_key, m_type_value>> p_init) : \
Dictionary() { \
set_typed(m_variant_type_key, StringName(), Variant(), m_variant_type_value, StringName(), Variant()); \
for (const KeyValue<m_type_key, m_type_value> &E : p_init) { \
operator[](E.key) = E.value; \
} \
} \
};
#define MAKE_TYPED_DICTIONARY_NIL(m_type, m_variant_type) \
@@ -435,5 +463,3 @@ MAKE_TYPED_DICTIONARY_INFO(IPAddress, Variant::STRING)
#undef MAKE_TYPED_DICTIONARY_INFO_WITH_OBJECT
} // namespace godot
#endif // GODOT_TYPED_DICTIONARY_HPP

View File

@@ -28,11 +28,11 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef GODOT_VARIANT_HPP
#define GODOT_VARIANT_HPP
#pragma once
#include <godot_cpp/core/defs.hpp>
#include <godot_cpp/variant/array.hpp>
#include <godot_cpp/variant/builtin_types.hpp>
#include <godot_cpp/variant/variant_size.hpp>
@@ -145,7 +145,7 @@ private:
static GDExtensionTypeFromVariantConstructorFunc to_type_constructor[VARIANT_MAX];
public:
_FORCE_INLINE_ GDExtensionVariantPtr _native_ptr() const { return const_cast<uint8_t(*)[GODOT_CPP_VARIANT_SIZE]>(&opaque); }
_FORCE_INLINE_ GDExtensionVariantPtr _native_ptr() const { return const_cast<uint8_t (*)[GODOT_CPP_VARIANT_SIZE]>(&opaque); }
Variant();
Variant(std::nullptr_t n) :
Variant() {}
@@ -358,6 +358,66 @@ String vformat(const String &p_text, const VarArgs... p_args) {
return p_text % args_array;
}
Variant &Array::Iterator::operator*() const {
return *elem_ptr;
}
Variant *Array::Iterator::operator->() const {
return elem_ptr;
}
Array::Iterator &Array::Iterator::operator++() {
elem_ptr++;
return *this;
}
Array::Iterator &Array::Iterator::operator--() {
elem_ptr--;
return *this;
}
const Variant &Array::ConstIterator::operator*() const {
return *elem_ptr;
}
const Variant *Array::ConstIterator::operator->() const {
return elem_ptr;
}
Array::ConstIterator &Array::ConstIterator::operator++() {
elem_ptr++;
return *this;
}
Array::ConstIterator &Array::ConstIterator::operator--() {
elem_ptr--;
return *this;
}
Array::Iterator Array::begin() {
return Array::Iterator(ptrw());
}
Array::Iterator Array::end() {
return Array::Iterator(ptrw() + size());
}
Array::ConstIterator Array::begin() const {
return Array::ConstIterator(ptr());
}
Array::ConstIterator Array::end() const {
return Array::ConstIterator(ptr() + size());
}
Array::Array(std::initializer_list<Variant> p_init) :
Array() {
ERR_FAIL_COND(resize(p_init.size()) != 0);
size_t i = 0;
for (const Variant &element : p_init) {
set(i++, element);
}
}
#include <godot_cpp/variant/builtin_vararg_methods.hpp>
#ifdef REAL_T_IS_DOUBLE
@@ -367,5 +427,3 @@ using PackedRealArray = PackedFloat32Array;
#endif // REAL_T_IS_DOUBLE
} // namespace godot
#endif // GODOT_VARIANT_HPP

View File

@@ -28,8 +28,7 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef GODOT_VARIANT_INTERNAL_HPP
#define GODOT_VARIANT_INTERNAL_HPP
#pragma once
#include <gdextension_interface.h>
#include <godot_cpp/variant/variant.hpp>
@@ -505,5 +504,3 @@ struct VariantDefaultInitializer {
};
} // namespace godot
#endif // GODOT_VARIANT_INTERNAL_HPP

View File

@@ -28,8 +28,7 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef GODOT_VECTOR2_HPP
#define GODOT_VECTOR2_HPP
#pragma once
#include <godot_cpp/core/error_macros.hpp>
#include <godot_cpp/core/math.hpp>
@@ -62,13 +61,13 @@ struct [[nodiscard]] Vector2 {
real_t coord[2] = { 0 };
};
_FORCE_INLINE_ real_t &operator[](int p_idx) {
DEV_ASSERT((unsigned int)p_idx < 2);
return coord[p_idx];
_FORCE_INLINE_ real_t &operator[](int p_axis) {
DEV_ASSERT((unsigned int)p_axis < 2);
return coord[p_axis];
}
_FORCE_INLINE_ const real_t &operator[](int p_idx) const {
DEV_ASSERT((unsigned int)p_idx < 2);
return coord[p_idx];
_FORCE_INLINE_ const real_t &operator[](int p_axis) const {
DEV_ASSERT((unsigned int)p_axis < 2);
return coord[p_axis];
}
_FORCE_INLINE_ Vector2::Axis min_axis_index() const {
@@ -85,7 +84,7 @@ struct [[nodiscard]] Vector2 {
real_t length() const;
real_t length_squared() const;
Vector2 limit_length(const real_t p_len = 1.0) const;
Vector2 limit_length(real_t p_len = 1.0) const;
Vector2 min(const Vector2 &p_vector2) const {
return Vector2(MIN(x, p_vector2.x), MIN(y, p_vector2.y));
@@ -111,19 +110,20 @@ struct [[nodiscard]] Vector2 {
real_t dot(const Vector2 &p_other) const;
real_t cross(const Vector2 &p_other) const;
Vector2 posmod(const real_t p_mod) const;
Vector2 posmod(real_t p_mod) const;
Vector2 posmodv(const Vector2 &p_modv) const;
Vector2 project(const Vector2 &p_to) const;
Vector2 plane_project(const real_t p_d, const Vector2 &p_vec) const;
Vector2 plane_project(real_t p_d, const Vector2 &p_vec) const;
_FORCE_INLINE_ Vector2 lerp(const Vector2 &p_to, const real_t p_weight) const;
_FORCE_INLINE_ Vector2 slerp(const Vector2 &p_to, const real_t p_weight) const;
_FORCE_INLINE_ Vector2 cubic_interpolate(const Vector2 &p_b, const Vector2 &p_pre_a, const Vector2 &p_post_b, const real_t p_weight) const;
_FORCE_INLINE_ Vector2 cubic_interpolate_in_time(const Vector2 &p_b, const Vector2 &p_pre_a, const Vector2 &p_post_b, const real_t p_weight, const real_t &p_b_t, const real_t &p_pre_a_t, const real_t &p_post_b_t) const;
_FORCE_INLINE_ Vector2 bezier_interpolate(const Vector2 &p_control_1, const Vector2 &p_control_2, const Vector2 &p_end, const real_t p_t) const;
_FORCE_INLINE_ Vector2 lerp(const Vector2 &p_to, real_t p_weight) const;
_FORCE_INLINE_ Vector2 slerp(const Vector2 &p_to, real_t p_weight) const;
_FORCE_INLINE_ Vector2 cubic_interpolate(const Vector2 &p_b, const Vector2 &p_pre_a, const Vector2 &p_post_b, real_t p_weight) const;
_FORCE_INLINE_ Vector2 cubic_interpolate_in_time(const Vector2 &p_b, const Vector2 &p_pre_a, const Vector2 &p_post_b, real_t p_weight, real_t p_b_t, real_t p_pre_a_t, real_t p_post_b_t) const;
_FORCE_INLINE_ Vector2 bezier_interpolate(const Vector2 &p_control_1, const Vector2 &p_control_2, const Vector2 &p_end, real_t p_t) const;
_FORCE_INLINE_ Vector2 bezier_derivative(const Vector2 &p_control_1, const Vector2 &p_control_2, const Vector2 &p_end, real_t p_t) const;
Vector2 move_toward(const Vector2 &p_to, const real_t p_delta) const;
Vector2 move_toward(const Vector2 &p_to, real_t p_delta) const;
Vector2 slide(const Vector2 &p_normal) const;
Vector2 bounce(const Vector2 &p_normal) const;
@@ -139,16 +139,16 @@ struct [[nodiscard]] Vector2 {
void operator-=(const Vector2 &p_v);
Vector2 operator*(const Vector2 &p_v1) const;
Vector2 operator*(const real_t &rvalue) const;
void operator*=(const real_t &rvalue);
void operator*=(const Vector2 &rvalue) { *this = *this * rvalue; }
Vector2 operator*(real_t p_rvalue) const;
void operator*=(real_t p_rvalue);
void operator*=(const Vector2 &p_rvalue) { *this = *this * p_rvalue; }
Vector2 operator/(const Vector2 &p_v1) const;
Vector2 operator/(const real_t &rvalue) const;
Vector2 operator/(real_t p_rvalue) const;
void operator/=(const real_t &rvalue);
void operator/=(const Vector2 &rvalue) { *this = *this / rvalue; }
void operator/=(real_t p_rvalue);
void operator/=(const Vector2 &p_rvalue) { *this = *this / p_rvalue; }
Vector2 operator-() const;
@@ -161,13 +161,13 @@ struct [[nodiscard]] Vector2 {
bool operator>=(const Vector2 &p_vec2) const { return x == p_vec2.x ? (y >= p_vec2.y) : (x > p_vec2.x); }
real_t angle() const;
static Vector2 from_angle(const real_t p_angle);
static Vector2 from_angle(real_t p_angle);
_FORCE_INLINE_ Vector2 abs() const {
return Vector2(Math::abs(x), Math::abs(y));
}
Vector2 rotated(const real_t p_by) const;
Vector2 rotated(real_t p_by) const;
Vector2 orthogonal() const {
return Vector2(y, -x);
}
@@ -186,13 +186,13 @@ struct [[nodiscard]] Vector2 {
operator Vector2i() const;
_FORCE_INLINE_ Vector2() {}
_FORCE_INLINE_ Vector2(const real_t p_x, const real_t p_y) {
_FORCE_INLINE_ Vector2(real_t p_x, real_t p_y) {
x = p_x;
y = p_y;
}
};
_FORCE_INLINE_ Vector2 Vector2::plane_project(const real_t p_d, const Vector2 &p_vec) const {
_FORCE_INLINE_ Vector2 Vector2::plane_project(real_t p_d, const Vector2 &p_vec) const {
return p_vec - *this * (dot(p_vec) - p_d);
}
@@ -218,26 +218,26 @@ _FORCE_INLINE_ Vector2 Vector2::operator*(const Vector2 &p_v1) const {
return Vector2(x * p_v1.x, y * p_v1.y);
}
_FORCE_INLINE_ Vector2 Vector2::operator*(const real_t &rvalue) const {
return Vector2(x * rvalue, y * rvalue);
_FORCE_INLINE_ Vector2 Vector2::operator*(real_t p_rvalue) const {
return Vector2(x * p_rvalue, y * p_rvalue);
}
_FORCE_INLINE_ void Vector2::operator*=(const real_t &rvalue) {
x *= rvalue;
y *= rvalue;
_FORCE_INLINE_ void Vector2::operator*=(real_t p_rvalue) {
x *= p_rvalue;
y *= p_rvalue;
}
_FORCE_INLINE_ Vector2 Vector2::operator/(const Vector2 &p_v1) const {
return Vector2(x / p_v1.x, y / p_v1.y);
}
_FORCE_INLINE_ Vector2 Vector2::operator/(const real_t &rvalue) const {
return Vector2(x / rvalue, y / rvalue);
_FORCE_INLINE_ Vector2 Vector2::operator/(real_t p_rvalue) const {
return Vector2(x / p_rvalue, y / p_rvalue);
}
_FORCE_INLINE_ void Vector2::operator/=(const real_t &rvalue) {
x /= rvalue;
y /= rvalue;
_FORCE_INLINE_ void Vector2::operator/=(real_t p_rvalue) {
x /= p_rvalue;
y /= p_rvalue;
}
_FORCE_INLINE_ Vector2 Vector2::operator-() const {
@@ -252,16 +252,14 @@ _FORCE_INLINE_ bool Vector2::operator!=(const Vector2 &p_vec2) const {
return x != p_vec2.x || y != p_vec2.y;
}
Vector2 Vector2::lerp(const Vector2 &p_to, const real_t p_weight) const {
Vector2 Vector2::lerp(const Vector2 &p_to, real_t p_weight) const {
Vector2 res = *this;
res.x += (p_weight * (p_to.x - x));
res.y += (p_weight * (p_to.y - y));
res.x = Math::lerp(res.x, p_to.x, p_weight);
res.y = Math::lerp(res.y, p_to.y, p_weight);
return res;
}
Vector2 Vector2::slerp(const Vector2 &p_to, const real_t p_weight) const {
Vector2 Vector2::slerp(const Vector2 &p_to, real_t p_weight) const {
real_t start_length_sq = length_squared();
real_t end_length_sq = p_to.length_squared();
if (unlikely(start_length_sq == 0.0f || end_length_sq == 0.0f)) {
@@ -274,31 +272,32 @@ Vector2 Vector2::slerp(const Vector2 &p_to, const real_t p_weight) const {
return rotated(angle * p_weight) * (result_length / start_length);
}
Vector2 Vector2::cubic_interpolate(const Vector2 &p_b, const Vector2 &p_pre_a, const Vector2 &p_post_b, const real_t p_weight) const {
Vector2 Vector2::cubic_interpolate(const Vector2 &p_b, const Vector2 &p_pre_a, const Vector2 &p_post_b, real_t p_weight) const {
Vector2 res = *this;
res.x = Math::cubic_interpolate(res.x, p_b.x, p_pre_a.x, p_post_b.x, p_weight);
res.y = Math::cubic_interpolate(res.y, p_b.y, p_pre_a.y, p_post_b.y, p_weight);
return res;
}
Vector2 Vector2::cubic_interpolate_in_time(const Vector2 &p_b, const Vector2 &p_pre_a, const Vector2 &p_post_b, const real_t p_weight, const real_t &p_b_t, const real_t &p_pre_a_t, const real_t &p_post_b_t) const {
Vector2 Vector2::cubic_interpolate_in_time(const Vector2 &p_b, const Vector2 &p_pre_a, const Vector2 &p_post_b, real_t p_weight, real_t p_b_t, real_t p_pre_a_t, real_t p_post_b_t) const {
Vector2 res = *this;
res.x = Math::cubic_interpolate_in_time(res.x, p_b.x, p_pre_a.x, p_post_b.x, p_weight, p_b_t, p_pre_a_t, p_post_b_t);
res.y = Math::cubic_interpolate_in_time(res.y, p_b.y, p_pre_a.y, p_post_b.y, p_weight, p_b_t, p_pre_a_t, p_post_b_t);
return res;
}
Vector2 Vector2::bezier_interpolate(const Vector2 &p_control_1, const Vector2 &p_control_2, const Vector2 &p_end, const real_t p_t) const {
Vector2 Vector2::bezier_interpolate(const Vector2 &p_control_1, const Vector2 &p_control_2, const Vector2 &p_end, real_t p_t) const {
Vector2 res = *this;
res.x = Math::bezier_interpolate(res.x, p_control_1.x, p_control_2.x, p_end.x, p_t);
res.y = Math::bezier_interpolate(res.y, p_control_1.y, p_control_2.y, p_end.y, p_t);
return res;
}
/* Formula from Wikipedia article on Bezier curves. */
real_t omt = (1.0 - p_t);
real_t omt2 = omt * omt;
real_t omt3 = omt2 * omt;
real_t t2 = p_t * p_t;
real_t t3 = t2 * p_t;
return res * omt3 + p_control_1 * omt2 * p_t * 3.0 + p_control_2 * omt * t2 * 3.0 + p_end * t3;
Vector2 Vector2::bezier_derivative(const Vector2 &p_control_1, const Vector2 &p_control_2, const Vector2 &p_end, real_t p_t) const {
Vector2 res = *this;
res.x = Math::bezier_derivative(res.x, p_control_1.x, p_control_2.x, p_end.x, p_t);
res.y = Math::bezier_derivative(res.y, p_control_1.y, p_control_2.y, p_end.y, p_t);
return res;
}
Vector2 Vector2::direction_to(const Vector2 &p_to) const {
@@ -310,19 +309,19 @@ Vector2 Vector2::direction_to(const Vector2 &p_to) const {
// Multiplication operators required to workaround issues with LLVM using implicit conversion
// to Vector2i instead for integers where it should not.
_FORCE_INLINE_ Vector2 operator*(const float p_scalar, const Vector2 &p_vec) {
_FORCE_INLINE_ Vector2 operator*(float p_scalar, const Vector2 &p_vec) {
return p_vec * p_scalar;
}
_FORCE_INLINE_ Vector2 operator*(const double p_scalar, const Vector2 &p_vec) {
_FORCE_INLINE_ Vector2 operator*(double p_scalar, const Vector2 &p_vec) {
return p_vec * p_scalar;
}
_FORCE_INLINE_ Vector2 operator*(const int32_t p_scalar, const Vector2 &p_vec) {
_FORCE_INLINE_ Vector2 operator*(int32_t p_scalar, const Vector2 &p_vec) {
return p_vec * p_scalar;
}
_FORCE_INLINE_ Vector2 operator*(const int64_t p_scalar, const Vector2 &p_vec) {
_FORCE_INLINE_ Vector2 operator*(int64_t p_scalar, const Vector2 &p_vec) {
return p_vec * p_scalar;
}
@@ -330,5 +329,3 @@ typedef Vector2 Size2;
typedef Vector2 Point2;
} // namespace godot
#endif // GODOT_VECTOR2_HPP

View File

@@ -28,8 +28,7 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef GODOT_VECTOR2I_HPP
#define GODOT_VECTOR2I_HPP
#pragma once
#include <godot_cpp/core/error_macros.hpp>
#include <godot_cpp/core/math.hpp>
@@ -62,13 +61,13 @@ struct [[nodiscard]] Vector2i {
int32_t coord[2] = { 0 };
};
_FORCE_INLINE_ int32_t &operator[](int p_idx) {
DEV_ASSERT((unsigned int)p_idx < 2);
return coord[p_idx];
_FORCE_INLINE_ int32_t &operator[](int p_axis) {
DEV_ASSERT((unsigned int)p_axis < 2);
return coord[p_axis];
}
_FORCE_INLINE_ const int32_t &operator[](int p_idx) const {
DEV_ASSERT((unsigned int)p_idx < 2);
return coord[p_idx];
_FORCE_INLINE_ const int32_t &operator[](int p_axis) const {
DEV_ASSERT((unsigned int)p_axis < 2);
return coord[p_axis];
}
_FORCE_INLINE_ Vector2i::Axis min_axis_index() const {
@@ -95,22 +94,30 @@ struct [[nodiscard]] Vector2i {
return Vector2i(MAX(x, p_scalar), MAX(y, p_scalar));
}
double distance_to(const Vector2i &p_to) const {
return (p_to - *this).length();
}
int64_t distance_squared_to(const Vector2i &p_to) const {
return (p_to - *this).length_squared();
}
Vector2i operator+(const Vector2i &p_v) const;
void operator+=(const Vector2i &p_v);
Vector2i operator-(const Vector2i &p_v) const;
void operator-=(const Vector2i &p_v);
Vector2i operator*(const Vector2i &p_v1) const;
Vector2i operator*(const int32_t &rvalue) const;
void operator*=(const int32_t &rvalue);
Vector2i operator*(int32_t p_rvalue) const;
void operator*=(int32_t p_rvalue);
Vector2i operator/(const Vector2i &p_v1) const;
Vector2i operator/(const int32_t &rvalue) const;
void operator/=(const int32_t &rvalue);
Vector2i operator/(int32_t p_rvalue) const;
void operator/=(int32_t p_rvalue);
Vector2i operator%(const Vector2i &p_v1) const;
Vector2i operator%(const int32_t &rvalue) const;
void operator%=(const int32_t &rvalue);
Vector2i operator%(int32_t p_rvalue) const;
void operator%=(int32_t p_rvalue);
Vector2i operator-() const;
bool operator<(const Vector2i &p_vec2) const { return (x == p_vec2.x) ? (y < p_vec2.y) : (x < p_vec2.x); }
@@ -125,22 +132,19 @@ struct [[nodiscard]] Vector2i {
int64_t length_squared() const;
double length() const;
int64_t distance_squared_to(const Vector2i &p_to) const;
double distance_to(const Vector2i &p_to) const;
real_t aspect() const { return width / (real_t)height; }
Vector2i sign() const { return Vector2i(SIGN(x), SIGN(y)); }
Vector2i abs() const { return Vector2i(Math::abs(x), Math::abs(y)); }
Vector2i snapped(const Vector2i &p_step) const;
Vector2i snappedi(int32_t p_step) const;
Vector2i clamp(const Vector2i &p_min, const Vector2i &p_max) const;
Vector2i clampi(int32_t p_min, int32_t p_max) const;
Vector2i snapped(const Vector2i &p_step) const;
Vector2i snappedi(int32_t p_step) const;
operator String() const;
operator Vector2() const;
inline Vector2i() {}
inline Vector2i(const int32_t p_x, const int32_t p_y) {
inline Vector2i(int32_t p_x, int32_t p_y) {
x = p_x;
y = p_y;
}
@@ -148,19 +152,19 @@ struct [[nodiscard]] Vector2i {
// Multiplication operators required to workaround issues with LLVM using implicit conversion.
_FORCE_INLINE_ Vector2i operator*(const int32_t p_scalar, const Vector2i &p_vector) {
_FORCE_INLINE_ Vector2i operator*(int32_t p_scalar, const Vector2i &p_vector) {
return p_vector * p_scalar;
}
_FORCE_INLINE_ Vector2i operator*(const int64_t p_scalar, const Vector2i &p_vector) {
_FORCE_INLINE_ Vector2i operator*(int64_t p_scalar, const Vector2i &p_vector) {
return p_vector * p_scalar;
}
_FORCE_INLINE_ Vector2i operator*(const float p_scalar, const Vector2i &p_vector) {
_FORCE_INLINE_ Vector2i operator*(float p_scalar, const Vector2i &p_vector) {
return p_vector * p_scalar;
}
_FORCE_INLINE_ Vector2i operator*(const double p_scalar, const Vector2i &p_vector) {
_FORCE_INLINE_ Vector2i operator*(double p_scalar, const Vector2i &p_vector) {
return p_vector * p_scalar;
}
@@ -168,5 +172,3 @@ typedef Vector2i Size2i;
typedef Vector2i Point2i;
} // namespace godot
#endif // GODOT_VECTOR2I_HPP

View File

@@ -28,15 +28,14 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef GODOT_VECTOR3_HPP
#define GODOT_VECTOR3_HPP
#pragma once
#include <godot_cpp/core/error_macros.hpp>
#include <godot_cpp/core/math.hpp>
#include <godot_cpp/variant/string.hpp>
namespace godot {
class String;
struct Basis;
struct Vector2;
struct Vector3i;
@@ -60,12 +59,12 @@ struct [[nodiscard]] Vector3 {
real_t coord[3] = { 0 };
};
_FORCE_INLINE_ const real_t &operator[](const int p_axis) const {
_FORCE_INLINE_ const real_t &operator[](int p_axis) const {
DEV_ASSERT((unsigned int)p_axis < 3);
return coord[p_axis];
}
_FORCE_INLINE_ real_t &operator[](const int p_axis) {
_FORCE_INLINE_ real_t &operator[](int p_axis) {
DEV_ASSERT((unsigned int)p_axis < 3);
return coord[p_axis];
}
@@ -101,36 +100,38 @@ struct [[nodiscard]] Vector3 {
_FORCE_INLINE_ Vector3 normalized() const;
_FORCE_INLINE_ bool is_normalized() const;
_FORCE_INLINE_ Vector3 inverse() const;
Vector3 limit_length(const real_t p_len = 1.0) const;
Vector3 limit_length(real_t p_len = 1.0) const;
_FORCE_INLINE_ void zero();
void snap(const Vector3 p_val);
void snapf(real_t p_val);
Vector3 snapped(const Vector3 p_val) const;
Vector3 snappedf(real_t p_val) const;
void snap(const Vector3 &p_step);
void snapf(real_t p_step);
Vector3 snapped(const Vector3 &p_step) const;
Vector3 snappedf(real_t p_step) const;
void rotate(const Vector3 &p_axis, const real_t p_angle);
Vector3 rotated(const Vector3 &p_axis, const real_t p_angle) const;
void rotate(const Vector3 &p_axis, real_t p_angle);
Vector3 rotated(const Vector3 &p_axis, real_t p_angle) const;
/* Static Methods between 2 vector3s */
_FORCE_INLINE_ Vector3 lerp(const Vector3 &p_to, const real_t p_weight) const;
_FORCE_INLINE_ Vector3 slerp(const Vector3 &p_to, const real_t p_weight) const;
_FORCE_INLINE_ Vector3 cubic_interpolate(const Vector3 &p_b, const Vector3 &p_pre_a, const Vector3 &p_post_b, const real_t p_weight) const;
_FORCE_INLINE_ Vector3 cubic_interpolate_in_time(const Vector3 &p_b, const Vector3 &p_pre_a, const Vector3 &p_post_b, const real_t p_weight, const real_t &p_b_t, const real_t &p_pre_a_t, const real_t &p_post_b_t) const;
_FORCE_INLINE_ Vector3 bezier_interpolate(const Vector3 &p_control_1, const Vector3 &p_control_2, const Vector3 &p_end, const real_t p_t) const;
_FORCE_INLINE_ Vector3 lerp(const Vector3 &p_to, real_t p_weight) const;
_FORCE_INLINE_ Vector3 slerp(const Vector3 &p_to, real_t p_weight) const;
_FORCE_INLINE_ Vector3 cubic_interpolate(const Vector3 &p_b, const Vector3 &p_pre_a, const Vector3 &p_post_b, real_t p_weight) const;
_FORCE_INLINE_ Vector3 cubic_interpolate_in_time(const Vector3 &p_b, const Vector3 &p_pre_a, const Vector3 &p_post_b, real_t p_weight, real_t p_b_t, real_t p_pre_a_t, real_t p_post_b_t) const;
_FORCE_INLINE_ Vector3 bezier_interpolate(const Vector3 &p_control_1, const Vector3 &p_control_2, const Vector3 &p_end, real_t p_t) const;
_FORCE_INLINE_ Vector3 bezier_derivative(const Vector3 &p_control_1, const Vector3 &p_control_2, const Vector3 &p_end, real_t p_t) const;
Vector3 move_toward(const Vector3 &p_to, const real_t p_delta) const;
Vector3 move_toward(const Vector3 &p_to, real_t p_delta) const;
Vector2 octahedron_encode() const;
static Vector3 octahedron_decode(const Vector2 &p_oct);
Vector2 octahedron_tangent_encode(const float sign) const;
static Vector3 octahedron_tangent_decode(const Vector2 &p_oct, float *sign);
Vector2 octahedron_tangent_encode(float p_sign) const;
static Vector3 octahedron_tangent_decode(const Vector2 &p_oct, float *r_sign);
_FORCE_INLINE_ Vector3 cross(const Vector3 &p_with) const;
_FORCE_INLINE_ real_t dot(const Vector3 &p_with) const;
Basis outer(const Vector3 &p_with) const;
_FORCE_INLINE_ Vector3 get_any_perpendicular() const;
_FORCE_INLINE_ Vector3 abs() const;
_FORCE_INLINE_ Vector3 floor() const;
@@ -143,7 +144,7 @@ struct [[nodiscard]] Vector3 {
_FORCE_INLINE_ real_t distance_to(const Vector3 &p_to) const;
_FORCE_INLINE_ real_t distance_squared_to(const Vector3 &p_to) const;
_FORCE_INLINE_ Vector3 posmod(const real_t p_mod) const;
_FORCE_INLINE_ Vector3 posmod(real_t p_mod) const;
_FORCE_INLINE_ Vector3 posmodv(const Vector3 &p_modv) const;
_FORCE_INLINE_ Vector3 project(const Vector3 &p_to) const;
@@ -170,10 +171,10 @@ struct [[nodiscard]] Vector3 {
_FORCE_INLINE_ Vector3 &operator/=(const Vector3 &p_v);
_FORCE_INLINE_ Vector3 operator/(const Vector3 &p_v) const;
_FORCE_INLINE_ Vector3 &operator*=(const real_t p_scalar);
_FORCE_INLINE_ Vector3 operator*(const real_t p_scalar) const;
_FORCE_INLINE_ Vector3 &operator/=(const real_t p_scalar);
_FORCE_INLINE_ Vector3 operator/(const real_t p_scalar) const;
_FORCE_INLINE_ Vector3 &operator*=(real_t p_scalar);
_FORCE_INLINE_ Vector3 operator*(real_t p_scalar) const;
_FORCE_INLINE_ Vector3 &operator/=(real_t p_scalar);
_FORCE_INLINE_ Vector3 operator/(real_t p_scalar) const;
_FORCE_INLINE_ Vector3 operator-() const;
@@ -188,7 +189,7 @@ struct [[nodiscard]] Vector3 {
operator Vector3i() const;
_FORCE_INLINE_ Vector3() {}
_FORCE_INLINE_ Vector3(const real_t p_x, const real_t p_y, const real_t p_z) {
_FORCE_INLINE_ Vector3(real_t p_x, real_t p_y, real_t p_z) {
x = p_x;
y = p_y;
z = p_z;
@@ -228,14 +229,15 @@ Vector3 Vector3::round() const {
return Vector3(Math::round(x), Math::round(y), Math::round(z));
}
Vector3 Vector3::lerp(const Vector3 &p_to, const real_t p_weight) const {
return Vector3(
x + (p_weight * (p_to.x - x)),
y + (p_weight * (p_to.y - y)),
z + (p_weight * (p_to.z - z)));
Vector3 Vector3::lerp(const Vector3 &p_to, real_t p_weight) const {
Vector3 res = *this;
res.x = Math::lerp(res.x, p_to.x, p_weight);
res.y = Math::lerp(res.y, p_to.y, p_weight);
res.z = Math::lerp(res.z, p_to.z, p_weight);
return res;
}
Vector3 Vector3::slerp(const Vector3 &p_to, const real_t p_weight) const {
Vector3 Vector3::slerp(const Vector3 &p_to, real_t p_weight) const {
// This method seems more complicated than it really is, since we write out
// the internals of some methods for efficiency (mainly, checking length).
real_t start_length_sq = length_squared();
@@ -257,7 +259,7 @@ Vector3 Vector3::slerp(const Vector3 &p_to, const real_t p_weight) const {
return rotated(axis, angle * p_weight) * (result_length / start_length);
}
Vector3 Vector3::cubic_interpolate(const Vector3 &p_b, const Vector3 &p_pre_a, const Vector3 &p_post_b, const real_t p_weight) const {
Vector3 Vector3::cubic_interpolate(const Vector3 &p_b, const Vector3 &p_pre_a, const Vector3 &p_post_b, real_t p_weight) const {
Vector3 res = *this;
res.x = Math::cubic_interpolate(res.x, p_b.x, p_pre_a.x, p_post_b.x, p_weight);
res.y = Math::cubic_interpolate(res.y, p_b.y, p_pre_a.y, p_post_b.y, p_weight);
@@ -265,7 +267,7 @@ Vector3 Vector3::cubic_interpolate(const Vector3 &p_b, const Vector3 &p_pre_a, c
return res;
}
Vector3 Vector3::cubic_interpolate_in_time(const Vector3 &p_b, const Vector3 &p_pre_a, const Vector3 &p_post_b, const real_t p_weight, const real_t &p_b_t, const real_t &p_pre_a_t, const real_t &p_post_b_t) const {
Vector3 Vector3::cubic_interpolate_in_time(const Vector3 &p_b, const Vector3 &p_pre_a, const Vector3 &p_post_b, real_t p_weight, real_t p_b_t, real_t p_pre_a_t, real_t p_post_b_t) const {
Vector3 res = *this;
res.x = Math::cubic_interpolate_in_time(res.x, p_b.x, p_pre_a.x, p_post_b.x, p_weight, p_b_t, p_pre_a_t, p_post_b_t);
res.y = Math::cubic_interpolate_in_time(res.y, p_b.y, p_pre_a.y, p_post_b.y, p_weight, p_b_t, p_pre_a_t, p_post_b_t);
@@ -273,17 +275,20 @@ Vector3 Vector3::cubic_interpolate_in_time(const Vector3 &p_b, const Vector3 &p_
return res;
}
Vector3 Vector3::bezier_interpolate(const Vector3 &p_control_1, const Vector3 &p_control_2, const Vector3 &p_end, const real_t p_t) const {
Vector3 Vector3::bezier_interpolate(const Vector3 &p_control_1, const Vector3 &p_control_2, const Vector3 &p_end, real_t p_t) const {
Vector3 res = *this;
res.x = Math::bezier_interpolate(res.x, p_control_1.x, p_control_2.x, p_end.x, p_t);
res.y = Math::bezier_interpolate(res.y, p_control_1.y, p_control_2.y, p_end.y, p_t);
res.z = Math::bezier_interpolate(res.z, p_control_1.z, p_control_2.z, p_end.z, p_t);
return res;
}
/* Formula from Wikipedia article on Bezier curves. */
real_t omt = (1.0 - p_t);
real_t omt2 = omt * omt;
real_t omt3 = omt2 * omt;
real_t t2 = p_t * p_t;
real_t t3 = t2 * p_t;
return res * omt3 + p_control_1 * omt2 * p_t * 3.0 + p_control_2 * omt * t2 * 3.0 + p_end * t3;
Vector3 Vector3::bezier_derivative(const Vector3 &p_control_1, const Vector3 &p_control_2, const Vector3 &p_end, real_t p_t) const {
Vector3 res = *this;
res.x = Math::bezier_derivative(res.x, p_control_1.x, p_control_2.x, p_end.x, p_t);
res.y = Math::bezier_derivative(res.y, p_control_1.y, p_control_2.y, p_end.y, p_t);
res.z = Math::bezier_derivative(res.z, p_control_1.z, p_control_2.z, p_end.z, p_t);
return res;
}
real_t Vector3::distance_to(const Vector3 &p_to) const {
@@ -294,7 +299,7 @@ real_t Vector3::distance_squared_to(const Vector3 &p_to) const {
return (p_to - *this).length_squared();
}
Vector3 Vector3::posmod(const real_t p_mod) const {
Vector3 Vector3::posmod(real_t p_mod) const {
return Vector3(Math::fposmod(x, p_mod), Math::fposmod(y, p_mod), Math::fposmod(z, p_mod));
}
@@ -323,6 +328,16 @@ Vector3 Vector3::direction_to(const Vector3 &p_to) const {
return ret;
}
Vector3 Vector3::get_any_perpendicular() const {
// Return the any perpendicular vector by cross product with the Vector3.RIGHT or Vector3.UP,
// whichever has the greater angle to the current vector with the sign of each element positive.
// The only essence is "to avoid being parallel to the current vector", and there is no mathematical basis for using Vector3.RIGHT and Vector3.UP,
// since it could be a different vector depending on the prior branching code Math::abs(x) <= Math::abs(y) && Math::abs(x) <= Math::abs(z).
// However, it would be reasonable to use any of the axes of the basis, as it is simpler to calculate.
ERR_FAIL_COND_V_MSG(is_zero_approx(), Vector3(0, 0, 0), "The Vector3 must not be zero.");
return cross((Math::abs(x) <= Math::abs(y) && Math::abs(x) <= Math::abs(z)) ? Vector3(1, 0, 0) : Vector3(0, 1, 0)).normalized();
}
/* Operators */
Vector3 &Vector3::operator+=(const Vector3 &p_v) {
@@ -369,7 +384,7 @@ Vector3 Vector3::operator/(const Vector3 &p_v) const {
return Vector3(x / p_v.x, y / p_v.y, z / p_v.z);
}
Vector3 &Vector3::operator*=(const real_t p_scalar) {
Vector3 &Vector3::operator*=(real_t p_scalar) {
x *= p_scalar;
y *= p_scalar;
z *= p_scalar;
@@ -379,34 +394,34 @@ Vector3 &Vector3::operator*=(const real_t p_scalar) {
// Multiplication operators required to workaround issues with LLVM using implicit conversion
// to Vector3i instead for integers where it should not.
_FORCE_INLINE_ Vector3 operator*(const float p_scalar, const Vector3 &p_vec) {
_FORCE_INLINE_ Vector3 operator*(float p_scalar, const Vector3 &p_vec) {
return p_vec * p_scalar;
}
_FORCE_INLINE_ Vector3 operator*(const double p_scalar, const Vector3 &p_vec) {
_FORCE_INLINE_ Vector3 operator*(double p_scalar, const Vector3 &p_vec) {
return p_vec * p_scalar;
}
_FORCE_INLINE_ Vector3 operator*(const int32_t p_scalar, const Vector3 &p_vec) {
_FORCE_INLINE_ Vector3 operator*(int32_t p_scalar, const Vector3 &p_vec) {
return p_vec * p_scalar;
}
_FORCE_INLINE_ Vector3 operator*(const int64_t p_scalar, const Vector3 &p_vec) {
_FORCE_INLINE_ Vector3 operator*(int64_t p_scalar, const Vector3 &p_vec) {
return p_vec * p_scalar;
}
Vector3 Vector3::operator*(const real_t p_scalar) const {
Vector3 Vector3::operator*(real_t p_scalar) const {
return Vector3(x * p_scalar, y * p_scalar, z * p_scalar);
}
Vector3 &Vector3::operator/=(const real_t p_scalar) {
Vector3 &Vector3::operator/=(real_t p_scalar) {
x /= p_scalar;
y /= p_scalar;
z /= p_scalar;
return *this;
}
Vector3 Vector3::operator/(const real_t p_scalar) const {
Vector3 Vector3::operator/(real_t p_scalar) const {
return Vector3(x / p_scalar, y / p_scalar, z / p_scalar);
}
@@ -520,9 +535,9 @@ void Vector3::zero() {
// slide returns the component of the vector along the given plane, specified by its normal vector.
Vector3 Vector3::slide(const Vector3 &p_normal) const {
#ifdef MATH_CHECKS
ERR_FAIL_COND_V_MSG(!p_normal.is_normalized(), Vector3(), "The normal Vector3 must be normalized.");
ERR_FAIL_COND_V_MSG(!p_normal.is_normalized(), Vector3(), "The normal Vector3 " + p_normal.operator String() + " must be normalized.");
#endif
return *this - p_normal * this->dot(p_normal);
return *this - p_normal * dot(p_normal);
}
Vector3 Vector3::bounce(const Vector3 &p_normal) const {
@@ -531,11 +546,9 @@ Vector3 Vector3::bounce(const Vector3 &p_normal) const {
Vector3 Vector3::reflect(const Vector3 &p_normal) const {
#ifdef MATH_CHECKS
ERR_FAIL_COND_V_MSG(!p_normal.is_normalized(), Vector3(), "The normal Vector3 must be normalized.");
ERR_FAIL_COND_V_MSG(!p_normal.is_normalized(), Vector3(), "The normal Vector3 " + p_normal.operator String() + " must be normalized.");
#endif
return 2.0f * p_normal * this->dot(p_normal) - *this;
return 2.0f * p_normal * dot(p_normal) - *this;
}
} // namespace godot
#endif // GODOT_VECTOR3_HPP

View File

@@ -28,8 +28,7 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef GODOT_VECTOR3I_HPP
#define GODOT_VECTOR3I_HPP
#pragma once
#include <godot_cpp/core/error_macros.hpp>
#include <godot_cpp/core/math.hpp>
@@ -58,12 +57,12 @@ struct [[nodiscard]] Vector3i {
int32_t coord[3] = { 0 };
};
_FORCE_INLINE_ const int32_t &operator[](const int p_axis) const {
_FORCE_INLINE_ const int32_t &operator[](int p_axis) const {
DEV_ASSERT((unsigned int)p_axis < 3);
return coord[p_axis];
}
_FORCE_INLINE_ int32_t &operator[](const int p_axis) {
_FORCE_INLINE_ int32_t &operator[](int p_axis) {
DEV_ASSERT((unsigned int)p_axis < 3);
return coord[p_axis];
}
@@ -90,17 +89,17 @@ struct [[nodiscard]] Vector3i {
_FORCE_INLINE_ int64_t length_squared() const;
_FORCE_INLINE_ double length() const;
_FORCE_INLINE_ int64_t distance_squared_to(const Vector3i &p_to) const;
_FORCE_INLINE_ double distance_to(const Vector3i &p_to) const;
_FORCE_INLINE_ void zero();
_FORCE_INLINE_ Vector3i abs() const;
_FORCE_INLINE_ Vector3i sign() const;
Vector3i snapped(const Vector3i &p_step) const;
Vector3i snappedi(int32_t p_step) const;
Vector3i clamp(const Vector3i &p_min, const Vector3i &p_max) const;
Vector3i clampi(int32_t p_min, int32_t p_max) const;
Vector3i snapped(const Vector3i &p_step) const;
Vector3i snappedi(int32_t p_step) const;
_FORCE_INLINE_ double distance_to(const Vector3i &p_to) const;
_FORCE_INLINE_ int64_t distance_squared_to(const Vector3i &p_to) const;
/* Operators */
@@ -115,12 +114,12 @@ struct [[nodiscard]] Vector3i {
_FORCE_INLINE_ Vector3i &operator%=(const Vector3i &p_v);
_FORCE_INLINE_ Vector3i operator%(const Vector3i &p_v) const;
_FORCE_INLINE_ Vector3i &operator*=(const int32_t p_scalar);
_FORCE_INLINE_ Vector3i operator*(const int32_t p_scalar) const;
_FORCE_INLINE_ Vector3i &operator/=(const int32_t p_scalar);
_FORCE_INLINE_ Vector3i operator/(const int32_t p_scalar) const;
_FORCE_INLINE_ Vector3i &operator%=(const int32_t p_scalar);
_FORCE_INLINE_ Vector3i operator%(const int32_t p_scalar) const;
_FORCE_INLINE_ Vector3i &operator*=(int32_t p_scalar);
_FORCE_INLINE_ Vector3i operator*(int32_t p_scalar) const;
_FORCE_INLINE_ Vector3i &operator/=(int32_t p_scalar);
_FORCE_INLINE_ Vector3i operator/(int32_t p_scalar) const;
_FORCE_INLINE_ Vector3i &operator%=(int32_t p_scalar);
_FORCE_INLINE_ Vector3i operator%(int32_t p_scalar) const;
_FORCE_INLINE_ Vector3i operator-() const;
@@ -135,7 +134,7 @@ struct [[nodiscard]] Vector3i {
operator Vector3() const;
_FORCE_INLINE_ Vector3i() {}
_FORCE_INLINE_ Vector3i(const int32_t p_x, const int32_t p_y, const int32_t p_z) {
_FORCE_INLINE_ Vector3i(int32_t p_x, int32_t p_y, int32_t p_z) {
x = p_x;
y = p_y;
z = p_z;
@@ -150,14 +149,6 @@ double Vector3i::length() const {
return Math::sqrt((double)length_squared());
}
int64_t Vector3i::distance_squared_to(const Vector3i &p_to) const {
return (p_to - *this).length_squared();
}
double Vector3i::distance_to(const Vector3i &p_to) const {
return (p_to - *this).length();
}
Vector3i Vector3i::abs() const {
return Vector3i(Math::abs(x), Math::abs(y), Math::abs(z));
}
@@ -166,6 +157,14 @@ Vector3i Vector3i::sign() const {
return Vector3i(SIGN(x), SIGN(y), SIGN(z));
}
double Vector3i::distance_to(const Vector3i &p_to) const {
return (p_to - *this).length();
}
int64_t Vector3i::distance_squared_to(const Vector3i &p_to) const {
return (p_to - *this).length_squared();
}
/* Operators */
Vector3i &Vector3i::operator+=(const Vector3i &p_v) {
@@ -223,54 +222,54 @@ Vector3i Vector3i::operator%(const Vector3i &p_v) const {
return Vector3i(x % p_v.x, y % p_v.y, z % p_v.z);
}
Vector3i &Vector3i::operator*=(const int32_t p_scalar) {
Vector3i &Vector3i::operator*=(int32_t p_scalar) {
x *= p_scalar;
y *= p_scalar;
z *= p_scalar;
return *this;
}
Vector3i Vector3i::operator*(const int32_t p_scalar) const {
Vector3i Vector3i::operator*(int32_t p_scalar) const {
return Vector3i(x * p_scalar, y * p_scalar, z * p_scalar);
}
// Multiplication operators required to workaround issues with LLVM using implicit conversion.
_FORCE_INLINE_ Vector3i operator*(const int32_t p_scalar, const Vector3i &p_vector) {
_FORCE_INLINE_ Vector3i operator*(int32_t p_scalar, const Vector3i &p_vector) {
return p_vector * p_scalar;
}
_FORCE_INLINE_ Vector3i operator*(const int64_t p_scalar, const Vector3i &p_vector) {
_FORCE_INLINE_ Vector3i operator*(int64_t p_scalar, const Vector3i &p_vector) {
return p_vector * p_scalar;
}
_FORCE_INLINE_ Vector3i operator*(const float p_scalar, const Vector3i &p_vector) {
_FORCE_INLINE_ Vector3i operator*(float p_scalar, const Vector3i &p_vector) {
return p_vector * p_scalar;
}
_FORCE_INLINE_ Vector3i operator*(const double p_scalar, const Vector3i &p_vector) {
_FORCE_INLINE_ Vector3i operator*(double p_scalar, const Vector3i &p_vector) {
return p_vector * p_scalar;
}
Vector3i &Vector3i::operator/=(const int32_t p_scalar) {
Vector3i &Vector3i::operator/=(int32_t p_scalar) {
x /= p_scalar;
y /= p_scalar;
z /= p_scalar;
return *this;
}
Vector3i Vector3i::operator/(const int32_t p_scalar) const {
Vector3i Vector3i::operator/(int32_t p_scalar) const {
return Vector3i(x / p_scalar, y / p_scalar, z / p_scalar);
}
Vector3i &Vector3i::operator%=(const int32_t p_scalar) {
Vector3i &Vector3i::operator%=(int32_t p_scalar) {
x %= p_scalar;
y %= p_scalar;
z %= p_scalar;
return *this;
}
Vector3i Vector3i::operator%(const int32_t p_scalar) const {
Vector3i Vector3i::operator%(int32_t p_scalar) const {
return Vector3i(x % p_scalar, y % p_scalar, z % p_scalar);
}
@@ -339,5 +338,3 @@ void Vector3i::zero() {
}
} // namespace godot
#endif // GODOT_VECTOR3I_HPP

View File

@@ -28,8 +28,7 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef GODOT_VECTOR4_HPP
#define GODOT_VECTOR4_HPP
#pragma once
#include <godot_cpp/core/error_macros.hpp>
#include <godot_cpp/core/math.hpp>
@@ -37,6 +36,7 @@
namespace godot {
class String;
struct Vector4i;
struct [[nodiscard]] Vector4 {
static const int AXIS_COUNT = 4;
@@ -55,15 +55,14 @@ struct [[nodiscard]] Vector4 {
real_t z;
real_t w;
};
[[deprecated("Use coord instead")]] real_t components[4];
real_t coord[4] = { 0, 0, 0, 0 };
};
_FORCE_INLINE_ real_t &operator[](const int p_axis) {
_FORCE_INLINE_ real_t &operator[](int p_axis) {
DEV_ASSERT((unsigned int)p_axis < 4);
return coord[p_axis];
}
_FORCE_INLINE_ const real_t &operator[](const int p_axis) const {
_FORCE_INLINE_ const real_t &operator[](int p_axis) const {
DEV_ASSERT((unsigned int)p_axis < 4);
return coord[p_axis];
}
@@ -105,11 +104,11 @@ struct [[nodiscard]] Vector4 {
Vector4 floor() const;
Vector4 ceil() const;
Vector4 round() const;
Vector4 lerp(const Vector4 &p_to, const real_t p_weight) const;
Vector4 cubic_interpolate(const Vector4 &p_b, const Vector4 &p_pre_a, const Vector4 &p_post_b, const real_t p_weight) const;
Vector4 cubic_interpolate_in_time(const Vector4 &p_b, const Vector4 &p_pre_a, const Vector4 &p_post_b, const real_t p_weight, const real_t &p_b_t, const real_t &p_pre_a_t, const real_t &p_post_b_t) const;
Vector4 lerp(const Vector4 &p_to, real_t p_weight) const;
Vector4 cubic_interpolate(const Vector4 &p_b, const Vector4 &p_pre_a, const Vector4 &p_post_b, real_t p_weight) const;
Vector4 cubic_interpolate_in_time(const Vector4 &p_b, const Vector4 &p_pre_a, const Vector4 &p_post_b, real_t p_weight, real_t p_b_t, real_t p_pre_a_t, real_t p_post_b_t) const;
Vector4 posmod(const real_t p_mod) const;
Vector4 posmod(real_t p_mod) const;
Vector4 posmodv(const Vector4 &p_modv) const;
void snap(const Vector4 &p_step);
void snapf(real_t p_step);
@@ -125,15 +124,15 @@ struct [[nodiscard]] Vector4 {
_FORCE_INLINE_ void operator-=(const Vector4 &p_vec4);
_FORCE_INLINE_ void operator*=(const Vector4 &p_vec4);
_FORCE_INLINE_ void operator/=(const Vector4 &p_vec4);
_FORCE_INLINE_ void operator*=(const real_t &s);
_FORCE_INLINE_ void operator/=(const real_t &s);
_FORCE_INLINE_ void operator*=(real_t p_s);
_FORCE_INLINE_ void operator/=(real_t p_s);
_FORCE_INLINE_ Vector4 operator+(const Vector4 &p_vec4) const;
_FORCE_INLINE_ Vector4 operator-(const Vector4 &p_vec4) const;
_FORCE_INLINE_ Vector4 operator*(const Vector4 &p_vec4) const;
_FORCE_INLINE_ Vector4 operator/(const Vector4 &p_vec4) const;
_FORCE_INLINE_ Vector4 operator-() const;
_FORCE_INLINE_ Vector4 operator*(const real_t &s) const;
_FORCE_INLINE_ Vector4 operator/(const real_t &s) const;
_FORCE_INLINE_ Vector4 operator*(real_t p_s) const;
_FORCE_INLINE_ Vector4 operator/(real_t p_s) const;
_FORCE_INLINE_ bool operator==(const Vector4 &p_vec4) const;
_FORCE_INLINE_ bool operator!=(const Vector4 &p_vec4) const;
@@ -143,28 +142,14 @@ struct [[nodiscard]] Vector4 {
_FORCE_INLINE_ bool operator<=(const Vector4 &p_vec4) const;
operator String() const;
operator Vector4i() const;
_FORCE_INLINE_ Vector4() {}
_FORCE_INLINE_ Vector4(real_t p_x, real_t p_y, real_t p_z, real_t p_w) :
x(p_x),
y(p_y),
z(p_z),
w(p_w) {
}
Vector4(const Vector4 &p_vec4) :
x(p_vec4.x),
y(p_vec4.y),
z(p_vec4.z),
w(p_vec4.w) {
}
void operator=(const Vector4 &p_vec4) {
x = p_vec4.x;
y = p_vec4.y;
z = p_vec4.z;
w = p_vec4.w;
_FORCE_INLINE_ Vector4(real_t p_x, real_t p_y, real_t p_z, real_t p_w) {
x = p_x;
y = p_y;
z = p_z;
w = p_w;
}
};
@@ -203,15 +188,15 @@ void Vector4::operator/=(const Vector4 &p_vec4) {
z /= p_vec4.z;
w /= p_vec4.w;
}
void Vector4::operator*=(const real_t &s) {
x *= s;
y *= s;
z *= s;
w *= s;
void Vector4::operator*=(real_t p_s) {
x *= p_s;
y *= p_s;
z *= p_s;
w *= p_s;
}
void Vector4::operator/=(const real_t &s) {
*this *= 1.0f / s;
void Vector4::operator/=(real_t p_s) {
*this *= 1.0f / p_s;
}
Vector4 Vector4::operator+(const Vector4 &p_vec4) const {
@@ -234,12 +219,12 @@ Vector4 Vector4::operator-() const {
return Vector4(-x, -y, -z, -w);
}
Vector4 Vector4::operator*(const real_t &s) const {
return Vector4(x * s, y * s, z * s, w * s);
Vector4 Vector4::operator*(real_t p_s) const {
return Vector4(x * p_s, y * p_s, z * p_s, w * p_s);
}
Vector4 Vector4::operator/(const real_t &s) const {
return *this * (1.0f / s);
Vector4 Vector4::operator/(real_t p_s) const {
return *this * (1.0f / p_s);
}
bool Vector4::operator==(const Vector4 &p_vec4) const {
@@ -302,22 +287,20 @@ bool Vector4::operator>=(const Vector4 &p_v) const {
return x > p_v.x;
}
_FORCE_INLINE_ Vector4 operator*(const float p_scalar, const Vector4 &p_vec) {
_FORCE_INLINE_ Vector4 operator*(float p_scalar, const Vector4 &p_vec) {
return p_vec * p_scalar;
}
_FORCE_INLINE_ Vector4 operator*(const double p_scalar, const Vector4 &p_vec) {
_FORCE_INLINE_ Vector4 operator*(double p_scalar, const Vector4 &p_vec) {
return p_vec * p_scalar;
}
_FORCE_INLINE_ Vector4 operator*(const int32_t p_scalar, const Vector4 &p_vec) {
_FORCE_INLINE_ Vector4 operator*(int32_t p_scalar, const Vector4 &p_vec) {
return p_vec * p_scalar;
}
_FORCE_INLINE_ Vector4 operator*(const int64_t p_scalar, const Vector4 &p_vec) {
_FORCE_INLINE_ Vector4 operator*(int64_t p_scalar, const Vector4 &p_vec) {
return p_vec * p_scalar;
}
} // namespace godot
#endif // GODOT_VECTOR4_HPP

View File

@@ -28,8 +28,7 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef GODOT_VECTOR4I_HPP
#define GODOT_VECTOR4I_HPP
#pragma once
#include <godot_cpp/core/error_macros.hpp>
#include <godot_cpp/core/math.hpp>
@@ -60,12 +59,12 @@ struct [[nodiscard]] Vector4i {
int32_t coord[4] = { 0 };
};
_FORCE_INLINE_ const int32_t &operator[](const int p_axis) const {
_FORCE_INLINE_ const int32_t &operator[](int p_axis) const {
DEV_ASSERT((unsigned int)p_axis < 4);
return coord[p_axis];
}
_FORCE_INLINE_ int32_t &operator[](const int p_axis) {
_FORCE_INLINE_ int32_t &operator[](int p_axis) {
DEV_ASSERT((unsigned int)p_axis < 4);
return coord[p_axis];
}
@@ -92,17 +91,17 @@ struct [[nodiscard]] Vector4i {
_FORCE_INLINE_ int64_t length_squared() const;
_FORCE_INLINE_ double length() const;
_FORCE_INLINE_ int64_t distance_squared_to(const Vector4i &p_to) const;
_FORCE_INLINE_ double distance_to(const Vector4i &p_to) const;
_FORCE_INLINE_ void zero();
_FORCE_INLINE_ double distance_to(const Vector4i &p_to) const;
_FORCE_INLINE_ int64_t distance_squared_to(const Vector4i &p_to) const;
_FORCE_INLINE_ Vector4i abs() const;
_FORCE_INLINE_ Vector4i sign() const;
Vector4i snapped(const Vector4i &p_step) const;
Vector4i snappedi(int32_t p_step) const;
Vector4i clamp(const Vector4i &p_min, const Vector4i &p_max) const;
Vector4i clampi(int32_t p_min, int32_t p_max) const;
Vector4i snapped(const Vector4i &p_step) const;
Vector4i snappedi(int32_t p_step) const;
/* Operators */
@@ -117,12 +116,12 @@ struct [[nodiscard]] Vector4i {
_FORCE_INLINE_ Vector4i &operator%=(const Vector4i &p_v);
_FORCE_INLINE_ Vector4i operator%(const Vector4i &p_v) const;
_FORCE_INLINE_ Vector4i &operator*=(const int32_t p_scalar);
_FORCE_INLINE_ Vector4i operator*(const int32_t p_scalar) const;
_FORCE_INLINE_ Vector4i &operator/=(const int32_t p_scalar);
_FORCE_INLINE_ Vector4i operator/(const int32_t p_scalar) const;
_FORCE_INLINE_ Vector4i &operator%=(const int32_t p_scalar);
_FORCE_INLINE_ Vector4i operator%(const int32_t p_scalar) const;
_FORCE_INLINE_ Vector4i &operator*=(int32_t p_scalar);
_FORCE_INLINE_ Vector4i operator*(int32_t p_scalar) const;
_FORCE_INLINE_ Vector4i &operator/=(int32_t p_scalar);
_FORCE_INLINE_ Vector4i operator/(int32_t p_scalar) const;
_FORCE_INLINE_ Vector4i &operator%=(int32_t p_scalar);
_FORCE_INLINE_ Vector4i operator%(int32_t p_scalar) const;
_FORCE_INLINE_ Vector4i operator-() const;
@@ -138,7 +137,7 @@ struct [[nodiscard]] Vector4i {
_FORCE_INLINE_ Vector4i() {}
Vector4i(const Vector4 &p_vec4);
_FORCE_INLINE_ Vector4i(const int32_t p_x, const int32_t p_y, const int32_t p_z, const int32_t p_w) {
_FORCE_INLINE_ Vector4i(int32_t p_x, int32_t p_y, int32_t p_z, int32_t p_w) {
x = p_x;
y = p_y;
z = p_z;
@@ -154,20 +153,20 @@ double Vector4i::length() const {
return Math::sqrt((double)length_squared());
}
int64_t Vector4i::distance_squared_to(const Vector4i &p_to) const {
return (p_to - *this).length_squared();
}
double Vector4i::distance_to(const Vector4i &p_to) const {
return (p_to - *this).length();
}
int64_t Vector4i::distance_squared_to(const Vector4i &p_to) const {
return (p_to - *this).length_squared();
}
Vector4i Vector4i::abs() const {
return Vector4i(Math::abs(x), Math::abs(y), Math::abs(z), Math::abs(w));
}
Vector4i Vector4i::sign() const {
return Vector4i(Math::sign(x), Math::sign(y), Math::sign(z), Math::sign(w));
return Vector4i(SIGN(x), SIGN(y), SIGN(z), SIGN(w));
}
/* Operators */
@@ -232,7 +231,7 @@ Vector4i Vector4i::operator%(const Vector4i &p_v) const {
return Vector4i(x % p_v.x, y % p_v.y, z % p_v.z, w % p_v.w);
}
Vector4i &Vector4i::operator*=(const int32_t p_scalar) {
Vector4i &Vector4i::operator*=(int32_t p_scalar) {
x *= p_scalar;
y *= p_scalar;
z *= p_scalar;
@@ -240,29 +239,29 @@ Vector4i &Vector4i::operator*=(const int32_t p_scalar) {
return *this;
}
Vector4i Vector4i::operator*(const int32_t p_scalar) const {
Vector4i Vector4i::operator*(int32_t p_scalar) const {
return Vector4i(x * p_scalar, y * p_scalar, z * p_scalar, w * p_scalar);
}
// Multiplication operators required to workaround issues with LLVM using implicit conversion.
_FORCE_INLINE_ Vector4i operator*(const int32_t p_scalar, const Vector4i &p_vector) {
_FORCE_INLINE_ Vector4i operator*(int32_t p_scalar, const Vector4i &p_vector) {
return p_vector * p_scalar;
}
_FORCE_INLINE_ Vector4i operator*(const int64_t p_scalar, const Vector4i &p_vector) {
_FORCE_INLINE_ Vector4i operator*(int64_t p_scalar, const Vector4i &p_vector) {
return p_vector * p_scalar;
}
_FORCE_INLINE_ Vector4i operator*(const float p_scalar, const Vector4i &p_vector) {
_FORCE_INLINE_ Vector4i operator*(float p_scalar, const Vector4i &p_vector) {
return p_vector * p_scalar;
}
_FORCE_INLINE_ Vector4i operator*(const double p_scalar, const Vector4i &p_vector) {
_FORCE_INLINE_ Vector4i operator*(double p_scalar, const Vector4i &p_vector) {
return p_vector * p_scalar;
}
Vector4i &Vector4i::operator/=(const int32_t p_scalar) {
Vector4i &Vector4i::operator/=(int32_t p_scalar) {
x /= p_scalar;
y /= p_scalar;
z /= p_scalar;
@@ -270,11 +269,11 @@ Vector4i &Vector4i::operator/=(const int32_t p_scalar) {
return *this;
}
Vector4i Vector4i::operator/(const int32_t p_scalar) const {
Vector4i Vector4i::operator/(int32_t p_scalar) const {
return Vector4i(x / p_scalar, y / p_scalar, z / p_scalar, w / p_scalar);
}
Vector4i &Vector4i::operator%=(const int32_t p_scalar) {
Vector4i &Vector4i::operator%=(int32_t p_scalar) {
x %= p_scalar;
y %= p_scalar;
z %= p_scalar;
@@ -282,7 +281,7 @@ Vector4i &Vector4i::operator%=(const int32_t p_scalar) {
return *this;
}
Vector4i Vector4i::operator%(const int32_t p_scalar) const {
Vector4i Vector4i::operator%(int32_t p_scalar) const {
return Vector4i(x % p_scalar, y % p_scalar, z % p_scalar, w % p_scalar);
}
@@ -367,5 +366,3 @@ void Vector4i::zero() {
}
} // namespace godot
#endif // GODOT_VECTOR4I_HPP

View File

@@ -2,121 +2,82 @@
# -*- coding: utf-8 -*-
import sys
from pathlib import Path
if len(sys.argv) < 2:
print("Invalid usage of header_guards.py, it should be called with a path to one or multiple files.")
sys.exit(1)
HEADER_CHECK_OFFSET = 30
HEADER_BEGIN_OFFSET = 31
HEADER_END_OFFSET = -1
changed = []
invalid = []
for file in sys.argv[1:]:
with open(file, "rt", encoding="utf-8", newline="\n") as f:
header_start = -1
header_end = -1
with open(file.strip(), "rt", encoding="utf-8", newline="\n") as f:
lines = f.readlines()
if len(lines) <= HEADER_BEGIN_OFFSET:
continue # Most likely a dummy file.
for idx, line in enumerate(lines):
sline = line.strip()
if lines[HEADER_CHECK_OFFSET].startswith("#import"):
continue # Early catch obj-c file.
if header_start < 0:
if sline == "": # Skip empty lines at the top.
continue
name = f"GODOT_{Path(file).name}".upper().replace(".", "_").replace("-", "_").replace(" ", "_")
if sline.startswith("/**********"): # Godot header starts this way.
header_start = idx
else:
header_end = 0 # There is no Godot header.
break
else:
if not sline.startswith(("*", "/*")): # Not in the Godot header anymore.
header_end = idx + 1 # The guard should be two lines below the Godot header.
break
HEADER_CHECK = f"#ifndef {name}\n"
HEADER_BEGIN = f"#define {name}\n"
HEADER_END = f"#endif // {name}\n"
if (
lines[HEADER_CHECK_OFFSET] == HEADER_CHECK
and lines[HEADER_BEGIN_OFFSET] == HEADER_BEGIN
and lines[HEADER_END_OFFSET] == HEADER_END
):
if (HEADER_CHECK_OFFSET := header_end) < 0 or HEADER_CHECK_OFFSET >= len(lines):
invalid.append(file)
continue
if lines[HEADER_CHECK_OFFSET].startswith("#pragma once"):
continue
# Might be using legacy header guards.
HEADER_BEGIN_OFFSET = HEADER_CHECK_OFFSET + 1
HEADER_END_OFFSET = len(lines) - 1
if HEADER_BEGIN_OFFSET >= HEADER_END_OFFSET:
invalid.append(file)
continue
# Guards might exist but with the wrong names.
if (
lines[HEADER_CHECK_OFFSET].startswith("#ifndef")
and lines[HEADER_BEGIN_OFFSET].startswith("#define")
and lines[HEADER_END_OFFSET].startswith("#endif")
):
lines[HEADER_CHECK_OFFSET] = HEADER_CHECK
lines[HEADER_BEGIN_OFFSET] = HEADER_BEGIN
lines[HEADER_END_OFFSET] = HEADER_END
lines[HEADER_CHECK_OFFSET] = "#pragma once"
lines[HEADER_BEGIN_OFFSET] = "\n"
lines.pop()
with open(file, "wt", encoding="utf-8", newline="\n") as f:
f.writelines(lines)
changed.append(file)
continue
header_check = -1
header_begin = -1
header_end = -1
pragma_once = -1
objc = False
for idx, line in enumerate(lines):
if not line.startswith("#"):
continue
elif line.startswith("#ifndef") and header_check == -1:
header_check = idx
elif line.startswith("#define") and header_begin == -1:
header_begin = idx
elif line.startswith("#endif") and header_end == -1:
header_end = idx
elif line.startswith("#pragma once"):
pragma_once = idx
break
elif line.startswith("#import"):
objc = True
# Verify `#pragma once` doesn't exist at invalid location.
misplaced = False
for line in lines:
if line.startswith("#pragma once"):
misplaced = True
break
if objc:
if misplaced:
invalid.append(file)
continue
if pragma_once != -1:
lines.pop(pragma_once)
lines.insert(HEADER_CHECK_OFFSET, HEADER_CHECK)
lines.insert(HEADER_BEGIN_OFFSET, HEADER_BEGIN)
lines.append("\n")
lines.append(HEADER_END)
with open(file, "wt", encoding="utf-8", newline="\n") as f:
f.writelines(lines)
changed.append(file)
continue
if header_check == -1 and header_begin == -1 and header_end == -1:
# Guards simply didn't exist
lines.insert(HEADER_CHECK_OFFSET, HEADER_CHECK)
lines.insert(HEADER_BEGIN_OFFSET, HEADER_BEGIN)
lines.append("\n")
lines.append(HEADER_END)
with open(file, "wt", encoding="utf-8", newline="\n") as f:
f.writelines(lines)
changed.append(file)
continue
if header_check != -1 and header_begin != -1 and header_end != -1:
# All prepends "found", see if we can salvage this.
if header_check == header_begin - 1 and header_begin < header_end:
lines.pop(header_check)
lines.pop(header_begin - 1)
lines.pop(header_end - 2)
if lines[header_end - 3] == "\n":
lines.pop(header_end - 3)
lines.insert(HEADER_CHECK_OFFSET, HEADER_CHECK)
lines.insert(HEADER_BEGIN_OFFSET, HEADER_BEGIN)
lines.append("\n")
lines.append(HEADER_END)
with open(file, "wt", encoding="utf-8", newline="\n") as f:
f.writelines(lines)
changed.append(file)
continue
invalid.append(file)
# Assume that we're simply missing a guard entirely.
lines.insert(HEADER_CHECK_OFFSET, "#pragma once\n\n")
with open(file, "wt", encoding="utf-8", newline="\n") as f:
f.writelines(lines)
changed.append(file)
if changed:
for file in changed:

View File

@@ -5,16 +5,16 @@ ignore_missing_imports = true
namespace_packages = true
no_implicit_optional = true
pretty = true
scripts_are_modules = true
show_column_numbers = true
warn_redundant_casts = true
warn_return_any = true
warn_unreachable = true
python_version = "3.8"
[tool.ruff]
extend-include = ["SConstruct"]
extend-include = ["*SConstruct"]
line-length = 120
target-version = "py37"
target-version = "py38"
[tool.ruff.lint]
extend-select = [
@@ -27,32 +27,32 @@ extend-select = [
]
[tool.codespell]
enable-colors = ""
write-changes = ""
check-hidden = ""
enable-colors = true
write-changes = true
check-hidden = true
quiet-level = 3
builtin = "clear,rare,en-GB_to_en-US"
ignore-words-list = """\
breaked,
cancelled,
checkin,
curvelinear,
doubleclick,
expct,
findn,
gird,
hel,
inout,
labelin,
lod,
mis,
nd,
numer,
ot,
outin,
requestor,
te,
textin,
thirdparty,
vai
"""
builtin = ["clear", "rare", "en-GB_to_en-US"]
ignore-words-list = [
"breaked",
"cancelled",
"checkin",
"curvelinear",
"doubleclick",
"expct",
"findn",
"gird",
"hel",
"inout",
"labelin",
"lod",
"mis",
"nd",
"numer",
"ot",
"outin",
"requestor",
"te",
"textin",
"thirdparty",
"vai",
]

View File

@@ -392,7 +392,7 @@ void ClassDB::initialize_class(const ClassInfo &p_cl) {
}
void ClassDB::initialize(GDExtensionInitializationLevel p_level) {
for (const std::pair<StringName, ClassInfo> pair : classes) {
for (const std::pair<const StringName, ClassInfo> &pair : classes) {
const ClassInfo &cl = pair.second;
if (cl.level != p_level) {
continue;

View File

@@ -32,6 +32,22 @@
namespace godot {
void MethodBind::_set_const(bool p_const) {
_const = p_const;
}
void MethodBind::_set_static(bool p_static) {
_static = p_static;
}
void MethodBind::_set_returns(bool p_returns) {
_returns = p_returns;
}
void MethodBind::_set_vararg(bool p_vararg) {
_vararg = p_vararg;
}
StringName MethodBind::get_name() const {
return name;
}
@@ -40,26 +56,6 @@ void MethodBind::set_name(const StringName &p_name) {
name = p_name;
}
void MethodBind::set_argument_count(int p_count) {
argument_count = p_count;
}
void MethodBind::set_const(bool p_const) {
_is_const = p_const;
}
void MethodBind::set_return(bool p_return) {
_has_return = p_return;
}
void MethodBind::set_static(bool p_static) {
_static = p_static;
}
void MethodBind::set_vararg(bool p_vararg) {
_vararg = p_vararg;
}
void MethodBind::set_argument_names(const std::vector<StringName> &p_names) {
argument_names = p_names;
}
@@ -68,7 +64,7 @@ std::vector<StringName> MethodBind::get_argument_names() const {
return argument_names;
}
void MethodBind::generate_argument_types(int p_count) {
void MethodBind::_generate_argument_types(int p_count) {
set_argument_count(p_count);
if (argument_types != nullptr) {

View File

@@ -119,55 +119,75 @@ AABB AABB::intersection(const AABB &p_aabb) const {
return AABB(min, max - min);
}
bool AABB::intersects_ray(const Vector3 &p_from, const Vector3 &p_dir, Vector3 *r_clip, Vector3 *r_normal) const {
// Note that this routine returns the BACKTRACKED (i.e. behind the ray origin)
// intersection point + normal if INSIDE the AABB.
// The caller can therefore decide when INSIDE whether to use the
// backtracked intersection, or use p_from as the intersection, and
// carry on progressing without e.g. reflecting against the normal.
bool AABB::find_intersects_ray(const Vector3 &p_from, const Vector3 &p_dir, bool &r_inside, Vector3 *r_intersection_point, Vector3 *r_normal) const {
#ifdef MATH_CHECKS
if (unlikely(size.x < 0 || size.y < 0 || size.z < 0)) {
ERR_PRINT("AABB size is negative, this is not supported. Use AABB.abs() to get an AABB with a positive size.");
}
#endif
Vector3 c1, c2;
Vector3 end = position + size;
real_t near = -1e20;
real_t far = 1e20;
real_t tmin = -1e20;
real_t tmax = 1e20;
int axis = 0;
// Make sure r_inside is always initialized,
// to prevent reading uninitialized data in the client code.
r_inside = false;
for (int i = 0; i < 3; i++) {
if (p_dir[i] == 0) {
if ((p_from[i] < position[i]) || (p_from[i] > end[i])) {
return false;
}
} else { // ray not parallel to planes in this direction
c1[i] = (position[i] - p_from[i]) / p_dir[i];
c2[i] = (end[i] - p_from[i]) / p_dir[i];
real_t t1 = (position[i] - p_from[i]) / p_dir[i];
real_t t2 = (end[i] - p_from[i]) / p_dir[i];
if (c1[i] > c2[i]) {
SWAP(c1, c2);
if (t1 > t2) {
SWAP(t1, t2);
}
if (c1[i] > near) {
near = c1[i];
if (t1 >= tmin) {
tmin = t1;
axis = i;
}
if (c2[i] < far) {
far = c2[i];
if (t2 < tmax) {
if (t2 < 0) {
return false;
}
tmax = t2;
}
if ((near > far) || (far < 0)) {
if (tmin > tmax) {
return false;
}
}
}
if (r_clip) {
*r_clip = c1;
// Did the ray start from inside the box?
// In which case the intersection returned is the point of entry
// (behind the ray start) or the calling routine can use the ray origin as intersection point.
r_inside = tmin < 0;
if (r_intersection_point) {
*r_intersection_point = p_from + p_dir * tmin;
// Prevent float error by making sure the point is exactly
// on the AABB border on the relevant axis.
r_intersection_point->coord[axis] = (p_dir[axis] >= 0) ? position.coord[axis] : end.coord[axis];
}
if (r_normal) {
*r_normal = Vector3();
(*r_normal)[axis] = p_dir[axis] ? -1 : 1;
(*r_normal)[axis] = (p_dir[axis] >= 0) ? -1 : 1;
}
return true;
}
bool AABB::intersects_segment(const Vector3 &p_from, const Vector3 &p_to, Vector3 *r_clip, Vector3 *r_normal) const {
bool AABB::intersects_segment(const Vector3 &p_from, const Vector3 &p_to, Vector3 *r_intersection_point, Vector3 *r_normal) const {
#ifdef MATH_CHECKS
if (unlikely(size.x < 0 || size.y < 0 || size.z < 0)) {
ERR_PRINT("AABB size is negative, this is not supported. Use AABB.abs() to get an AABB with a positive size.");
@@ -225,8 +245,8 @@ bool AABB::intersects_segment(const Vector3 &p_from, const Vector3 &p_to, Vector
*r_normal = normal;
}
if (r_clip) {
*r_clip = p_from + rel * min;
if (r_intersection_point) {
*r_intersection_point = p_from + rel * min;
}
return true;
@@ -412,7 +432,15 @@ Variant AABB::intersects_segment_bind(const Vector3 &p_from, const Vector3 &p_to
Variant AABB::intersects_ray_bind(const Vector3 &p_from, const Vector3 &p_dir) const {
Vector3 inters;
if (intersects_ray(p_from, p_dir, &inters)) {
bool inside = false;
if (find_intersects_ray(p_from, p_dir, inside, &inters)) {
// When inside the intersection point may be BEHIND the ray,
// so for general use we return the ray origin.
if (inside) {
return p_from;
}
return inters;
}
return Variant();

View File

@@ -29,31 +29,17 @@
/**************************************************************************/
#include <godot_cpp/core/error_macros.hpp>
#include <godot_cpp/core/math.hpp>
#include <godot_cpp/variant/basis.hpp>
#include <godot_cpp/variant/string.hpp>
using namespace godot;
#define cofac(row1, col1, row2, col2) \
(rows[row1][col1] * rows[row2][col2] - rows[row1][col2] * rows[row2][col1])
namespace godot {
void Basis::from_z(const Vector3 &p_z) {
if (Math::abs(p_z.z) > (real_t)Math_SQRT12) {
// choose p in y-z plane
real_t a = p_z[1] * p_z[1] + p_z[2] * p_z[2];
real_t k = 1.0f / Math::sqrt(a);
rows[0] = Vector3(0, -p_z[2] * k, p_z[1] * k);
rows[1] = Vector3(a * k, -p_z[0] * rows[0][2], p_z[0] * rows[0][1]);
} else {
// choose p in x-y plane
real_t a = p_z.x * p_z.x + p_z.y * p_z.y;
real_t k = 1.0f / Math::sqrt(a);
rows[0] = Vector3(-p_z.y * k, p_z.x * k, 0);
rows[1] = Vector3(-p_z.z * rows[0].y, p_z.z * rows[0].x, a * k);
}
rows[2] = p_z;
}
void Basis::invert() {
real_t co[3] = {
cofac(1, 1, 2, 2), cofac(1, 2, 2, 0), cofac(1, 0, 2, 1)
@@ -107,13 +93,35 @@ Basis Basis::orthogonalized() const {
return c;
}
// Returns true if the basis vectors are orthogonal (perpendicular), so it has no skew or shear, and can be decomposed into rotation and scale.
// See https://en.wikipedia.org/wiki/Orthogonal_basis
bool Basis::is_orthogonal() const {
Basis identity;
Basis m = (*this) * transposed();
return m.is_equal_approx(identity);
const Vector3 x = get_column(0);
const Vector3 y = get_column(1);
const Vector3 z = get_column(2);
return Math::is_zero_approx(x.dot(y)) && Math::is_zero_approx(x.dot(z)) && Math::is_zero_approx(y.dot(z));
}
// Returns true if the basis vectors are orthonormal (orthogonal and normalized), so it has no scale, skew, or shear.
// See https://en.wikipedia.org/wiki/Orthonormal_basis
bool Basis::is_orthonormal() const {
const Vector3 x = get_column(0);
const Vector3 y = get_column(1);
const Vector3 z = get_column(2);
return Math::is_equal_approx(x.length_squared(), 1) && Math::is_equal_approx(y.length_squared(), 1) && Math::is_equal_approx(z.length_squared(), 1) && Math::is_zero_approx(x.dot(y)) && Math::is_zero_approx(x.dot(z)) && Math::is_zero_approx(y.dot(z));
}
// Returns true if the basis is conformal (orthogonal, uniform scale, preserves angles and distance ratios).
// See https://en.wikipedia.org/wiki/Conformal_linear_transformation
bool Basis::is_conformal() const {
const Vector3 x = get_column(0);
const Vector3 y = get_column(1);
const Vector3 z = get_column(2);
const real_t x_len_sq = x.length_squared();
return Math::is_equal_approx(x_len_sq, y.length_squared()) && Math::is_equal_approx(x_len_sq, z.length_squared()) && Math::is_zero_approx(x.dot(y)) && Math::is_zero_approx(x.dot(z)) && Math::is_zero_approx(y.dot(z));
}
// Returns true if the basis only has diagonal elements, so it may only have scale or flip, but no rotation, skew, or shear.
bool Basis::is_diagonal() const {
return (
Math::is_zero_approx(rows[0][1]) && Math::is_zero_approx(rows[0][2]) &&
@@ -121,8 +129,9 @@ bool Basis::is_diagonal() const {
Math::is_zero_approx(rows[2][0]) && Math::is_zero_approx(rows[2][1]));
}
// Returns true if the basis is a pure rotation matrix, so it has no scale, skew, shear, or flip.
bool Basis::is_rotation() const {
return Math::is_equal_approx(determinant(), 1, (real_t)UNIT_EPSILON) && is_orthogonal();
return is_conformal() && Math::is_equal_approx(determinant(), 1, (real_t)UNIT_EPSILON);
}
#ifdef MATH_CHECKS
@@ -257,29 +266,26 @@ void Basis::scale_orthogonal(const Vector3 &p_scale) {
Basis Basis::scaled_orthogonal(const Vector3 &p_scale) const {
Basis m = *this;
Vector3 s = Vector3(-1, -1, -1) + p_scale;
bool sign = std::signbit(s.x + s.y + s.z);
Basis b = m.orthonormalized();
s = b.xform_inv(s);
Vector3 dots;
Basis b;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
dots[j] += s[i] * Math::abs(m.get_column(i).normalized().dot(b.get_column(j)));
}
}
if (sign != std::signbit(dots.x + dots.y + dots.z)) {
dots = -dots;
}
m.scale_local(Vector3(1, 1, 1) + dots);
return m;
}
float Basis::get_uniform_scale() const {
real_t Basis::get_uniform_scale() const {
return (rows[0].length() + rows[1].length() + rows[2].length()) / 3.0f;
}
void Basis::make_scale_uniform() {
float l = (rows[0].length() + rows[1].length() + rows[2].length()) / 3.0f;
for (int i = 0; i < 3; i++) {
rows[i].normalize();
rows[i] *= l;
}
}
Basis Basis::scaled_local(const Vector3 &p_scale) const {
return (*this) * Basis::from_scale(p_scale);
}
@@ -291,7 +297,7 @@ Vector3 Basis::get_scale_abs() const {
Vector3(rows[0][2], rows[1][2], rows[2][2]).length());
}
Vector3 Basis::get_scale_local() const {
Vector3 Basis::get_scale_global() const {
real_t det_sign = SIGN(determinant());
return det_sign * Vector3(rows[0].length(), rows[1].length(), rows[2].length());
}
@@ -418,7 +424,7 @@ void Basis::rotate_to_align(Vector3 p_start_direction, Vector3 p_end_direction)
real_t dot = p_start_direction.dot(p_end_direction);
dot = CLAMP(dot, -1.0f, 1.0f);
const real_t angle_rads = Math::acos(dot);
set_axis_angle(axis, angle_rads);
*this = Basis(axis, angle_rads) * (*this);
}
}
@@ -453,8 +459,13 @@ void Basis::get_rotation_axis_angle_local(Vector3 &p_axis, real_t &p_angle) cons
}
Vector3 Basis::get_euler(EulerOrder p_order) const {
// This epsilon value results in angles within a +/- 0.04 degree range being simplified/truncated.
// Based on testing, this is the largest the epsilon can be without the angle truncation becoming
// visually noticeable.
const real_t epsilon = 0.00000025;
switch (p_order) {
case EULER_ORDER_XYZ: {
case EulerOrder::EULER_ORDER_XYZ: {
// Euler angles in XYZ convention.
// See https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix
//
@@ -464,8 +475,8 @@ Vector3 Basis::get_euler(EulerOrder p_order) const {
Vector3 euler;
real_t sy = rows[0][2];
if (sy < (1.0f - (real_t)CMP_EPSILON)) {
if (sy > -(1.0f - (real_t)CMP_EPSILON)) {
if (sy < (1.0f - epsilon)) {
if (sy > -(1.0f - epsilon)) {
// is this a pure Y rotation?
if (rows[1][0] == 0 && rows[0][1] == 0 && rows[1][2] == 0 && rows[2][1] == 0 && rows[1][1] == 1) {
// return the simplest form (human friendlier in editor and scripts)
@@ -489,7 +500,7 @@ Vector3 Basis::get_euler(EulerOrder p_order) const {
}
return euler;
}
case EULER_ORDER_XZY: {
case EulerOrder::EULER_ORDER_XZY: {
// Euler angles in XZY convention.
// See https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix
//
@@ -499,8 +510,8 @@ Vector3 Basis::get_euler(EulerOrder p_order) const {
Vector3 euler;
real_t sz = rows[0][1];
if (sz < (1.0f - (real_t)CMP_EPSILON)) {
if (sz > -(1.0f - (real_t)CMP_EPSILON)) {
if (sz < (1.0f - epsilon)) {
if (sz > -(1.0f - epsilon)) {
euler.x = Math::atan2(rows[2][1], rows[1][1]);
euler.y = Math::atan2(rows[0][2], rows[0][0]);
euler.z = Math::asin(-sz);
@@ -518,7 +529,7 @@ Vector3 Basis::get_euler(EulerOrder p_order) const {
}
return euler;
}
case EULER_ORDER_YXZ: {
case EulerOrder::EULER_ORDER_YXZ: {
// Euler angles in YXZ convention.
// See https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix
//
@@ -530,8 +541,8 @@ Vector3 Basis::get_euler(EulerOrder p_order) const {
real_t m12 = rows[1][2];
if (m12 < (1 - (real_t)CMP_EPSILON)) {
if (m12 > -(1 - (real_t)CMP_EPSILON)) {
if (m12 < (1 - epsilon)) {
if (m12 > -(1 - epsilon)) {
// is this a pure X rotation?
if (rows[1][0] == 0 && rows[0][1] == 0 && rows[0][2] == 0 && rows[2][0] == 0 && rows[0][0] == 1) {
// return the simplest form (human friendlier in editor and scripts)
@@ -556,7 +567,7 @@ Vector3 Basis::get_euler(EulerOrder p_order) const {
return euler;
}
case EULER_ORDER_YZX: {
case EulerOrder::EULER_ORDER_YZX: {
// Euler angles in YZX convention.
// See https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix
//
@@ -566,8 +577,8 @@ Vector3 Basis::get_euler(EulerOrder p_order) const {
Vector3 euler;
real_t sz = rows[1][0];
if (sz < (1.0f - (real_t)CMP_EPSILON)) {
if (sz > -(1.0f - (real_t)CMP_EPSILON)) {
if (sz < (1.0f - epsilon)) {
if (sz > -(1.0f - epsilon)) {
euler.x = Math::atan2(-rows[1][2], rows[1][1]);
euler.y = Math::atan2(-rows[2][0], rows[0][0]);
euler.z = Math::asin(sz);
@@ -584,8 +595,8 @@ Vector3 Basis::get_euler(EulerOrder p_order) const {
euler.z = Math_PI / 2.0f;
}
return euler;
}
case EULER_ORDER_ZXY: {
} break;
case EulerOrder::EULER_ORDER_ZXY: {
// Euler angles in ZXY convention.
// See https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix
//
@@ -594,8 +605,8 @@ Vector3 Basis::get_euler(EulerOrder p_order) const {
// -cx*sy sx cx*cy
Vector3 euler;
real_t sx = rows[2][1];
if (sx < (1.0f - (real_t)CMP_EPSILON)) {
if (sx > -(1.0f - (real_t)CMP_EPSILON)) {
if (sx < (1.0f - epsilon)) {
if (sx > -(1.0f - epsilon)) {
euler.x = Math::asin(sx);
euler.y = Math::atan2(-rows[2][0], rows[2][2]);
euler.z = Math::atan2(-rows[0][1], rows[1][1]);
@@ -612,8 +623,8 @@ Vector3 Basis::get_euler(EulerOrder p_order) const {
euler.z = 0;
}
return euler;
}
case EULER_ORDER_ZYX: {
} break;
case EulerOrder::EULER_ORDER_ZYX: {
// Euler angles in ZYX convention.
// See https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix
//
@@ -622,8 +633,8 @@ Vector3 Basis::get_euler(EulerOrder p_order) const {
// -sy cy*sx cy*cx
Vector3 euler;
real_t sy = rows[2][0];
if (sy < (1.0f - (real_t)CMP_EPSILON)) {
if (sy > -(1.0f - (real_t)CMP_EPSILON)) {
if (sy < (1.0f - epsilon)) {
if (sy > -(1.0f - epsilon)) {
euler.x = Math::atan2(rows[2][1], rows[2][2]);
euler.y = Math::asin(-sy);
euler.z = Math::atan2(rows[1][0], rows[0][0]);
@@ -664,26 +675,26 @@ void Basis::set_euler(const Vector3 &p_euler, EulerOrder p_order) {
Basis zmat(c, -s, 0, s, c, 0, 0, 0, 1);
switch (p_order) {
case EULER_ORDER_XYZ: {
case EulerOrder::EULER_ORDER_XYZ: {
*this = xmat * (ymat * zmat);
} break;
case EULER_ORDER_XZY: {
case EulerOrder::EULER_ORDER_XZY: {
*this = xmat * zmat * ymat;
} break;
case EULER_ORDER_YXZ: {
case EulerOrder::EULER_ORDER_YXZ: {
*this = ymat * xmat * zmat;
} break;
case EULER_ORDER_YZX: {
case EulerOrder::EULER_ORDER_YZX: {
*this = ymat * zmat * xmat;
} break;
case EULER_ORDER_ZXY: {
case EulerOrder::EULER_ORDER_ZXY: {
*this = zmat * xmat * ymat;
} break;
case EULER_ORDER_ZYX: {
case EulerOrder::EULER_ORDER_ZYX: {
*this = zmat * ymat * xmat;
} break;
default: {
ERR_FAIL_MSG("Invalid order parameter for set_euler(vec3,order)");
ERR_FAIL_MSG("Invalid Euler order parameter.");
}
}
}
@@ -720,7 +731,7 @@ Basis::operator String() const {
Quaternion Basis::get_quaternion() const {
#ifdef MATH_CHECKS
ERR_FAIL_COND_V_MSG(!is_rotation(), Quaternion(), "Basis must be normalized in order to be casted to a Quaternion. Use get_rotation_quaternion() or call orthonormalized() if the Basis contains linearly independent vectors.");
ERR_FAIL_COND_V_MSG(!is_rotation(), Quaternion(), "Basis " + operator String() + " must be normalized in order to be casted to a Quaternion. Use get_rotation_quaternion() or call orthonormalized() if the Basis contains linearly independent vectors.");
#endif
/* Allow getting a quaternion from an unnormalized transform */
Basis m = *this;
@@ -828,8 +839,8 @@ void Basis::get_axis_angle(Vector3 &r_axis, real_t &r_angle) const {
z = (rows[1][0] - rows[0][1]) / s;
r_axis = Vector3(x, y, z);
// CLAMP to avoid NaN if the value passed to acos is not in [0,1].
r_angle = Math::acos(CLAMP((rows[0][0] + rows[1][1] + rows[2][2] - 1) / 2, (real_t)0.0, (real_t)1.0));
// acos does clamping.
r_angle = Math::acos((rows[0][0] + rows[1][1] + rows[2][2] - 1) / 2);
}
void Basis::set_quaternion(const Quaternion &p_quaternion) {
@@ -847,7 +858,7 @@ void Basis::set_quaternion(const Quaternion &p_quaternion) {
void Basis::set_axis_angle(const Vector3 &p_axis, real_t p_angle) {
// Rotation matrix from axis and angle, see https://en.wikipedia.org/wiki/Rotation_matrix#Rotation_matrix_from_axis_angle
#ifdef MATH_CHECKS
ERR_FAIL_COND_MSG(!p_axis.is_normalized(), "The axis Vector3 must be normalized.");
ERR_FAIL_COND_MSG(!p_axis.is_normalized(), "The axis Vector3 " + p_axis.operator String() + " must be normalized.");
#endif
Vector3 axis_sq(p_axis.x * p_axis.x, p_axis.y * p_axis.y, p_axis.z * p_axis.z);
real_t cosine = Math::cos(p_angle);
@@ -905,7 +916,7 @@ void Basis::_set_diagonal(const Vector3 &p_diag) {
rows[2][2] = p_diag.z;
}
Basis Basis::lerp(const Basis &p_to, const real_t &p_weight) const {
Basis Basis::lerp(const Basis &p_to, real_t p_weight) const {
Basis b;
b.rows[0] = rows[0].lerp(p_to.rows[0], p_weight);
b.rows[1] = rows[1].lerp(p_to.rows[1], p_weight);
@@ -914,7 +925,7 @@ Basis Basis::lerp(const Basis &p_to, const real_t &p_weight) const {
return b;
}
Basis Basis::slerp(const Basis &p_to, const real_t &p_weight) const {
Basis Basis::slerp(const Basis &p_to, real_t p_weight) const {
//consider scale
Quaternion from(*this);
Quaternion to(p_to);
@@ -1047,9 +1058,10 @@ Basis Basis::looking_at(const Vector3 &p_target, const Vector3 &p_up, bool p_use
v_z = -v_z;
}
Vector3 v_x = p_up.cross(v_z);
#ifdef MATH_CHECKS
ERR_FAIL_COND_V_MSG(v_x.is_zero_approx(), Basis(), "The target vector and up vector can't be parallel to each other.");
#endif
if (v_x.is_zero_approx()) {
WARN_PRINT("Target and up vectors are colinear. This is not advised as it may cause unwanted rotation around local Z axis.");
v_x = p_up.get_any_perpendicular(); // Vectors are almost parallel.
}
v_x.normalize();
Vector3 v_y = v_z.cross(v_x);

View File

@@ -29,6 +29,7 @@
/**************************************************************************/
#include <godot_cpp/core/error_macros.hpp>
#include <godot_cpp/templates/hash_map.hpp>
#include <godot_cpp/variant/color.hpp>
#include <godot_cpp/variant/color_names.inc.hpp>
#include <godot_cpp/variant/string.hpp>
@@ -131,20 +132,20 @@ String _to_hex(float p_val) {
String Color::to_html(bool p_alpha) const {
String txt;
txt = txt + _to_hex(r);
txt = txt + _to_hex(g);
txt = txt + _to_hex(b);
txt += _to_hex(r);
txt += _to_hex(g);
txt += _to_hex(b);
if (p_alpha) {
txt = txt + _to_hex(a);
txt += _to_hex(a);
}
return txt;
}
float Color::get_h() const {
float min = Math::min(r, g);
min = Math::min(min, b);
float max = Math::max(r, g);
max = Math::max(max, b);
float min = MIN(r, g);
min = MIN(min, b);
float max = MAX(r, g);
max = MAX(max, b);
float delta = max - min;
@@ -170,10 +171,10 @@ float Color::get_h() const {
}
float Color::get_s() const {
float min = Math::min(r, g);
min = Math::min(min, b);
float max = Math::max(r, g);
max = Math::max(max, b);
float min = MIN(r, g);
min = MIN(min, b);
float max = MAX(r, g);
max = MAX(max, b);
float delta = max - min;
@@ -181,8 +182,8 @@ float Color::get_s() const {
}
float Color::get_v() const {
float max = Math::max(r, g);
max = Math::max(max, b);
float max = MAX(r, g);
max = MAX(max, b);
return max;
}
@@ -385,7 +386,6 @@ Color Color::named(const String &p_name) {
int idx = find_named_color(p_name);
if (idx == -1) {
ERR_FAIL_V_MSG(Color(), "Invalid color name: " + p_name + ".");
return Color();
}
return named_colors[idx].color;
}
@@ -400,7 +400,7 @@ Color Color::named(const String &p_name, const Color &p_default) {
int Color::find_named_color(const String &p_name) {
String name = p_name;
// Normalize name
// Normalize name.
name = name.replace(" ", "");
name = name.replace("-", "");
name = name.replace("_", "");
@@ -408,23 +408,24 @@ int Color::find_named_color(const String &p_name) {
name = name.replace(".", "");
name = name.to_upper();
int idx = 0;
while (named_colors[idx].name != nullptr) {
if (name == String(named_colors[idx].name).replace("_", "")) {
return idx;
static HashMap<String, int> named_colors_hashmap;
if (unlikely(named_colors_hashmap.is_empty())) {
const int named_color_count = get_named_color_count();
for (int i = 0; i < named_color_count; i++) {
named_colors_hashmap[String(named_colors[i].name).replace("_", "")] = i;
}
idx++;
}
const HashMap<String, int>::ConstIterator E = named_colors_hashmap.find(name);
if (E) {
return E->value;
}
return -1;
}
int Color::get_named_color_count() {
int idx = 0;
while (named_colors[idx].name != nullptr) {
idx++;
}
return idx;
return sizeof(named_colors) / sizeof(NamedColor);
}
String Color::get_named_color_name(int p_idx) {
@@ -467,6 +468,10 @@ Color Color::from_rgbe9995(uint32_t p_rgbe) {
return Color(rd, gd, bd, 1.0f);
}
Color Color::from_rgba8(int64_t p_r8, int64_t p_g8, int64_t p_b8, int64_t p_a8) {
return Color(p_r8 / 255.0f, p_g8 / 255.0f, p_b8 / 255.0f, p_a8 / 255.0f);
}
Color::operator String() const {
return "(" + String::num(r, 4) + ", " + String::num(g, 4) + ", " + String::num(b, 4) + ", " + String::num(a, 4) + ")";
}

View File

@@ -236,6 +236,14 @@ void Array::_ref(const Array &p_from) const {
internal::gdextension_interface_array_ref((GDExtensionTypePtr *)this, (GDExtensionConstTypePtr *)&p_from);
}
const Variant *Array::ptr() const {
return (const Variant *)internal::gdextension_interface_array_operator_index_const((GDExtensionTypePtr *)this, 0);
}
Variant *Array::ptrw() {
return (Variant *)internal::gdextension_interface_array_operator_index((GDExtensionTypePtr *)this, 0);
}
const Variant &Dictionary::operator[](const Variant &p_key) const {
const Variant *var = (const Variant *)internal::gdextension_interface_dictionary_operator_index_const((GDExtensionTypePtr *)this, (GDExtensionVariantPtr)&p_key);
return *var;

View File

@@ -100,13 +100,11 @@ bool Plane::intersects_ray(const Vector3 &p_from, const Vector3 &p_dir, Vector3
Vector3 segment = p_dir;
real_t den = normal.dot(segment);
//printf("den is %i\n",den);
if (Math::is_zero_approx(den)) {
return false;
}
real_t dist = (normal.dot(p_from) - d) / den;
//printf("dist is %i\n",dist);
if (dist > (real_t)CMP_EPSILON) { //this is a ray, before the emitting pos (p_from) doesn't exist
@@ -123,13 +121,11 @@ bool Plane::intersects_segment(const Vector3 &p_begin, const Vector3 &p_end, Vec
Vector3 segment = p_begin - p_end;
real_t den = normal.dot(segment);
//printf("den is %i\n",den);
if (Math::is_zero_approx(den)) {
return false;
}
real_t dist = (normal.dot(p_begin) - d) / den;
//printf("dist is %i\n",dist);
if (dist < (real_t)-CMP_EPSILON || dist > (1.0f + (real_t)CMP_EPSILON)) {
return false;

View File

@@ -39,7 +39,7 @@
namespace godot {
float Projection::determinant() const {
real_t Projection::determinant() const {
return columns[0][3] * columns[1][2] * columns[2][1] * columns[3][0] - columns[0][2] * columns[1][3] * columns[2][1] * columns[3][0] -
columns[0][3] * columns[1][1] * columns[2][2] * columns[3][0] + columns[0][1] * columns[1][3] * columns[2][2] * columns[3][0] +
columns[0][2] * columns[1][1] * columns[2][3] * columns[3][0] - columns[0][1] * columns[1][2] * columns[2][3] * columns[3][0] -
@@ -171,7 +171,7 @@ Projection Projection::perspective_znear_adjusted(real_t p_new_znear) const {
}
Plane Projection::get_projection_plane(Planes p_plane) const {
const real_t *matrix = (const real_t *)this->columns;
const real_t *matrix = (const real_t *)columns;
switch (p_plane) {
case PLANE_NEAR: {
@@ -404,20 +404,19 @@ void Projection::set_frustum(real_t p_size, real_t p_aspect, Vector2 p_offset, r
}
real_t Projection::get_z_far() const {
const real_t *matrix = (const real_t *)this->columns;
const real_t *matrix = (const real_t *)columns;
Plane new_plane = Plane(matrix[3] - matrix[2],
matrix[7] - matrix[6],
matrix[11] - matrix[10],
matrix[15] - matrix[14]);
new_plane.normal = -new_plane.normal;
new_plane.normalize();
return new_plane.d;
}
real_t Projection::get_z_near() const {
const real_t *matrix = (const real_t *)this->columns;
const real_t *matrix = (const real_t *)columns;
Plane new_plane = Plane(matrix[3] + matrix[2],
matrix[7] + matrix[6],
matrix[11] + matrix[10],
@@ -428,7 +427,7 @@ real_t Projection::get_z_near() const {
}
Vector2 Projection::get_viewport_half_extents() const {
const real_t *matrix = (const real_t *)this->columns;
const real_t *matrix = (const real_t *)columns;
///////--- Near Plane ---///////
Plane near_plane = Plane(matrix[3] + matrix[2],
matrix[7] + matrix[6],
@@ -456,7 +455,7 @@ Vector2 Projection::get_viewport_half_extents() const {
}
Vector2 Projection::get_far_plane_half_extents() const {
const real_t *matrix = (const real_t *)this->columns;
const real_t *matrix = (const real_t *)columns;
///////--- Far Plane ---///////
Plane far_plane = Plane(matrix[3] - matrix[2],
matrix[7] - matrix[6],
@@ -484,7 +483,7 @@ Vector2 Projection::get_far_plane_half_extents() const {
}
bool Projection::get_endpoints(const Transform3D &p_transform, Vector3 *p_8points) const {
Array planes = get_projection_planes(Transform3D());
Vector<Plane> planes = get_projection_planes(Transform3D());
const Planes intersections[8][3] = {
{ PLANE_FAR, PLANE_LEFT, PLANE_TOP },
{ PLANE_FAR, PLANE_LEFT, PLANE_BOTTOM },
@@ -509,17 +508,17 @@ bool Projection::get_endpoints(const Transform3D &p_transform, Vector3 *p_8point
return true;
}
Array Projection::get_projection_planes(const Transform3D &p_transform) const {
Vector<Plane> Projection::get_projection_planes(const Transform3D &p_transform) const {
/** Fast Plane Extraction from combined modelview/projection matrices.
* References:
* https://web.archive.org/web/20011221205252/https://www.markmorley.com/opengl/frustumculling.html
* https://web.archive.org/web/20061020020112/https://www2.ravensoft.com/users/ggribb/plane%20extraction.pdf
*/
Array planes;
Vector<Plane> planes;
planes.resize(6);
const real_t *matrix = (const real_t *)this->columns;
const real_t *matrix = (const real_t *)columns;
Plane new_plane;
@@ -532,7 +531,7 @@ Array Projection::get_projection_planes(const Transform3D &p_transform) const {
new_plane.normal = -new_plane.normal;
new_plane.normalize();
planes[0] = p_transform.xform(new_plane);
planes.write[0] = p_transform.xform(new_plane);
///////--- Far Plane ---///////
new_plane = Plane(matrix[3] - matrix[2],
@@ -543,7 +542,7 @@ Array Projection::get_projection_planes(const Transform3D &p_transform) const {
new_plane.normal = -new_plane.normal;
new_plane.normalize();
planes[1] = p_transform.xform(new_plane);
planes.write[1] = p_transform.xform(new_plane);
///////--- Left Plane ---///////
new_plane = Plane(matrix[3] + matrix[0],
@@ -554,7 +553,7 @@ Array Projection::get_projection_planes(const Transform3D &p_transform) const {
new_plane.normal = -new_plane.normal;
new_plane.normalize();
planes[2] = p_transform.xform(new_plane);
planes.write[2] = p_transform.xform(new_plane);
///////--- Top Plane ---///////
new_plane = Plane(matrix[3] - matrix[1],
@@ -565,7 +564,7 @@ Array Projection::get_projection_planes(const Transform3D &p_transform) const {
new_plane.normal = -new_plane.normal;
new_plane.normalize();
planes[3] = p_transform.xform(new_plane);
planes.write[3] = p_transform.xform(new_plane);
///////--- Right Plane ---///////
new_plane = Plane(matrix[3] - matrix[0],
@@ -576,7 +575,7 @@ Array Projection::get_projection_planes(const Transform3D &p_transform) const {
new_plane.normal = -new_plane.normal;
new_plane.normalize();
planes[4] = p_transform.xform(new_plane);
planes.write[4] = p_transform.xform(new_plane);
///////--- Bottom Plane ---///////
new_plane = Plane(matrix[3] + matrix[1],
@@ -587,7 +586,7 @@ Array Projection::get_projection_planes(const Transform3D &p_transform) const {
new_plane.normal = -new_plane.normal;
new_plane.normalize();
planes[5] = p_transform.xform(new_plane);
planes.write[5] = p_transform.xform(new_plane);
return planes;
}
@@ -599,101 +598,229 @@ Projection Projection::inverse() const {
}
void Projection::invert() {
int i, j, k;
int pvt_i[4], pvt_j[4]; /* Locations of pivot matrix */
real_t pvt_val; /* Value of current pivot element */
real_t hold; /* Temporary storage */
real_t determinant = 1.0f;
for (k = 0; k < 4; k++) {
/** Locate k'th pivot element **/
pvt_val = columns[k][k]; /** Initialize for search **/
pvt_i[k] = k;
pvt_j[k] = k;
for (i = k; i < 4; i++) {
for (j = k; j < 4; j++) {
if (Math::abs(columns[i][j]) > Math::abs(pvt_val)) {
pvt_i[k] = i;
pvt_j[k] = j;
pvt_val = columns[i][j];
}
}
}
// Adapted from Mesa's `src/util/u_math.c` `util_invert_mat4x4`.
// MIT licensed. Copyright 2008 VMware, Inc. Authored by Jacques Leroy.
Projection temp;
real_t *out = (real_t *)temp.columns;
real_t *m = (real_t *)columns;
/** Product of pivots, gives determinant when finished **/
determinant *= pvt_val;
if (Math::is_zero_approx(determinant)) {
return; /** Matrix is singular (zero determinant). **/
}
real_t wtmp[4][8];
real_t m0, m1, m2, m3, s;
real_t *r0, *r1, *r2, *r3;
/** "Interchange" rows (with sign change stuff) **/
i = pvt_i[k];
if (i != k) { /** If rows are different **/
for (j = 0; j < 4; j++) {
hold = -columns[k][j];
columns[k][j] = columns[i][j];
columns[i][j] = hold;
}
}
#define MAT(m, r, c) (m)[(c) * 4 + (r)]
/** "Interchange" columns **/
j = pvt_j[k];
if (j != k) { /** If columns are different **/
for (i = 0; i < 4; i++) {
hold = -columns[i][k];
columns[i][k] = columns[i][j];
columns[i][j] = hold;
}
}
r0 = wtmp[0];
r1 = wtmp[1];
r2 = wtmp[2];
r3 = wtmp[3];
/** Divide column by minus pivot value **/
for (i = 0; i < 4; i++) {
if (i != k) {
columns[i][k] /= (-pvt_val);
}
}
r0[0] = MAT(m, 0, 0);
r0[1] = MAT(m, 0, 1);
r0[2] = MAT(m, 0, 2);
r0[3] = MAT(m, 0, 3);
r0[4] = 1.0;
r0[5] = 0.0;
r0[6] = 0.0;
r0[7] = 0.0;
/** Reduce the matrix **/
for (i = 0; i < 4; i++) {
hold = columns[i][k];
for (j = 0; j < 4; j++) {
if (i != k && j != k) {
columns[i][j] += hold * columns[k][j];
}
}
}
r1[0] = MAT(m, 1, 0);
r1[1] = MAT(m, 1, 1);
r1[2] = MAT(m, 1, 2);
r1[3] = MAT(m, 1, 3);
r1[5] = 1.0;
r1[4] = 0.0;
r1[6] = 0.0;
r1[7] = 0.0;
/** Divide row by pivot **/
for (j = 0; j < 4; j++) {
if (j != k) {
columns[k][j] /= pvt_val;
}
}
r2[0] = MAT(m, 2, 0);
r2[1] = MAT(m, 2, 1);
r2[2] = MAT(m, 2, 2);
r2[3] = MAT(m, 2, 3);
r2[6] = 1.0;
r2[4] = 0.0;
r2[5] = 0.0;
r2[7] = 0.0;
/** Replace pivot by reciprocal (at last we can touch it). **/
columns[k][k] = 1.0 / pvt_val;
r3[0] = MAT(m, 3, 0);
r3[1] = MAT(m, 3, 1);
r3[2] = MAT(m, 3, 2);
r3[3] = MAT(m, 3, 3);
r3[7] = 1.0;
r3[4] = 0.0;
r3[5] = 0.0;
r3[6] = 0.0;
/* choose pivot - or die */
if (Math::abs(r3[0]) > Math::abs(r2[0])) {
SWAP(r3, r2);
}
if (Math::abs(r2[0]) > Math::abs(r1[0])) {
SWAP(r2, r1);
}
if (Math::abs(r1[0]) > Math::abs(r0[0])) {
SWAP(r1, r0);
}
ERR_FAIL_COND(0.0 == r0[0]);
/* eliminate first variable */
m1 = r1[0] / r0[0];
m2 = r2[0] / r0[0];
m3 = r3[0] / r0[0];
s = r0[1];
r1[1] -= m1 * s;
r2[1] -= m2 * s;
r3[1] -= m3 * s;
s = r0[2];
r1[2] -= m1 * s;
r2[2] -= m2 * s;
r3[2] -= m3 * s;
s = r0[3];
r1[3] -= m1 * s;
r2[3] -= m2 * s;
r3[3] -= m3 * s;
s = r0[4];
if (s != 0.0) {
r1[4] -= m1 * s;
r2[4] -= m2 * s;
r3[4] -= m3 * s;
}
s = r0[5];
if (s != 0.0) {
r1[5] -= m1 * s;
r2[5] -= m2 * s;
r3[5] -= m3 * s;
}
s = r0[6];
if (s != 0.0) {
r1[6] -= m1 * s;
r2[6] -= m2 * s;
r3[6] -= m3 * s;
}
s = r0[7];
if (s != 0.0) {
r1[7] -= m1 * s;
r2[7] -= m2 * s;
r3[7] -= m3 * s;
}
/* That was most of the work, one final pass of row/column interchange */
/* to finish */
for (k = 4 - 2; k >= 0; k--) { /* Don't need to work with 1 by 1 corner*/
i = pvt_j[k]; /* Rows to swap correspond to pivot COLUMN */
if (i != k) { /* If rows are different */
for (j = 0; j < 4; j++) {
hold = columns[k][j];
columns[k][j] = -columns[i][j];
columns[i][j] = hold;
}
}
j = pvt_i[k]; /* Columns to swap correspond to pivot ROW */
if (j != k) { /* If columns are different */
for (i = 0; i < 4; i++) {
hold = columns[i][k];
columns[i][k] = -columns[i][j];
columns[i][j] = hold;
}
}
/* choose pivot - or die */
if (Math::abs(r3[1]) > Math::abs(r2[1])) {
SWAP(r3, r2);
}
if (Math::abs(r2[1]) > Math::abs(r1[1])) {
SWAP(r2, r1);
}
ERR_FAIL_COND(0.0 == r1[1]);
/* eliminate second variable */
m2 = r2[1] / r1[1];
m3 = r3[1] / r1[1];
r2[2] -= m2 * r1[2];
r3[2] -= m3 * r1[2];
r2[3] -= m2 * r1[3];
r3[3] -= m3 * r1[3];
s = r1[4];
if (0.0 != s) {
r2[4] -= m2 * s;
r3[4] -= m3 * s;
}
s = r1[5];
if (0.0 != s) {
r2[5] -= m2 * s;
r3[5] -= m3 * s;
}
s = r1[6];
if (0.0 != s) {
r2[6] -= m2 * s;
r3[6] -= m3 * s;
}
s = r1[7];
if (0.0 != s) {
r2[7] -= m2 * s;
r3[7] -= m3 * s;
}
/* choose pivot - or die */
if (Math::abs(r3[2]) > Math::abs(r2[2])) {
SWAP(r3, r2);
}
ERR_FAIL_COND(0.0 == r2[2]);
/* eliminate third variable */
m3 = r3[2] / r2[2];
r3[3] -= m3 * r2[3];
r3[4] -= m3 * r2[4];
r3[5] -= m3 * r2[5];
r3[6] -= m3 * r2[6];
r3[7] -= m3 * r2[7];
/* last check */
ERR_FAIL_COND(0.0 == r3[3]);
s = 1.0 / r3[3]; /* now back substitute row 3 */
r3[4] *= s;
r3[5] *= s;
r3[6] *= s;
r3[7] *= s;
m2 = r2[3]; /* now back substitute row 2 */
s = 1.0 / r2[2];
r2[4] = s * (r2[4] - r3[4] * m2);
r2[5] = s * (r2[5] - r3[5] * m2);
r2[6] = s * (r2[6] - r3[6] * m2);
r2[7] = s * (r2[7] - r3[7] * m2);
m1 = r1[3];
r1[4] -= r3[4] * m1;
r1[5] -= r3[5] * m1;
r1[6] -= r3[6] * m1;
r1[7] -= r3[7] * m1;
m0 = r0[3];
r0[4] -= r3[4] * m0;
r0[5] -= r3[5] * m0;
r0[6] -= r3[6] * m0;
r0[7] -= r3[7] * m0;
m1 = r1[2]; /* now back substitute row 1 */
s = 1.0 / r1[1];
r1[4] = s * (r1[4] - r2[4] * m1);
r1[5] = s * (r1[5] - r2[5] * m1),
r1[6] = s * (r1[6] - r2[6] * m1);
r1[7] = s * (r1[7] - r2[7] * m1);
m0 = r0[2];
r0[4] -= r2[4] * m0;
r0[5] -= r2[5] * m0;
r0[6] -= r2[6] * m0;
r0[7] -= r2[7] * m0;
m0 = r0[1]; /* now back substitute row 0 */
s = 1.0 / r0[0];
r0[4] = s * (r0[4] - r1[4] * m0);
r0[5] = s * (r0[5] - r1[5] * m0),
r0[6] = s * (r0[6] - r1[6] * m0);
r0[7] = s * (r0[7] - r1[7] * m0);
MAT(out, 0, 0) = r0[4];
MAT(out, 0, 1) = r0[5];
MAT(out, 0, 2) = r0[6];
MAT(out, 0, 3) = r0[7];
MAT(out, 1, 0) = r1[4];
MAT(out, 1, 1) = r1[5];
MAT(out, 1, 2) = r1[6];
MAT(out, 1, 3) = r1[7];
MAT(out, 2, 0) = r2[4];
MAT(out, 2, 1) = r2[5];
MAT(out, 2, 2) = r2[6];
MAT(out, 2, 3) = r2[7];
MAT(out, 3, 0) = r3[4];
MAT(out, 3, 1) = r3[5];
MAT(out, 3, 2) = r3[6];
MAT(out, 3, 3) = r3[7];
#undef MAT
*this = temp;
}
void Projection::flip_y() {
@@ -722,7 +849,8 @@ Projection Projection::operator*(const Projection &p_matrix) const {
return new_matrix;
}
void Projection::set_depth_correction(bool p_flip_y) {
void Projection::set_depth_correction(bool p_flip_y, bool p_reverse_z, bool p_remap_z) {
// p_remap_z is used to convert from OpenGL-style clip space (-1 - 1) to Vulkan style (0 - 1).
real_t *m = &columns[0][0];
m[0] = 1;
@@ -735,11 +863,11 @@ void Projection::set_depth_correction(bool p_flip_y) {
m[7] = 0.0;
m[8] = 0.0;
m[9] = 0.0;
m[10] = 0.5;
m[10] = p_remap_z ? (p_reverse_z ? -0.5 : 0.5) : (p_reverse_z ? -1.0 : 1.0);
m[11] = 0.0;
m[12] = 0.0;
m[13] = 0.0;
m[14] = 0.5;
m[14] = p_remap_z ? 0.5 : 0.0;
m[15] = 1.0;
}
@@ -786,14 +914,10 @@ void Projection::set_light_atlas_rect(const Rect2 &p_rect) {
}
Projection::operator String() const {
String str;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
str = str + String((j > 0) ? ", " : "\n") + rtos(columns[i][j]);
}
}
return str;
return "[X: " + columns[0].operator String() +
", Y: " + columns[1].operator String() +
", Z: " + columns[2].operator String() +
", W: " + columns[3].operator String() + "]";
}
real_t Projection::get_aspect() const {
@@ -812,7 +936,7 @@ bool Projection::is_orthogonal() const {
}
real_t Projection::get_fov() const {
const real_t *matrix = (const real_t *)this->columns;
const real_t *matrix = (const real_t *)columns;
Plane right_plane = Plane(matrix[3] - matrix[0],
matrix[7] - matrix[4],
@@ -834,13 +958,13 @@ real_t Projection::get_fov() const {
}
}
float Projection::get_lod_multiplier() const {
real_t Projection::get_lod_multiplier() const {
if (is_orthogonal()) {
return get_viewport_half_extents().x;
} else {
float zn = get_z_near();
float width = get_viewport_half_extents().x * 2.0;
return 1.0 / (zn / width);
const real_t zn = get_z_near();
const real_t width = get_viewport_half_extents().x * 2.0f;
return 1.0f / (zn / width);
}
// Usage is lod_size / (lod_distance * multiplier) < threshold
@@ -913,6 +1037,13 @@ Projection::Projection(const Vector4 &p_x, const Vector4 &p_y, const Vector4 &p_
columns[3] = p_w;
}
Projection::Projection(real_t p_xx, real_t p_xy, real_t p_xz, real_t p_xw, real_t p_yx, real_t p_yy, real_t p_yz, real_t p_yw, real_t p_zx, real_t p_zy, real_t p_zz, real_t p_zw, real_t p_wx, real_t p_wy, real_t p_wz, real_t p_ww) {
columns[0] = Vector4(p_xx, p_xy, p_xz, p_xw);
columns[1] = Vector4(p_yx, p_yy, p_yz, p_yw);
columns[2] = Vector4(p_zx, p_zy, p_zz, p_zw);
columns[3] = Vector4(p_wx, p_wy, p_wz, p_ww);
}
Projection::Projection(const Transform3D &p_transform) {
const Transform3D &tr = p_transform;
real_t *m = &columns[0][0];

View File

@@ -194,11 +194,11 @@ Quaternion Quaternion::spherical_cubic_interpolate(const Quaternion &p_b, const
post_q = Basis(post_q).get_rotation_quaternion();
// Flip quaternions to shortest path if necessary.
bool flip1 = Math::sign(from_q.dot(pre_q));
bool flip1 = std::signbit(from_q.dot(pre_q));
pre_q = flip1 ? -pre_q : pre_q;
bool flip2 = Math::sign(from_q.dot(to_q));
bool flip2 = std::signbit(from_q.dot(to_q));
to_q = flip2 ? -to_q : to_q;
bool flip3 = flip2 ? to_q.dot(post_q) <= 0 : Math::sign(to_q.dot(post_q));
bool flip3 = flip2 ? to_q.dot(post_q) <= 0 : std::signbit(to_q.dot(post_q));
post_q = flip3 ? -post_q : post_q;
// Calc by Expmap in from_q space.
@@ -245,11 +245,11 @@ Quaternion Quaternion::spherical_cubic_interpolate_in_time(const Quaternion &p_b
post_q = Basis(post_q).get_rotation_quaternion();
// Flip quaternions to shortest path if necessary.
bool flip1 = Math::sign(from_q.dot(pre_q));
bool flip1 = std::signbit(from_q.dot(pre_q));
pre_q = flip1 ? -pre_q : pre_q;
bool flip2 = Math::sign(from_q.dot(to_q));
bool flip2 = std::signbit(from_q.dot(to_q));
to_q = flip2 ? -to_q : to_q;
bool flip3 = flip2 ? to_q.dot(post_q) <= 0 : Math::sign(to_q.dot(post_q));
bool flip3 = flip2 ? to_q.dot(post_q) <= 0 : std::signbit(to_q.dot(post_q));
post_q = flip3 ? -post_q : post_q;
// Calc by Expmap in from_q space.

View File

@@ -211,31 +211,31 @@ next4:
real_t mina = maxa;
real_t dp = p_xform.columns[0].dot(xf_points2[1]);
maxa = Math::max(dp, maxa);
mina = Math::min(dp, mina);
maxa = MAX(dp, maxa);
mina = MIN(dp, mina);
dp = p_xform.columns[0].dot(xf_points2[2]);
maxa = Math::max(dp, maxa);
mina = Math::min(dp, mina);
maxa = MAX(dp, maxa);
mina = MIN(dp, mina);
dp = p_xform.columns[0].dot(xf_points2[3]);
maxa = Math::max(dp, maxa);
mina = Math::min(dp, mina);
maxa = MAX(dp, maxa);
mina = MIN(dp, mina);
real_t maxb = p_xform.columns[0].dot(xf_points[0]);
real_t minb = maxb;
dp = p_xform.columns[0].dot(xf_points[1]);
maxb = Math::max(dp, maxb);
minb = Math::min(dp, minb);
maxb = MAX(dp, maxb);
minb = MIN(dp, minb);
dp = p_xform.columns[0].dot(xf_points[2]);
maxb = Math::max(dp, maxb);
minb = Math::min(dp, minb);
maxb = MAX(dp, maxb);
minb = MIN(dp, minb);
dp = p_xform.columns[0].dot(xf_points[3]);
maxb = Math::max(dp, maxb);
minb = Math::min(dp, minb);
maxb = MAX(dp, maxb);
minb = MIN(dp, minb);
if (mina > maxb) {
return false;
@@ -248,31 +248,31 @@ next4:
mina = maxa;
dp = p_xform.columns[1].dot(xf_points2[1]);
maxa = Math::max(dp, maxa);
mina = Math::min(dp, mina);
maxa = MAX(dp, maxa);
mina = MIN(dp, mina);
dp = p_xform.columns[1].dot(xf_points2[2]);
maxa = Math::max(dp, maxa);
mina = Math::min(dp, mina);
maxa = MAX(dp, maxa);
mina = MIN(dp, mina);
dp = p_xform.columns[1].dot(xf_points2[3]);
maxa = Math::max(dp, maxa);
mina = Math::min(dp, mina);
maxa = MAX(dp, maxa);
mina = MIN(dp, mina);
maxb = p_xform.columns[1].dot(xf_points[0]);
minb = maxb;
dp = p_xform.columns[1].dot(xf_points[1]);
maxb = Math::max(dp, maxb);
minb = Math::min(dp, minb);
maxb = MAX(dp, maxb);
minb = MIN(dp, minb);
dp = p_xform.columns[1].dot(xf_points[2]);
maxb = Math::max(dp, maxb);
minb = Math::min(dp, minb);
maxb = MAX(dp, maxb);
minb = MIN(dp, minb);
dp = p_xform.columns[1].dot(xf_points[3]);
maxb = Math::max(dp, maxb);
minb = Math::min(dp, minb);
maxb = MAX(dp, maxb);
minb = MIN(dp, minb);
if (mina > maxb) {
return false;
@@ -285,7 +285,7 @@ next4:
}
Rect2::operator String() const {
return "[P: " + position.operator String() + ", S: " + size + "]";
return "[P: " + position.operator String() + ", S: " + size.operator String() + "]";
}
Rect2::operator Rect2i() const {

View File

@@ -48,7 +48,7 @@ Transform2D Transform2D::inverse() const {
}
void Transform2D::affine_invert() {
real_t det = basis_determinant();
real_t det = determinant();
#ifdef MATH_CHECKS
ERR_FAIL_COND(det == 0);
#endif
@@ -67,17 +67,17 @@ Transform2D Transform2D::affine_inverse() const {
return inv;
}
void Transform2D::rotate(const real_t p_angle) {
void Transform2D::rotate(real_t p_angle) {
*this = Transform2D(p_angle, Vector2()) * (*this);
}
real_t Transform2D::get_skew() const {
real_t det = basis_determinant();
real_t det = determinant();
return Math::acos(columns[0].normalized().dot(SIGN(det) * columns[1].normalized())) - (real_t)Math_PI * 0.5f;
}
void Transform2D::set_skew(const real_t p_angle) {
real_t det = basis_determinant();
void Transform2D::set_skew(real_t p_angle) {
real_t det = determinant();
columns[1] = SIGN(det) * columns[0].rotated(((real_t)Math_PI * 0.5f + p_angle)).normalized() * columns[1].length();
}
@@ -85,7 +85,7 @@ real_t Transform2D::get_rotation() const {
return Math::atan2(columns[0].y, columns[0].x);
}
void Transform2D::set_rotation(const real_t p_rot) {
void Transform2D::set_rotation(real_t p_rot) {
Size2 scale = get_scale();
real_t cr = Math::cos(p_rot);
real_t sr = Math::sin(p_rot);
@@ -96,7 +96,7 @@ void Transform2D::set_rotation(const real_t p_rot) {
set_scale(scale);
}
Transform2D::Transform2D(const real_t p_rot, const Vector2 &p_pos) {
Transform2D::Transform2D(real_t p_rot, const Vector2 &p_pos) {
real_t cr = Math::cos(p_rot);
real_t sr = Math::sin(p_rot);
columns[0][0] = cr;
@@ -106,7 +106,7 @@ Transform2D::Transform2D(const real_t p_rot, const Vector2 &p_pos) {
columns[2] = p_pos;
}
Transform2D::Transform2D(const real_t p_rot, const Size2 &p_scale, const real_t p_skew, const Vector2 &p_pos) {
Transform2D::Transform2D(real_t p_rot, const Size2 &p_scale, real_t p_skew, const Vector2 &p_pos) {
columns[0][0] = Math::cos(p_rot) * p_scale.x;
columns[1][1] = Math::cos(p_rot + p_skew) * p_scale.y;
columns[1][0] = -Math::sin(p_rot + p_skew) * p_scale.y;
@@ -115,7 +115,7 @@ Transform2D::Transform2D(const real_t p_rot, const Size2 &p_scale, const real_t
}
Size2 Transform2D::get_scale() const {
real_t det_sign = Math::sign(basis_determinant());
real_t det_sign = SIGN(determinant());
return Size2(columns[0].length(), det_sign * columns[1].length());
}
@@ -138,7 +138,7 @@ void Transform2D::scale_basis(const Size2 &p_scale) {
columns[1][1] *= p_scale.y;
}
void Transform2D::translate_local(const real_t p_tx, const real_t p_ty) {
void Transform2D::translate_local(real_t p_tx, real_t p_ty) {
translate_local(Vector2(p_tx, p_ty));
}
@@ -153,7 +153,7 @@ void Transform2D::orthonormalize() {
Vector2 y = columns[1];
x.normalize();
y = (y - x * (x.dot(y)));
y = y - x * x.dot(y);
y.normalize();
columns[0] = x;
@@ -161,9 +161,21 @@ void Transform2D::orthonormalize() {
}
Transform2D Transform2D::orthonormalized() const {
Transform2D on = *this;
on.orthonormalize();
return on;
Transform2D ortho = *this;
ortho.orthonormalize();
return ortho;
}
bool Transform2D::is_conformal() const {
// Non-flipped case.
if (Math::is_equal_approx(columns[0][0], columns[1][1]) && Math::is_equal_approx(columns[0][1], -columns[1][0])) {
return true;
}
// Flipped case.
if (Math::is_equal_approx(columns[0][0], -columns[1][1]) && Math::is_equal_approx(columns[0][1], columns[1][0])) {
return true;
}
return false;
}
bool Transform2D::is_equal_approx(const Transform2D &p_transform) const {
@@ -223,12 +235,6 @@ Transform2D Transform2D::operator*(const Transform2D &p_transform) const {
return t;
}
Transform2D Transform2D::basis_scaled(const Size2 &p_scale) const {
Transform2D copy = *this;
copy.scale_basis(p_scale);
return copy;
}
Transform2D Transform2D::scaled(const Size2 &p_scale) const {
// Equivalent to left multiplication
Transform2D copy = *this;
@@ -257,67 +263,52 @@ Transform2D Transform2D::translated_local(const Vector2 &p_offset) const {
return Transform2D(columns[0], columns[1], columns[2] + basis_xform(p_offset));
}
Transform2D Transform2D::rotated(const real_t p_angle) const {
Transform2D Transform2D::rotated(real_t p_angle) const {
// Equivalent to left multiplication
return Transform2D(p_angle, Vector2()) * (*this);
}
Transform2D Transform2D::rotated_local(const real_t p_angle) const {
Transform2D Transform2D::rotated_local(real_t p_angle) const {
// Equivalent to right multiplication
return (*this) * Transform2D(p_angle, Vector2()); // Could be optimized, because origin transform can be skipped.
}
real_t Transform2D::basis_determinant() const {
real_t Transform2D::determinant() const {
return columns[0].x * columns[1].y - columns[0].y * columns[1].x;
}
Transform2D Transform2D::interpolate_with(const Transform2D &p_transform, const real_t p_c) const {
//extract parameters
Vector2 p1 = get_origin();
Vector2 p2 = p_transform.get_origin();
real_t r1 = get_rotation();
real_t r2 = p_transform.get_rotation();
Size2 s1 = get_scale();
Size2 s2 = p_transform.get_scale();
//slerp rotation
Vector2 v1(Math::cos(r1), Math::sin(r1));
Vector2 v2(Math::cos(r2), Math::sin(r2));
real_t dot = v1.dot(v2);
dot = Math::clamp(dot, (real_t)-1.0, (real_t)1.0);
Vector2 v;
if (dot > 0.9995f) {
v = v1.lerp(v2, p_c).normalized(); //linearly interpolate to avoid numerical precision issues
} else {
real_t angle = p_c * Math::acos(dot);
Vector2 v3 = (v2 - v1 * dot).normalized();
v = v1 * Math::cos(angle) + v3 * Math::sin(angle);
}
//construct matrix
Transform2D res(v.angle(), p1.lerp(p2, p_c));
res.scale_basis(s1.lerp(s2, p_c));
return res;
Transform2D Transform2D::interpolate_with(const Transform2D &p_transform, real_t p_weight) const {
return Transform2D(
Math::lerp_angle(get_rotation(), p_transform.get_rotation(), p_weight),
get_scale().lerp(p_transform.get_scale(), p_weight),
Math::lerp_angle(get_skew(), p_transform.get_skew(), p_weight),
get_origin().lerp(p_transform.get_origin(), p_weight));
}
void Transform2D::operator*=(const real_t p_val) {
void Transform2D::operator*=(real_t p_val) {
columns[0] *= p_val;
columns[1] *= p_val;
columns[2] *= p_val;
}
Transform2D Transform2D::operator*(const real_t p_val) const {
Transform2D Transform2D::operator*(real_t p_val) const {
Transform2D ret(*this);
ret *= p_val;
return ret;
}
void Transform2D::operator/=(real_t p_val) {
columns[0] /= p_val;
columns[1] /= p_val;
columns[2] /= p_val;
}
Transform2D Transform2D::operator/(real_t p_val) const {
Transform2D ret(*this);
ret /= p_val;
return ret;
}
Transform2D::operator String() const {
return "[X: " + columns[0].operator String() +
", Y: " + columns[1].operator String() +

Some files were not shown because too many files have changed in this diff Show More