From a6084fd9111eee840f1bf4bcc82ff31b3f543cd8 Mon Sep 17 00:00:00 2001 From: Aaron Franke Date: Tue, 6 Jul 2021 21:27:44 -0400 Subject: [PATCH 1/2] Add Linux support to simple C++ demo --- .gitmodules | 1 - cpp/simple/Makefile | 2 -- cpp/simple/SConstruct | 27 ++++++++++++++++++- cpp/simple/project/gdnative/linux/.gitignore | 2 ++ cpp/simple/project/gdnative/osx/.gitignore | 2 ++ cpp/simple/project/gdnative/simple.gdnlib | 4 +++ .../project/gdnative/windows/.gitignore | 2 ++ 7 files changed, 36 insertions(+), 4 deletions(-) delete mode 100644 cpp/simple/Makefile create mode 100644 cpp/simple/project/gdnative/linux/.gitignore create mode 100644 cpp/simple/project/gdnative/osx/.gitignore create mode 100644 cpp/simple/project/gdnative/windows/.gitignore diff --git a/.gitmodules b/.gitmodules index 97cb732..65294b3 100644 --- a/.gitmodules +++ b/.gitmodules @@ -10,4 +10,3 @@ [submodule "cpp/simple/godot-cpp"] path = cpp/simple/godot-cpp url = https://github.com/godotengine/godot-cpp.git - diff --git a/cpp/simple/Makefile b/cpp/simple/Makefile deleted file mode 100644 index d4f144c..0000000 --- a/cpp/simple/Makefile +++ /dev/null @@ -1,2 +0,0 @@ -all: - scons cpp_bindings="../../godot-cpp/" headers="../../godot-cpp/godot_headers/" diff --git a/cpp/simple/SConstruct b/cpp/simple/SConstruct index 8177f96..ebdbcc8 100644 --- a/cpp/simple/SConstruct +++ b/cpp/simple/SConstruct @@ -18,9 +18,32 @@ opts.Add(BoolVariable("use_llvm", "Use the LLVM / Clang compiler", "no")) opts.Add(PathVariable("target_path", "The path where the lib is installed.", "project/gdnative/")) opts.Add(PathVariable("target_name", "The library name.", "libsimple", PathVariable.PathAccept)) +# Updates the environment with the option variables. opts.Update(env) -if ARGUMENTS.get("use_llvm", "no") == "yes": +# Process platform arguments. Here we use the same names as GDNative. +if env["p"] != "": + env["platform"] = env["p"] + +if env["platform"] == "macos": + env["platform"] = "osx" +elif env["platform"] in ("x11", "linuxbsd"): + env["platform"] = "linux" +elif env["platform"] == "bsd": + env["platform"] = "freebsd" + +if env["platform"] == "": + print("No valid target platform selected.") + quit() + +platform = env["platform"] + +# Process other arguments. +if env["platform"] == "osx" and not env["use_llvm"]: + env["use_llvm"] = "yes" + +if env["use_llvm"] == "yes": + env["CC"] = "clang" env["CXX"] = "clang++" # put stuff that is the same for all first, saves duplication @@ -49,6 +72,8 @@ elif env["platform"] == "windows": else: env.Append(CCFLAGS=["-O2", "-EHsc", "-DNDEBUG", "-MD"]) +SConscript("godot-cpp/SConstruct") + def add_sources(sources, dir): for f in os.listdir(dir): diff --git a/cpp/simple/project/gdnative/linux/.gitignore b/cpp/simple/project/gdnative/linux/.gitignore new file mode 100644 index 0000000..d6b7ef3 --- /dev/null +++ b/cpp/simple/project/gdnative/linux/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore diff --git a/cpp/simple/project/gdnative/osx/.gitignore b/cpp/simple/project/gdnative/osx/.gitignore new file mode 100644 index 0000000..d6b7ef3 --- /dev/null +++ b/cpp/simple/project/gdnative/osx/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore diff --git a/cpp/simple/project/gdnative/simple.gdnlib b/cpp/simple/project/gdnative/simple.gdnlib index aed22c4..98c6745 100644 --- a/cpp/simple/project/gdnative/simple.gdnlib +++ b/cpp/simple/project/gdnative/simple.gdnlib @@ -8,7 +8,11 @@ reloadable=true [entry] Windows.64="res://gdnative/windows/libsimple.dll" +X11.64="res://gdnative/linux/libsimple.so" +OSX.64="res://gdnative/osx/libsimple.dylib" [dependencies] Windows.64=[ ] +X11.64=[ ] +OSX.64=[ ] diff --git a/cpp/simple/project/gdnative/windows/.gitignore b/cpp/simple/project/gdnative/windows/.gitignore new file mode 100644 index 0000000..d6b7ef3 --- /dev/null +++ b/cpp/simple/project/gdnative/windows/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore From 3aa846ac796c5a0e4a8f2620ab9e521e46fa536c Mon Sep 17 00:00:00 2001 From: Aaron Franke Date: Tue, 6 Jul 2021 21:43:49 -0400 Subject: [PATCH 2/2] Add ability for SimpleSprite to move as intended --- cpp/simple/project/gdnative/simple_sprite.gdns | 9 +++++++++ cpp/simple/project/main.tscn | 7 ++++++- cpp/simple/src/Simple.cpp | 18 +++++------------- 3 files changed, 20 insertions(+), 14 deletions(-) create mode 100644 cpp/simple/project/gdnative/simple_sprite.gdns diff --git a/cpp/simple/project/gdnative/simple_sprite.gdns b/cpp/simple/project/gdnative/simple_sprite.gdns new file mode 100644 index 0000000..91c1ecc --- /dev/null +++ b/cpp/simple/project/gdnative/simple_sprite.gdns @@ -0,0 +1,9 @@ +[gd_resource type="NativeScript" load_steps=2 format=2] + +[ext_resource path="res://gdnative/simple.gdnlib" type="GDNativeLibrary" id=1] + +[resource] + +resource_name = "SimpleSprite" +class_name = "SimpleSprite" +library = ExtResource( 1 ) diff --git a/cpp/simple/project/main.tscn b/cpp/simple/project/main.tscn index 27abaec..1961238 100644 --- a/cpp/simple/project/main.tscn +++ b/cpp/simple/project/main.tscn @@ -1,7 +1,8 @@ -[gd_scene load_steps=3 format=2] +[gd_scene load_steps=4 format=2] [ext_resource path="res://main.gd" type="Script" id=1] [ext_resource path="res://icon.png" type="Texture" id=2] +[ext_resource path="res://gdnative/simple_sprite.gdns" type="Script" id=3] [node name="Node" type="Node"] script = ExtResource( 1 ) @@ -19,8 +20,12 @@ margin_top = 360.0 margin_right = 662.0 margin_bottom = 374.0 align = 1 +__meta__ = { +"_edit_use_anchors_": false +} [node name="Sprite" type="Sprite" parent="."] texture = ExtResource( 2 ) +script = ExtResource( 3 ) [connection signal="pressed" from="Button" to="." method="_on_Button_pressed"] diff --git a/cpp/simple/src/Simple.cpp b/cpp/simple/src/Simple.cpp index 5e6e100..a10108c 100644 --- a/cpp/simple/src/Simple.cpp +++ b/cpp/simple/src/Simple.cpp @@ -18,25 +18,17 @@ void SimpleSprite::_register_methods() { godot::register_method("_process", &SimpleSprite::_process); } +// `_init` doesn't need to be registered in `_register_methods`. void SimpleSprite::_init() { - godot::Godot::print("Wheeeeey"); + godot::Godot::print("A SimpleSprite was initialized in GDNative!"); } void SimpleSprite::_process(double delta) { + godot::Input *input = godot::Input::get_singleton(); godot::Vector2 input_dir(0, 0); - if (godot::Input::get_singleton()->is_action_pressed("ui_right")) { - input_dir.x += 1; - } - if (godot::Input::get_singleton()->is_action_pressed("ui_left")) { - input_dir.x -= 1; - } - if (godot::Input::get_singleton()->is_action_pressed("ui_down")) { - input_dir.y += 1; - } - if (godot::Input::get_singleton()->is_action_pressed("ui_up")) { - input_dir.y -= 1; - } + input_dir.x = input->get_action_strength("ui_right") - input->get_action_strength("ui_left"); + input_dir.y = input->get_action_strength("ui_down") - input->get_action_strength("ui_up"); set_position(get_position() + input_dir.normalized() * delta * 300); }