Skip to content

Commit

Permalink
Nativescript 1.1
Browse files Browse the repository at this point in the history
implemented instance binding data usage

This commit changes the way C++ wrapper classes work.
Previously, wrapper classes were merely wrapper *interfaces*.
They used the `this` pointer to store the actual foreign Godot
Object.

With the NativeScript 1.1 extension it is now possible to have
low-overhead language binding data attached to Objects.

The C++ bindings use that feature to implement *proper* wrappers
and enable regular C++ inheritance usage that way.

Some things might still be buggy and untested, but the C++
SimpleDemo works with those changes.

new and free change, custom free will crash engine, be wary

fix exporting of non-object types

fix free() crash with custom resources

added type tags and safe object casting

fix global type registration order

fix cast_to

changed build system to be more self contained

updated .gitignore

use typeid() for type tags now

fix indentation in bindings generator

remove accidentally added files

fix gitignore

Fixed up registering tool and updated godot_headers

Fix crash when calling String::split/split_floats

Was casting to the wrong object type.
Also adds parse_ints function to String with the same logic

Better warning/error macros

Change gitignore so we get our gen folders

New documentation based on nativescript 1.1

Fixed GODOT_SUBCLASS macro

Preventing crash when function returned null ptr

Adds needed include <typeinfo>

 Solves this issue #168 due to not having the include of typeinfo

Fix compile error of 'WARN_PRINT' and 'ERR_PRINT'.

cannot pass non-trivial object of type 'godot::String' to variadic function; expected type from format string was 'char *' [-Wnon-pod-varargs]

update vector3::distance_to

Remove godot_api.json as its now in the godot_headers submodule (api.json)
  • Loading branch information
