Browse Source

Merge branch 'master' of https://github.com/GodotNativeTools/godot-cpp

Nickolai Korshunov 7 years ago
parent
commit
ef37d8d76e

+ 50 - 0
Makefile

@@ -0,0 +1,50 @@
+
+GODOT_BIN_PATH = ../godot_fork/bin/godot.x11.tools.64.llvm
+REGENERATE_BINDINGS = no
+HEADERS = ../godot_headers
+TARGET = debug
+NAME = godot-cpp
+USE_CLANG = no
+
+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
+
+
+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

+ 25 - 33
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
@@ -87,6 +78,7 @@ public:
 
            /** For registering signal **/
            // register_signal<SimpleClass>("signal_name");
+           // register_signal<SimpleClass>("signal_name", "string_argument", GODOT_VARIANT_TYPE_STRING)
         }
 	
 	String _name;
@@ -115,9 +107,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.
 

+ 76 - 36
SConstruct

@@ -1,62 +1,102 @@
 #!python
-import os, subprocess
 
+import os, subprocess, platform
 
-# 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"))
 
-target = ARGUMENTS.get("target", "debug")
-platform = ARGUMENTS.get("p", ARGUMENTS.get("platform", "linux"))
+def add_sources(sources, dir, extension):
+  for f in os.listdir(dir):
+      if f.endswith('.' + extension):
+          sources.append(dir + '/' + f)
+
 
-# 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":
-    env = Environment(ENV = os.environ)
+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 = 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('.', '..')))
+
+
+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\'' ])
+
+    if target_arch == '32':
+        env.Append(CCFLAGS = [ '-m32' ])
+        env.Append(LINKFLAGS = [ '-m32' ])
+    elif target_arch == '64':
+        env.Append(CCFLAGS = [ '-m64' ])
+        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')
 
-if ARGUMENTS.get("use_llvm", "no") == "yes":
-    env["CXX"] = "clang++"
+    result_name += '.windows.' + target_arch
 
-def add_sources(sources, directory):
-    for file in os.listdir(directory):
-        if file.endswith('.cpp'):
-            sources.append(directory + '/' + file)
+    if host_platform == 'Windows':
+        result_name += '.lib'
 
+        env.Append(LINKFLAGS = [ '/WX' ])
+        if target == 'debug':
+            env.Append(CCFLAGS = ['/EHsc', '/D_DEBUG', '/MDd' ])
+        else:
+            env.Append(CCFLAGS = ['/O2', '/EHsc', '/DNDEBUG', '/MD' ])
+    else:
+        if target_arch == '32':
+            env['CXX']='i686-w64-mingw32-g++'
+        elif target_arch == '64':
+            env['CXX']='x86_64-w64-mingw32-g++'
 
-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'])
+        env.Append(CCFLAGS = [ '-g', '-O3', '-std=c++14', '-Wwrite-strings' ])
+        env.Append(LINKFLAGS = [ '--static', '-Wl,--no-undefined', '-static-libgcc', '-static-libstdc++' ])
 
-if platform == "linux":
-    env.Append(CCFLAGS = ['-fPIC', '-g','-O3', '-std=c++14'])
+elif target_platform == 'osx':
+    if ARGUMENTS.get('use_llvm', 'no') == 'yes':
+        env['CXX'] = 'clang++'
 
-env.Append(CPPPATH=['.', godot_headers_path, 'include', 'include/core'])
+    # Only 64-bits is supported for OS X
+    target_arch = '64'
+    result_name += '.osx.' + target_arch
 
-if platform == "windows":
-    if target == "debug":
-        env.Append(CCFLAGS = ['-EHsc', '-D_DEBUG', '/MDd'])
-    else:
-        env.Append(CCFLAGS = ['-O2', '-EHsc', '-DNDEBUG', '/MD'])
+    env.Append(CCFLAGS = [ '-g','-O3', '-std=c++14', '-arch', 'x86_64' ])
+    env.Append(LINKFLAGS = [ '-arch', 'x86_64', '-framework', 'Cocoa', '-Wl,-undefined,dynamic_lookup' ])
 
