diff --git a/cpp/SConstruct b/cpp/SConstruct new file mode 100644 index 0000000..80b12ce --- /dev/null +++ b/cpp/SConstruct @@ -0,0 +1,93 @@ +#!python +import os, subprocess + +opts = Variables([], ARGUMENTS) + +# Gets the standard flags CC, CCX, etc. +env = DefaultEnvironment() + +# Define our options +opts.Add(EnumVariable('target', "Compilation target", 'debug', ['d', 'debug', 'r', 'release'])) +opts.Add(EnumVariable('platform', "Compilation platform", '', ['', 'windows', 'x11', 'linux', 'osx'])) +opts.Add(EnumVariable('p', "Compilation target, alias for 'platform'", '', ['', 'windows', 'x11', 'linux', 'osx'])) +opts.Add(BoolVariable('use_llvm', "Use the LLVM / Clang compiler", 'no')) +opts.Add(PathVariable('target_path', 'The path where the lib is installed.', '../game/gdn/')) +opts.Add(PathVariable('target_name', 'The library name.', 'libgdexample', PathVariable.PathAccept)) + +# Local dependency paths, adapt them to your setup +godot_headers_path = "gdnative_cpp/pandemonium_headers/" +cpp_bindings_path = "gdnative_cpp/" +cpp_library = "libgodot-cpp" + +# only support 64 at this time.. +bits = 64 + +# Updates the environment with the option variables. +opts.Update(env) + +# Process some arguments +if env['use_llvm']: + env['CC'] = 'clang' + env['CXX'] = 'clang++' + +if env['p'] != '': + env['platform'] = env['p'] + +if env['platform'] == '': + print("No valid target platform selected.") + quit(); + +# Check our platform specifics +if env['platform'] == "osx": + env['target_path'] += 'osx/' + cpp_library += '.osx' + if env['target'] in ('debug', 'd'): + env.Append(CCFLAGS = ['-g','-O2', '-arch', 'x86_64', '-std=c++17']) + env.Append(LINKFLAGS = ['-arch', 'x86_64']) + else: + env.Append(CCFLAGS = ['-g','-O3', '-arch', 'x86_64', '-std=c++17']) + env.Append(LINKFLAGS = ['-arch', 'x86_64']) + +elif env['platform'] in ('x11', 'linux'): + env['target_path'] += 'x11/' + cpp_library += '.linux' + if env['target'] in ('debug', 'd'): + env.Append(CCFLAGS = ['-fPIC', '-g3','-Og', '-std=c++17']) + else: + env.Append(CCFLAGS = ['-fPIC', '-g','-O3', '-std=c++17']) + +elif env['platform'] == "windows": + env['target_path'] += 'win64/' + cpp_library += '.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 + env.Append(ENV = os.environ) + + env.Append(CCFLAGS = ['-DWIN32', '-D_WIN32', '-D_WINDOWS', '-W3', '-GR', '-D_CRT_SECURE_NO_WARNINGS']) + if env['target'] in ('debug', 'd'): + env.Append(CCFLAGS = ['-EHsc', '-D_DEBUG', '-MDd']) + else: + env.Append(CCFLAGS = ['-O2', '-EHsc', '-DNDEBUG', '-MD']) + +if env['target'] in ('debug', 'd'): + cpp_library += '.debug' +else: + cpp_library += '.release' + +cpp_library += '.' + str(bits) + +# make sure our binding library is properly includes +env.Append(CPPPATH=['.', godot_headers_path, cpp_bindings_path + 'include/', cpp_bindings_path + 'include/core/', cpp_bindings_path + 'include/gen/']) +env.Append(LIBPATH=[cpp_bindings_path + 'bin/']) +env.Append(LIBS=[cpp_library]) + +# tweak this if you want to use different folders, or more folders, to store your source code in. +env.Append(CPPPATH=['src/']) +sources = Glob('src/*.cpp') + +library = env.SharedLibrary(target=env['target_path'] + env['target_name'] , source=sources) + +Default(library) + +# Generates help for the -h scons option. +Help(opts.GenerateHelpText(env)) diff --git a/cpp/src/gdexample.cpp b/cpp/src/gdexample.cpp new file mode 100644 index 0000000..bb900bf --- /dev/null +++ b/cpp/src/gdexample.cpp @@ -0,0 +1,54 @@ +#include "gdexample.h" + +using namespace godot; + +void GDExample::_register_methods() { + register_method("_process", &GDExample::_process); + register_property("amplitude", &GDExample::amplitude, 10.0); + register_property("speed", &GDExample::set_speed, &GDExample::get_speed, 1.0); + + //register_signal((char *)"position_changed", "node", PANDEMONIUM_VARIANT_TYPE_OBJECT, "new_pos", PANDEMONIUM_VARIANT_TYPE_VECTOR2); + register_signal("position_changed"); +} + +GDExample::GDExample() { +} + +GDExample::~GDExample() { + // add your cleanup here +} + +void GDExample::_init() { + // initialize any variables here + time_passed = 0.0; + amplitude = 10.0; + speed = 1.0; +} + +void GDExample::_process(float delta) { + time_passed += speed * delta; + + Vector2 pos = get_position(); + + Vector2 new_position = Vector2( + amplitude * sin(time_passed * speed), + amplitude * cos(time_passed * speed) + ); + + set_position(new_position); + + time_emit += delta; + if (time_emit > 1.0) { + emit_signal("position_changed"); + + time_emit = 0.0; + } +} + +void GDExample::set_speed(float p_speed) { + speed = p_speed; +} + +float GDExample::get_speed() { + return speed; +} diff --git a/cpp/src/gdexample.h b/cpp/src/gdexample.h new file mode 100644 index 0000000..fead7bb --- /dev/null +++ b/cpp/src/gdexample.h @@ -0,0 +1,33 @@ +#ifndef GDEXAMPLE_H +#define GDEXAMPLE_H + +#include +#include + +namespace godot { + +class GDExample : public Sprite { + GODOT_CLASS(GDExample, Sprite) + +private: + float time_passed; + float time_emit; + float amplitude; + float speed; + +public: + static void _register_methods(); + + GDExample(); + ~GDExample(); + + void _init(); // our initializer called by Godot + + void _process(float delta); + void set_speed(float p_speed); + float get_speed(); +}; + +} + +#endif diff --git a/cpp/src/gdexample.os b/cpp/src/gdexample.os new file mode 100644 index 0000000..2a4b7d1 Binary files /dev/null and b/cpp/src/gdexample.os differ diff --git a/cpp/src/gdlibrary.cpp b/cpp/src/gdlibrary.cpp new file mode 100644 index 0000000..811b13d --- /dev/null +++ b/cpp/src/gdlibrary.cpp @@ -0,0 +1,15 @@ +#include "gdexample.h" + +extern "C" void GDN_EXPORT pandemonium_gdnative_init(pandemonium_gdnative_init_options *o) { + godot::Godot::gdnative_init(o); +} + +extern "C" void GDN_EXPORT pandemonium_gdnative_terminate(pandemonium_gdnative_terminate_options *o) { + godot::Godot::gdnative_terminate(o); +} + +extern "C" void GDN_EXPORT pandemonium_nativescript_init(void *handle) { + godot::Godot::nativescript_init(handle); + + godot::register_class(); +} diff --git a/cpp/src/gdlibrary.os b/cpp/src/gdlibrary.os new file mode 100644 index 0000000..4a21a95 Binary files /dev/null and b/cpp/src/gdlibrary.os differ diff --git a/game/Main.tscn b/game/Main.tscn index 0f1756c..63829bf 100644 --- a/game/Main.tscn +++ b/game/Main.tscn @@ -1,3 +1,19 @@ -[gd_scene format=2] +[gd_scene load_steps=4 format=2] + +[ext_resource path="res://gdn/gdexample.gdns" type="Script" id=1] +[ext_resource path="res://icon.png" type="Texture" id=2] +[ext_resource path="res://Node2D.gd" type="Script" id=3] [node name="Main" type="Node"] + +[node name="Node2D" type="Node2D" parent="."] +position = Vector2( 392, 232 ) +script = ExtResource( 3 ) + +[node name="Sprite" type="Sprite" parent="Node2D"] +texture = ExtResource( 2 ) +script = ExtResource( 1 ) +amplitude = 150.0 +speed = 4.0 + +[connection signal="position_changed" from="Node2D/Sprite" to="Node2D" method="_on_Sprite_position_changed"] diff --git a/game/Node2D.gd b/game/Node2D.gd new file mode 100644 index 0000000..e8b0e82 --- /dev/null +++ b/game/Node2D.gd @@ -0,0 +1,21 @@ +extends Node2D + + +# Declare member variables here. Examples: +# var a: int = 2 +# var b: String = "text" + + +# Called when the node enters the scene tree for the first time. +func _ready() -> void: + pass # Replace with function body. + + +# Called every frame. 'delta' is the elapsed time since the previous frame. +#func _process(delta: float) -> void: +# pass + + +func _on_Sprite_position_changed() -> void: +# print("!") + pass diff --git a/game/gdn/gdexample.gdns b/game/gdn/gdexample.gdns new file mode 100644 index 0000000..4cf6844 --- /dev/null +++ b/game/gdn/gdexample.gdns @@ -0,0 +1,7 @@ +[gd_resource type="NativeScript" load_steps=2 format=2] + +[ext_resource path="res://gdn/tgdn.tres" type="GDNativeLibrary" id=1] + +[resource] +class_name = "GDExample" +library = ExtResource( 1 ) diff --git a/game/gdn/tgdn.tres b/game/gdn/tgdn.tres new file mode 100644 index 0000000..e9bcdc0 --- /dev/null +++ b/game/gdn/tgdn.tres @@ -0,0 +1,5 @@ +[gd_resource type="GDNativeLibrary" format=2] + +[resource] +entry/X11.64 = "res://gdn/x11/libgdexample.so" +dependency/X11.64 = [ ] diff --git a/game/project.pandemonium b/game/project.pandemonium index fe543f6..b80fb10 100644 --- a/game/project.pandemonium +++ b/game/project.pandemonium @@ -11,6 +11,7 @@ config_version=4 [application] config/name="Sample" +run/main_scene="res://Main.tscn" config/icon="res://icon.png" [physics]