From 55ce1da2dc277dc989a7265641d7c1c560d77dd1 Mon Sep 17 00:00:00 2001 From: QuentinCaffeino Date: Thu, 22 Feb 2018 23:16:25 +0100 Subject: [PATCH 01/14] Added crosscompiling for linux-windows; Added makefile --- Makefile | 48 +++++++++++++++++++++++++++ SConstruct | 97 +++++++++++++++++++++++++++++++++++++----------------- 2 files changed, 115 insertions(+), 30 deletions(-) create mode 100644 Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 00000000..a3285edc --- /dev/null +++ b/Makefile @@ -0,0 +1,48 @@ + +GODOT_BIN_PATH = ../godot_fork/bin/godot.x11.tools.64.llvm +HEADERS = ../godot_headers +TARGET = debug +NAME = godot-cpp + +BASE = scons n=$(NAME) generate_bindings=yes target=$(TARGET) headers=$(HEADERS) godotbinpath=godot -j4 +LINUX = $(BASE) p=linux +WINDOWS = $(BASE) p=windows +OSX = $(BASE) p=osx + + +all: + make linux + make windows + + +linux: + make linux32 + make linux64 + +linux32: SConstruct + $(LINUX) a=32 + +linux64: SConstruct + $(LINUX) a=64 + + +windows: + make windows32 + make windows64 + +windows32: SConstruct + $(WINDOWS) a=32 + +windows64: SConstruct + $(WINDOWS) a=64 + + +osx: + make osx32 + make osx64 + +osx32: SConstruct + $(OSX) a=32 + +osx64: SConstruct + $(OSX) a=64 diff --git a/SConstruct b/SConstruct index 50a63cdf..1c36cc09 100644 --- a/SConstruct +++ b/SConstruct @@ -1,48 +1,83 @@ #!python -import os, subprocess + +import os, subprocess, platform +def add_source(sources, name): + sources.append(name) + +def add_sources(sources, dir, extension): + for f in os.listdir(dir): + if f.endswith('.' + extension): + sources.append(dir + '/' + f) + + +env = Environment() +host_platform = platform.system() +target_platform = ARGUMENTS.get('p', ARGUMENTS.get('platform', 'linux')) +target_arch = ARGUMENTS.get('a', ARGUMENTS.get('arch', '64')) +# default to debug build, must be same setting as used for cpp_bindings +target = ARGUMENTS.get('target', 'debug') # Local dependency paths, adapt them to your setup -godot_headers_path = ARGUMENTS.get("headers", os.getenv("GODOT_HEADERS", "../godot_headers/")) -godot_bin_path = ARGUMENTS.get("godotbinpath", os.getenv("GODOT_BIN_PATH", "../godot_fork/bin/godot.x11.tools.64.llvm")) +godot_headers = ARGUMENTS.get('headers', '../godot_headers') +godot_bin_path = ARGUMENTS.get('godotbinpath', os.getenv('GODOT_BIN_PATH', '../godot_fork/bin/godot.x11.tools.64.llvm')) +result_path = 'bin/' +result_name = ARGUMENTS.get('n', ARGUMENTS.get('name', os.path.relpath('.', '..'))) -target = ARGUMENTS.get("target", "debug") -platform = ARGUMENTS.get("p", ARGUMENTS.get("platform", "linux")) # This makes sure to keep the session environment variables on windows, # that way you can run scons in a vs 2017 prompt and it will find all the required tools env = Environment() -if platform == "windows": +if target_platform == 'windows': env = Environment(ENV = os.environ) -if ARGUMENTS.get("use_llvm", "no") == "yes": - env["CXX"] = "clang++" - -def add_sources(sources, directory): - for file in os.listdir(directory): - if file.endswith('.cpp'): - sources.append(directory + '/' + file) +if ARGUMENTS.get('use_llvm', 'no') == 'yes': + env['CXX'] = 'clang++' -if platform == "osx": - env.Append(CCFLAGS = ['-g','-O3', '-std=c++14', '-arch', 'x86_64']) - env.Append(LINKFLAGS = ['-arch', 'x86_64', '-framework', 'Cocoa', '-Wl,-undefined,dynamic_lookup']) +if target_platform == 'linux': + result_name += '.linux.' + target_arch -if platform == "linux": - env.Append(CCFLAGS = ['-fPIC', '-g','-O3', '-std=c++14']) + env['CXX']='gcc-5' + env.Append(CCFLAGS = [ '-fPIC', '-g', '-O3', '-std=c++14', '-Wwrite-strings' ]) + env.Append(LINKFLAGS = [ '-Wl,-R,\'$$ORIGIN\'' ]) -env.Append(CPPPATH=['.', godot_headers_path, 'include', 'include/core']) + if target_arch == '32': + env.Append(CCFLAGS = [ '-m32' ]) + env.Append(LINKFLAGS = [ '-m32' ]) + elif target_arch == '64': + env.Append(CCFLAGS = [ '-m64' ]) + env.Append(LINKFLAGS = [ '-m64' ]) -if platform == "windows": - if target == "debug": - env.Append(CCFLAGS = ['-EHsc', '-D_DEBUG', '/MDd']) +elif target_platform == 'windows': + result_name += '.windows.' + target_arch + + if host_platform == 'Windows': + result_name += '.dll' + + env.Append(LINKFLAGS = [ '/WX' ]) + if target == 'debug': + env.Append(CCFLAGS = ['-EHsc', '-D_DEBUG', '/MDd' ]) + else: + env.Append(CCFLAGS = ['-O2', '-EHsc', '-DNDEBUG', '/MD' ]) else: - env.Append(CCFLAGS = ['-O2', '-EHsc', '-DNDEBUG', '/MD']) + if target_arch == '32': + env['CXX']='i686-w64-mingw32-g++' + elif target_arch == '64': + env['CXX']='x86_64-w64-mingw32-g++' -sources = [] -add_sources(sources, "src/core") + env.Append(CCFLAGS = [ '-g', '-O3', '-std=c++14', '-Wwrite-strings' ]) + env.Append(LINKFLAGS = [ '--static', '-Wl,--no-undefined', '-static-libgcc', '-static-libstdc++' ]) -if ARGUMENTS.get("generate_bindings", "no") == "yes": +elif platform == 'osx': + env.Append(CCFLAGS = [ '-g','-O3', '-std=c++14', '-arch', 'x86_64' ]) + env.Append(LINKFLAGS = [ '-arch', 'x86_64', '-framework', 'Cocoa', '-Wl,-undefined,dynamic_lookup' ]) + + +env.Append(CPPPATH=['.', godot_headers, 'include', 'include/core']) + + +if ARGUMENTS.get('generate_bindings', 'no') == 'yes': # TODO Generating the API should be done only if the Godot build is more recent than the JSON file json_api_file = os.path.join(os.getcwd(), 'godot_api.json') @@ -52,11 +87,13 @@ if ARGUMENTS.get("generate_bindings", "no") == "yes": import binding_generator - binding_generator.generate_bindings(json_api_file) -add_sources(sources, "src") -library = env.StaticLibrary(target='bin/godot_cpp_bindings', source=sources) +sources = [] +add_sources(sources, 'src/core', 'cpp') +add_sources(sources, 'src', 'cpp') + + +library = env.StaticLibrary(target=result_path + '/' + result_name, source=sources) Default(library) - From 38cfd627f394862bf09ce9c44bc270f3b230ad98 Mon Sep 17 00:00:00 2001 From: QuentinCaffeino Date: Thu, 22 Feb 2018 23:20:25 +0100 Subject: [PATCH 02/14] Fixed godo bin path in makefile --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index a3285edc..976f0022 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ HEADERS = ../godot_headers TARGET = debug NAME = godot-cpp -BASE = scons n=$(NAME) generate_bindings=yes target=$(TARGET) headers=$(HEADERS) godotbinpath=godot -j4 +BASE = scons n=$(NAME) generate_bindings=yes target=$(TARGET) headers=$(HEADERS) godotbinpath=$(GODOT_BIN_PATH) -j4 LINUX = $(BASE) p=linux WINDOWS = $(BASE) p=windows OSX = $(BASE) p=osx From 43f57c7f80a3014fa8d9491420ca33ff93968625 Mon Sep 17 00:00:00 2001 From: QuentinCaffeino Date: Sun, 25 Feb 2018 16:25:42 +0100 Subject: [PATCH 03/14] Bindings are generated by default; Replaced generate with regenerate flag; Updated readme --- Makefile | 3 ++- README.md | 57 +++++++++++++++++++++++------------------------------- SConstruct | 14 +++++--------- 3 files changed, 31 insertions(+), 43 deletions(-) diff --git a/Makefile b/Makefile index 976f0022..95989ff9 100644 --- a/Makefile +++ b/Makefile @@ -1,10 +1,11 @@ GODOT_BIN_PATH = ../godot_fork/bin/godot.x11.tools.64.llvm +REGENERATE_BINDINGS = no HEADERS = ../godot_headers TARGET = debug NAME = godot-cpp -BASE = scons n=$(NAME) generate_bindings=yes target=$(TARGET) headers=$(HEADERS) godotbinpath=$(GODOT_BIN_PATH) -j4 +BASE = scons n=$(NAME) regenerate_bindings=$(REGENERATE_BINDINGS) target=$(TARGET) headers=$(HEADERS) godotbinpath=$(GODOT_BIN_PATH) -j4 LINUX = $(BASE) p=linux WINDOWS = $(BASE) p=windows OSX = $(BASE) p=osx diff --git a/README.md b/README.md index 918c39f8..4766a8ed 100644 --- a/README.md +++ b/README.md @@ -1,54 +1,45 @@ -# cpp_bindings +# godot-cpp C++ bindings for the Godot script API # Creating a GDNative library (Linux) Create a directory named `SimpleLibrary` with subdirectories `lib, src` -Getting latest `cpp_bindings` and `godot_headers` +Getting latest `godot-cpp` and `godot_headers` ``` -$ cd SimpleLibrary -$ git clone https://github.com/GodotNativeTools/cpp_bindings +$ git clone https://github.com/GodotNativeTools/godot-cpp $ git clone https://github.com/GodotNativeTools/godot_headers ``` -right now our directory structure should look like this +right now our directory structure should look like this: ``` -[SimpleLibrary] - ├── cpp_bindings/ - ├── godot_headers/ - ├── lib/ - └── src/ +godot-cpp +godot_headers +SimpleLibrary +├── lib/ +└── src/ ``` Now to generate cpp bindings ``` -$ cd cpp_bindings -``` - -Edit `SConstruct` file and assign your godot executable path at line:7 `godot_bin_path = "../godot_fork/bin/"`, - -Building cpp_bindings -``` -$ scons godotbinpath="../godot_fork/bin/godot_binary" headers="../godot_headers/" p=linux generate_bindings=yes +$ cd godot-cpp +$ scons godotbinpath="../godot_fork/bin/godot_binary" p=linux +$ cd .. ``` resulting libraries will be placed under `bin/` and the generated headers will be placed under `include/*` **Note:** -> `generate_bindings=yes` is used to generate C++ bindings (`godot_api.json` - Godot API) +> `regenerate_bindings=yes` is used to force regenerating C++ bindings (`godot_api.json` - Godot API) + > Include `use_llvm=yes` for using clang++ -Copy binding libraries into the `SimpleLibrary/lib` folder -``` -$ cd .. -$ cp cpp_bindings/bin/libgodot_cpp_bindings.a lib/ -``` + And our directory structure will be ``` -[SimpleLibrary] - ├── cpp_bindings/ - ├── godot_headers/ - ├── lib/ - │ └──libgodot_cpp_bindings.a - └── src/ +godot-cpp +└── bin/libgodot-cpp.a +godot_headers +SimpleLibrary +├── lib/ +└── src/ ``` # Creating simple class @@ -115,9 +106,9 @@ extern "C" void GDN_EXPORT godot_nativescript_init(void *handle) # Compiling ``` -$ cd .. -$ clang -fPIC -o src/init.os -c src/init.cpp -g -O3 -std=c++14 -Icpp_bindings/include -Igodot_headers -$ clang -o lib/libtest.so -shared src/init.os -Llib -lgodot_cpp_bindings +$ cd SimpleLibrary +$ clang -fPIC -o src/init.os -c src/init.cpp -g -O3 -std=c++14 -I../godot-cpp/include -Igodot_headers +$ clang -o lib/libtest.so -shared src/init.os -L../godot-cpp/lib -lgodot-cpp ``` This creates the file `libtest.so` in your `SimpleLibrary/lib` directory. For windows you need to find out what compiler flags need to be used. diff --git a/SConstruct b/SConstruct index 1c36cc09..2171328e 100644 --- a/SConstruct +++ b/SConstruct @@ -3,9 +3,6 @@ import os, subprocess, platform -def add_source(sources, name): - sources.append(name) - def add_sources(sources, dir, extension): for f in os.listdir(dir): if f.endswith('.' + extension): @@ -21,7 +18,7 @@ target = ARGUMENTS.get('target', 'debug') # Local dependency paths, adapt them to your setup godot_headers = ARGUMENTS.get('headers', '../godot_headers') godot_bin_path = ARGUMENTS.get('godotbinpath', os.getenv('GODOT_BIN_PATH', '../godot_fork/bin/godot.x11.tools.64.llvm')) -result_path = 'bin/' +result_path = 'bin' result_name = ARGUMENTS.get('n', ARGUMENTS.get('name', os.path.relpath('.', '..'))) @@ -53,7 +50,7 @@ elif target_platform == 'windows': result_name += '.windows.' + target_arch if host_platform == 'Windows': - result_name += '.dll' + result_name += '.lib' env.Append(LINKFLAGS = [ '/WX' ]) if target == 'debug': @@ -77,10 +74,9 @@ elif platform == 'osx': env.Append(CPPPATH=['.', godot_headers, 'include', 'include/core']) -if ARGUMENTS.get('generate_bindings', 'no') == 'yes': - # TODO Generating the API should be done only if the Godot build is more recent than the JSON file - json_api_file = os.path.join(os.getcwd(), 'godot_api.json') - +# Generate bindings +json_api_file = os.path.join(os.getcwd(), 'godot_api.json') +if os.path.exists(json_api_file) == False or ARGUMENTS.get('regenerate_bindings', 'no') == 'yes': subprocess.call([os.path.expanduser(godot_bin_path), '--gdnative-generate-json-api', json_api_file]) # actually create the bindings here From af2861faf7073df6da34a0a272d96dde427b770d Mon Sep 17 00:00:00 2001 From: QuentinCaffeino Date: Sun, 25 Feb 2018 17:03:55 +0100 Subject: [PATCH 04/14] use_llvm fix --- SConstruct | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/SConstruct b/SConstruct index 2171328e..b251d432 100644 --- a/SConstruct +++ b/SConstruct @@ -28,14 +28,14 @@ env = Environment() if target_platform == 'windows': env = Environment(ENV = os.environ) -if ARGUMENTS.get('use_llvm', 'no') == 'yes': - env['CXX'] = 'clang++' - if target_platform == 'linux': result_name += '.linux.' + target_arch env['CXX']='gcc-5' + if ARGUMENTS.get('use_llvm', 'no') == 'yes': + env['CXX'] = 'clang++' + env.Append(CCFLAGS = [ '-fPIC', '-g', '-O3', '-std=c++14', '-Wwrite-strings' ]) env.Append(LINKFLAGS = [ '-Wl,-R,\'$$ORIGIN\'' ]) @@ -67,6 +67,9 @@ elif target_platform == 'windows': env.Append(LINKFLAGS = [ '--static', '-Wl,--no-undefined', '-static-libgcc', '-static-libstdc++' ]) elif platform == 'osx': + if ARGUMENTS.get('use_llvm', 'no') == 'yes': + env['CXX'] = 'clang++' + env.Append(CCFLAGS = [ '-g','-O3', '-std=c++14', '-arch', 'x86_64' ]) env.Append(LINKFLAGS = [ '-arch', 'x86_64', '-framework', 'Cocoa', '-Wl,-undefined,dynamic_lookup' ]) From 5162132236b1cbdd8a4f284ca102bea1e4320fd8 Mon Sep 17 00:00:00 2001 From: Marcelo Fernandez Date: Thu, 1 Mar 2018 20:30:55 -0300 Subject: [PATCH 05/14] Fix compile problem on OS X --- SConstruct | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SConstruct b/SConstruct index b251d432..18fb5709 100644 --- a/SConstruct +++ b/SConstruct @@ -66,7 +66,7 @@ elif target_platform == 'windows': env.Append(CCFLAGS = [ '-g', '-O3', '-std=c++14', '-Wwrite-strings' ]) env.Append(LINKFLAGS = [ '--static', '-Wl,--no-undefined', '-static-libgcc', '-static-libstdc++' ]) -elif platform == 'osx': +elif target_platform == 'osx': if ARGUMENTS.get('use_llvm', 'no') == 'yes': env['CXX'] = 'clang++' From a212746a3d249929eff1c4ca2fee4a2186ccf794 Mon Sep 17 00:00:00 2001 From: karroffel Date: Fri, 2 Mar 2018 19:03:57 +0100 Subject: [PATCH 06/14] add clang option to makefile --- Makefile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 95989ff9..f8f4d1fa 100644 --- a/Makefile +++ b/Makefile @@ -4,8 +4,9 @@ REGENERATE_BINDINGS = no HEADERS = ../godot_headers TARGET = debug NAME = godot-cpp +USE_CLANG = no -BASE = scons n=$(NAME) regenerate_bindings=$(REGENERATE_BINDINGS) target=$(TARGET) headers=$(HEADERS) godotbinpath=$(GODOT_BIN_PATH) -j4 +BASE = scons n=$(NAME) use_llvm=$(USE_CLANG) regenerate_bindings=$(REGENERATE_BINDINGS) target=$(TARGET) headers=$(HEADERS) godotbinpath=$(GODOT_BIN_PATH) -j4 LINUX = $(BASE) p=linux WINDOWS = $(BASE) p=windows OSX = $(BASE) p=osx From 2343a8a54c13c2ccc617ed6716419dd568bb997f Mon Sep 17 00:00:00 2001 From: karroffel Date: Fri, 2 Mar 2018 19:04:57 +0100 Subject: [PATCH 07/14] fix #101 --- include/core/NodePath.hpp | 2 ++ src/core/NodePath.cpp | 12 ++++++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/include/core/NodePath.hpp b/include/core/NodePath.hpp index 026414bf..e9e60ac7 100644 --- a/include/core/NodePath.hpp +++ b/include/core/NodePath.hpp @@ -36,6 +36,8 @@ public: void operator =(const NodePath& other); + bool operator ==(const NodePath& other); + ~NodePath(); }; diff --git a/src/core/NodePath.cpp b/src/core/NodePath.cpp index 25ce8820..b1fb9ccc 100644 --- a/src/core/NodePath.cpp +++ b/src/core/NodePath.cpp @@ -17,7 +17,6 @@ NodePath::NodePath(const NodePath &other) { String from = other; godot::api->godot_node_path_new(&_node_path, (godot_string *) &from); - godot::api->godot_node_path_operator_equal(&_node_path, &other._node_path); } NodePath::NodePath(const String &from) @@ -71,9 +70,18 @@ NodePath::operator String() const return *(String *) &str; } +bool NodePath::operator ==(const NodePath& other) +{ + return godot::api->godot_node_path_operator_equal(&_node_path, &other._node_path); +} + void NodePath::operator =(const NodePath& other) { - godot::api->godot_node_path_operator_equal(&_node_path, &other._node_path); + godot::api->godot_node_path_destroy(&_node_path); + + String other_string = (String) other; + + godot::api->godot_node_path_new(&_node_path, (godot_string *) &other_string); } NodePath::~NodePath() From 98e5f78fa0d2ce173f2b189ef54bb0be4a009e44 Mon Sep 17 00:00:00 2001 From: Marcelo Fernandez Date: Mon, 5 Mar 2018 13:28:49 -0300 Subject: [PATCH 08/14] Fix compile issues on Windows --- SConstruct | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/SConstruct b/SConstruct index 18fb5709..d1e7880d 100644 --- a/SConstruct +++ b/SConstruct @@ -22,13 +22,6 @@ result_path = 'bin' result_name = ARGUMENTS.get('n', ARGUMENTS.get('name', os.path.relpath('.', '..'))) -# This makes sure to keep the session environment variables on windows, -# that way you can run scons in a vs 2017 prompt and it will find all the required tools -env = Environment() -if target_platform == 'windows': - env = Environment(ENV = os.environ) - - if target_platform == 'linux': result_name += '.linux.' + target_arch @@ -47,6 +40,13 @@ if target_platform == 'linux': env.Append(LINKFLAGS = [ '-m64' ]) elif target_platform == 'windows': + # This makes sure to keep the session environment variables on windows, + # that way you can run scons in a vs 2017 prompt and it will find all the required tools + if (target_arch == '64'): + env = Environment(ENV = os.environ, TARGET_ARCH='amd64') + else: + env = Environment(ENV = os.environ, TARGET_ARCH='x86') + result_name += '.windows.' + target_arch if host_platform == 'Windows': @@ -54,9 +54,9 @@ elif target_platform == 'windows': env.Append(LINKFLAGS = [ '/WX' ]) if target == 'debug': - env.Append(CCFLAGS = ['-EHsc', '-D_DEBUG', '/MDd' ]) + env.Append(CCFLAGS = ['/EHsc', '/D_DEBUG', '/MDd' ]) else: - env.Append(CCFLAGS = ['-O2', '-EHsc', '-DNDEBUG', '/MD' ]) + env.Append(CCFLAGS = ['/O2', '/EHsc', '/DNDEBUG', '/MD' ]) else: if target_arch == '32': env['CXX']='i686-w64-mingw32-g++' From c7b03c4132f9be5846141fb101f8f89522eb8efa Mon Sep 17 00:00:00 2001 From: danielytics Date: Tue, 6 Mar 2018 10:26:07 +0000 Subject: [PATCH 09/14] make register_signal and auto-generated variadic funcitons use variadic templates to streamline their use --- binding_generator.py | 14 +++++++++++--- include/core/Defs.hpp | 36 ++++++++++++++++++++++++++++++++++++ include/core/Godot.hpp | 5 +++++ 3 files changed, 52 insertions(+), 3 deletions(-) diff --git a/binding_generator.py b/binding_generator.py index 54c56f2d..a7d874df 100644 --- a/binding_generator.py +++ b/binding_generator.py @@ -115,6 +115,7 @@ def generate_class_header(used_classes, c): source.append("") + vararg_templates = "" # generate the class definition here source.append("class " + class_name + ("" if c["base_class"] == "" else (" : public " + strip_name(c["base_class"])) ) + " {") @@ -157,14 +158,18 @@ def generate_class_header(used_classes, c): method_signature += "static " if c["singleton"] else "" method_signature += make_gdnative_type(method["return_type"]) - method_signature += escape_cpp(method["name"]) + "(" + method_name = escape_cpp(method["name"]) + method_signature += method_name + "(" has_default_argument = False + method_arguments = "" for i, argument in enumerate(method["arguments"]): method_signature += "const " + make_gdnative_type(argument["type"]) - method_signature += escape_cpp(argument["name"]) + argument_name = escape_cpp(argument["name"]) + method_signature += argument_name + method_arguments += argument_name # default arguments @@ -210,10 +215,13 @@ def generate_class_header(used_classes, c): if i != len(method["arguments"]) - 1: method_signature += ", " + method_arguments += "," if method["has_varargs"]: if len(method["arguments"]) > 0: method_signature += ", " + method_arguments += ", " + vararg_templates += "\ttemplate " + method_signature + "Args... args){\n\t\treturn " + method_name + "(" + method_arguments + "godot::helpers::append_all(Array(), args...));\n\t}\n""" method_signature += "const Array& __var_args = Array()" method_signature += ")" + (" const" if method["is_const"] and not c["singleton"] else "") @@ -221,7 +229,7 @@ def generate_class_header(used_classes, c): source.append("\t" + method_signature + ";") - + source.append(vararg_templates) source.append("};") source.append("") diff --git a/include/core/Defs.hpp b/include/core/Defs.hpp index f8c76d3e..0306219c 100644 --- a/include/core/Defs.hpp +++ b/include/core/Defs.hpp @@ -58,6 +58,42 @@ enum Error { ERR_WTF = ERR_OMFG_THIS_IS_VERY_VERY_BAD ///< short version of the above }; + namespace helpers { + template + T append_all (T appendable, ValueT value) { + appendable.append(value); + return appendable; + } + + template + T append_all (T appendable, ValueT value, Args... args) { + appendable.append(value); + return append_all(appendable, args...); + } + + template + T append_all (T appendable) { + return appendable; + } + + template + KV add_all (KV kv, KeyT key, ValueT value) { + kv[key] = value; + return kv; + } + + template + KV add_all (KV kv, KeyT key, ValueT value, Args... args) { + kv[key] = value; + return add_all(kv, args...); + } + + template + KV add_all (KV kv) { + return kv; + } + } + } #include diff --git a/include/core/Godot.hpp b/include/core/Godot.hpp index 6d0289f6..8c463b69 100644 --- a/include/core/Godot.hpp +++ b/include/core/Godot.hpp @@ -484,6 +484,11 @@ void register_signal(String name, Dictionary args = Dictionary()) } } +template +void register_signal(String name, Args... varargs) +{ + register_signal(name, helpers::add_all(Dictionary(), varargs...)); +} } From 01db553c49345b683ff9da3283bc92a8c2933af2 Mon Sep 17 00:00:00 2001 From: danielytics Date: Wed, 7 Mar 2018 10:27:34 +0000 Subject: [PATCH 10/14] adds Array::make and Dictionary::make static methods and has variadic template functions use those --- binding_generator.py | 2 +- include/core/Array.hpp | 6 ++++++ include/core/Dictionary.hpp | 5 +++++ include/core/Godot.hpp | 2 +- 4 files changed, 13 insertions(+), 2 deletions(-) diff --git a/binding_generator.py b/binding_generator.py index a7d874df..c39e7edd 100644 --- a/binding_generator.py +++ b/binding_generator.py @@ -221,7 +221,7 @@ def generate_class_header(used_classes, c): if len(method["arguments"]) > 0: method_signature += ", " method_arguments += ", " - vararg_templates += "\ttemplate " + method_signature + "Args... args){\n\t\treturn " + method_name + "(" + method_arguments + "godot::helpers::append_all(Array(), args...));\n\t}\n""" + vararg_templates += "\ttemplate " + method_signature + "Args... args){\n\t\treturn " + method_name + "(" + method_arguments + "Array::make(args...));\n\t}\n""" method_signature += "const Array& __var_args = Array()" method_signature += ")" + (" const" if method["is_const"] and not c["singleton"] else "") diff --git a/include/core/Array.hpp b/include/core/Array.hpp index 23205686..ead3ba3b 100644 --- a/include/core/Array.hpp +++ b/include/core/Array.hpp @@ -3,6 +3,7 @@ #include +#include "Defs.hpp" #include "String.hpp" namespace godot { @@ -39,6 +40,11 @@ public: Array(const PoolColorArray& a); + template + static Array make(Args... args) { + return helpers::append_all(Array(), args...); + } + Variant& operator [](const int idx); Variant operator [](const int idx) const; diff --git a/include/core/Dictionary.hpp b/include/core/Dictionary.hpp index ec496af6..613d6ce3 100644 --- a/include/core/Dictionary.hpp +++ b/include/core/Dictionary.hpp @@ -16,6 +16,11 @@ public: Dictionary(const Dictionary & other); Dictionary & operator=(const Dictionary & other); + template + static Dictionary make(Args... args) { + return helpers::add_all(Dictionary(), args...); + } + void clear(); bool empty() const; diff --git a/include/core/Godot.hpp b/include/core/Godot.hpp index 8c463b69..01bee09f 100644 --- a/include/core/Godot.hpp +++ b/include/core/Godot.hpp @@ -487,7 +487,7 @@ void register_signal(String name, Dictionary args = Dictionary()) template void register_signal(String name, Args... varargs) { - register_signal(name, helpers::add_all(Dictionary(), varargs...)); + register_signal(name, Dictionary::make(varargs...)); } } From 85e73de2e0a19160b67430162bd7ec7c3c672b89 Mon Sep 17 00:00:00 2001 From: danielytics Date: Wed, 7 Mar 2018 10:34:25 +0000 Subject: [PATCH 11/14] adds an example of registering signal with arguments to README --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 4766a8ed..e2d4163b 100644 --- a/README.md +++ b/README.md @@ -78,6 +78,7 @@ public: /** For registering signal **/ // register_signal("signal_name"); + // register_signal("signal_name", "string_argument", GODOT_VARIANT_TYPE_STRING) } String _name; From e74b8f593ecd412ee410f538ac88aa8b91d24b33 Mon Sep 17 00:00:00 2001 From: danielytics Date: Wed, 7 Mar 2018 12:21:33 +0000 Subject: [PATCH 12/14] adds variadic printing function, makes String.format custom placeholder work --- include/core/GodotGlobal.hpp | 6 ++++++ include/core/String.hpp | 2 ++ src/core/String.cpp | 11 ++++++++++- 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/include/core/GodotGlobal.hpp b/include/core/GodotGlobal.hpp index 7d28dba3..a9133593 100644 --- a/include/core/GodotGlobal.hpp +++ b/include/core/GodotGlobal.hpp @@ -3,6 +3,7 @@ #include #include "String.hpp" +#include "Array.hpp" namespace godot { @@ -20,6 +21,11 @@ public: static void gdnative_init(godot_gdnative_init_options *o); static void gdnative_terminate(godot_gdnative_terminate_options *o); static void nativescript_init(void *handle); + + template + static void print(const String& fmt, Args... values) { + print(fmt.format(Array::make(values...))); + } }; diff --git a/include/core/String.hpp b/include/core/String.hpp index de65f3cf..7826fcb6 100644 --- a/include/core/String.hpp +++ b/include/core/String.hpp @@ -81,6 +81,7 @@ public: int find(String what, int from = 0) const; int find_last(String what) const; int findn(String what, int from = 0) const; + String format(Variant values) const; String format(Variant values, String placeholder) const; String get_base_dir() const; String get_basename() const; @@ -128,6 +129,7 @@ public: String to_upper() const; String xml_escape() const; String xml_unescape() const; + }; String operator+(const char *a, const String &b); diff --git a/src/core/String.cpp b/src/core/String.cpp index 3d822b1c..737c0408 100644 --- a/src/core/String.cpp +++ b/src/core/String.cpp @@ -267,13 +267,22 @@ int String::findn(String what, int from) const { return godot::api->godot_string_findn(&_godot_string, what._godot_string); } -String String::format(Variant values, String placeholder) const { +String String::format(Variant values) const { String new_string; new_string._godot_string = godot::api->godot_string_format(&_godot_string, (godot_variant *)&values); return new_string; } +String String::format(Variant values, String placeholder) const { + String new_string; + godot_char_string contents = godot::api->godot_string_utf8(&placeholder._godot_string); + new_string._godot_string = godot::api->godot_string_format_with_custom_placeholder(&_godot_string, (godot_variant *)&values, godot::api->godot_char_string_get_data(&contents)); + godot::api->godot_char_string_destroy(&contents); + + return new_string; +} + String String::get_base_dir() const { String new_string; new_string._godot_string = godot::api->godot_string_get_base_dir(&_godot_string); From a6689b21322e976488aa7482aafb748b47ca8158 Mon Sep 17 00:00:00 2001 From: Gary Oberbrunner Date: Tue, 13 Mar 2018 10:07:50 -0400 Subject: [PATCH 13/14] Make all Pool*Array::operator[] as const This is needed since that operator returns a local copy, not an lvalue. Attempting to write to the return value of these operators wouldn't change the array element. PoolVectors need locking when writing, so this operator can't return a writable reference. To update a Pool*Array, use the `set()` method which locks and unlocks the array. For multiple writes, use the `write()` method which returns a locked writable view, and unlocks when it goes out of scope. --- include/core/PoolArrays.hpp | 14 +++++++------- src/core/PoolArrays.cpp | 14 +++++++------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/include/core/PoolArrays.hpp b/include/core/PoolArrays.hpp index 78a370d1..fadcfad0 100644 --- a/include/core/PoolArrays.hpp +++ b/include/core/PoolArrays.hpp @@ -104,7 +104,7 @@ public: void set(const int idx, const uint8_t data); - uint8_t operator [](const int idx); + const uint8_t operator [](const int idx); int size() const; @@ -200,7 +200,7 @@ public: void set(const int idx, const int data); - int operator [](const int idx); + const int operator [](const int idx); int size() const; @@ -296,7 +296,7 @@ public: void set(const int idx, const real_t data); - real_t operator [](const int idx); + const real_t operator [](const int idx); int size() const; @@ -392,7 +392,7 @@ public: void set(const int idx, const String& data); - String operator [](const int idx); + const String operator [](const int idx); int size() const; @@ -489,7 +489,7 @@ public: void set(const int idx, const Vector2& data); - Vector2 operator [](const int idx); + const Vector2 operator [](const int idx); int size() const; @@ -585,7 +585,7 @@ public: void set(const int idx, const Vector3& data); - Vector3 operator [](const int idx); + const Vector3 operator [](const int idx); int size() const; @@ -681,7 +681,7 @@ public: void set(const int idx, const Color& data); - Color operator [](const int idx); + const Color operator [](const int idx); int size() const; diff --git a/src/core/PoolArrays.cpp b/src/core/PoolArrays.cpp index d66de4ce..8a6f9f80 100644 --- a/src/core/PoolArrays.cpp +++ b/src/core/PoolArrays.cpp @@ -86,7 +86,7 @@ void PoolByteArray::set(const int idx, const uint8_t data) godot::api->godot_pool_byte_array_set(&_godot_array, idx, data); } -uint8_t PoolByteArray::operator [](const int idx) +const uint8_t PoolByteArray::operator [](const int idx) { return godot::api->godot_pool_byte_array_get(&_godot_array, idx); } @@ -180,7 +180,7 @@ void PoolIntArray::set(const int idx, const int data) godot::api->godot_pool_int_array_set(&_godot_array, idx, data); } -int PoolIntArray::operator [](const int idx) +const int PoolIntArray::operator [](const int idx) { return godot::api->godot_pool_int_array_get(&_godot_array, idx); } @@ -273,7 +273,7 @@ void PoolRealArray::set(const int idx, const real_t data) godot::api->godot_pool_real_array_set(&_godot_array, idx, data); } -real_t PoolRealArray::operator [](const int idx) +const real_t PoolRealArray::operator [](const int idx) { return godot::api->godot_pool_real_array_get(&_godot_array, idx); } @@ -367,7 +367,7 @@ void PoolStringArray::set(const int idx, const String& data) godot::api->godot_pool_string_array_set(&_godot_array, idx, (godot_string *) &data); } -String PoolStringArray::operator [](const int idx) +const String PoolStringArray::operator [](const int idx) { String s; godot_string str = godot::api->godot_pool_string_array_get(&_godot_array, idx); @@ -465,7 +465,7 @@ void PoolVector2Array::set(const int idx, const Vector2& data) godot::api->godot_pool_vector2_array_set(&_godot_array, idx, (godot_vector2 *) &data); } -Vector2 PoolVector2Array::operator [](const int idx) +const Vector2 PoolVector2Array::operator [](const int idx) { Vector2 v; *(godot_vector2 *) &v = godot::api->godot_pool_vector2_array_get(&_godot_array, idx); @@ -561,7 +561,7 @@ void PoolVector3Array::set(const int idx, const Vector3& data) godot::api->godot_pool_vector3_array_set(&_godot_array, idx, (godot_vector3 *) &data); } -Vector3 PoolVector3Array::operator [](const int idx) +const Vector3 PoolVector3Array::operator [](const int idx) { Vector3 v; *(godot_vector3 *) &v = godot::api->godot_pool_vector3_array_get(&_godot_array, idx); @@ -656,7 +656,7 @@ void PoolColorArray::set(const int idx, const Color& data) godot::api->godot_pool_color_array_set(&_godot_array, idx, (godot_color *) &data); } -Color PoolColorArray::operator [](const int idx) +const Color PoolColorArray::operator [](const int idx) { Color v; *(godot_color *) &v = godot::api->godot_pool_color_array_get(&_godot_array, idx); From b52d55927a7df0d77d7c3b2974c9032767a6af17 Mon Sep 17 00:00:00 2001 From: Marcelo Fernandez Date: Sun, 8 Apr 2018 13:00:54 -0300 Subject: [PATCH 14/14] On OS X append .osx.64 to the result_name var on SConstruct --- SConstruct | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/SConstruct b/SConstruct index d1e7880d..fdec6b75 100644 --- a/SConstruct +++ b/SConstruct @@ -70,6 +70,10 @@ elif target_platform == 'osx': if ARGUMENTS.get('use_llvm', 'no') == 'yes': env['CXX'] = 'clang++' + # Only 64-bits is supported for OS X + target_arch = '64' + result_name += '.osx.' + target_arch + env.Append(CCFLAGS = [ '-g','-O3', '-std=c++14', '-arch', 'x86_64' ]) env.Append(LINKFLAGS = [ '-arch', 'x86_64', '-framework', 'Cocoa', '-Wl,-undefined,dynamic_lookup' ])