diff options
| author | Marc <marc.gilleron@gmail.com> | 2021-01-31 20:06:56 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-01-31 20:06:56 +0000 |
| commit | b400dba87534640eeddbcdb6b319335a6a7639d8 (patch) | |
| tree | 286a8e2e25c18fe11f346835b3021afae2e9b77f /include | |
| parent | fb71edd45b2473bf0ac502c777a1850fb564087e (diff) | |
| parent | 05ba977cc60653952b73dc03498ebc7a93cef120 (diff) | |
| download | redot-cpp-b400dba87534640eeddbcdb6b319335a6a7639d8.tar.gz | |
Merge branch 'master' into container_leaks
Diffstat (limited to 'include')
| -rw-r--r-- | include/core/Array.hpp | 6 | ||||
| -rw-r--r-- | include/core/Basis.hpp | 5 | ||||
| -rw-r--r-- | include/core/Defs.hpp | 7 | ||||
| -rw-r--r-- | include/core/Godot.hpp | 22 | ||||
| -rw-r--r-- | include/core/Math.hpp | 26 | ||||
| -rw-r--r-- | include/core/Quat.hpp | 2 | ||||
| -rw-r--r-- | include/core/Ref.hpp | 40 | ||||
| -rw-r--r-- | include/core/Transform.hpp | 5 | ||||
| -rw-r--r-- | include/core/Transform2D.hpp | 4 | ||||
| -rw-r--r-- | include/core/Vector2.hpp | 15 | ||||
| -rw-r--r-- | include/core/Vector3.hpp | 13 |
11 files changed, 92 insertions, 53 deletions
diff --git a/include/core/Array.hpp b/include/core/Array.hpp index 84dc012..5913fae 100644 --- a/include/core/Array.hpp +++ b/include/core/Array.hpp @@ -106,9 +106,9 @@ public: Variant back() const; - int find(const Variant &what, const int from = 0); + int find(const Variant &what, const int from = 0) const; - int find_last(const Variant &what); + int find_last(const Variant &what) const; bool has(const Variant &what) const; @@ -134,7 +134,7 @@ public: void resize(const int size); - int rfind(const Variant &what, const int from = -1); + int rfind(const Variant &what, const int from = -1) const; void sort(); diff --git a/include/core/Basis.hpp b/include/core/Basis.hpp index 76a345b..0fd697e 100644 --- a/include/core/Basis.hpp +++ b/include/core/Basis.hpp @@ -13,6 +13,11 @@ class Quat; class Basis { private: + static const Basis IDENTITY; + static const Basis FLIP_X; + static const Basis FLIP_Y; + static const Basis FLIP_Z; + // This helper template is for mimicking the behavior difference between the engine // and script interfaces that logically script sees matrices as column major, while // the engine stores them in row major to efficiently take advantage of SIMD diff --git a/include/core/Defs.hpp b/include/core/Defs.hpp index 67e3279..e90f6ce 100644 --- a/include/core/Defs.hpp +++ b/include/core/Defs.hpp @@ -70,9 +70,12 @@ enum class Error { typedef float real_t; -#define CMP_EPSILON 0.00001 +// 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) -#define Math_PI 3.14159265358979323846 + +#define Math_PI 3.1415926535897932384626433833 #define Math_TAU 6.2831853071795864769252867666 #define _PLANE_EQ_DOT_EPSILON 0.999 diff --git a/include/core/Godot.hpp b/include/core/Godot.hpp index 322a763..db5a871 100644 --- a/include/core/Godot.hpp +++ b/include/core/Godot.hpp @@ -149,7 +149,7 @@ struct _ArgCast<Variant> { // instance and destroy funcs template <class T> -void *_godot_class_instance_func(godot_object *p, void *method_data) { +void *_godot_class_instance_func(godot_object *p, void * /*method_data*/) { T *d = new T(); d->_owner = p; d->_type_tag = typeid(T).hash_code(); @@ -158,7 +158,7 @@ void *_godot_class_instance_func(godot_object *p, void *method_data) { } template <class T> -void _godot_class_destroy_func(godot_object *p, void *method_data, void *data) { +void _godot_class_destroy_func(godot_object * /*p*/, void * /*method_data*/, void *data) { T *d = (T *)data; delete d; } @@ -210,13 +210,13 @@ void register_tool_class() { typedef godot_variant (*__godot_wrapper_method)(godot_object *, void *, void *, int, godot_variant **); template <class T, class R, class... args> -const char *___get_method_class_name(R (T::*p)(args... a)) { +const char *___get_method_class_name(R (T::*/*p*/)(args... a)) { static_assert(T::___CLASS_IS_SCRIPT, "This function must only be used on custom classes"); return T::___get_class_name(); } template <class T, class R, class... args> -const char *___get_method_class_name(R (T::*p)(args... a) const) { +const char *___get_method_class_name(R (T::*/*p*/)(args... a) const) { static_assert(T::___CLASS_IS_SCRIPT, "This function must only be used on custom classes"); return T::___get_class_name(); } @@ -256,13 +256,13 @@ struct _WrappedMethod<T, void, As...> { void (T::*f)(As...); template <int... I> - void apply(Variant *ret, T *obj, Variant **args, __Sequence<I...>) { + void apply(Variant * /*ret*/, T *obj, Variant **args, __Sequence<I...>) { (obj->*f)(_ArgCast<As>::_arg_cast(*args[I])...); } }; template <class T, class R, class... As> -godot_variant __wrapped_method(godot_object *, void *method_data, void *user_data, int num_args, godot_variant **args) { +godot_variant __wrapped_method(godot_object *, void *method_data, void *user_data, int /*num_args*/, godot_variant **args) { godot_variant v; godot::api->godot_variant_new_nil(&v); @@ -286,7 +286,7 @@ void *___make_wrapper_function(R (T::*f)(As...)) { } template <class T, class R, class... As> -__godot_wrapper_method ___get_wrapper_function(R (T::*f)(As...)) { +__godot_wrapper_method ___get_wrapper_function(R (T::* /*f*/)(As...)) { return (__godot_wrapper_method)&__wrapped_method<T, R, As...>; } @@ -326,7 +326,7 @@ void register_method_explicit(const char *name, R (B::*method_ptr)(As...), template <class T, class P> struct _PropertySetFunc { void (T::*f)(P); - static void _wrapped_setter(godot_object *object, void *method_data, void *user_data, godot_variant *value) { + static void _wrapped_setter(godot_object * /*object*/, void *method_data, void *user_data, godot_variant *value) { _PropertySetFunc<T, P> *set_func = (_PropertySetFunc<T, P> *)method_data; T *obj = (T *)user_data; @@ -340,7 +340,7 @@ template <class T, class P> struct _PropertyGetFunc { P(T::*f) (); - static godot_variant _wrapped_getter(godot_object *object, void *method_data, void *user_data) { + static godot_variant _wrapped_getter(godot_object * /*object*/, void *method_data, void *user_data) { _PropertyGetFunc<T, P> *get_func = (_PropertyGetFunc<T, P> *)method_data; T *obj = (T *)user_data; @@ -358,7 +358,7 @@ struct _PropertyGetFunc { template <class T, class P> struct _PropertyDefaultSetFunc { P(T::*f); - static void _wrapped_setter(godot_object *object, void *method_data, void *user_data, godot_variant *value) { + static void _wrapped_setter(godot_object * /*object*/, void *method_data, void *user_data, godot_variant *value) { _PropertyDefaultSetFunc<T, P> *set_func = (_PropertyDefaultSetFunc<T, P> *)method_data; T *obj = (T *)user_data; @@ -371,7 +371,7 @@ struct _PropertyDefaultSetFunc { template <class T, class P> struct _PropertyDefaultGetFunc { P(T::*f); - static godot_variant _wrapped_getter(godot_object *object, void *method_data, void *user_data) { + static godot_variant _wrapped_getter(godot_object * /*object*/, void *method_data, void *user_data) { _PropertyDefaultGetFunc<T, P> *get_func = (_PropertyDefaultGetFunc<T, P> *)method_data; T *obj = (T *)user_data; diff --git a/include/core/Math.hpp b/include/core/Math.hpp index 4d0d32e..a971bb8 100644 --- a/include/core/Math.hpp +++ b/include/core/Math.hpp @@ -107,21 +107,21 @@ inline T max(T a, T b) { template <typename T>
inline T sign(T x) {
- return x < 0 ? -1 : 1;
+ return static_cast<T>(x < 0 ? -1 : 1);
}
inline double deg2rad(double p_y) {
return p_y * Math_PI / 180.0;
}
inline float deg2rad(float p_y) {
- return p_y * Math_PI / 180.0;
+ return p_y * static_cast<float>(Math_PI) / 180.f;
}
inline double rad2deg(double p_y) {
return p_y * 180.0 / Math_PI;
}
inline float rad2deg(float p_y) {
- return p_y * 180.0 / Math_PI;
+ return p_y * 180.f / static_cast<float>(Math_PI);
}
inline double inverse_lerp(double p_from, double p_to, double p_value) {
@@ -165,7 +165,7 @@ inline bool is_zero_approx(real_t s) { }
inline double smoothstep(double p_from, double p_to, double p_weight) {
- if (is_equal_approx(p_from, p_to)) {
+ if (is_equal_approx(static_cast<real_t>(p_from), static_cast<real_t>(p_to))) {
return p_from;
}
double x = clamp((p_weight - p_from) / (p_to - p_from), 0.0, 1.0);
@@ -205,7 +205,7 @@ inline double round(double p_val) { return (p_val >= 0) ? floor(p_val + 0.5) : -floor(-p_val + 0.5);
}
inline float round(float p_val) {
- return (p_val >= 0) ? floor(p_val + 0.5) : -floor(-p_val + 0.5);
+ return (p_val >= 0) ? floor(p_val + 0.5f) : -floor(-p_val + 0.5f);
}
inline int64_t wrapi(int64_t value, int64_t min, int64_t max) {
@@ -213,16 +213,18 @@ inline int64_t wrapi(int64_t value, int64_t min, int64_t max) { return range == 0 ? min : min + ((((value - min) % range) + range) % range);
}
-inline double wrapf(double value, double min, double max) {
- double range = max - min;
- return is_zero_approx(range) ? min : value - (range * floor((value - min) / range));
-}
-inline float wrapf(float value, float min, float max) {
- float range = max - min;
+inline float wrapf(real_t value, real_t min, real_t max) {
+ const real_t range = max - min;
return is_zero_approx(range) ? min : value - (range * floor((value - min) / range));
}
-inline real_t stepify(real_t p_value, real_t p_step) {
+inline float stepify(float p_value, float p_step) {
+ if (p_step != 0) {
+ p_value = floor(p_value / p_step + 0.5f) * p_step;
+ }
+ return p_value;
+}
+inline double stepify(double p_value, double p_step) {
if (p_step != 0) {
p_value = floor(p_value / p_step + 0.5) * p_step;
}
diff --git a/include/core/Quat.hpp b/include/core/Quat.hpp index f047178..4add6be 100644 --- a/include/core/Quat.hpp +++ b/include/core/Quat.hpp @@ -11,6 +11,8 @@ namespace godot { class Quat { public: + static const Quat IDENTITY; + real_t x, y, z, w; real_t length_squared() const; diff --git a/include/core/Ref.hpp b/include/core/Ref.hpp index 8cbd134..80133a1 100644 --- a/include/core/Ref.hpp +++ b/include/core/Ref.hpp @@ -11,6 +11,10 @@ namespace godot { // Rewritten from f5234e70be7dec4930c2d5a0e829ff480d044b1d. template <class T> class Ref { + // TODO For this nice check to work, each class must actually #include Reference classes mentionned in its methods, + // which might be annoying for coders who prefer to forward-declare to reduce compile times + // static_assert(std::is_base_of<Reference, T>::value, + // "Ref<T> can only be used with classes deriving from Reference"); T *reference = nullptr; @@ -28,7 +32,7 @@ class Ref { void ref_pointer(T *p_ref) { - ERR_FAIL_COND(!p_ref); + ERR_FAIL_COND(p_ref == nullptr); if (p_ref->init_ref()) reference = p_ref; @@ -90,32 +94,25 @@ public: template <class T_Other> void operator=(const Ref<T_Other> &p_from) { - - // TODO We need a safe cast Reference *refb = const_cast<Reference *>(static_cast<const Reference *>(p_from.ptr())); - if (!refb) { + if (refb == nullptr) { unref(); return; } Ref r; - //r.reference = Object::cast_to<T>(refb); - r.reference = (T *)refb; + r.reference = Object::cast_to<T>(refb); ref(r); r.reference = nullptr; } void operator=(const Variant &p_variant) { - - // TODO We need a safe cast - Reference *refb = (Reference *)T::___get_from_variant(p_variant); - if (!refb) { + Object *refb = T::___get_from_variant(p_variant); + if (refb == nullptr) { unref(); return; } Ref r; - // TODO We need a safe cast - //r.reference = Object::cast_to<T>(refb); - r.reference = (T *)refb; + r.reference = Object::cast_to<T>(refb); ref(r); r.reference = nullptr; } @@ -128,18 +125,14 @@ public: template <class T_Other> Ref(const Ref<T_Other> &p_from) { - reference = nullptr; - // TODO We need a safe cast Reference *refb = const_cast<Reference *>(static_cast<const Reference *>(p_from.ptr())); - if (!refb) { + if (refb == nullptr) { unref(); return; } Ref r; - // TODO We need a safe cast - //r.reference = Object::cast_to<T>(refb); - r.reference = (T *)refb; + r.reference = Object::cast_to<T>(refb); ref(r); r.reference = nullptr; } @@ -155,16 +148,13 @@ public: Ref(const Variant &p_variant) { reference = nullptr; - // TODO We need a safe cast - Reference *refb = (Reference *)T::___get_from_variant(p_variant); - if (!refb) { + Object *refb = T::___get_from_variant(p_variant); + if (refb == nullptr) { unref(); return; } Ref r; - // TODO We need a safe cast - //r.reference = Object::cast_to<T>(refb); - r.reference = (T *)refb; + r.reference = Object::cast_to<T>(refb); ref(r); r.reference = nullptr; } diff --git a/include/core/Transform.hpp b/include/core/Transform.hpp index 9da6366..836792c 100644 --- a/include/core/Transform.hpp +++ b/include/core/Transform.hpp @@ -10,6 +10,11 @@ namespace godot { class Transform { public: + static const Transform IDENTITY; + static const Transform FLIP_X; + static const Transform FLIP_Y; + static const Transform FLIP_Z; + Basis basis; Vector3 origin; diff --git a/include/core/Transform2D.hpp b/include/core/Transform2D.hpp index ae01b7f..6a5010f 100644 --- a/include/core/Transform2D.hpp +++ b/include/core/Transform2D.hpp @@ -10,6 +10,10 @@ typedef Vector2 Size2; struct Rect2; struct Transform2D { + static const Transform2D IDENTITY; + static const Transform2D FLIP_X; + static const Transform2D FLIP_Y; + // Warning #1: basis of Transform2D is stored differently from Basis. In terms of elements array, the basis matrix looks like "on paper": // M = (elements[0][0] elements[1][0]) // (elements[0][1] elements[1][1]) diff --git a/include/core/Vector2.hpp b/include/core/Vector2.hpp index 031d47f..3c2a403 100644 --- a/include/core/Vector2.hpp +++ b/include/core/Vector2.hpp @@ -12,6 +12,21 @@ namespace godot { class String; struct Vector2 { + enum Axis { + AXIS_X = 0, + AXIS_Y, + AXIS_COUNT + }; + + static const Vector2 ZERO; + static const Vector2 ONE; + static const Vector2 INF; + + // Coordinate system of the 2D engine + static const Vector2 LEFT; + static const Vector2 RIGHT; + static const Vector2 UP; + static const Vector2 DOWN; union { real_t x; diff --git a/include/core/Vector3.hpp b/include/core/Vector3.hpp index 5971787..cbd4f75 100644 --- a/include/core/Vector3.hpp +++ b/include/core/Vector3.hpp @@ -19,8 +19,21 @@ struct Vector3 { AXIS_X, AXIS_Y, AXIS_Z, + AXIS_COUNT }; + static const Vector3 ZERO; + static const Vector3 ONE; + static const Vector3 INF; + + // Coordinate system of the 3D engine + static const Vector3 LEFT; + static const Vector3 RIGHT; + static const Vector3 UP; + static const Vector3 DOWN; + static const Vector3 FORWARD; + static const Vector3 BACK; + union { struct { real_t x; |
