-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Raymath] Add C++ operator overloads for common math function #4385
Merged
Merged
Changes from 20 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
c35e253
Update raylib_api.* by CI
github-actions[bot] 49a7a64
Merge branch 'master' of github.com:JeffM2501/raylib
JeffM2501 a126ff8
Merge branch 'master' of github.com:raysan5/raylib
JeffM2501 841e03d
Merge branch 'master' of github.com:raysan5/raylib
JeffM2501 ee3f5ec
Merge branch 'master' of github.com:raysan5/raylib
JeffM2501 661004a
Merge branch 'master' of github.com:raysan5/raylib
JeffM2501 02da1bb
Merge branch 'master' of github.com:raysan5/raylib
JeffM2501 81d145f
Merge branch 'master' of github.com:raysan5/raylib
JeffM2501 6060519
Merge branch 'master' of github.com:raysan5/raylib
JeffM2501 8235c10
Merge branch 'master' of github.com:raysan5/raylib
JeffM2501 ce7f191
Merge branch 'master' of github.com:JeffM2501/raylib
JeffM2501 5a8acfa
Merge branch 'master' of github.com:raysan5/raylib
JeffM2501 95d0826
Merge branch 'master' of github.com:raysan5/raylib
JeffM2501 7229666
Merge branch 'master' of github.com:raysan5/raylib
JeffM2501 c8d907e
Merge branch 'master' of github.com:raysan5/raylib
JeffM2501 cc6a9bd
Merge branch 'master' of github.com:JeffM2501/raylib
JeffM2501 f59f208
Merge branch 'master' of github.com:raysan5/raylib
JeffM2501 8c45d89
Merge branch 'master' of github.com:JeffM2501/raylib
JeffM2501 0ba02eb
Merge branch 'master' of github.com:raysan5/raylib
JeffM2501 c1c9851
Add math operators for C++ to raymath
JeffM2501 6d60173
better #define for disabling C++ operators
JeffM2501 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2580,4 +2580,319 @@ RMAPI void MatrixDecompose(Matrix mat, Vector3 *translation, Quaternion *rotatio | |
} | ||
} | ||
|
||
|
||
// optional C++ math operators | ||
// define RAYLIB_DISABLE_VECTOR_OPERATORS to disable | ||
#if defined(__cplusplus) && !defined(RAYLIB_DISABLE_VECTOR_OPERATORS) | ||
|
||
//------------------Vector2-----------------// | ||
static constexpr Vector2 Vector2Zeros = { 0, 0 }; | ||
static constexpr Vector2 Vector2Ones = { 1, 1 }; | ||
static constexpr Vector2 Vector2UnitX = { 1, 0 }; | ||
static constexpr Vector2 Vector2UnitY = { 0, 1 }; | ||
|
||
inline Vector2 operator + (const Vector2& lhs, const Vector2& rhs) | ||
{ | ||
return Vector2Add(lhs, rhs); | ||
} | ||
|
||
inline const Vector2& operator += (Vector2& lhs, const Vector2& rhs) | ||
{ | ||
lhs = Vector2Add(lhs, rhs); | ||
return lhs; | ||
} | ||
|
||
inline Vector2 operator - (const Vector2& lhs, const Vector2& rhs) | ||
{ | ||
return Vector2Subtract(lhs, rhs); | ||
} | ||
|
||
inline const Vector2& operator -= (Vector2& lhs, const Vector2& rhs) | ||
{ | ||
lhs = Vector2Subtract(lhs, rhs); | ||
return lhs; | ||
} | ||
|
||
inline Vector2 operator * (const Vector2& lhs, const float& rhs) | ||
{ | ||
return Vector2Scale(lhs, rhs); | ||
} | ||
|
||
inline const Vector2& operator *= (Vector2& lhs, const float& rhs) | ||
{ | ||
lhs = Vector2Scale(lhs, rhs); | ||
return lhs; | ||
} | ||
|
||
inline Vector2 operator * (const Vector2& lhs, const Vector2& rhs) | ||
{ | ||
return Vector2Multiply(lhs, rhs); | ||
} | ||
|
||
inline const Vector2& operator *= (Vector2& lhs, const Vector2& rhs) | ||
{ | ||
lhs = Vector2Multiply(lhs, rhs); | ||
return lhs; | ||
} | ||
|
||
inline Vector2 operator * (const Vector2& lhs, const Matrix& rhs) | ||
{ | ||
return Vector2Transform(lhs, rhs); | ||
} | ||
|
||
inline const Vector2& operator -= (Vector2& lhs, const Matrix& rhs) | ||
{ | ||
lhs = Vector2Transform(lhs, rhs); | ||
return lhs; | ||
} | ||
|
||
inline Vector2 operator / (const Vector2& lhs, const float& rhs) | ||
{ | ||
return Vector2Scale(lhs, 1.0f / rhs); | ||
} | ||
|
||
inline const Vector2& operator /= (Vector2& lhs, const float& rhs) | ||
{ | ||
lhs = Vector2Scale(lhs, rhs); | ||
return lhs; | ||
} | ||
|
||
inline Vector2 operator / (const Vector2& lhs, const Vector2& rhs) | ||
{ | ||
return Vector2Divide(lhs, rhs); | ||
} | ||
|
||
inline const Vector2& operator /= (Vector2& lhs, const Vector2& rhs) | ||
{ | ||
lhs = Vector2Divide(lhs, rhs); | ||
return lhs; | ||
} | ||
|
||
inline bool operator == (const Vector2& lhs, const Vector2& rhs) | ||
{ | ||
return FloatEquals(lhs.x, rhs.x) && FloatEquals(lhs.y, rhs.y); | ||
} | ||
|
||
inline bool operator != (const Vector2& lhs, const Vector2& rhs) | ||
{ | ||
return !FloatEquals(lhs.x, rhs.x) || !FloatEquals(lhs.y, rhs.y); | ||
} | ||
|
||
//------------------Vector3-----------------// | ||
static constexpr Vector3 Vector3Zeros = { 0, 0, 0 }; | ||
static constexpr Vector3 Vector3Ones = { 1, 1, 1 }; | ||
static constexpr Vector3 Vector3UnitX = { 1, 0, 0 }; | ||
static constexpr Vector3 Vector3UnitY = { 0, 1, 0 }; | ||
static constexpr Vector3 Vector3UnitZ = { 0, 0, 1 }; | ||
|
||
inline Vector3 operator + (const Vector3& lhs, const Vector3& rhs) | ||
{ | ||
return Vector3Add(lhs, rhs); | ||
} | ||
|
||
inline const Vector3& operator += (Vector3& lhs, const Vector3& rhs) | ||
{ | ||
lhs = Vector3Add(lhs, rhs); | ||
return lhs; | ||
} | ||
|
||
inline Vector3 operator - (const Vector3& lhs, const Vector3& rhs) | ||
{ | ||
return Vector3Subtract(lhs, rhs); | ||
} | ||
|
||
inline const Vector3& operator -= (Vector3& lhs, const Vector3& rhs) | ||
{ | ||
lhs = Vector3Subtract(lhs, rhs); | ||
return lhs; | ||
} | ||
|
||
inline Vector3 operator * (const Vector3& lhs, const float& rhs) | ||
{ | ||
return Vector3Scale(lhs, rhs); | ||
} | ||
|
||
inline const Vector3& operator *= (Vector3& lhs, const float& rhs) | ||
{ | ||
lhs = Vector3Scale(lhs, rhs); | ||
return lhs; | ||
} | ||
|
||
inline Vector3 operator * (const Vector3& lhs, const Vector3& rhs) | ||
{ | ||
return Vector3Multiply(lhs, rhs); | ||
} | ||
|
||
inline const Vector3& operator *= (Vector3& lhs, const Vector3& rhs) | ||
{ | ||
lhs = Vector3Multiply(lhs, rhs); | ||
return lhs; | ||
} | ||
|
||
inline Vector3 operator * (const Vector3& lhs, const Matrix& rhs) | ||
{ | ||
return Vector3Transform(lhs, rhs); | ||
} | ||
|
||
inline const Vector3& operator -= (Vector3& lhs, const Matrix& rhs) | ||
{ | ||
lhs = Vector3Transform(lhs, rhs); | ||
return lhs; | ||
} | ||
|
||
inline Vector3 operator / (const Vector3& lhs, const float& rhs) | ||
{ | ||
return Vector3Scale(lhs, 1.0f / rhs); | ||
} | ||
|
||
inline const Vector3& operator /= (Vector3& lhs, const float& rhs) | ||
{ | ||
lhs = Vector3Scale(lhs, rhs); | ||
return lhs; | ||
} | ||
|
||
inline Vector3 operator / (const Vector3& lhs, const Vector3& rhs) | ||
{ | ||
return Vector3Divide(lhs, rhs); | ||
} | ||
|
||
inline const Vector3& operator /= (Vector3& lhs, const Vector3& rhs) | ||
{ | ||
lhs = Vector3Divide(lhs, rhs); | ||
return lhs; | ||
} | ||
|
||
inline bool operator == (const Vector3& lhs, const Vector3& rhs) | ||
{ | ||
return FloatEquals(lhs.x, rhs.x) && FloatEquals(lhs.y, rhs.y) && FloatEquals(lhs.z, rhs.z); | ||
} | ||
|
||
inline bool operator != (const Vector3& lhs, const Vector3& rhs) | ||
{ | ||
return !FloatEquals(lhs.x, rhs.x) || !FloatEquals(lhs.y, rhs.y) || !FloatEquals(lhs.z, rhs.z); | ||
} | ||
|
||
//------------------Vector4-----------------// | ||
static constexpr Vector4 Vector4Zeros = { 0, 0, 0, 0 }; | ||
static constexpr Vector4 Vector4Ones = { 1, 1, 1, 1 }; | ||
static constexpr Vector4 Vector4UnitX = { 1, 0, 0, 0 }; | ||
static constexpr Vector4 Vector4UnitY = { 0, 1, 0, 0 }; | ||
static constexpr Vector4 Vector4UnitZ = { 0, 0, 1, 0 }; | ||
static constexpr Vector4 Vector4UnitW = { 0, 0, 0, 1 }; | ||
|
||
inline Vector4 operator + (const Vector4& lhs, const Vector4& rhs) | ||
{ | ||
return Vector4Add(lhs, rhs); | ||
} | ||
|
||
inline const Vector4& operator += (Vector4& lhs, const Vector4& rhs) | ||
{ | ||
lhs = Vector4Add(lhs, rhs); | ||
return lhs; | ||
} | ||
|
||
inline Vector4 operator - (const Vector4& lhs, const Vector4& rhs) | ||
{ | ||
return Vector4Subtract(lhs, rhs); | ||
} | ||
|
||
inline const Vector4& operator -= (Vector4& lhs, const Vector4& rhs) | ||
{ | ||
lhs = Vector4Subtract(lhs, rhs); | ||
return lhs; | ||
} | ||
|
||
inline Vector4 operator * (const Vector4& lhs, const float& rhs) | ||
{ | ||
return Vector4Scale(lhs, rhs); | ||
} | ||
|
||
inline const Vector4& operator *= (Vector4& lhs, const float& rhs) | ||
{ | ||
lhs = Vector4Scale(lhs, rhs); | ||
return lhs; | ||
} | ||
|
||
inline Vector4 operator * (const Vector4& lhs, const Vector4& rhs) | ||
{ | ||
return Vector4Multiply(lhs, rhs); | ||
} | ||
|
||
inline const Vector4& operator *= (Vector4& lhs, const Vector4& rhs) | ||
{ | ||
lhs = Vector4Multiply(lhs, rhs); | ||
return lhs; | ||
} | ||
|
||
inline Vector4 operator / (const Vector4& lhs, const float& rhs) | ||
{ | ||
return Vector4Scale(lhs, 1.0f / rhs); | ||
} | ||
|
||
inline const Vector4& operator /= (Vector4& lhs, const float& rhs) | ||
{ | ||
lhs = Vector4Scale(lhs, rhs); | ||
return lhs; | ||
} | ||
|
||
inline Vector4 operator / (const Vector4& lhs, const Vector4& rhs) | ||
{ | ||
return Vector4Divide(lhs, rhs); | ||
} | ||
|
||
inline const Vector4& operator /= (Vector4& lhs, const Vector4& rhs) | ||
{ | ||
lhs = Vector4Divide(lhs, rhs); | ||
return lhs; | ||
} | ||
|
||
inline bool operator == (const Vector4& lhs, const Vector4& rhs) | ||
{ | ||
return FloatEquals(lhs.x, rhs.x) && FloatEquals(lhs.y, rhs.y) && FloatEquals(lhs.z, rhs.z) && FloatEquals(lhs.w, rhs.w); | ||
} | ||
|
||
inline bool operator != (const Vector4& lhs, const Vector4& rhs) | ||
{ | ||
return !FloatEquals(lhs.x, rhs.x) || !FloatEquals(lhs.y, rhs.y) || !FloatEquals(lhs.z, rhs.z) || !FloatEquals(lhs.w, rhs.w); | ||
} | ||
|
||
//------------------Quaternion-----------------// | ||
static constexpr Quaternion QuaternionZeros = { 0, 0, 0, 0 }; | ||
static constexpr Quaternion QuaternionOnes = { 1, 1, 1, 1 }; | ||
static constexpr Quaternion QuaternionUnitX = { 0, 0, 0, 1 }; | ||
|
||
inline Quaternion operator + (const Quaternion& lhs, const float& rhs) | ||
{ | ||
return QuaternionAddValue(lhs, rhs); | ||
} | ||
|
||
inline const Quaternion& operator += (Quaternion& lhs, const float& rhs) | ||
{ | ||
lhs = QuaternionAddValue(lhs, rhs); | ||
return lhs; | ||
} | ||
|
||
inline Quaternion operator - (const Quaternion& lhs, const float& rhs) | ||
{ | ||
return QuaternionSubtractValue(lhs, rhs); | ||
} | ||
|
||
inline const Quaternion& operator -= (Quaternion& lhs, const float& rhs) | ||
{ | ||
lhs = QuaternionSubtractValue(lhs, rhs); | ||
return lhs; | ||
} | ||
|
||
inline Quaternion operator * (const Quaternion& lhs, const Matrix& rhs) | ||
{ | ||
return QuaternionTransform(lhs, rhs); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please, could you replace TABs by 4 spaces? |
||
} | ||
|
||
inline const Quaternion& operator *= (Quaternion& lhs, const Matrix& rhs) | ||
{ | ||
lhs = QuaternionTransform(lhs, rhs); | ||
return lhs; | ||
} | ||
#endif // C++ operators | ||
|
||
#endif // RAYMATH_H |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The operators aren't just for vectors, and raymath can be used separately from raylib.
Probably this should just be named RAYMATH_DISABLE_OPERATORS or RAYMATH_NO_OPERATORS.