Added base working example

This commit is contained in:
Alejandro Saucedo 2020-09-20 12:40:35 +01:00
parent 9483739d3c
commit b76cc00b46
11 changed files with 189 additions and 84 deletions

View file

@ -0,0 +1,15 @@
#include "summator.h"
extern "C" void GDN_EXPORT godot_gdnative_init(godot_gdnative_init_options *o) {
godot::Godot::gdnative_init(o);
}
extern "C" void GDN_EXPORT godot_gdnative_terminate(godot_gdnative_terminate_options *o) {
godot::Godot::gdnative_terminate(o);
}
extern "C" void GDN_EXPORT godot_nativescript_init(void *handle) {
godot::Godot::nativescript_init(handle);
godot::register_class<godot::Summator>();
}

View file

@ -0,0 +1,39 @@
#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;
}

View file

@ -0,0 +1,42 @@
#pragma once
#include <Godot.hpp>
#include <KinematicBody2D.hpp>
#include <Input.hpp>
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:
};
}

View file

@ -1,14 +0,0 @@
/* register_types.cpp */
#include "register_types.h"
#include "core/class_db.h"
#include "summator.h"
void register_summator_types() {
ClassDB::register_class<Summator>();
}
void unregister_summator_types() {
// Nothing to do here in this example.
}

View file

@ -1,6 +0,0 @@
/* register_types.h */
#pragma once
void register_summator_types();
void unregister_summator_types();
/* yes, the word in the middle must be the same as the module folder name */

View file

@ -0,0 +1 @@
#include "Root.h"

View file

@ -0,0 +1,5 @@
#pragma once
class Root
{
};

View file

@ -1,10 +1,38 @@
/* summator.cpp */
#include <vector>
#include <iostream>
#include "summator.h"
namespace godot {
Summator::Summator() {
std::cout << "CALLING CONSTRUCTOR" << std::endl;
this->_init();
}
void Summator::add(float value) {
// Set the new data in the local device
this->mSecondaryTensor->setData({value});
// Execute recorded sequence
if (std::shared_ptr<kp::Sequence> sq = this->mSequence.lock()) {
sq->eval();
}
else {
throw std::runtime_error("Sequence pointer no longer available");
}
}
void Summator::reset() {
}
float Summator::get_total() const {
return this->mPrimaryTensor->data()[0];
}
void Summator::_init() {
std::cout << "CALLING INIT" << std::endl;
this->mPrimaryTensor = this->mManager.buildTensor({ 0.0 });
this->mSecondaryTensor = this->mManager.buildTensor({ 0.0 });
this->mSequence = this->mManager.getOrCreateManagedSequence("AdditionSeq");
@ -50,28 +78,24 @@ Summator::Summator() {
}
}
void Summator::add(float value) {
// Set the new data in the local device
this->mSecondaryTensor->setData({value});
// Execute recorded sequence
if (std::shared_ptr<kp::Sequence> sq = this->mSequence.lock()) {
sq->eval();
}
else {
throw std::runtime_error("Sequence pointer no longer available");
}
void Summator::_process(float delta) {
std::cout << "CALLING PROCESS" << std::endl;
}
void Summator::reset() {
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);
}
float Summator::get_total() const {
return this->mPrimaryTensor->data()[0];
}
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);
//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);
//}
}

View file

@ -1,16 +1,16 @@
/* summator.h */
#pragma once
#ifndef SUMMATOR_H
#define SUMMATOR_H
#include <Godot.hpp>
#include <Node2D.hpp>
#include <memory>
#include "kompute/Kompute.hpp"
#include "scene/main/node.h"
class Summator : public Node {
GDCLASS(Summator, Node);
namespace godot {
class Summator : public Node2D {
private:
GODOT_CLASS(Summator, Node2D);
public:
Summator();
@ -19,8 +19,13 @@ public:
void reset();
float get_total() const;
void _process(float delta);
void _init();
static void _register_methods();
protected:
static void _bind_methods();
//static void _bind_methods();
private:
kp::Manager mManager;
@ -29,4 +34,4 @@ private:
std::shared_ptr<kp::Tensor> mSecondaryTensor;
};
#endif // SUMMATOR_H
}