Experimental work on getting the module to build as a gdnative module.

This commit is contained in:
Relintai 2023-10-24 21:38:45 +02:00
parent 11d10c5ca5
commit cf7cc6dec1
9 changed files with 262 additions and 0 deletions

5
.gitignore vendored
View File

@ -6,3 +6,8 @@
*.pyc *.pyc
*.bc *.bc
*.os *.os
*.so
.sconsign.dblite
gdnative_cpp/

105
SConstruct Normal file
View File

@ -0,0 +1,105 @@
#!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 = "libpandemonium-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, cpp_bindings_path + '/core/', cpp_bindings_path + '/gen/'])
env.Append(LIBPATH=[cpp_bindings_path + 'bin/'])
env.Append(LIBS=[cpp_library])
env.Append(CCFLAGS = ['-DGDNATIVE'])
# 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')
sources = [
"gdlibrary.cpp",
"mlpp/lin_alg/mlpp_vector.cpp",
"mlpp/lin_alg/mlpp_matrix.cpp",
"mlpp/lin_alg/mlpp_tensor3.cpp",
"mlpp/lin_alg/lin_alg.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))

16
gdlibrary.cpp Normal file
View File

@ -0,0 +1,16 @@
#include "core/pandemonium_global.h"
extern "C" void GDN_EXPORT pandemonium_gdnative_init(pandemonium_gdnative_init_options *o) {
Pandemonium::gdnative_init(o);
}
extern "C" void GDN_EXPORT pandemonium_gdnative_terminate(pandemonium_gdnative_terminate_options *o) {
Pandemonium::gdnative_terminate(o);
}
extern "C" void GDN_EXPORT pandemonium_nativescript_init(void *handle) {
Pandemonium::nativescript_init(handle);
}

View File

@ -10,10 +10,21 @@
//TODO Methods here should probably use error macros in a way where they get disabled in non-tools(?) (maybe release?) builds //TODO Methods here should probably use error macros in a way where they get disabled in non-tools(?) (maybe release?) builds
#ifndef GDNATIVE
#include "core/math/math_defs.h" #include "core/math/math_defs.h"
#include "core/object/reference.h" #include "core/object/reference.h"
#else
#include "core/defs.h"
#include "core/math_funcs.h"
#include "gen/resource.h"
#endif
#include "../lin_alg/mlpp_matrix.h" #include "../lin_alg/mlpp_matrix.h"
#include "../lin_alg/mlpp_vector.h" #include "../lin_alg/mlpp_vector.h"

View File

@ -1,6 +1,8 @@
#ifndef MLPP_MATRIX_H #ifndef MLPP_MATRIX_H
#define MLPP_MATRIX_H #define MLPP_MATRIX_H
#ifndef GDNATIVE
#include "core/math/math_defs.h" #include "core/math/math_defs.h"
#include "core/containers/pool_vector.h" #include "core/containers/pool_vector.h"
@ -12,6 +14,19 @@
#include "core/object/resource.h" #include "core/object/resource.h"
#else
#include "core/defs.h"
#include "core/math_funcs.h"
#include "core/pool_arrays.h"
#include "core/containers/vector.h"
#include "core/os/memory.h"
#include "gen/resource.h"
#endif
#include "mlpp_vector.h" #include "mlpp_vector.h"
class Image; class Image;

View File

@ -1,6 +1,8 @@
#ifndef MLPP_TENSOR3_H #ifndef MLPP_TENSOR3_H
#define MLPP_TENSOR3_H #define MLPP_TENSOR3_H
#ifndef GDNATIVE
#include "core/math/math_defs.h" #include "core/math/math_defs.h"
#include "core/containers/pool_vector.h" #include "core/containers/pool_vector.h"
@ -12,6 +14,18 @@
#include "core/object/resource.h" #include "core/object/resource.h"
#else
#include "core/defs.h"
#include "core/math_funcs.h"
#include "core/pool_arrays.h"
#include "core/containers/vector.h"
#include "core/os/memory.h"
#include "gen/resource.h"
#endif
#include "mlpp_matrix.h" #include "mlpp_matrix.h"
#include "mlpp_vector.h" #include "mlpp_vector.h"

View File

@ -1,6 +1,8 @@
#ifndef MLPP_VECTOR_H #ifndef MLPP_VECTOR_H
#define MLPP_VECTOR_H #define MLPP_VECTOR_H
#ifndef GDNATIVE
#include "core/math/math_defs.h" #include "core/math/math_defs.h"
#include "core/math/math_funcs.h" #include "core/math/math_funcs.h"
@ -12,6 +14,18 @@
#include "core/object/resource.h" #include "core/object/resource.h"
#else
#include "core/defs.h"
#include "core/math_funcs.h"
#include "core/pool_arrays.h"
#include "core/containers/vector.h"
#include "core/os/memory.h"
#include "gen/resource.h"
#endif
//REMOVE //REMOVE
#include <vector> #include <vector>

51
src/gdexample.cpp Normal file
View File

@ -0,0 +1,51 @@
#include "gdexample.h"
void GDExample::_register_methods() {
register_method("_process", &GDExample::_process);
register_property<GDExample, float>("amplitude", &GDExample::amplitude, 10.0);
register_property<GDExample, float>("speed", &GDExample::set_speed, &GDExample::get_speed, 1.0);
//register_signal<GDExample>((char *)"position_changed", "node", PANDEMONIUM_VARIANT_TYPE_OBJECT, "new_pos", PANDEMONIUM_VARIANT_TYPE_VECTOR2);
register_signal<GDExample>("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;
}

31
src/gdexample.h Normal file
View File

@ -0,0 +1,31 @@
#ifndef GDEXAMPLE_H
#define GDEXAMPLE_H
#include "gen/object.h"
#include "gen/sprite.h"
#include "core/pandemonium.h"
class GDExample : public Sprite {
PANDEMONIUM_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