karroffel authored and BastiaanOlij committed Nov 7, 2018
1 parent 13f4f0e commit 200bf22
Show file tree
Hide file tree
Showing 23 changed files with 543 additions and 140,915 deletions.
10 changes: 5 additions & 5 deletions .gitignore
@@ -1,8 +1,3 @@
# Generated bindings
src/*.cpp
src/*.hpp
include/*.hpp

# Misc
logs/*

Expand All @@ -16,3 +11,8 @@ logs/*
*.pdb
*.lib
bin
*.config
*.creator
*.creator.user
*.files
*.includes
197 changes: 127 additions & 70 deletions README.md
@@ -1,123 +1,180 @@
# godot-cpp
C++ bindings for the Godot script API

# Creating a GDNative library (Linux)
Create a directory named `SimpleLibrary` with subdirectories `lib, src`
Note that the master branch in this repository is for use with Godot build from its latest master.
If you need to support older versions of Godot use the relevant branch for that version in this repository.

The instructions below feature the new NativeScript 1.1 class structure and will only work for modules created for Godot 3.1 and later.

- [**Getting Started**](#getting-started)
- [**Creating a simple class**](#creating-a-simple-class)

## Getting Started

| **Build latest version of Godot** | [**GitHub**](https://github.com/godotengine/godot) | [**Docs**](https://godot.readthedocs.io/en/latest/development/compiling/index.html) |
| --- | --- | --- |

### Setting up a new project

We recommend using git for managing your project and the instructions below assume so. Alternatively you can download the source code directly from GitHub in which case you need to download both [godot-cpp](https://github.com/GodotNativeTools/godot-cpp) and [godot_headers](https://github.com/GodotNativeTools/godot_headers).

Getting latest `godot-cpp` and `godot_headers`
```
$ git clone https://github.com/GodotNativeTools/godot-cpp
$ git clone https://github.com/GodotNativeTools/godot_headers
$ mkdir SimpleLibrary
$ cd SimpleLibrary
$ mkdir bin
$ mkdir src
$ git clone --recursive https://github.com/GodotNativeTools/godot-cpp
```
right now our directory structure should look like this:

Note that if you wish to use a specific branch, add the -b option to the clone command:
```
godot-cpp
godot_headers
SimpleLibrary
├── lib/
└── src/
$ git clone --recursive https://github.com/GodotNativeTools/godot-cpp -b 3.0
```

Now to generate cpp bindings
Right now our directory structure should look like this:
```
SimpleLibrary/
├─godot-cpp/
| └─godot_headers/
├─bin/
└─src/
```

### Updating the api.json
Our api.json file contains meta data of all the classes that are part of the Godot core and are needed to generate the C++ binding classes for use in GDNative modules.

A file is supplied in our repository for your convinience but if you are running a custom build of Godot and need access to classes that have recent changes a new api.json file must be generated. You do this by starting your Godot executable with the following parameters:

```
$ godot --gdnative-generate-json-api api.json
```

Now copy the api.json file into your folder structure so its easy to access.

### Compiling the cpp bindings library
The final step is to compile our cpp bindings library:
```
$ cd godot-cpp
$ scons godotbinpath="../godot_fork/bin/godot_binary" p=linux
$ scons platform=<your platform> generate_bindings=yes
$ cd ..
```
resulting libraries will be placed under `bin/` and the generated headers will be placed under `include/*`

**Note:**
> `generate_bindings=yes` is used to force regenerating C++ bindings (`godot_api.json` - Godot API)
> Replace `<your platform>` with either `windows`, `linux` or `osx`.
> Include `use_llvm=yes` for using clang++
> You may need to specify `headers=../godot_headers` if you have compilation issues related to missing include files
> Include `target=runtime` to build a runtime build (windows only at the moment)
And our directory structure will be
```
godot-cpp
└── bin/libgodot-cpp.a
godot_headers
SimpleLibrary
├── lib/
└── src/
```
> The resulting library will be created in `godot-cpp/bin/`, take note of its name as it will be different depending on platform.
# Creating simple class
> If you want to use an alternative api.json file add `use_custom_api_file=yes custom_api_file=../api.json`, be sure to specify the correct location of where you placed your file.
## Creating a simple class

Create `init.cpp` under `SimpleLibrary/src/` and add the following code
```cpp
#include <core/Godot.hpp>
#include <Godot.hpp>
#include <Reference.hpp>

using namespace godot;

class SimpleClass : public GodotScript<Reference> {
GODOT_CLASS(SimpleClass);
class SimpleClass : public Reference {
GODOT_CLASS(SimpleClass, Reference);
public:
SimpleClass() { }

void test_void_method() {
Godot::print("This is test");
}

Variant method(Variant arg) {
Variant ret;
ret = arg;

return ret;
}

static void _register_methods() {
register_method("method", &SimpleClass::method);
/**
* How to register exports like gdscript
* export var _name = "SimpleClass"
**/
register_property((char *)"base/name", &SimpleClass::_name, String("SimpleClass"));

/** For registering signal **/
// register_signal<SimpleClass>("signal_name");
// register_signal<SimpleClass>("signal_name", "string_argument", GODOT_VARIANT_TYPE_STRING)
}

String _name;
SimpleClass() { }

/* _init must exist as it is called by Godot */
void _init() { }

void test_void_method() {
Godot::print("This is test");
}

Variant method(Variant arg) {
Variant ret;
ret = arg;

return ret;
}

static void _register_methods() {
register_method("method", &SimpleClass::method);

/**
* How to register exports like gdscript
* export var _name = "SimpleClass"
**/
register_property<SimpleClass, String>("base/name", &SimpleClass::_name, String("SimpleClass"));

/* or alternatively with getter and setter methods */
register_property<SimpleClass, int>("base/value", &SimpleClass::set_value, &SimpleClass::get_value, 0);

/** For registering signal **/
// register_signal<SimpleClass>("signal_name");
// register_signal<SimpleClass>("signal_name", "string_argument", GODOT_VARIANT_TYPE_STRING)
}

String _name;
int _value;

void set_value(int p_value) {
_value = p_value;
}

int get_value() const {
return _value;
}
};

