├── demo ├── icon.png ├── Main.gd ├── default_env.tres ├── bin │ ├── gdexample.gdns │ └── gdexample.gdnlib ├── Main.tscn ├── project.godot └── icon.png.import ├── .gitmodules ├── .gitattributes ├── .gitignore ├── README.md ├── src ├── gdlibrary.cpp ├── gdexample.h └── gdexample.cpp ├── LICENSE └── SConstruct /demo/icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BastiaanOlij/gdnative_cpp_example/HEAD/demo/icon.png -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "godot-cpp"] 2 | path = godot-cpp 3 | url = https://github.com/GodotNativeTools/godot-cpp 4 | -------------------------------------------------------------------------------- /demo/Main.gd: -------------------------------------------------------------------------------- 1 | extends Node 2 | 3 | func _on_Sprite_position_changed(node, new_pos): 4 | print("The position of " + node.name + " is now " + str(new_pos)) 5 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | *.c eol=lf 2 | *.cpp eol=lf 3 | *.gd eol=lf 4 | *.tscn eol=lf 5 | *.cfg eol=lf 6 | *.godot eol=lf 7 | *.tres eol=lf 8 | *.gdnlib eol=lf 9 | *.gdns eol=lf 10 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | build 2 | zconf.h 3 | zconf.h.included 4 | *.o 5 | *.os 6 | *.so 7 | *.dylib 8 | .import/ 9 | *.dblite 10 | *.dll 11 | *.exp 12 | *.lib 13 | *.obj 14 | *.TMP 15 | logs 16 | Thumbs.db 17 | -------------------------------------------------------------------------------- /demo/default_env.tres: -------------------------------------------------------------------------------- 1 | [gd_resource type="Environment" load_steps=2 format=2] 2 | 3 | [sub_resource type="ProceduralSky" id=1] 4 | 5 | 6 | [resource] 7 | 8 | background_mode = 2 9 | background_sky = SubResource( 1 ) 10 | 11 | -------------------------------------------------------------------------------- /demo/bin/gdexample.gdns: -------------------------------------------------------------------------------- 1 | [gd_resource type="NativeScript" load_steps=2 format=2] 2 | 3 | [ext_resource path="res://bin/gdexample.gdnlib" type="GDNativeLibrary" id=1] 4 | 5 | [resource] 6 | 7 | resource_name = "gdexample" 8 | class_name = "GDExample" 9 | library = ExtResource( 1 ) 10 | _sections_unfolded = [ "Resource" ] 11 | -------------------------------------------------------------------------------- /demo/bin/gdexample.gdnlib: -------------------------------------------------------------------------------- 1 | [general] 2 | 3 | singleton=false 4 | load_once=true 5 | symbol_prefix="godot_" 6 | reloadable=false 7 | 8 | [entry] 9 | 10 | X11.64="res://bin/x11/libgdexample.so" 11 | Windows.64="res://bin/win64/libgdexample.dll" 12 | OSX.64="res://bin/osx/libgdexample.dylib" 13 | 14 | [dependencies] 15 | 16 | X11.64=[ ] 17 | Windows.64=[ ] 18 | OSX.64=[ ] 19 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | GDNative C++ Example 2 | ==================== 3 | 4 | This repository contains the example GDNative C++ library in support of this tutorial: 5 | http://docs.godotengine.org/en/latest/tutorials/plugins/gdnative/gdnative-cpp-example.html 6 | 7 | It is now based on the new NativeScript 1.1 Godot-cpp bindings library and will only work with Godot 3.1 and onwards. 8 | Switch to the `3.0` branch to see the original NativeScript 1.0 version. 9 | -------------------------------------------------------------------------------- /src/gdlibrary.cpp: -------------------------------------------------------------------------------- 1 | #include "gdexample.h" 2 | 3 | extern "C" void GDN_EXPORT godot_gdnative_init(godot_gdnative_init_options *o) { 4 | godot::Godot::gdnative_init(o); 5 | } 6 | 7 | extern "C" void GDN_EXPORT godot_gdnative_terminate(godot_gdnative_terminate_options *o) { 8 | godot::Godot::gdnative_terminate(o); 9 | } 10 | 11 | extern "C" void GDN_EXPORT godot_nativescript_init(void *handle) { 12 | godot::Godot::nativescript_init(handle); 13 | 14 | godot::register_class(); 15 | } 16 | -------------------------------------------------------------------------------- /demo/Main.tscn: -------------------------------------------------------------------------------- 1 | [gd_scene load_steps=4 format=2] 2 | 3 | [ext_resource path="res://Main.gd" type="Script" id=1] 4 | [ext_resource path="res://icon.png" type="Texture" id=2] 5 | [ext_resource path="res://bin/gdexample.gdns" type="Script" id=3] 6 | 7 | [node name="Main" type="Node"] 8 | script = ExtResource( 1 ) 9 | 10 | [node name="Sprite" type="Sprite" parent="."] 11 | texture = ExtResource( 2 ) 12 | centered = false 13 | script = ExtResource( 3 ) 14 | amplitude = 20.0 15 | speed = 3.0 16 | 17 | [connection signal="position_changed" from="Sprite" to="." method="_on_Sprite_position_changed"] 18 | -------------------------------------------------------------------------------- /demo/project.godot: -------------------------------------------------------------------------------- 1 | ; Engine configuration file. 2 | ; It's best edited using the editor UI and not directly, 3 | ; since the parameters that go here are not all obvious. 4 | ; 5 | ; Format: 6 | ; [section] ; section goes between [] 7 | ; param=value ; assign values to parameters 8 | 9 | config_version=4 10 | 11 | _global_script_classes=[ ] 12 | _global_script_class_icons={ 13 | 14 | } 15 | 16 | [application] 17 | 18 | config/name="demo" 19 | run/main_scene="res://Main.tscn" 20 | config/icon="res://icon.png" 21 | 22 | [rendering] 23 | 24 | environment/default_environment="res://default_env.tres" 25 | -------------------------------------------------------------------------------- /src/gdexample.h: -------------------------------------------------------------------------------- 1 | #ifndef GDEXAMPLE_H 2 | #define GDEXAMPLE_H 3 | 4 | #include 5 | #include 6 | 7 | namespace godot { 8 | 9 | class GDExample : public Sprite { 10 | GODOT_CLASS(GDExample, Sprite) 11 | 12 | private: 13 | float time_passed; 14 | float time_emit; 15 | float amplitude; 16 | float speed; 17 | 18 | public: 19 | static void _register_methods(); 20 | 21 | GDExample(); 22 | ~GDExample(); 23 | 24 | void _init(); // our initializer called by Godot 25 | 26 | void _process(float delta); 27 | void set_speed(float p_speed); 28 | float get_speed(); 29 | }; 30 | 31 | } 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /demo/icon.png.import: -------------------------------------------------------------------------------- 1 | [remap] 2 | 3 | importer="texture" 4 | type="StreamTexture" 5 | path="res://.import/icon.png-487276ed1e3a0c39cad0279d744ee560.stex" 6 | 7 | [deps] 8 | 9 | source_file="res://icon.png" 10 | dest_files=[ "res://.import/icon.png-487276ed1e3a0c39cad0279d744ee560.stex" ] 11 | 12 | [params] 13 | 14 | compress/mode=0 15 | compress/lossy_quality=0.7 16 | compress/hdr_mode=0 17 | compress/normal_map=0 18 | flags/repeat=0 19 | flags/filter=true 20 | flags/mipmaps=false 21 | flags/anisotropic=false 22 | flags/srgb=2 23 | process/fix_alpha_border=true 24 | process/premult_alpha=false 25 | process/HDR_as_SRGB=false 26 | stream=false 27 | size_limit=0 28 | detect_3d=true 29 | svg/scale=1.0 30 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Bastiaan Olij 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/gdexample.cpp: -------------------------------------------------------------------------------- 1 | #include "gdexample.h" 2 | 3 | using namespace godot; 4 | 5 | void GDExample::_register_methods() { 6 | register_method("_process", &GDExample::_process); 7 | register_property("amplitude", &GDExample::amplitude, 10.0); 8 | register_property("speed", &GDExample::set_speed, &GDExample::get_speed, 1.0); 9 | 10 | register_signal((char *)"position_changed", "node", GODOT_VARIANT_TYPE_OBJECT, "new_pos", GODOT_VARIANT_TYPE_VECTOR2); 11 | } 12 | 13 | GDExample::GDExample() { 14 | } 15 | 16 | GDExample::~GDExample() { 17 | // add your cleanup here 18 | } 19 | 20 | void GDExample::_init() { 21 | // initialize any variables here 22 | time_passed = 0.0; 23 | amplitude = 10.0; 24 | speed = 1.0; 25 | } 26 | 27 | void GDExample::_process(float delta) { 28 | time_passed += speed * delta; 29 | 30 | Vector2 new_position = Vector2( 31 | amplitude + (amplitude * sin(time_passed * 2.0)), 32 | amplitude + (amplitude * cos(time_passed * 1.5)) 33 | ); 34 | 35 | set_position(new_position); 36 | 37 | time_emit += delta; 38 | if (time_emit > 1.0) { 39 | emit_signal("position_changed", this, new_position); 40 | 41 | time_emit = 0.0; 42 | } 43 | } 44 | 45 | void GDExample::set_speed(float p_speed) { 46 | speed = p_speed; 47 | } 48 | 49 | float GDExample::get_speed() { 50 | return speed; 51 | } 52 | -------------------------------------------------------------------------------- /SConstruct: -------------------------------------------------------------------------------- 1 | #!python 2 | import os, subprocess 3 | 4 | opts = Variables([], ARGUMENTS) 5 | 6 | # Gets the standard flags CC, CCX, etc. 7 | env = DefaultEnvironment() 8 | 9 | # Define our options 10 | opts.Add(EnumVariable('target', "Compilation target", 'debug', ['d', 'debug', 'r', 'release'])) 11 | opts.Add(EnumVariable('platform', "Compilation platform", '', ['', 'windows', 'x11', 'linux', 'osx'])) 12 | opts.Add(EnumVariable('p', "Compilation target, alias for 'platform'", '', ['', 'windows', 'x11', 'linux', 'osx'])) 13 | opts.Add(BoolVariable('use_llvm', "Use the LLVM / Clang compiler", 'no')) 14 | opts.Add(PathVariable('target_path', 'The path where the lib is installed.', 'demo/bin/')) 15 | opts.Add(PathVariable('target_name', 'The library name.', 'libgdexample', PathVariable.PathAccept)) 16 | 17 | # Local dependency paths, adapt them to your setup 18 | godot_headers_path = "godot-cpp/godot-headers/" 19 | cpp_bindings_path = "godot-cpp/" 20 | cpp_library = "libgodot-cpp" 21 | 22 | # only support 64 at this time.. 23 | bits = 64 24 | 25 | # Updates the environment with the option variables. 26 | opts.Update(env) 27 | 28 | # Process some arguments 29 | if env['use_llvm']: 30 | env['CC'] = 'clang' 31 | env['CXX'] = 'clang++' 32 | 33 | if env['p'] != '': 34 | env['platform'] = env['p'] 35 | 36 | if env['platform'] == '': 37 | print("No valid target platform selected.") 38 | quit(); 39 | 40 | # Check our platform specifics 41 | if env['platform'] == "osx": 42 | env['target_path'] += 'osx/' 43 | cpp_library += '.osx' 44 | if env['target'] in ('debug', 'd'): 45 | env.Append(CCFLAGS = ['-g','-O2', '-arch', 'x86_64', '-std=c++17']) 46 | env.Append(LINKFLAGS = ['-arch', 'x86_64']) 47 | else: 48 | env.Append(CCFLAGS = ['-g','-O3', '-arch', 'x86_64', '-std=c++17']) 49 | env.Append(LINKFLAGS = ['-arch', 'x86_64']) 50 | 51 | elif env['platform'] in ('x11', 'linux'): 52 | env['target_path'] += 'x11/' 53 | cpp_library += '.linux' 54 | if env['target'] in ('debug', 'd'): 55 | env.Append(CCFLAGS = ['-fPIC', '-g3','-Og', '-std=c++17']) 56 | else: 57 | env.Append(CCFLAGS = ['-fPIC', '-g','-O3', '-std=c++17']) 58 | 59 | elif env['platform'] == "windows": 60 | env['target_path'] += 'win64/' 61 | cpp_library += '.windows' 62 | # This makes sure to keep the session environment variables on windows, 63 | # that way you can run scons in a vs 2017 prompt and it will find all the required tools 64 | env.Append(ENV = os.environ) 65 | 66 | env.Append(CCFLAGS = ['-DWIN32', '-D_WIN32', '-D_WINDOWS', '-W3', '-GR', '-D_CRT_SECURE_NO_WARNINGS']) 67 | if env['target'] in ('debug', 'd'): 68 | env.Append(CCFLAGS = ['-EHsc', '-D_DEBUG', '-MDd']) 69 | else: 70 | env.Append(CCFLAGS = ['-O2', '-EHsc', '-DNDEBUG', '-MD']) 71 | 72 | if env['target'] in ('debug', 'd'): 73 | cpp_library += '.debug' 74 | else: 75 | cpp_library += '.release' 76 | 77 | cpp_library += '.' + str(bits) 78 | 79 | # make sure our binding library is properly includes 80 | env.Append(CPPPATH=['.', godot_headers_path, cpp_bindings_path + 'include/', cpp_bindings_path + 'include/core/', cpp_bindings_path + 'include/gen/']) 81 | env.Append(LIBPATH=[cpp_bindings_path + 'bin/']) 82 | env.Append(LIBS=[cpp_library]) 83 | 84 | # tweak this if you want to use different folders, or more folders, to store your source code in. 85 | env.Append(CPPPATH=['src/']) 86 | sources = Glob('src/*.cpp') 87 | 88 | library = env.SharedLibrary(target=env['target_path'] + env['target_name'] , source=sources) 89 | 90 | Default(library) 91 | 92 | # Generates help for the -h scons option. 93 | Help(opts.GenerateHelpText(env)) 94 | --------------------------------------------------------------------------------