-sources = []
-add_sources(sources, "src/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')
+env.Append(CPPPATH=['.', godot_headers, 'include', 'include/core'])
 
+
+# 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
     
     import binding_generator
 
-    
     binding_generator.generate_bindings(json_api_file)
 
-add_sources(sources, "src")
 
-library = env.StaticLibrary(target='bin/godot_cpp_bindings', source=sources)
-Default(library)
+sources = []
+add_sources(sources, 'src/core', 'cpp')
+add_sources(sources, 'src', 'cpp')
+
 
+library = env.StaticLibrary(target=result_path + '/' + result_name, source=sources)
+Default(library)

+ 11 - 3
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 <class... Args> " + 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 "")
@@ -221,7 +229,7 @@ def generate_class_header(used_classes, c):
 
         source.append("\t" + method_signature + ";")
     
-    
+    source.append(vararg_templates)
     source.append("};")
     source.append("")
     

+ 6 - 0
include/core/Array.hpp

@@ -3,6 +3,7 @@
 
 #include <gdnative/array.h>
 
+#include "Defs.hpp"
 #include "String.hpp"
 
 namespace godot {
@@ -39,6 +40,11 @@ public:
 
 	Array(const PoolColorArray& a);
 
+	template <class... Args>
+	static Array make(Args... args) {
+		return helpers::append_all(Array(), args...);
+	}
+
 	Variant& operator [](const int idx);
 
 	Variant operator [](const int idx) const;

+ 36 - 0
include/core/Defs.hpp

@@ -58,6 +58,42 @@ enum class Error {
 	ERR_WTF = ERR_OMFG_THIS_IS_VERY_VERY_BAD ///< short version of the above
 };
 
+	namespace helpers {
+		template <typename T, typename ValueT>
+		T append_all (T appendable, ValueT value) {
+			appendable.append(value);
+			return appendable;
+		}
+
+		template <typename T, typename ValueT, typename... Args>
+		T append_all (T appendable, ValueT value, Args... args) {
+			appendable.append(value);
+			return append_all(appendable, args...);
+		}
+
+		template <typename T>
+		T append_all (T appendable) {
+			return appendable;
+		}
+
+		template <typename KV, typename KeyT, typename ValueT>
+		KV add_all (KV kv, KeyT key, ValueT value) {
+			kv[key] = value;
+			return kv;
+		}
+
+		template <typename KV, typename KeyT, typename ValueT, typename... Args>
+		KV add_all (KV kv, KeyT key, ValueT value, Args... args) {
+			kv[key] = value;
+			return add_all(kv, args...);
+		}
+
+		template <typename KV>
+		KV add_all (KV kv) {
+			return kv;
+		}
+	}
+
 }
 
 #include <stdio.h>

+ 5 - 0
include/core/Dictionary.hpp

@@ -16,6 +16,11 @@ public:
 	Dictionary(const Dictionary & other);
 	Dictionary & operator=(const Dictionary & other);
 
+	template <class... Args>
+	static Dictionary make(Args... args) {
+		return helpers::add_all(Dictionary(), args...);
+	}
+
 	void clear();
 
 	bool empty() const;

+ 5 - 0
include/core/Godot.hpp

@@ -484,6 +484,11 @@ void register_signal(String name, Dictionary args = Dictionary())
 	}
 }
 
+template<class T, class... Args>
+void register_signal(String name, Args... varargs)
+{
+	register_signal<T>(name, Dictionary::make(varargs...));
+}
 
 }
 

+ 6 - 0
include/core/GodotGlobal.hpp

@@ -3,6 +3,7 @@
 
 #include <gdnative_api_struct.gen.h>
 #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 <class... Args>
+	static void print(const String& fmt, Args... values) {
+		print(fmt.format(Array::make(values...)));
+	}
 };
 
 

+ 2 - 0
include/core/NodePath.hpp

@@ -36,6 +36,8 @@ public:
 
 	void operator =(const NodePath& other);
 
+	bool operator ==(const NodePath& other);
+
 	~NodePath();
 };
 

+ 7 - 7
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;
 

+ 2 - 0
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);

+ 10 - 2
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()

+ 7 - 7
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);

+ 10 - 1
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);