From a15e7b7189c7e08ddea10d79d51c2b6db7ad16bb Mon Sep 17 00:00:00 2001 From: Karroffel Date: Sun, 5 Mar 2017 03:02:29 +0100 Subject: [PATCH] I hate templates --- .gitignore | 3 + binding_generator/src/main.rs | 18 +- include/godot_cpp/Godot.h | 280 +++++++++++++++++++++++++++++++ include/godot_cpp/core/Variant.h | 6 +- 4 files changed, 301 insertions(+), 6 deletions(-) create mode 100644 .gitignore create mode 100644 include/godot_cpp/Godot.h diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..f5beb398 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +include/godot_cpp/*.h +include/godot.h +include/godot diff --git a/binding_generator/src/main.rs b/binding_generator/src/main.rs index f30b4486..f9748f12 100644 --- a/binding_generator/src/main.rs +++ b/binding_generator/src/main.rs @@ -142,6 +142,12 @@ fn generate_class_content(forward_declares: &String, class: &GodotClass) -> Stri contents = contents + "\t}\n\n\n"; } + if class.base_class != "" { + contents = contents + "\tvoid _init() {\n"; + contents = contents + "\t\t\n"; + contents = contents + "\t}\n\n"; + } + if class.instanciable { contents = contents + "\tstatic " + strip_name(&class.name) + "& _new();\n"; contents = contents + "\tvoid _destroy();\n\n"; @@ -227,7 +233,7 @@ fn generate_class_content(forward_declares: &String, class: &GodotClass) -> Stri contents = contents + "namespace godot {\n\n"; contents = contents + "" + strip_name(&class.name) + "& " + strip_name(&class.name) + "::_new() {\n"; - contents = contents + "\tObject *ptr = ClassDB::instance(\"" + class.name.as_str() + "\");\n"; + contents = contents + "\tObject ptr = ClassDB::instance(\"" + class.name.as_str() + "\");\n"; contents = contents + "\treturn reinterpret_cast<" + strip_name(&class.name) + "&>(ptr);"; contents = contents + "}\n\n"; @@ -235,6 +241,15 @@ fn generate_class_content(forward_declares: &String, class: &GodotClass) -> Stri contents = contents + "\tgodot_object_destroy(__core_object);\n"; contents = contents + "}\n\n\n"; + if class.base_class == "" { + // Object + contents = contents + "Variant::operator Object()const {\n\n"; + + contents = contents + "\treturn Object(godot_variant_as_object(&_godot_variant));\n\n"; + + contents = contents + "}\n\n"; + } + contents = contents + "}\n"; } @@ -260,6 +275,7 @@ fn escape_cpp(name: &String) -> &str { match name.as_str() { "class" => "_class", "char" => "_char", + "short" => "_short", x => x } } \ No newline at end of file diff --git a/include/godot_cpp/Godot.h b/include/godot_cpp/Godot.h new file mode 100644 index 00000000..49021c13 --- /dev/null +++ b/include/godot_cpp/Godot.h @@ -0,0 +1,280 @@ +#ifndef GODOT_H +#define GODOT_H + +#include + +#include + + +#include + +namespace godot { + + +#define GODOT_CLASS(Name, Base) \ + public: static char *___get_type_name() { return (char *) #Name; } \ + static char *___get_base_type_name() { return (char *) #Base; } \ + Name(godot_object *o) { __core_object = o; } \ + private: + + + +template +A object_cast(B b) +{ + A *a = (A*) &b; + return *a; +} + + + + + +// instance and destroy funcs + +template +void *_godot_class_instance_func(godot_object *p) +{ + T *d = new T(p); + d->_init(); + return d; +} + +template +void _godot_class_destroy_func(godot_object *p, void *data) +{ + T *d = (T *) data; + delete d; +} + + + +template +void register_class() +{ + godot_script_register(T::___get_type_name(), T::___get_base_type_name(), _godot_class_instance_func, _godot_class_destroy_func); + T::_register_methods(); +} + + + + + + +// wrapped methods + +template +struct WrappedMethod5 { + static godot_variant wrapped_method(godot_object *_, void *data, int num_args, godot_variant **args) + { + godot_variant _variant; + godot_variant_new_nil(&_variant); + godot::Variant *v = (godot::Variant *) &_variant; + T *obj = (T *) data; + godot::Variant **arg = (godot::Variant **) args; + *v = (obj->*p)(*arg[0], *arg[1], *arg[2], *arg[3], *arg[4]); + return _variant; + } +}; + +template +struct WrappedMethod5 { + static godot_variant wrapped_method(godot_object *_, void *data, int num_args, godot_variant **args) + { + godot_variant _variant; + godot_variant_new_nil(&_variant); + T *obj = (T *) data; + godot::Variant **arg = (godot::Variant **) args; + (obj->*p)(*arg[0], *arg[1], *arg[2], *arg[3], *arg[4]); + return _variant; + } +}; + +template +struct WrappedMethod4 { + static godot_variant wrapped_method(godot_object *_, void *data, int num_args, godot_variant **args) + { + godot_variant _variant; + godot_variant_new_nil(&_variant); + godot::Variant *v = (godot::Variant *) &_variant; + T *obj = (T *) data; + godot::Variant **arg = (godot::Variant **) args; + *v = (obj->*p)(*arg[0], *arg[1], *arg[2], *arg[3]); + return _variant; + } +}; + +template +struct WrappedMethod4 { + static godot_variant wrapped_method(godot_object *_, void *data, int num_args, godot_variant **args) + { + godot_variant _variant; + godot_variant_new_nil(&_variant); + T *obj = (T *) data; + godot::Variant **arg = (godot::Variant **) args; + (obj->*p)(*arg[0], *arg[1], *arg[2], *arg[3]); + return _variant; + } +}; + + +template +struct WrappedMethod3 { + static godot_variant wrapped_method(godot_object *_, void *data, int num_args, godot_variant **args) + { + godot_variant _variant; + godot_variant_new_nil(&_variant); + godot::Variant *v = (godot::Variant *) &_variant; + T *obj = (T *) data; + godot::Variant **arg = (godot::Variant **) args; + *v = (obj->*p)(*arg[0], *arg[1], *arg[2]); + return _variant; + } +}; + +template +struct WrappedMethod3 { + static godot_variant wrapped_method(godot_object *_, void *data, int num_args, godot_variant **args) + { + godot_variant _variant; + godot_variant_new_nil(&_variant); + T *obj = (T *) data; + godot::Variant **arg = (godot::Variant **) args; + (obj->*p)(*arg[0], *arg[1], *arg[2]); + return _variant; + } +}; + +template +struct WrappedMethod2 { + static godot_variant wrapped_method(godot_object *_, void *data, int num_args, godot_variant **args) + { + godot_variant _variant; + godot_variant_new_nil(&_variant); + godot::Variant *v = (godot::Variant *) &_variant; + T *obj = (T *) data; + godot::Variant **arg = (godot::Variant **) args; + *v = (obj->*p)(*arg[0], *arg[1]); + return _variant; + } +}; + +template +struct WrappedMethod2 { + static godot_variant wrapped_method(godot_object *_, void *data, int num_args, godot_variant **args) + { + godot_variant _variant; + godot_variant_new_nil(&_variant); + T *obj = (T *) data; + godot::Variant **arg = (godot::Variant **) args; + (obj->*p)(*arg[0], *arg[1]); + return _variant; + } +}; + +template +struct WrappedMethod1 { + static godot_variant wrapped_method(godot_object *_, void *data, int num_args, godot_variant **args) + { + godot_variant _variant; + godot_variant_new_nil(&_variant); + godot::Variant *v = (godot::Variant *) &_variant; + T *obj = (T *) data; + godot::Variant **arg = (godot::Variant **) args; + *v = (obj->*p)(*arg[0]); + return _variant; + } +}; + +template +struct WrappedMethod1 { + static godot_variant wrapped_method(godot_object *_, void *data, int num_args, godot_variant **args) + { + godot_variant _variant; + godot_variant_new_nil(&_variant); + T *obj = (T *) data; + godot::Variant **arg = (godot::Variant **) args; + (obj->*p)(*arg[0]); + return _variant; + } +}; + + + +template +struct WrappedMethod0 { + static godot_variant wrapped_method(godot_object *_, void *data, int num_args, godot_variant **args) + { + godot_variant _variant; + godot_variant_new_nil(&_variant); + godot::Variant *v = (godot::Variant *) &_variant; + T *obj = (T *) data; + *v = (obj->*p)(); + return _variant; + } +}; + +template +struct WrappedMethod0 { + static godot_variant wrapped_method(godot_object *_, void *data, int num_args, godot_variant **args) + { + godot_variant _variant; + godot_variant_new_nil(&_variant); + T *obj = (T *) data; + (obj->*p)(); + return _variant; + } +}; + + + +template +struct WrappedMethod2; + +template +struct WrappedMethod3; + +template +struct WrappedMethod4; + +template +struct WrappedMethod5; + +// method registering + + +template +void register_method(char *name, godot_method_attributes attr = {}) { + godot_script_add_method(T::___get_type_name(), name, &attr, &WrappedMethod0::wrapped_method); +} + +template +void register_method(char *name, godot_method_attributes attr = {}) { + godot_script_add_method(T::___get_type_name(), name, &attr, &WrappedMethod1::wrapped_method); +} + +template +void register_method(char *name, godot_method_attributes attr = {}) { + godot_script_add_method(T::___get_type_name(), name, &attr, &WrappedMethod2::wrapped_method); +} + +template +void register_method(char *name, godot_method_attributes attr = {}) { + godot_script_add_method(T::___get_type_name(), name, &attr, &WrappedMethod3::wrapped_method); +} + +template +void register_method(char *name, godot_method_attributes attr = {}) { + godot_script_add_method(T::___get_type_name(), name, &attr, &WrappedMethod4::wrapped_method); +} + + +template +void register_method(char *name, godot_method_attributes attr = {}) { + godot_script_add_method(T::___get_type_name(), name, &attr, &WrappedMethod5::wrapped_method); +} + + +} + +#endif // GODOT_H diff --git a/include/godot_cpp/core/Variant.h b/include/godot_cpp/core/Variant.h index 7d4b103c..b0349060 100644 --- a/include/godot_cpp/core/Variant.h +++ b/include/godot_cpp/core/Variant.h @@ -409,11 +409,7 @@ public: godot_input_event s = godot_variant_as_input_event(&_godot_variant); return *(InputEvent *) &s; } - operator Object*() const - { - godot_object *s = godot_variant_as_object(&_godot_variant); - return (Object *) s; - } + operator Object() const; operator Dictionary() const; operator Array() const;