Skip to content

Commit

Permalink
Use forward declares for vector math types
Browse files Browse the repository at this point in the history
Adds operators to convert from int vector types to float vector types
as done in the upstream engine implementations.
  • Loading branch information
akien-mga committed Feb 20, 2022
1 parent 917b0c2 commit 7e3321d
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 37 deletions.
3 changes: 3 additions & 0 deletions include/godot_cpp/variant/rect2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@

namespace godot {

class Rect2i;
class String;
class Transform2D;

class Rect2 {
Expand Down Expand Up @@ -326,6 +328,7 @@ class Rect2 {
}

operator String() const;
operator Rect2i() const;

Rect2() {}
Rect2(real_t p_x, real_t p_y, real_t p_width, real_t p_height) :
Expand Down
15 changes: 7 additions & 8 deletions include/godot_cpp/variant/rect2i.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,15 @@
#ifndef GODOT_RECT2I_HPP
#define GODOT_RECT2I_HPP

#include <godot_cpp/variant/rect2.hpp>
#include <godot_cpp/classes/global_constants.hpp>
#include <godot_cpp/core/math.hpp>
#include <godot_cpp/variant/vector2i.hpp>

namespace godot {

class Rect2;
class String;

class Rect2i {
_FORCE_INLINE_ GDNativeTypePtr _native_ptr() const { return (void *)this; }

Expand Down Expand Up @@ -206,15 +210,10 @@ class Rect2i {
return position + size;
}

operator String() const { return String(position) + ", " + String(size); }

operator Rect2() const { return Rect2(position, size); }
operator String() const;
operator Rect2() const;

Rect2i() {}
Rect2i(const Rect2 &p_r2) :
position(p_r2.position),
size(p_r2.size) {
}
Rect2i(int p_x, int p_y, int p_width, int p_height) :
position(Point2i(p_x, p_y)),
size(Size2i(p_width, p_height)) {
Expand Down
2 changes: 1 addition & 1 deletion include/godot_cpp/variant/vector2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@
#define GODOT_VECTOR2_HPP

#include <godot_cpp/core/math.hpp>
#include <godot_cpp/variant/string.hpp>

namespace godot {

class String;
class Vector2i;

class Vector2 {
Expand Down
12 changes: 4 additions & 8 deletions include/godot_cpp/variant/vector2i.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@
#define GODOT_VECTOR2I_HPP

#include <godot_cpp/core/math.hpp>
#include <godot_cpp/variant/string.hpp>
#include <godot_cpp/variant/vector2.hpp>

namespace godot {

class String;
class Vector2;

class Vector2i {
_FORCE_INLINE_ GDNativeTypePtr _native_ptr() const { return (void *)this; }

Expand Down Expand Up @@ -96,14 +97,9 @@ class Vector2i {
Vector2i abs() const { return Vector2i(Math::abs(x), Math::abs(y)); }

operator String() const;

operator Vector2() const { return Vector2((real_t)x, (real_t)y); }
operator Vector2() const;

inline Vector2i() {}
inline Vector2i(const Vector2 &p_vec2) {
x = (int32_t)p_vec2.x;
y = (int32_t)p_vec2.y;
}
inline Vector2i(int32_t p_x, int32_t p_y) {
x = p_x;
y = p_y;
Expand Down
3 changes: 1 addition & 2 deletions include/godot_cpp/variant/vector3.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@
#define GODOT_VECTOR3_HPP

#include <godot_cpp/core/math.hpp>
#include <godot_cpp/variant/string.hpp>

namespace godot {

class Basis;
class String;
class Vector3i;

class Vector3 {
Expand Down Expand Up @@ -159,7 +159,6 @@ class Vector3 {
y = p_y;
z = p_z;
}
Vector3(const Vector3i &p_ivec);
};

Vector3 Vector3::cross(const Vector3 &p_b) const {
Expand Down
5 changes: 4 additions & 1 deletion include/godot_cpp/variant/vector3i.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@
#define GODOT_VECTOR3I_HPP

#include <godot_cpp/core/math.hpp>
#include <godot_cpp/variant/string.hpp>

namespace godot {

class String;
class Vector3;

class Vector3i {
_FORCE_INLINE_ GDNativeTypePtr _native_ptr() const { return (void *)this; }

Expand Down Expand Up @@ -107,6 +109,7 @@ class Vector3i {
inline bool operator>=(const Vector3i &p_v) const;

operator String() const;
operator Vector3() const;

inline Vector3i() {}
inline Vector3i(int32_t p_x, int32_t p_y, int32_t p_z) {
Expand Down
10 changes: 10 additions & 0 deletions src/variant/rect2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@

#include <godot_cpp/variant/rect2.hpp>

#include <godot_cpp/variant/rect2i.hpp>
#include <godot_cpp/variant/string.hpp>
#include <godot_cpp/variant/transform2d.hpp>

namespace godot {
Expand Down Expand Up @@ -268,4 +270,12 @@ bool Rect2::intersects_transformed(const Transform2D &p_xform, const Rect2 &p_re
return true;
}

Rect2::operator String() const {
return String(position) + ", " + String(size);
}

Rect2::operator Rect2i() const {
return Rect2i(position, size);
}

} // namespace godot
15 changes: 14 additions & 1 deletion src/variant/rect2i.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,17 @@

#include <godot_cpp/variant/rect2i.hpp>

// No implementation left. This is here to add the header as a compiled unit.
#include <godot_cpp/variant/rect2.hpp>
#include <godot_cpp/variant/string.hpp>

namespace godot {

Rect2i::operator String() const {
return String(position) + ", " + String(size);
}

Rect2i::operator Rect2() const {
return Rect2(position, size);
}

} // namespace godot
19 changes: 10 additions & 9 deletions src/variant/vector2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,14 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/

#include <godot_cpp/variant/vector2.hpp>

#include <godot_cpp/core/error_macros.hpp>
#include <godot_cpp/variant/string.hpp>
#include <godot_cpp/variant/vector2.hpp>
#include <godot_cpp/variant/vector2i.hpp>

namespace godot {

Vector2::operator String() const {
return String::num(x, 5) + ", " + String::num(y, 5);
}

Vector2::operator Vector2i() const {
return Vector2i(x, y);
}

real_t Vector2::angle() const {
return Math::atan2(y, x);
}
Expand Down Expand Up @@ -200,4 +193,12 @@ bool Vector2::is_equal_approx(const Vector2 &p_v) const {
return Math::is_equal_approx(x, p_v.x) && Math::is_equal_approx(y, p_v.y);
}

Vector2::operator String() const {
return String::num(x, 5) + ", " + String::num(y, 5);
}

Vector2::operator Vector2i() const {
return Vector2i(x, y);
}

} // namespace godot
16 changes: 11 additions & 5 deletions src/variant/vector2i.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,14 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/

#include <godot_cpp/variant/vector2i.hpp>

#include <godot_cpp/core/error_macros.hpp>
#include <godot_cpp/variant/string.hpp>
#include <godot_cpp/variant/vector2i.hpp>
#include <godot_cpp/variant/vector2.hpp>

namespace godot {

Vector2i::operator String() const {
return String::num(x, 0) + ", " + String::num(y, 0);
}

Vector2i Vector2i::operator+(const Vector2i &p_v) const {
return Vector2i(x + p_v.x, y + p_v.y);
}
Expand Down Expand Up @@ -107,4 +105,12 @@ bool Vector2i::operator!=(const Vector2i &p_vec2) const {
return x != p_vec2.x || y != p_vec2.y;
}

Vector2i::operator String() const {
return String::num(x, 0) + ", " + String::num(y, 0);
}

Vector2i::operator Vector2() const {
return Vector2((real_t)x, (real_t)y);
}

} // namespace godot
3 changes: 2 additions & 1 deletion src/variant/vector3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/

#include <godot_cpp/variant/vector3.hpp>

#include <godot_cpp/core/error_macros.hpp>
#include <godot_cpp/variant/basis.hpp>
#include <godot_cpp/variant/vector3.hpp>
#include <godot_cpp/variant/vector3i.hpp>

namespace godot {
Expand Down
8 changes: 7 additions & 1 deletion src/variant/vector3i.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/

#include <godot_cpp/variant/vector3i.hpp>

#include <godot_cpp/core/error_macros.hpp>
#include <godot_cpp/variant/string.hpp>
#include <godot_cpp/variant/vector3i.hpp>
#include <godot_cpp/variant/vector3.hpp>

namespace godot {

Expand All @@ -56,4 +58,8 @@ Vector3i::operator String() const {
return (String::num(x, 0) + ", " + String::num(y, 0) + ", " + String::num(z, 5));
}

Vector3i::operator Vector3() const {
return Vector3((real_t)x, (real_t)y, (real_t)z);
}

} // namespace godot

0 comments on commit 7e3321d

Please sign in to comment.