From 6ff55d4f3fa8b71937430a5e69021f06d9e97b9f Mon Sep 17 00:00:00 2001 From: Alejandro Saucedo Date: Sun, 20 Sep 2020 15:20:03 +0100 Subject: [PATCH] Updated to use komputesummator class --- .../gdnative_shared/CMakeLists.txt | 4 +- .../{gdlibrary.cpp => KomputeGdNative.cpp} | 4 +- .../src/{summator.cpp => KomputeSummator.cpp} | 34 ++++++-------- .../src/{summator.h => KomputeSummator.hpp} | 6 +-- .../gdnative_shared/src/player.cpp | 39 ---------------- .../gdnative_shared/src/player.h | 42 ------------------ .../gdnative_shared/src/root.cpp | 1 - .../godot_examples/gdnative_shared/src/root.h | 5 --- .../assets/DynamicExampleScene.tscn | 10 +++++ .../{ => assets}/default_env.tres | 0 .../godot_resources/{ => assets}/icon.png | Bin .../{ => assets}/icon.png.import | 6 +-- .../scripts/DynamicExampleScript.gd | 29 ++++++++++++ .../scripts/KomputeNativeClass.gdns | 8 ++++ .../scripts/KomputeNativeLibrary.gdnlib | 14 ++++++ examples/godot_examples/project.godot | 14 ++++-- 16 files changed, 95 insertions(+), 121 deletions(-) rename examples/godot_examples/gdnative_shared/src/{gdlibrary.cpp => KomputeGdNative.cpp} (81%) rename examples/godot_examples/gdnative_shared/src/{summator.cpp => KomputeSummator.cpp} (70%) rename examples/godot_examples/gdnative_shared/src/{summator.h => KomputeSummator.hpp} (83%) delete mode 100644 examples/godot_examples/gdnative_shared/src/player.cpp delete mode 100644 examples/godot_examples/gdnative_shared/src/player.h delete mode 100644 examples/godot_examples/gdnative_shared/src/root.cpp delete mode 100644 examples/godot_examples/gdnative_shared/src/root.h create mode 100755 examples/godot_examples/godot_resources/assets/DynamicExampleScene.tscn rename examples/godot_examples/godot_resources/{ => assets}/default_env.tres (100%) rename examples/godot_examples/godot_resources/{ => assets}/icon.png (100%) rename examples/godot_examples/godot_resources/{ => assets}/icon.png.import (70%) create mode 100755 examples/godot_examples/godot_resources/scripts/DynamicExampleScript.gd create mode 100755 examples/godot_examples/godot_resources/scripts/KomputeNativeClass.gdns create mode 100755 examples/godot_examples/godot_resources/scripts/KomputeNativeLibrary.gdnlib diff --git a/examples/godot_examples/gdnative_shared/CMakeLists.txt b/examples/godot_examples/gdnative_shared/CMakeLists.txt index 520a0613d..1f54060cc 100644 --- a/examples/godot_examples/gdnative_shared/CMakeLists.txt +++ b/examples/godot_examples/gdnative_shared/CMakeLists.txt @@ -16,8 +16,8 @@ find_package(Vulkan REQUIRED) add_library(kompute_godot SHARED - src/summator.cpp - src/gdlibrary.cpp) + src/KomputeSummator.cpp + src/KomputeGdNative.cpp) target_include_directories( kompute_godot PUBLIC diff --git a/examples/godot_examples/gdnative_shared/src/gdlibrary.cpp b/examples/godot_examples/gdnative_shared/src/KomputeGdNative.cpp similarity index 81% rename from examples/godot_examples/gdnative_shared/src/gdlibrary.cpp rename to examples/godot_examples/gdnative_shared/src/KomputeGdNative.cpp index 599bba3ac..d0b83940a 100644 --- a/examples/godot_examples/gdnative_shared/src/gdlibrary.cpp +++ b/examples/godot_examples/gdnative_shared/src/KomputeGdNative.cpp @@ -1,4 +1,4 @@ -#include "summator.h" +#include "KomputeSummator.hpp" extern "C" void GDN_EXPORT godot_gdnative_init(godot_gdnative_init_options *o) { godot::Godot::gdnative_init(o); @@ -11,5 +11,5 @@ extern "C" void GDN_EXPORT godot_gdnative_terminate(godot_gdnative_terminate_opt extern "C" void GDN_EXPORT godot_nativescript_init(void *handle) { godot::Godot::nativescript_init(handle); - godot::register_class(); + godot::register_class(); } diff --git a/examples/godot_examples/gdnative_shared/src/summator.cpp b/examples/godot_examples/gdnative_shared/src/KomputeSummator.cpp similarity index 70% rename from examples/godot_examples/gdnative_shared/src/summator.cpp rename to examples/godot_examples/gdnative_shared/src/KomputeSummator.cpp index 89aed9c27..f64e0d088 100644 --- a/examples/godot_examples/gdnative_shared/src/summator.cpp +++ b/examples/godot_examples/gdnative_shared/src/KomputeSummator.cpp @@ -3,16 +3,16 @@ #include #include -#include "summator.h" +#include "KomputeSummator.hpp" namespace godot { -Summator::Summator() { +KomputeSummator::KomputeSummator() { std::cout << "CALLING CONSTRUCTOR" << std::endl; this->_init(); } -void Summator::add(float value) { +void KomputeSummator::add(float value) { // Set the new data in the local device this->mSecondaryTensor->setData({value}); // Execute recorded sequence @@ -24,14 +24,14 @@ void Summator::add(float value) { } } -void Summator::reset() { +void KomputeSummator::reset() { } -float Summator::get_total() const { +float KomputeSummator::get_total() const { return this->mPrimaryTensor->data()[0]; } -void Summator::_init() { +void KomputeSummator::_init() { std::cout << "CALLING INIT" << std::endl; this->mPrimaryTensor = this->mManager.buildTensor({ 0.0 }); this->mSecondaryTensor = this->mManager.buildTensor({ 0.0 }); @@ -78,24 +78,18 @@ void Summator::_init() { } } -void Summator::_process(float delta) { - std::cout << "CALLING PROCESS" << std::endl; +void KomputeSummator::_process(float delta) { } -void Summator::_register_methods() { - register_method((char *)"_process", &Summator::_process); - register_method((char *)"_init", &Summator::_init); - register_method((char *)"add", &Summator::add); - register_method((char *)"reset", &Summator::reset); - register_method((char *)"get_total", &Summator::get_total); -} +void KomputeSummator::_register_methods() { + register_method((char *)"_process", &KomputeSummator::_process); + register_method((char *)"_init", &KomputeSummator::_init); -//void Summator::_bind_methods() { -// ClassDB::bind_method(D_METHOD("add", "value"), &Summator::add); -// ClassDB::bind_method(D_METHOD("reset"), &Summator::reset); -// ClassDB::bind_method(D_METHOD("get_total"), &Summator::get_total); -//} + register_method((char *)"add", &KomputeSummator::add); + register_method((char *)"reset", &KomputeSummator::reset); + register_method((char *)"get_total", &KomputeSummator::get_total); +} } diff --git a/examples/godot_examples/gdnative_shared/src/summator.h b/examples/godot_examples/gdnative_shared/src/KomputeSummator.hpp similarity index 83% rename from examples/godot_examples/gdnative_shared/src/summator.h rename to examples/godot_examples/gdnative_shared/src/KomputeSummator.hpp index fdd8b8970..7a5029ffe 100644 --- a/examples/godot_examples/gdnative_shared/src/summator.h +++ b/examples/godot_examples/gdnative_shared/src/KomputeSummator.hpp @@ -8,12 +8,12 @@ #include "kompute/Kompute.hpp" namespace godot { -class Summator : public Node2D { +class KomputeSummator : public Node2D { private: - GODOT_CLASS(Summator, Node2D); + GODOT_CLASS(KomputeSummator, Node2D); public: - Summator(); + KomputeSummator(); void add(float value); void reset(); diff --git a/examples/godot_examples/gdnative_shared/src/player.cpp b/examples/godot_examples/gdnative_shared/src/player.cpp deleted file mode 100644 index 79c8c79ae..000000000 --- a/examples/godot_examples/gdnative_shared/src/player.cpp +++ /dev/null @@ -1,39 +0,0 @@ -#include "Player.h" - -using namespace godot; - -void Player::_register_methods() { - register_method((char*)"_process", &Player::_process); -} - -void Player::_init() {} - -Player::Player() { - motion = Vector2(0, 0); -} - -Player::~Player() {} - -void Player::_process(float delta) -{ - UpdateMotionFromInput(); - move_and_slide(motion); -} - -void Player::UpdateMotionFromInput() -{ - motion = Vector2(0, 0); - - - Input* i = Input::get_singleton(); - - if (i->is_action_pressed("ui_up")) - motion.y -= speed; - if (i->is_action_pressed("ui_down")) - motion.y += speed; - if (i->is_action_pressed("ui_left")) - motion.x -= speed; - if (i->is_action_pressed("ui_right")) - motion.x += speed; - -} diff --git a/examples/godot_examples/gdnative_shared/src/player.h b/examples/godot_examples/gdnative_shared/src/player.h deleted file mode 100644 index 9d836af13..000000000 --- a/examples/godot_examples/gdnative_shared/src/player.h +++ /dev/null @@ -1,42 +0,0 @@ -#pragma once - -#include -#include -#include - -namespace godot { - class Player : public KinematicBody2D - { - - // Godot structure - private: - GODOT_CLASS(Player, KinematicBody2D) - public: - static void _register_methods(); - void _init(); - void _process(float delta); - - Player(); - ~Player(); - - - // Gameplay variables - public: - const int speed = 300; - - private: - Vector2 motion; - - - - // Gameplay methods - public: - void UpdateMotionFromInput(); - - private: - - - - - }; -} diff --git a/examples/godot_examples/gdnative_shared/src/root.cpp b/examples/godot_examples/gdnative_shared/src/root.cpp deleted file mode 100644 index 384c905c1..000000000 --- a/examples/godot_examples/gdnative_shared/src/root.cpp +++ /dev/null @@ -1 +0,0 @@ -#include "Root.h" diff --git a/examples/godot_examples/gdnative_shared/src/root.h b/examples/godot_examples/gdnative_shared/src/root.h deleted file mode 100644 index fba0e2470..000000000 --- a/examples/godot_examples/gdnative_shared/src/root.h +++ /dev/null @@ -1,5 +0,0 @@ -#pragma once -class Root -{ -}; - diff --git a/examples/godot_examples/godot_resources/assets/DynamicExampleScene.tscn b/examples/godot_examples/godot_resources/assets/DynamicExampleScene.tscn new file mode 100755 index 000000000..f390717c9 --- /dev/null +++ b/examples/godot_examples/godot_resources/assets/DynamicExampleScene.tscn @@ -0,0 +1,10 @@ +[gd_scene load_steps=3 format=2] + +[ext_resource path="res://godot_resources/scripts/DynamicExampleScript.gd" type="Script" id=1] +[ext_resource path="res://godot_resources/scripts/KomputeNativeClass.gdns" type="Script" id=2] + +[node name="Parent" type="Node2D"] +script = ExtResource( 1 ) + +[node name="KomputeNode" type="Node2D" parent="."] +script = ExtResource( 2 ) diff --git a/examples/godot_examples/godot_resources/default_env.tres b/examples/godot_examples/godot_resources/assets/default_env.tres similarity index 100% rename from examples/godot_examples/godot_resources/default_env.tres rename to examples/godot_examples/godot_resources/assets/default_env.tres diff --git a/examples/godot_examples/godot_resources/icon.png b/examples/godot_examples/godot_resources/assets/icon.png similarity index 100% rename from examples/godot_examples/godot_resources/icon.png rename to examples/godot_examples/godot_resources/assets/icon.png diff --git a/examples/godot_examples/godot_resources/icon.png.import b/examples/godot_examples/godot_resources/assets/icon.png.import similarity index 70% rename from examples/godot_examples/godot_resources/icon.png.import rename to examples/godot_examples/godot_resources/assets/icon.png.import index 204014664..edc407e0a 100755 --- a/examples/godot_examples/godot_resources/icon.png.import +++ b/examples/godot_examples/godot_resources/assets/icon.png.import @@ -2,15 +2,15 @@ importer="texture" type="StreamTexture" -path="res://.import/icon.png-410975027061d785c4229e33fe5ba462.stex" +path="res://.import/icon.png-dceb2295401b72087458cab0725ade4f.stex" metadata={ "vram_texture": false } [deps] -source_file="res://godot_resources/icon.png" -dest_files=[ "res://.import/icon.png-410975027061d785c4229e33fe5ba462.stex" ] +source_file="res://godot_resources/assets/icon.png" +dest_files=[ "res://.import/icon.png-dceb2295401b72087458cab0725ade4f.stex" ] [params] diff --git a/examples/godot_examples/godot_resources/scripts/DynamicExampleScript.gd b/examples/godot_examples/godot_resources/scripts/DynamicExampleScript.gd new file mode 100755 index 000000000..454314a57 --- /dev/null +++ b/examples/godot_examples/godot_resources/scripts/DynamicExampleScript.gd @@ -0,0 +1,29 @@ +extends Node2D + +# Called when the node enters the scene tree for the first time. +func _ready(): + + print("hello") + + # Use existing node + print($KomputeNode.get_total()) + + $KomputeNode.add(10) + print($KomputeNode.get_total()) + + $KomputeNode.add(10) + print($KomputeNode.get_total()) + + # Create new instance + var s = KomputeSummator.new() + + # This will print 0 as it's a new instance + print(s.get_total()) + + # Now we can again send further commands + s.add(10) + print(s.get_total()) + + s.add(10) + print(s.get_total()) + diff --git a/examples/godot_examples/godot_resources/scripts/KomputeNativeClass.gdns b/examples/godot_examples/godot_resources/scripts/KomputeNativeClass.gdns new file mode 100755 index 000000000..53ab73f77 --- /dev/null +++ b/examples/godot_examples/godot_resources/scripts/KomputeNativeClass.gdns @@ -0,0 +1,8 @@ +[gd_resource type="NativeScript" load_steps=2 format=2] + +[ext_resource path="res://godot_resources/scripts/KomputeNativeLibrary.gdnlib" type="GDNativeLibrary" id=1] + +[resource] +class_name = "KomputeSummator" +library = ExtResource( 1 ) +script_class_name = "KomputeSummator" diff --git a/examples/godot_examples/godot_resources/scripts/KomputeNativeLibrary.gdnlib b/examples/godot_examples/godot_resources/scripts/KomputeNativeLibrary.gdnlib new file mode 100755 index 000000000..6bc8c0700 --- /dev/null +++ b/examples/godot_examples/godot_resources/scripts/KomputeNativeLibrary.gdnlib @@ -0,0 +1,14 @@ +[general] + +singleton=false +load_once=true +symbol_prefix="godot_" +reloadable=false + +[entry] + +Windows.64="res://gdnative_shared/build/Release/kompute_godot.dll" + +[dependencies] + +Windows.64=[ ] diff --git a/examples/godot_examples/project.godot b/examples/godot_examples/project.godot index a3aa39692..ece04bef7 100755 --- a/examples/godot_examples/project.godot +++ b/examples/godot_examples/project.godot @@ -8,16 +8,22 @@ config_version=4 -_global_script_classes=[ ] +_global_script_classes=[ { +"base": "Node2D", +"class": "KomputeSummator", +"language": "NativeScript", +"path": "res://godot_resources/scripts/KomputeNativeClass.gdns" +} ] _global_script_class_icons={ - +"KomputeSummator": "" } [application] config/name="KomputeGame" -config/icon="res://godot_resources/icon.png" +run/main_scene="res://godot_resources/assets/DynamicExampleScene.tscn" +config/icon="res://godot_resources/assets/icon.png" [rendering] -environment/default_environment="res://godot_resources/default_env.tres" +environment/default_environment="res://godot_resources/assets/default_env.tres"