From 0f773c2d72a4666e2ecf7235201f035773775f71 Mon Sep 17 00:00:00 2001 From: BastiaanOlij Date: Thu, 23 Nov 2017 22:50:05 +1100 Subject: Update bindings to use new Api extensions and rename Rect3->AABB --- binding_generator.py | 5 +- include/core/AABB.hpp | 83 ++++++ include/core/CoreTypes.hpp | 2 +- include/core/Defs.hpp | 2 +- include/core/Godot.hpp | 14 +- include/core/GodotGlobal.hpp | 3 +- include/core/NodePath.hpp | 2 - include/core/Rect3.hpp | 83 ------ include/core/String.hpp | 5 +- include/core/Transform.hpp | 6 +- include/core/Variant.hpp | 6 +- src/core/AABB.cpp | 640 +++++++++++++++++++++++++++++++++++++++++++ src/core/GodotGlobal.cpp | 60 +++- src/core/NodePath.cpp | 6 - src/core/Rect3.cpp | 640 ------------------------------------------- src/core/String.cpp | 12 +- src/core/Transform.cpp | 10 +- src/core/Variant.cpp | 10 +- 18 files changed, 824 insertions(+), 765 deletions(-) create mode 100644 include/core/AABB.hpp delete mode 100644 include/core/Rect3.hpp create mode 100644 src/core/AABB.cpp delete mode 100644 src/core/Rect3.cpp diff --git a/binding_generator.py b/binding_generator.py index d770f29..e87d963 100644 --- a/binding_generator.py +++ b/binding_generator.py @@ -684,7 +684,7 @@ def is_core_type(name): "PoolColorArray", "Quat", "Rect2", - "Rect3", + "AABB", "RID", "String", "Transform", @@ -713,7 +713,8 @@ def escape_cpp(name): "switch": "_switch", "export": "_export", "template": "_template", - "new": "new_" + "new": "new_", + "operator": "_operator" } if name in escapes: return escapes[name] diff --git a/include/core/AABB.hpp b/include/core/AABB.hpp new file mode 100644 index 0000000..8b96a74 --- /dev/null +++ b/include/core/AABB.hpp @@ -0,0 +1,83 @@ +#ifndef AABB_H +#define AABB_H + +#include "Vector3.hpp" + +#include "Plane.hpp" + +#include + +namespace godot { + +class AABB { +public: + Vector3 pos; + Vector3 size; + + real_t get_area() const; /// get area + inline bool has_no_area() const { + + return (size.x<=CMP_EPSILON || size.y<=CMP_EPSILON || size.z<=CMP_EPSILON); + } + + inline bool has_no_surface() const { + + return (size.x<=CMP_EPSILON && size.y<=CMP_EPSILON && size.z<=CMP_EPSILON); + } + + inline const Vector3& get_pos() const { return pos; } + inline void set_pos(const Vector3& p_pos) { pos=p_pos; } + inline const Vector3& get_size() const { return size; } + inline void set_size(const Vector3& p_size) { size=p_size; } + + + bool operator==(const AABB& p_rval) const; + bool operator!=(const AABB& p_rval) const; + + bool intersects(const AABB& p_aabb) const; /// Both AABBs overlap + bool intersects_inclusive(const AABB& p_aabb) const; /// Both AABBs (or their faces) overlap + bool encloses(const AABB & p_aabb) const; /// p_aabb is completely inside this + + 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=NULL,Vector3* r_normal=NULL) const; + bool intersects_ray(const Vector3& p_from, const Vector3& p_dir,Vector3* r_clip=NULL,Vector3* r_normal=NULL) const; + bool smits_intersect_ray(const Vector3 &from,const Vector3& p_dir, real_t t0, real_t t1) const; + + bool intersects_convex_shape(const Plane *p_plane, int p_plane_count) const; + bool intersects_plane(const Plane &p_plane) const; + + bool has_point(const Vector3& p_point) const; + Vector3 get_support(const Vector3& p_normal) const; + + + Vector3 get_longest_axis() const; + int get_longest_axis_index() const; + real_t get_longest_axis_size() const; + + Vector3 get_shortest_axis() const; + int get_shortest_axis_index() const; + real_t get_shortest_axis_size() const; + + AABB grow(real_t p_by) const; + void grow_by(real_t p_amount); + + void get_edge(int p_edge,Vector3& r_from,Vector3& r_to) const; + Vector3 get_endpoint(int p_point) const; + + AABB expand(const Vector3& p_vector) const; + void project_range_in_plane(const Plane& p_plane,real_t &r_min,real_t& r_max) const; + void expand_to(const Vector3& p_vector); /** expand to contain a point if necesary */ + + operator String() const; + + inline AABB() {} + inline AABB(const Vector3 &p_pos,const Vector3& p_size) { pos=p_pos; size=p_size; } + + +}; + +} + +#endif // RECT3_H diff --git a/include/core/CoreTypes.hpp b/include/core/CoreTypes.hpp index 2ee91a4..8f72c4c 100644 --- a/include/core/CoreTypes.hpp +++ b/include/core/CoreTypes.hpp @@ -3,6 +3,7 @@ #include "Defs.hpp" +#include "AABB.hpp" #include "Array.hpp" #include "Basis.hpp" #include "Color.hpp" @@ -12,7 +13,6 @@ #include "PoolArrays.hpp" #include "Quat.hpp" #include "Rect2.hpp" -#include "Rect3.hpp" #include "RID.hpp" #include "String.hpp" #include "Transform.hpp" diff --git a/include/core/Defs.hpp b/include/core/Defs.hpp index d3a87d8..f8c76d3 100644 --- a/include/core/Defs.hpp +++ b/include/core/Defs.hpp @@ -87,7 +87,7 @@ typedef float real_t; #ifndef ERR_PRINT -#define ERR_PRINT(msg) fprintf(stderr, "ERROR: %s\n", (msg).c_string()) +#define ERR_PRINT(msg) fprintf(stderr, "ERROR: %S\n", (msg).unicode_str()) #endif #ifndef ERR_FAIL_INDEX_V diff --git a/include/core/Godot.hpp b/include/core/Godot.hpp index f54a2e5..972a00f 100644 --- a/include/core/Godot.hpp +++ b/include/core/Godot.hpp @@ -86,7 +86,7 @@ struct _ArgCast { template T *as(Object *obj) { - return (T *) godot::api->godot_nativescript_get_userdata(obj); + return (T *) godot::nativescript_api->godot_nativescript_get_userdata(obj); } // instance and destroy funcs @@ -118,7 +118,7 @@ void register_class() destroy.destroy_func = _godot_class_destroy_func; - godot::api->godot_nativescript_register_class(godot::_RegisterState::nativescript_handle, T::___get_type_name(), T::___get_base_type_name(), create, destroy); + godot::nativescript_api->godot_nativescript_register_class(godot::_RegisterState::nativescript_handle, T::___get_type_name(), T::___get_base_type_name(), create, destroy); T::_register_methods(); } @@ -132,7 +132,7 @@ void register_tool_class() destroy.destroy_func = _godot_class_destroy_func; - godot::api->godot_nativescript_register_tool_class(godot::_RegisterState::nativescript_handle, T::___get_type_name(), T::___get_base_type_name(), create, destroy); + godot::nativescript_api->godot_nativescript_register_tool_class(godot::_RegisterState::nativescript_handle, T::___get_type_name(), T::___get_base_type_name(), create, destroy); T::_register_methods(); } @@ -285,7 +285,7 @@ void register_method(const char *name, M method_ptr, godot_method_rpc_mode rpc_t godot_method_attributes attr = {}; attr.rpc_type = rpc_type; - godot::api->godot_nativescript_register_method(godot::_RegisterState::nativescript_handle, ___get_method_class_name(method_ptr), name, attr, method); + godot::nativescript_api->godot_nativescript_register_method(godot::_RegisterState::nativescript_handle, ___get_method_class_name(method_ptr), name, attr, method); } @@ -403,7 +403,7 @@ void register_property(const char *name, P (T::*var), P default_value, godot_met get_func.free_func = godot::api->godot_free; get_func.get_func = &_PropertyDefaultGetFunc::_wrapped_getter; - godot::api->godot_nativescript_register_property(godot::_RegisterState::nativescript_handle, T::___get_type_name(), name, &attr, set_func, get_func); + godot::nativescript_api->godot_nativescript_register_property(godot::_RegisterState::nativescript_handle, T::___get_type_name(), name, &attr, set_func, get_func); } @@ -437,7 +437,7 @@ void register_property(const char *name, void (T::*setter)(P), P (T::*getter)(), get_func.free_func = godot::api->godot_free; get_func.get_func = &_PropertyGetFunc::_wrapped_getter; - godot::api->godot_nativescript_register_property(godot::_RegisterState::nativescript_handle, T::___get_type_name(), name, &attr, set_func, get_func); + godot::nativescript_api->godot_nativescript_register_property(godot::_RegisterState::nativescript_handle, T::___get_type_name(), name, &attr, set_func, get_func); } @@ -466,7 +466,7 @@ void register_signal(String name, Dictionary args = Dictionary()) signal.args[i].type = args.values()[i]; } - godot::api->godot_nativescript_register_signal(godot::_RegisterState::nativescript_handle, T::___get_type_name(), &signal); + godot::nativescript_api->godot_nativescript_register_signal(godot::_RegisterState::nativescript_handle, T::___get_type_name(), &signal); for (int i = 0; i < signal.num_args; i++) { godot::api->godot_string_destroy(&signal.args[i].name); diff --git a/include/core/GodotGlobal.hpp b/include/core/GodotGlobal.hpp index fe7344d..5263e9d 100644 --- a/include/core/GodotGlobal.hpp +++ b/include/core/GodotGlobal.hpp @@ -7,7 +7,8 @@ namespace godot { -extern "C" const godot_gdnative_api_struct *api; +extern "C" const godot_gdnative_core_api_struct *api; +extern "C" const godot_gdnative_ext_nativescript_api_struct *nativescript_api; class Godot { diff --git a/include/core/NodePath.hpp b/include/core/NodePath.hpp index 235c95e..026414b 100644 --- a/include/core/NodePath.hpp +++ b/include/core/NodePath.hpp @@ -24,8 +24,6 @@ public: int get_name_count() const; - String get_property() const; - String get_subname(const int idx) const; int get_subname_count() const; diff --git a/include/core/Rect3.hpp b/include/core/Rect3.hpp deleted file mode 100644 index acd674f..0000000 --- a/include/core/Rect3.hpp +++ /dev/null @@ -1,83 +0,0 @@ -#ifndef RECT3_H -#define RECT3_H - -#include "Vector3.hpp" - -#include "Plane.hpp" - -#include - -namespace godot { - -class Rect3 { -public: - Vector3 pos; - Vector3 size; - - real_t get_area() const; /// get area - inline bool has_no_area() const { - - return (size.x<=CMP_EPSILON || size.y<=CMP_EPSILON || size.z<=CMP_EPSILON); - } - - inline bool has_no_surface() const { - - return (size.x<=CMP_EPSILON && size.y<=CMP_EPSILON && size.z<=CMP_EPSILON); - } - - inline const Vector3& get_pos() const { return pos; } - inline void set_pos(const Vector3& p_pos) { pos=p_pos; } - inline const Vector3& get_size() const { return size; } - inline void set_size(const Vector3& p_size) { size=p_size; } - - - bool operator==(const Rect3& p_rval) const; - bool operator!=(const Rect3& p_rval) const; - - bool intersects(const Rect3& p_aabb) const; /// Both AABBs overlap - bool intersects_inclusive(const Rect3& p_aabb) const; /// Both AABBs (or their faces) overlap - bool encloses(const Rect3 & p_aabb) const; /// p_aabb is completely inside this - - Rect3 merge(const Rect3& p_with) const; - void merge_with(const Rect3& p_aabb); ///merge with another AABB - Rect3 intersection(const Rect3& 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=NULL,Vector3* r_normal=NULL) const; - bool intersects_ray(const Vector3& p_from, const Vector3& p_dir,Vector3* r_clip=NULL,Vector3* r_normal=NULL) const; - bool smits_intersect_ray(const Vector3 &from,const Vector3& p_dir, real_t t0, real_t t1) const; - - bool intersects_convex_shape(const Plane *p_plane, int p_plane_count) const; - bool intersects_plane(const Plane &p_plane) const; - - bool has_point(const Vector3& p_point) const; - Vector3 get_support(const Vector3& p_normal) const; - - - Vector3 get_longest_axis() const; - int get_longest_axis_index() const; - real_t get_longest_axis_size() const; - - Vector3 get_shortest_axis() const; - int get_shortest_axis_index() const; - real_t get_shortest_axis_size() const; - - Rect3 grow(real_t p_by) const; - void grow_by(real_t p_amount); - - void get_edge(int p_edge,Vector3& r_from,Vector3& r_to) const; - Vector3 get_endpoint(int p_point) const; - - Rect3 expand(const Vector3& p_vector) const; - void project_range_in_plane(const Plane& p_plane,real_t &r_min,real_t& r_max) const; - void expand_to(const Vector3& p_vector); /** expand to contain a point if necesary */ - - operator String() const; - - inline Rect3() {} - inline Rect3(const Vector3 &p_pos,const Vector3& p_size) { pos=p_pos; size=p_size; } - - -}; - -} - -#endif // RECT3_H diff --git a/include/core/String.hpp b/include/core/String.hpp index 43da4a3..63e2119 100644 --- a/include/core/String.hpp +++ b/include/core/String.hpp @@ -40,7 +40,8 @@ public: operator NodePath() const; int length() const; - const char *c_string() const; + const wchar_t *unicode_str() const; + void get_c_string(char *p_dest, int *p_size) const; int64_t find(String p_what) const; int64_t find_from(String p_what, int64_t p_from) const; @@ -106,6 +107,8 @@ public: }; String operator+(const char *a, const String &b); +String operator+(const wchar_t *a, const String &b); + } #endif // STRING_H diff --git a/include/core/Transform.hpp b/include/core/Transform.hpp index c6b9b14..ebe83f7 100644 --- a/include/core/Transform.hpp +++ b/include/core/Transform.hpp @@ -4,7 +4,7 @@ #include "Basis.hpp" #include "Plane.hpp" -#include "Rect3.hpp" +#include "AABB.hpp" namespace godot { @@ -53,8 +53,8 @@ public: Plane xform(const Plane& p_plane) const; Plane xform_inv(const Plane& p_plane) const; - Rect3 xform(const Rect3& p_aabb) const; - Rect3 xform_inv(const Rect3& p_aabb) const; + AABB xform(const AABB& p_aabb) const; + AABB xform_inv(const AABB& p_aabb) const; void operator*=(const Transform& p_transform); Transform operator*(const Transform& p_transform) const; diff --git a/include/core/Variant.hpp b/include/core/Variant.hpp index b4741f9..718561b 100644 --- a/include/core/Variant.hpp +++ b/include/core/Variant.hpp @@ -5,6 +5,7 @@ #include "Defs.hpp" +#include "AABB.hpp" #include "Basis.hpp" #include "Color.hpp" #include "NodePath.hpp" @@ -12,7 +13,6 @@ #include "PoolArrays.hpp" #include "Quat.hpp" #include "Rect2.hpp" -#include "Rect3.hpp" #include "RID.hpp" #include "String.hpp" #include "Transform.hpp" @@ -154,7 +154,7 @@ public: Variant(const Plane& p_plane); - Variant(const Rect3& p_aabb); + Variant(const AABB& p_aabb); Variant(const Quat& p_quat); @@ -215,7 +215,7 @@ public: operator Rect2() const; operator Vector3() const; operator Plane() const; - operator Rect3() const; + operator AABB() const; operator Quat() const; operator Basis() const; operator Transform() const; diff --git a/src/core/AABB.cpp b/src/core/AABB.cpp new file mode 100644 index 0000000..071e528 --- /dev/null +++ b/src/core/AABB.cpp @@ -0,0 +1,640 @@ +#include "AABB.hpp" +#include "Vector3.hpp" +#include "Plane.hpp" + +#include + +namespace godot { + +bool AABB::intersects(const AABB& p_aabb) const { + + if ( pos.x >= (p_aabb.pos.x + p_aabb.size.x) ) + return false; + if ( (pos.x+size.x) <= p_aabb.pos.x ) + return false; + if ( pos.y >= (p_aabb.pos.y + p_aabb.size.y) ) + return false; + if ( (pos.y+size.y) <= p_aabb.pos.y ) + return false; + if ( pos.z >= (p_aabb.pos.z + p_aabb.size.z) ) + return false; + if ( (pos.z+size.z) <= p_aabb.pos.z ) + return false; + + return true; +} + +bool AABB::intersects_inclusive(const AABB& p_aabb) const { + + if ( pos.x > (p_aabb.pos.x + p_aabb.size.x) ) + return false; + if ( (pos.x+size.x) < p_aabb.pos.x ) + return false; + if ( pos.y > (p_aabb.pos.y + p_aabb.size.y) ) + return false; + if ( (pos.y+size.y) < p_aabb.pos.y ) + return false; + if ( pos.z > (p_aabb.pos.z + p_aabb.size.z) ) + return false; + if ( (pos.z+size.z) < p_aabb.pos.z ) + return false; + + return true; +} + +bool AABB::encloses(const AABB & p_aabb) const { + + Vector3 src_min=pos; + Vector3 src_max=pos+size; + Vector3 dst_min=p_aabb.pos; + Vector3 dst_max=p_aabb.pos+p_aabb.size; + + return ( + (src_min.x <= dst_min.x) && + (src_max.x > dst_max.x) && + (src_min.y <= dst_min.y) && + (src_max.y > dst_max.y) && + (src_min.z <= dst_min.z) && + (src_max.z > dst_max.z) ); + +} + +Vector3 AABB::get_support(const Vector3& p_normal) const { + + Vector3 half_extents = size * 0.5; + Vector3 ofs = pos + 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_endpoint(int p_point) const { + + switch(p_point) { + case 0: return Vector3( pos.x , pos.y , pos.z ); + case 1: return Vector3( pos.x , pos.y , pos.z+size.z ); + case 2: return Vector3( pos.x , pos.y+size.y , pos.z ); + case 3: return Vector3( pos.x , pos.y+size.y , pos.z+size.z ); + case 4: return Vector3( pos.x+size.x , pos.y , pos.z ); + case 5: return Vector3( pos.x+size.x , pos.y , pos.z+size.z ); + case 6: return Vector3( pos.x+size.x , pos.y+size.y , pos.z ); + case 7: return Vector3( pos.x+size.x , pos.y+size.y , pos.z+size.z ); + }; + + ERR_FAIL_V(Vector3()); +} + +bool AABB::intersects_convex_shape(const Plane *p_planes, int p_plane_count) const { + + Vector3 half_extents = size * 0.5; + Vector3 ofs = pos + half_extents; + + for(int i=0;i0) ? -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 + ); + point+=ofs; + if (p.is_point_over(point)) + return false; + } + + return true; +} + +bool AABB::has_point(const Vector3& p_point) const { + + if (p_point.xpos.x+size.x) + return false; + if (p_point.y>pos.y+size.y) + return false; + if (p_point.z>pos.z+size.z) + return false; + + return true; +} + + +void AABB::expand_to(const Vector3& p_vector) { + + Vector3 begin=pos; + Vector3 end=pos+size; + + if (p_vector.xend.x) + end.x=p_vector.x; + if (p_vector.y>end.y) + end.y=p_vector.y; + if (p_vector.z>end.z) + end.z=p_vector.z; + + pos=begin; + size=end-begin; +} + +void AABB::project_range_in_plane(const Plane& p_plane,real_t &r_min,real_t& r_max) const { + + Vector3 half_extents( size.x * 0.5, size.y * 0.5, size.z * 0.5 ); + Vector3 center( pos.x + half_extents.x, pos.y + half_extents.y, pos.z + half_extents.z ); + + real_t length = p_plane.normal.abs().dot(half_extents); + real_t distance = p_plane.distance_to( center ); + r_min = distance - length; + r_max = distance + length; +} + +real_t AABB::get_longest_axis_size() const { + + real_t max_size=size.x; + + if (size.y > max_size ) { + max_size=size.y; + } + + if (size.z > max_size ) { + max_size=size.z; + } + + return max_size; +} + +real_t AABB::get_shortest_axis_size() const { + + real_t max_size=size.x; + + if (size.y < max_size ) { + max_size=size.y; + } + + if (size.z < max_size ) { + max_size=size.z; + } + + return max_size; +} + +bool AABB::smits_intersect_ray(const Vector3 &from,const Vector3& dir, real_t t0, real_t t1) const { + + real_t divx=1.0/dir.x; + real_t divy=1.0/dir.y; + real_t divz=1.0/dir.z; + + Vector3 upbound=pos+size; + real_t tmin, tmax, tymin, tymax, tzmin, tzmax; + if (dir.x >= 0) { + tmin = (pos.x - from.x) * divx; + tmax = (upbound.x - from.x) * divx; + } + else { + tmin = (upbound.x - from.x) * divx; + tmax = (pos.x - from.x) * divx; + } + if (dir.y >= 0) { + tymin = (pos.y - from.y) * divy; + tymax = (upbound.y - from.y) * divy; + } + else { + tymin = (upbound.y - from.y) * divy; + tymax = (pos.y - from.y) * divy; + } + if ( (tmin > tymax) || (tymin > tmax) ) + return false; + if (tymin > tmin) + tmin = tymin; + if (tymax < tmax) + tmax = tymax; + if (dir.z >= 0) { + tzmin = (pos.z - from.z) * divz; + tzmax = (upbound.z - from.z) * divz; + } + else { + tzmin = (upbound.z - from.z) * divz; + tzmax = (pos.z - from.z) * divz; + } + if ( (tmin > tzmax) || (tzmin > tmax) ) + return false; + if (tzmin > tmin) + tmin = tzmin; + if (tzmax < tmax) + tmax = tzmax; + return ( (tmin < t1) && (tmax > t0) ); +} + +void AABB::grow_by(real_t p_amount) { + + pos.x-=p_amount; + pos.y-=p_amount; + pos.z-=p_amount; + size.x+=2.0*p_amount; + size.y+=2.0*p_amount; + size.z+=2.0*p_amount; +} + + +real_t AABB::get_area() const { + + return size.x*size.y*size.z; + +} + +bool AABB::operator==(const AABB& p_rval) const { + + return ((pos==p_rval.pos) && (size==p_rval.size)); + +} +bool AABB::operator!=(const AABB& p_rval) const { + + return ((pos!=p_rval.pos) || (size!=p_rval.size)); + +} + +void AABB::merge_with(const AABB& p_aabb) { + + Vector3 beg_1,beg_2; + Vector3 end_1,end_2; + Vector3 min,max; + + beg_1=pos; + beg_2=p_aabb.pos; + end_1=Vector3(size.x,size.y,size.z)+beg_1; + end_2=Vector3(p_aabb.size.x,p_aabb.size.y,p_aabb.size.z)+beg_2; + + min.x=(beg_1.xend_2.x)?end_1.x:end_2.x; + max.y=(end_1.y>end_2.y)?end_1.y:end_2.y; + max.z=(end_1.z>end_2.z)?end_1.z:end_2.z; + + pos=min; + size=max-min; +} + +AABB AABB::intersection(const AABB& p_aabb) const { + + Vector3 src_min=pos; + Vector3 src_max=pos+size; + Vector3 dst_min=p_aabb.pos; + Vector3 dst_max=p_aabb.pos+p_aabb.size; + + Vector3 min,max; + + if (src_min.x > dst_max.x || src_max.x < dst_min.x ) + return AABB(); + else { + + min.x= ( src_min.x > dst_min.x ) ? src_min.x :dst_min.x; + max.x= ( src_max.x < dst_max.x ) ? src_max.x :dst_max.x; + + } + + if (src_min.y > dst_max.y || src_max.y < dst_min.y ) + return AABB(); + else { + + min.y= ( src_min.y > dst_min.y ) ? src_min.y :dst_min.y; + max.y= ( src_max.y < dst_max.y ) ? src_max.y :dst_max.y; + + } + + if (src_min.z > dst_max.z || src_max.z < dst_min.z ) + return AABB(); + else { + + min.z= ( src_min.z > dst_min.z ) ? src_min.z :dst_min.z; + max.z= ( src_max.z < dst_max.z ) ? src_max.z :dst_max.z; + + } + + + return AABB( min, max-min ); +} + +bool AABB::intersects_ray(const Vector3& p_from, const Vector3& p_dir,Vector3* r_clip,Vector3* r_normal) const { + + Vector3 c1, c2; + Vector3 end = pos+size; + real_t near=-1e20; + real_t far=1e20; + int axis=0; + + for (int i=0;i<3;i++){ + if (p_dir[i] == 0){ + if ((p_from[i] < pos[i]) || (p_from[i] > end[i])) { + return false; + } + } else { // ray not parallel to planes in this direction + c1[i] = (pos[i] - p_from[i]) / p_dir[i]; + c2[i] = (end[i] - p_from[i]) / p_dir[i]; + + if(c1[i] > c2[i]){ + std::swap(c1,c2); + } + if (c1[i] > near){ + near = c1[i]; + axis=i; + } + if (c2[i] < far){ + far = c2[i]; + } + if( (near > far) || (far < 0) ){ + return false; + } + } + } + + if (r_clip) + *r_clip=c1; + if (r_normal) { + *r_normal=Vector3(); + (*r_normal)[axis]=p_dir[axis]?-1:1; + } + + return true; + +} + + +bool AABB::intersects_segment(const Vector3& p_from, const Vector3& p_to,Vector3* r_clip,Vector3* r_normal) const { + + real_t min=0,max=1; + int axis=0; + real_t sign=0; + + for(int i=0;i<3;i++) { + real_t seg_from=p_from[i]; + real_t seg_to=p_to[i]; + real_t box_begin=pos[i]; + real_t box_end=box_begin+size[i]; + real_t cmin,cmax; + real_t csign; + + if (seg_from < seg_to) { + + if (seg_from > box_end || seg_to < box_begin) + return false; + real_t length=seg_to-seg_from; + cmin = (seg_from < box_begin)?((box_begin - seg_from)/length):0; + cmax = (seg_to > box_end)?((box_end - seg_from)/length):1; + csign=-1.0; + + } else { + + if (seg_to > box_end || seg_from < box_begin) + return false; + real_t length=seg_to-seg_from; + cmin = (seg_from > box_end)?(box_end - seg_from)/length:0; + cmax = (seg_to < box_begin)?(box_begin - seg_from)/length:1; + csign=1.0; + } + + if (cmin > min) { + min = cmin; + axis=i; + sign=csign; + } + if (cmax < max) + max = cmax; + if (max < min) + return false; + } + + + Vector3 rel=p_to-p_from; + + if (r_normal) { + Vector3 normal; + normal[axis]=sign; + *r_normal=normal; + } + + if (r_clip) + *r_clip=p_from+rel*min; + + return true; + +} + + +bool AABB::intersects_plane(const Plane &p_plane) const { + + Vector3 points[8] = { + Vector3( pos.x , pos.y , pos.z ), + Vector3( pos.x , pos.y , pos.z+size.z ), + Vector3( pos.x , pos.y+size.y , pos.z ), + Vector3( pos.x , pos.y+size.y , pos.z+size.z ), + Vector3( pos.x+size.x , pos.y , pos.z ), + Vector3( pos.x+size.x , pos.y , pos.z+size.z ), + Vector3( pos.x+size.x , pos.y+size.y , pos.z ), + Vector3( pos.x+size.x , pos.y+size.y , pos.z+size.z ), + }; + + bool over=false; + bool under=false; + + for (int i=0;i<8;i++) { + + if (p_plane.distance_to(points[i])>0) + over=true; + else + under=true; + + } + + return under && over; +} + + + +Vector3 AABB::get_longest_axis() const { + + Vector3 axis(1,0,0); + real_t max_size=size.x; + + if (size.y > max_size ) { + axis=Vector3(0,1,0); + max_size=size.y; + } + + if (size.z > max_size ) { + axis=Vector3(0,0,1); + max_size=size.z; + } + + return axis; +} +int AABB::get_longest_axis_index() const { + + int axis=0; + real_t max_size=size.x; + + if (size.y > max_size ) { + axis=1; + max_size=size.y; + } + + if (size.z > max_size ) { + axis=2; + max_size=size.z; + } + + return axis; +} + + +Vector3 AABB::get_shortest_axis() const { + + Vector3 axis(1,0,0); + real_t max_size=size.x; + + if (size.y < max_size ) { + axis=Vector3(0,1,0); + max_size=size.y; + } + + if (size.z < max_size ) { + axis=Vector3(0,0,1); + max_size=size.z; + } + + return axis; +} +int AABB::get_shortest_axis_index() const { + + int axis=0; + real_t max_size=size.x; + + if (size.y < max_size ) { + axis=1; + max_size=size.y; + } + + if (size.z < max_size ) { + axis=2; + max_size=size.z; + } + + return axis; +} + +AABB AABB::merge(const AABB& p_with) const { + + AABB aabb=*this; + aabb.merge_with(p_with); + return aabb; +} +AABB AABB::expand(const Vector3& p_vector) const { + AABB aabb=*this; + aabb.expand_to(p_vector); + return aabb; + +} +AABB AABB::grow(real_t p_by) const { + + AABB aabb=*this; + aabb.grow_by(p_by); + return aabb; +} + +void AABB::get_edge(int p_edge,Vector3& r_from,Vector3& r_to) const { + + ERR_FAIL_INDEX(p_edge,12); + switch(p_edge) { + + case 0:{ + + r_from=Vector3( pos.x+size.x , pos.y , pos.z ); + r_to=Vector3( pos.x , pos.y , pos.z ); + } break; + case 1:{ + + r_from=Vector3( pos.x+size.x , pos.y , pos.z+size.z ); + r_to=Vector3( pos.x+size.x , pos.y , pos.z ); + } break; + case 2:{ + r_from=Vector3( pos.x , pos.y , pos.z+size.z ); + r_to=Vector3( pos.x+size.x , pos.y , pos.z+size.z ); + + } break; + case 3:{ + + r_from=Vector3( pos.x , pos.y , pos.z ); + r_to=Vector3( pos.x , pos.y , pos.z+size.z ); + + } break; + case 4:{ + + r_from=Vector3( pos.x , pos.y+size.y , pos.z ); + r_to=Vector3( pos.x+size.x , pos.y+size.y , pos.z ); + } break; + case 5:{ + + r_from=Vector3( pos.x+size.x , pos.y+size.y , pos.z ); + r_to=Vector3( pos.x+size.x , pos.y+size.y , pos.z+size.z ); + } break; + case 6:{ + r_from=Vector3( pos.x+size.x , pos.y+size.y , pos.z+size.z ); + r_to=Vector3( pos.x , pos.y+size.y , pos.z+size.z ); + + } break; + case 7:{ + + r_from=Vector3( pos.x , pos.y+size.y , pos.z+size.z ); + r_to=Vector3( pos.x , pos.y+size.y , pos.z ); + + } break; + case 8:{ + + r_from=Vector3( pos.x , pos.y , pos.z+size.z ); + r_to=Vector3( pos.x , pos.y+size.y , pos.z+size.z ); + + } break; + case 9:{ + + r_from=Vector3( pos.x , pos.y , pos.z ); + r_to=Vector3( pos.x , pos.y+size.y , pos.z ); + + } break; + case 10:{ + + r_from=Vector3( pos.x+size.x , pos.y , pos.z ); + r_to=Vector3( pos.x+size.x , pos.y+size.y , pos.z ); + + } break; + case 11:{ + + r_from=Vector3( pos.x+size.x , pos.y , pos.z+size.z ); + r_to=Vector3( pos.x+size.x , pos.y+size.y , pos.z+size.z ); + + } break; + + } + +} + +AABB::operator String() const { + + //return String()+pos +" - "+ size; + return String(); // @Todo +} + +} diff --git a/src/core/GodotGlobal.cpp b/src/core/GodotGlobal.cpp index dc21389..598fdc8 100644 --- a/src/core/GodotGlobal.cpp +++ b/src/core/GodotGlobal.cpp @@ -5,7 +5,8 @@ namespace godot { void *_RegisterState::nativescript_handle; -const godot_gdnative_api_struct *api = NULL; +const godot_gdnative_core_api_struct *api = NULL; +const godot_gdnative_ext_nativescript_api_struct *nativescript_api = NULL; void Godot::print(const String& message) { @@ -14,12 +15,54 @@ void Godot::print(const String& message) void Godot::print_warning(const String& description, const String& function, const String& file, int line) { - godot::api->godot_print_warning(description.c_string(), function.c_string(), file.c_string(), line); + int len; + + description.get_c_string(NULL, &len); + char * c_desc = (char *)godot::api->godot_alloc(len + 1); + description.get_c_string(c_desc, &len); + c_desc[len] = '\0'; + + function.get_c_string(NULL, &len); + char * c_func = (char *)godot::api->godot_alloc(len + 1); + function.get_c_string(c_func, &len); + c_func[len] = '\0'; + + file.get_c_string(NULL, &len); + char * c_file = (char *)godot::api->godot_alloc(len + 1); + file.get_c_string(c_file, &len); + c_file[len] = '\0'; + + godot::api->godot_print_warning(c_desc, c_func, c_file, line); + + godot::api->godot_free(c_desc); + godot::api->godot_free(c_func); + godot::api->godot_free(c_file); } void Godot::print_error(const String& description, const String& function, const String& file, int line) { - godot::api->godot_print_error(description.c_string(), function.c_string(), file.c_string(), line); + int len; + + description.get_c_string(NULL, &len); + char * c_desc = (char *)godot::api->godot_alloc(len + 1); + description.get_c_string(c_desc, &len); + c_desc[len] = '\0'; + + function.get_c_string(NULL, &len); + char * c_func = (char *)godot::api->godot_alloc(len + 1); + function.get_c_string(c_func, &len); + c_func[len] = '\0'; + + file.get_c_string(NULL, &len); + char * c_file = (char *)godot::api->godot_alloc(len + 1); + file.get_c_string(c_file, &len); + c_file[len] = '\0'; + + godot::api->godot_print_error(c_desc, c_func, c_file, line); + + godot::api->godot_free(c_desc); + godot::api->godot_free(c_func); + godot::api->godot_free(c_file); } }; @@ -28,6 +71,17 @@ void gdnative_init(godot_gdnative_init_options *options); extern "C" void GDN_EXPORT godot_gdnative_init(godot_gdnative_init_options *options) { godot::api = options->api_struct; + + // now find our extensions + for (int i = 0; i < godot::api->num_extensions; i++) { + switch (godot::api->extensions[i]->type) { + case GDNATIVE_EXT_NATIVESCRIPT: { + godot::nativescript_api = (godot_gdnative_ext_nativescript_api_struct *)godot::api->extensions[i]; + }; break; + default: break; + }; + }; + gdnative_init(options); } diff --git a/src/core/NodePath.cpp b/src/core/NodePath.cpp index bdfa22e..25ce882 100644 --- a/src/core/NodePath.cpp +++ b/src/core/NodePath.cpp @@ -43,12 +43,6 @@ int NodePath::get_name_count() const return godot::api->godot_node_path_get_name_count(&_node_path); } -String NodePath::get_property() const -{ - godot_string str = godot::api->godot_node_path_get_property(&_node_path); - return *(String *) &str; -} - String NodePath::get_subname(const int idx) const { godot_string str = godot::api->godot_node_path_get_subname(&_node_path, idx); diff --git a/src/core/Rect3.cpp b/src/core/Rect3.cpp deleted file mode 100644 index 7a3a515..0000000 --- a/src/core/Rect3.cpp +++ /dev/null @@ -1,640 +0,0 @@ -#include "Rect3.hpp" -#include "Vector3.hpp" -#include "Plane.hpp" - -#include - -namespace godot { - -bool Rect3::intersects(const Rect3& p_aabb) const { - - if ( pos.x >= (p_aabb.pos.x + p_aabb.size.x) ) - return false; - if ( (pos.x+size.x) <= p_aabb.pos.x ) - return false; - if ( pos.y >= (p_aabb.pos.y + p_aabb.size.y) ) - return false; - if ( (pos.y+size.y) <= p_aabb.pos.y ) - return false; - if ( pos.z >= (p_aabb.pos.z + p_aabb.size.z) ) - return false; - if ( (pos.z+size.z) <= p_aabb.pos.z ) - return false; - - return true; -} - -bool Rect3::intersects_inclusive(const Rect3& p_aabb) const { - - if ( pos.x > (p_aabb.pos.x + p_aabb.size.x) ) - return false; - if ( (pos.x+size.x) < p_aabb.pos.x ) - return false; - if ( pos.y > (p_aabb.pos.y + p_aabb.size.y) ) - return false; - if ( (pos.y+size.y) < p_aabb.pos.y ) - return false; - if ( pos.z > (p_aabb.pos.z + p_aabb.size.z) ) - return false; - if ( (pos.z+size.z) < p_aabb.pos.z ) - return false; - - return true; -} - -bool Rect3::encloses(const Rect3 & p_aabb) const { - - Vector3 src_min=pos; - Vector3 src_max=pos+size; - Vector3 dst_min=p_aabb.pos; - Vector3 dst_max=p_aabb.pos+p_aabb.size; - - return ( - (src_min.x <= dst_min.x) && - (src_max.x > dst_max.x) && - (src_min.y <= dst_min.y) && - (src_max.y > dst_max.y) && - (src_min.z <= dst_min.z) && - (src_max.z > dst_max.z) ); - -} - -Vector3 Rect3::get_support(const Vector3& p_normal) const { - - Vector3 half_extents = size * 0.5; - Vector3 ofs = pos + 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 Rect3::get_endpoint(int p_point) const { - - switch(p_point) { - case 0: return Vector3( pos.x , pos.y , pos.z ); - case 1: return Vector3( pos.x , pos.y , pos.z+size.z ); - case 2: return Vector3( pos.x , pos.y+size.y , pos.z ); - case 3: return Vector3( pos.x , pos.y+size.y , pos.z+size.z ); - case 4: return Vector3( pos.x+size.x , pos.y , pos.z ); - case 5: return Vector3( pos.x+size.x , pos.y , pos.z+size.z ); - case 6: return Vector3( pos.x+size.x , pos.y+size.y , pos.z ); - case 7: return Vector3( pos.x+size.x , pos.y+size.y , pos.z+size.z ); - }; - - ERR_FAIL_V(Vector3()); -} - -bool Rect3::intersects_convex_shape(const Plane *p_planes, int p_plane_count) const { - - Vector3 half_extents = size * 0.5; - Vector3 ofs = pos + half_extents; - - for(int i=0;i0) ? -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 - ); - point+=ofs; - if (p.is_point_over(point)) - return false; - } - - return true; -} - -bool Rect3::has_point(const Vector3& p_point) const { - - if (p_point.xpos.x+size.x) - return false; - if (p_point.y>pos.y+size.y) - return false; - if (p_point.z>pos.z+size.z) - return false; - - return true; -} - - -void Rect3::expand_to(const Vector3& p_vector) { - - Vector3 begin=pos; - Vector3 end=pos+size; - - if (p_vector.xend.x) - end.x=p_vector.x; - if (p_vector.y>end.y) - end.y=p_vector.y; - if (p_vector.z>end.z) - end.z=p_vector.z; - - pos=begin; - size=end-begin; -} - -void Rect3::project_range_in_plane(const Plane& p_plane,real_t &r_min,real_t& r_max) const { - - Vector3 half_extents( size.x * 0.5, size.y * 0.5, size.z * 0.5 ); - Vector3 center( pos.x + half_extents.x, pos.y + half_extents.y, pos.z + half_extents.z ); - - real_t length = p_plane.normal.abs().dot(half_extents); - real_t distance = p_plane.distance_to( center ); - r_min = distance - length; - r_max = distance + length; -} - -real_t Rect3::get_longest_axis_size() const { - - real_t max_size=size.x; - - if (size.y > max_size ) { - max_size=size.y; - } - - if (size.z > max_size ) { - max_size=size.z; - } - - return max_size; -} - -real_t Rect3::get_shortest_axis_size() const { - - real_t max_size=size.x; - - if (size.y < max_size ) { - max_size=size.y; - } - - if (size.z < max_size ) { - max_size=size.z; - } - - return max_size; -} - -bool Rect3::smits_intersect_ray(const Vector3 &from,const Vector3& dir, real_t t0, real_t t1) const { - - real_t divx=1.0/dir.x; - real_t divy=1.0/dir.y; - real_t divz=1.0/dir.z; - - Vector3 upbound=pos+size; - real_t tmin, tmax, tymin, tymax, tzmin, tzmax; - if (dir.x >= 0) { - tmin = (pos.x - from.x) * divx; - tmax = (upbound.x - from.x) * divx; - } - else { - tmin = (upbound.x - from.x) * divx; - tmax = (pos.x - from.x) * divx; - } - if (dir.y >= 0) { - tymin = (pos.y - from.y) * divy; - tymax = (upbound.y - from.y) * divy; - } - else { - tymin = (upbound.y - from.y) * divy; - tymax = (pos.y - from.y) * divy; - } - if ( (tmin > tymax) || (tymin > tmax) ) - return false; - if (tymin > tmin) - tmin = tymin; - if (tymax < tmax) - tmax = tymax; - if (dir.z >= 0) { - tzmin = (pos.z - from.z) * divz; - tzmax = (upbound.z - from.z) * divz; - } - else { - tzmin = (upbound.z - from.z) * divz; - tzmax = (pos.z - from.z) * divz; - } - if ( (tmin > tzmax) || (tzmin > tmax) ) - return false; - if (tzmin > tmin) - tmin = tzmin; - if (tzmax < tmax) - tmax = tzmax; - return ( (tmin < t1) && (tmax > t0) ); -} - -void Rect3::grow_by(real_t p_amount) { - - pos.x-=p_amount; - pos.y-=p_amount; - pos.z-=p_amount; - size.x+=2.0*p_amount; - size.y+=2.0*p_amount; - size.z+=2.0*p_amount; -} - - -real_t Rect3::get_area() const { - - return size.x*size.y*size.z; - -} - -bool Rect3::operator==(const Rect3& p_rval) const { - - return ((pos==p_rval.pos) && (size==p_rval.size)); - -} -bool Rect3::operator!=(const Rect3& p_rval) const { - - return ((pos!=p_rval.pos) || (size!=p_rval.size)); - -} - -void Rect3::merge_with(const Rect3& p_aabb) { - - Vector3 beg_1,beg_2; - Vector3 end_1,end_2; - Vector3 min,max; - - beg_1=pos; - beg_2=p_aabb.pos; - end_1=Vector3(size.x,size.y,size.z)+beg_1; - end_2=Vector3(p_aabb.size.x,p_aabb.size.y,p_aabb.size.z)+beg_2; - - min.x=(beg_1.xend_2.x)?end_1.x:end_2.x; - max.y=(end_1.y>end_2.y)?end_1.y:end_2.y; - max.z=(end_1.z>end_2.z)?end_1.z:end_2.z; - - pos=min; - size=max-min; -} - -Rect3 Rect3::intersection(const Rect3& p_aabb) const { - - Vector3 src_min=pos; - Vector3 src_max=pos+size; - Vector3 dst_min=p_aabb.pos; - Vector3 dst_max=p_aabb.pos+p_aabb.size; - - Vector3 min,max; - - if (src_min.x > dst_max.x || src_max.x < dst_min.x ) - return Rect3(); - else { - - min.x= ( src_min.x > dst_min.x ) ? src_min.x :dst_min.x; - max.x= ( src_max.x < dst_max.x ) ? src_max.x :dst_max.x; - - } - - if (src_min.y > dst_max.y || src_max.y < dst_min.y ) - return Rect3(); - else { - - min.y= ( src_min.y > dst_min.y ) ? src_min.y :dst_min.y; - max.y= ( src_max.y < dst_max.y ) ? src_max.y :dst_max.y; - - } - - if (src_min.z > dst_max.z || src_max.z < dst_min.z ) - return Rect3(); - else { - - min.z= ( src_min.z > dst_min.z ) ? src_min.z :dst_min.z; - max.z= ( src_max.z < dst_max.z ) ? src_max.z :dst_max.z; - - } - - - return Rect3( min, max-min ); -} - -bool Rect3::intersects_ray(const Vector3& p_from, const Vector3& p_dir,Vector3* r_clip,Vector3* r_normal) const { - - Vector3 c1, c2; - Vector3 end = pos+size; - real_t near=-1e20; - real_t far=1e20; - int axis=0; - - for (int i=0;i<3;i++){ - if (p_dir[i] == 0){ - if ((p_from[i] < pos[i]) || (p_from[i] > end[i])) { - return false; - } - } else { // ray not parallel to planes in this direction - c1[i] = (pos[i] - p_from[i]) / p_dir[i]; - c2[i] = (end[i] - p_from[i]) / p_dir[i]; - - if(c1[i] > c2[i]){ - std::swap(c1,c2); - } - if (c1[i] > near){ - near = c1[i]; - axis=i; - } - if (c2[i] < far){ - far = c2[i]; - } - if( (near > far) || (far < 0) ){ - return false; - } - } - } - - if (r_clip) - *r_clip=c1; - if (r_normal) { - *r_normal=Vector3(); - (*r_normal)[axis]=p_dir[axis]?-1:1; - } - - return true; - -} - - -bool Rect3::intersects_segment(const Vector3& p_from, const Vector3& p_to,Vector3* r_clip,Vector3* r_normal) const { - - real_t min=0,max=1; - int axis=0; - real_t sign=0; - - for(int i=0;i<3;i++) { - real_t seg_from=p_from[i]; - real_t seg_to=p_to[i]; - real_t box_begin=pos[i]; - real_t box_end=box_begin+size[i]; - real_t cmin,cmax; - real_t csign; - - if (seg_from < seg_to) { - - if (seg_from > box_end || seg_to < box_begin) - return false; - real_t length=seg_to-seg_from; - cmin = (seg_from < box_begin)?((box_begin - seg_from)/length):0; - cmax = (seg_to > box_end)?((box_end - seg_from)/length):1; - csign=-1.0; - - } else { - - if (seg_to > box_end || seg_from < box_begin) - return false; - real_t length=seg_to-seg_from; - cmin = (seg_from > box_end)?(box_end - seg_from)/length:0; - cmax = (seg_to < box_begin)?(box_begin - seg_from)/length:1; - csign=1.0; - } - - if (cmin > min) { - min = cmin; - axis=i; - sign=csign; - } - if (cmax < max) - max = cmax; - if (max < min) - return false; - } - - - Vector3 rel=p_to-p_from; - - if (r_normal) { - Vector3 normal; - normal[axis]=sign; - *r_normal=normal; - } - - if (r_clip) - *r_clip=p_from+rel*min; - - return true; - -} - - -bool Rect3::intersects_plane(const Plane &p_plane) const { - - Vector3 points[8] = { - Vector3( pos.x , pos.y , pos.z ), - Vector3( pos.x , pos.y , pos.z+size.z ), - Vector3( pos.x , pos.y+size.y , pos.z ), - Vector3( pos.x , pos.y+size.y , pos.z+size.z ), - Vector3( pos.x+size.x , pos.y , pos.z ), - Vector3( pos.x+size.x , pos.y , pos.z+size.z ), - Vector3( pos.x+size.x , pos.y+size.y , pos.z ), - Vector3( pos.x+size.x , pos.y+size.y , pos.z+size.z ), - }; - - bool over=false; - bool under=false; - - for (int i=0;i<8;i++) { - - if (p_plane.distance_to(points[i])>0) - over=true; - else - under=true; - - } - - return under && over; -} - - - -Vector3 Rect3::get_longest_axis() const { - - Vector3 axis(1,0,0); - real_t max_size=size.x; - - if (size.y > max_size ) { - axis=Vector3(0,1,0); - max_size=size.y; - } - - if (size.z > max_size ) { - axis=Vector3(0,0,1); - max_size=size.z; - } - - return axis; -} -int Rect3::get_longest_axis_index() const { - - int axis=0; - real_t max_size=size.x; - - if (size.y > max_size ) { - axis=1; - max_size=size.y; - } - - if (size.z > max_size ) { - axis=2; - max_size=size.z; - } - - return axis; -} - - -Vector3 Rect3::get_shortest_axis() const { - - Vector3 axis(1,0,0); - real_t max_size=size.x; - - if (size.y < max_size ) { - axis=Vector3(0,1,0); - max_size=size.y; - } - - if (size.z < max_size ) { - axis=Vector3(0,0,1); - max_size=size.z; - } - - return axis; -} -int Rect3::get_shortest_axis_index() const { - - int axis=0; - real_t max_size=size.x; - - if (size.y < max_size ) { - axis=1; - max_size=size.y; - } - - if (size.z < max_size ) { - axis=2; - max_size=size.z; - } - - return axis; -} - -Rect3 Rect3::merge(const Rect3& p_with) const { - - Rect3 aabb=*this; - aabb.merge_with(p_with); - return aabb; -} -Rect3 Rect3::expand(const Vector3& p_vector) const { - Rect3 aabb=*this; - aabb.expand_to(p_vector); - return aabb; - -} -Rect3 Rect3::grow(real_t p_by) const { - - Rect3 aabb=*this; - aabb.grow_by(p_by); - return aabb; -} - -void Rect3::get_edge(int p_edge,Vector3& r_from,Vector3& r_to) const { - - ERR_FAIL_INDEX(p_edge,12); - switch(p_edge) { - - case 0:{ - - r_from=Vector3( pos.x+size.x , pos.y , pos.z ); - r_to=Vector3( pos.x , pos.y , pos.z ); - } break; - case 1:{ - - r_from=Vector3( pos.x+size.x , pos.y , pos.z+size.z ); - r_to=Vector3( pos.x+size.x , pos.y , pos.z ); - } break; - case 2:{ - r_from=Vector3( pos.x , pos.y , pos.z+size.z ); - r_to=Vector3( pos.x+size.x , pos.y , pos.z+size.z ); - - } break; - case 3:{ - - r_from=Vector3( pos.x , pos.y , pos.z ); - r_to=Vector3( pos.x , pos.y , pos.z+size.z ); - - } break; - case 4:{ - - r_from=Vector3( pos.x , pos.y+size.y , pos.z ); - r_to=Vector3( pos.x+size.x , pos.y+size.y , pos.z ); - } break; - case 5:{ - - r_from=Vector3( pos.x+size.x , pos.y+size.y , pos.z ); - r_to=Vector3( pos.x+size.x , pos.y+size.y , pos.z+size.z ); - } break; - case 6:{ - r_from=Vector3( pos.x+size.x , pos.y+size.y , pos.z+size.z ); - r_to=Vector3( pos.x , pos.y+size.y , pos.z+size.z ); - - } break; - case 7:{ - - r_from=Vector3( pos.x , pos.y+size.y , pos.z+size.z ); - r_to=Vector3( pos.x , pos.y+size.y , pos.z ); - - } break; - case 8:{ - - r_from=Vector3( pos.x , pos.y , pos.z+size.z ); - r_to=Vector3( pos.x , pos.y+size.y , pos.z+size.z ); - - } break; - case 9:{ - - r_from=Vector3( pos.x , pos.y , pos.z ); - r_to=Vector3( pos.x , pos.y+size.y , pos.z ); - - } break; - case 10:{ - - r_from=Vector3( pos.x+size.x , pos.y , pos.z ); - r_to=Vector3( pos.x+size.x , pos.y+size.y , pos.z ); - - } break; - case 11:{ - - r_from=Vector3( pos.x+size.x , pos.y , pos.z+size.z ); - r_to=Vector3( pos.x+size.x , pos.y+size.y , pos.z+size.z ); - - } break; - - } - -} - -Rect3::operator String() const { - - //return String()+pos +" - "+ size; - return String(); // @Todo -} - -} diff --git a/src/core/String.cpp b/src/core/String.cpp index 0a1d455..9a7b96e 100644 --- a/src/core/String.cpp +++ b/src/core/String.cpp @@ -103,14 +103,22 @@ String::operator NodePath() const { return NodePath(*this); } -const char *String::c_string() const { - return godot::api->godot_string_c_str(&_godot_string); +const wchar_t *String::unicode_str() const { + return godot::api->godot_string_unicode_str(&_godot_string); +} + +void String::get_c_string(char *p_dest, int *p_size) const { + godot::api->godot_string_get_data(&_godot_string, p_dest, p_size); } String operator+(const char *a, const String &b) { return String(a) + b; } +String operator+(const wchar_t *a, const String &b) { + return String(a) + b; +} + bool String::begins_with(String &p_string) const { return godot::api->godot_string_begins_with(&_godot_string, &p_string._godot_string); } diff --git a/src/core/Transform.cpp b/src/core/Transform.cpp index b8a05df..c756d23 100644 --- a/src/core/Transform.cpp +++ b/src/core/Transform.cpp @@ -3,7 +3,7 @@ #include "Basis.hpp" #include "Plane.hpp" -#include "Rect3.hpp" +#include "AABB.hpp" #include "Quat.hpp" @@ -85,14 +85,14 @@ Plane Transform::xform_inv(const Plane& p_plane) const { } -Rect3 Transform::xform(const Rect3& p_aabb) const { +AABB Transform::xform(const AABB& p_aabb) const { /* define vertices */ Vector3 x=basis.get_axis(0)*p_aabb.size.x; Vector3 y=basis.get_axis(1)*p_aabb.size.y; Vector3 z=basis.get_axis(2)*p_aabb.size.z; Vector3 pos = xform( p_aabb.pos ); //could be even further optimized - Rect3 new_aabb; + AABB new_aabb; new_aabb.pos=pos; new_aabb.expand_to( pos+x ); new_aabb.expand_to( pos+y ); @@ -104,7 +104,7 @@ Rect3 Transform::xform(const Rect3& p_aabb) const { return new_aabb; } -Rect3 Transform::xform_inv(const Rect3& p_aabb) const { +AABB Transform::xform_inv(const AABB& p_aabb) const { /* define vertices */ Vector3 vertices[8]={ @@ -119,7 +119,7 @@ Rect3 Transform::xform_inv(const Rect3& p_aabb) const { }; - Rect3 ret; + AABB ret; ret.pos=xform_inv(vertices[0]); diff --git a/src/core/Variant.cpp b/src/core/Variant.cpp index 13cbe9a..e9e7ebd 100644 --- a/src/core/Variant.cpp +++ b/src/core/Variant.cpp @@ -99,9 +99,9 @@ Variant::Variant(const Plane& p_plane) } -Variant::Variant(const Rect3& p_aabb) +Variant::Variant(const AABB& p_aabb) { - godot::api->godot_variant_new_rect3(&_godot_variant, (godot_rect3 *) &p_aabb); + godot::api->godot_variant_new_aabb(&_godot_variant, (godot_aabb *) &p_aabb); } Variant::Variant(const Quat& p_quat) @@ -274,10 +274,10 @@ Variant::operator Plane() const godot_plane s = godot::api->godot_variant_as_plane(&_godot_variant); return *(Plane *) &s; } -Variant::operator Rect3() const +Variant::operator AABB() const { - godot_rect3 s = godot::api->godot_variant_as_rect3(&_godot_variant); - return *(Rect3 *) &s; + godot_aabb s = godot::api->godot_variant_as_aabb(&_godot_variant); + return *(AABB *) &s; } Variant::operator Quat() const { -- cgit v1.2.3 From d8faa4ec76bc73ca17b8e5dd72220d16996423c3 Mon Sep 17 00:00:00 2001 From: Bastiaan Olij Date: Sat, 25 Nov 2017 09:54:35 +1100 Subject: Added alloc_c_string --- include/core/String.hpp | 1 + src/core/GodotGlobal.cpp | 58 +++++++++++++++++------------------------------- src/core/String.cpp | 16 +++++++++++++ 3 files changed, 37 insertions(+), 38 deletions(-) diff --git a/include/core/String.hpp b/include/core/String.hpp index 63e2119..7788195 100644 --- a/include/core/String.hpp +++ b/include/core/String.hpp @@ -41,6 +41,7 @@ public: int length() const; const wchar_t *unicode_str() const; + char *alloc_c_string() const; void get_c_string(char *p_dest, int *p_size) const; int64_t find(String p_what) const; diff --git a/src/core/GodotGlobal.cpp b/src/core/GodotGlobal.cpp index 598fdc8..8566481 100644 --- a/src/core/GodotGlobal.cpp +++ b/src/core/GodotGlobal.cpp @@ -17,52 +17,34 @@ void Godot::print_warning(const String& description, const String& function, con { int len; - description.get_c_string(NULL, &len); - char * c_desc = (char *)godot::api->godot_alloc(len + 1); - description.get_c_string(c_desc, &len); - c_desc[len] = '\0'; - - function.get_c_string(NULL, &len); - char * c_func = (char *)godot::api->godot_alloc(len + 1); - function.get_c_string(c_func, &len); - c_func[len] = '\0'; - - file.get_c_string(NULL, &len); - char * c_file = (char *)godot::api->godot_alloc(len + 1); - file.get_c_string(c_file, &len); - c_file[len] = '\0'; - - godot::api->godot_print_warning(c_desc, c_func, c_file, line); - - godot::api->godot_free(c_desc); - godot::api->godot_free(c_func); - godot::api->godot_free(c_file); + char * c_desc = description.alloc_c_string(); + char * c_func = function.alloc_c_string(); + char * c_file = file.alloc_c_string(); + + if (c_desc != NULL && c_func !=NULL && c_file != NULL) { + godot::api->godot_print_warning(c_desc, c_func, c_file, line); + }; + + if (c_desc != NULL) godot::api->godot_free(c_desc); + if (c_func != NULL) godot::api->godot_free(c_func); + if (c_file != NULL) godot::api->godot_free(c_file); } void Godot::print_error(const String& description, const String& function, const String& file, int line) { int len; - description.get_c_string(NULL, &len); - char * c_desc = (char *)godot::api->godot_alloc(len + 1); - description.get_c_string(c_desc, &len); - c_desc[len] = '\0'; + char * c_desc = description.alloc_c_string(); + char * c_func = function.alloc_c_string(); + char * c_file = file.alloc_c_string(); - function.get_c_string(NULL, &len); - char * c_func = (char *)godot::api->godot_alloc(len + 1); - function.get_c_string(c_func, &len); - c_func[len] = '\0'; - - file.get_c_string(NULL, &len); - char * c_file = (char *)godot::api->godot_alloc(len + 1); - file.get_c_string(c_file, &len); - c_file[len] = '\0'; - - godot::api->godot_print_error(c_desc, c_func, c_file, line); + if (c_desc != NULL && c_func !=NULL && c_file != NULL) { + godot::api->godot_print_error(c_desc, c_func, c_file, line); + }; - godot::api->godot_free(c_desc); - godot::api->godot_free(c_func); - godot::api->godot_free(c_file); + if (c_desc != NULL) godot::api->godot_free(c_desc); + if (c_func != NULL) godot::api->godot_free(c_func); + if (c_file != NULL) godot::api->godot_free(c_file); } }; diff --git a/src/core/String.cpp b/src/core/String.cpp index 9a7b96e..494d976 100644 --- a/src/core/String.cpp +++ b/src/core/String.cpp @@ -107,6 +107,22 @@ const wchar_t *String::unicode_str() const { return godot::api->godot_string_unicode_str(&_godot_string); } +char *String::alloc_c_string() const { + int len; + + // figure out the lenght of our string + get_c_string(NULL, &len); + + // allocate our buffer + char * result = (char *)godot::api->godot_alloc(len + 1); + if (result != NULL) { + get_c_string(result, &len); + result[len] = '\0'; + } + + return result; +} + void String::get_c_string(char *p_dest, int *p_size) const { godot::api->godot_string_get_data(&_godot_string, p_dest, p_size); } -- cgit v1.2.3