/** GDNative Initialize **/
extern "C" void GDN_EXPORT godot_gdnative_init(godot_gdnative_init_options *o)
{
extern "C" void GDN_EXPORT godot_gdnative_init(godot_gdnative_init_options *o) {
godot::Godot::gdnative_init(o);
}

/** GDNative Terminate **/
extern "C" void GDN_EXPORT godot_gdnative_terminate(godot_gdnative_terminate_options *o)
{
extern "C" void GDN_EXPORT godot_gdnative_terminate(godot_gdnative_terminate_options *o) {
godot::Godot::gdnative_terminate(o);
}

/** NativeScript Initialize **/
extern "C" void GDN_EXPORT godot_nativescript_init(void *handle)
{
extern "C" void GDN_EXPORT godot_nativescript_init(void *handle) {
godot::Godot::nativescript_init(handle);

godot::register_class<SimpleClass>();
}
```
# Compiling
### Compiling
*Linux*
```
$ cd SimpleLibrary
$ clang -fPIC -o src/init.os -c src/init.cpp -g -O3 -std=c++14 -Igodot-cpp/include -Igodot-cpp/include/core -Igodot-cpp/include/gen -Igodot-cpp/godot_headers
$ clang -o bin/libtest.so -shared src/init.os -Lgodot-cpp/bin -l<name of the godot-cpp>
```
> This creates the file `libtest.so` in your `SimpleLibrary/bin` directory.
> You will need to replace `<name of the godot-cpp>` with the file that was created in [**Compiling the cpp bindings library**](#compiling-the-cpp-bindings-library)
*Windows*
```
$ 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
$ cl /Fosrc/init.obj /c src/init.cpp /nologo -EHsc -DNDEBUG /MDd /Igodot-cpp\include /Igodot-cpp\include\core /Igodot-cpp\include\gen /Igodot-cpp\godot_headers
$ link /nologo /dll /out:bin\libtest.dll /implib:bin\libsimple.lib src\init.obj godot-cpp\bin\<name of the godot-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.
> This creates the file `libtest.dll` in your `SimpleLibrary/bin` directory.
> You will need to replace `<name of the godot-cpp>` with the file that was created in [**Compiling the cpp bindings library**](#compiling-the-cpp-bindings-library)
> Finally replace `/MDd` with `/MD` if you're generated a runtime build.
*macOS*
For OSX you need to find out what compiler flags need to be used.
# Creating `.gdns` file
### Creating `.gdnlib` and `.gdns` files
follow [godot_header/README.md](https://github.com/GodotNativeTools/godot_headers/blob/master/README.md#how-do-i-use-native-scripts-from-the-editor) to create the `.gdns`
# Implementing with gdscript
### Implementing with gdscript
```gdscript
var simpleclass = load("res://simpleclass.gdns").new();
simpleclass.method("Test argument");
Expand Down
11 changes: 8 additions & 3 deletions SConstruct
Expand Up @@ -96,15 +96,18 @@ elif env['platform'] == 'windows':
env.Append(LINKFLAGS=['--static', '-Wl,--no-undefined', '-static-libgcc', '-static-libstdc++'])


env.Append(CPPPATH=['.', env['headers_dir'], 'include', 'include/core'])
env.Append(CPPPATH=['.', env['headers_dir'], 'include', 'include/gen', 'include/core'])

# Generate bindings?
json_api_file = ''

# Generate bindings?
json_api_file = ''

if ARGUMENTS.get('use_custom_api_file', 'no') == 'yes':
json_api_file = ARGUMENTS.get('custom_api_file', '')
else:
json_api_file = os.path.join(os.getcwd(), 'godot_api.json')
json_api_file = os.path.join(os.getcwd(), 'godot_headers', 'api.json')

if ARGUMENTS.get('generate_bindings', 'no') == 'yes':
# Actually create the bindings here
Expand All @@ -113,9 +116,11 @@ if ARGUMENTS.get('generate_bindings', 'no') == 'yes':

binding_generator.generate_bindings(json_api_file)


# source to compile
sources = []
add_sources(sources, 'src/core', 'cpp')
add_sources(sources, 'src', 'cpp')
add_sources(sources, 'src/gen', 'cpp')

library = env.StaticLibrary(
target='bin/' + 'libgodot-cpp.{}.{}.{}'.format(env['platform'], env['target'], env['bits']), source=sources
Expand Down

0 comments on commit 200bf22

Please sign in to comment.