Update cpp_constructors demo

This commit is contained in:
lupoDharkael
2018-12-01 16:16:31 +01:00
parent 9df4d39140
commit 9f333600b6
13 changed files with 193 additions and 121 deletions

View File

@@ -1,8 +1,7 @@
extends Control
onready var TestClass = preload("res://TestClass.gdn")
onready var TestClass = preload("res://bin/testclass.gdns")
func _ready():
var test_object = TestClass.new()
test_object.test_method("Hello World")

View File

@@ -3,11 +3,7 @@
[ext_resource path="res://Main.gd" type="Script" id=1]
[node name="Main" type="Control"]
margin_right = 40.0
margin_bottom = 40.0
rect_clip_content = false
mouse_filter = 0
script = ExtResource( 1 )

View File

@@ -0,0 +1,2 @@
all:
scons cpp_bindings="../../godot-cpp/" headers="../../godot-cpp/godot_headers/"

View File

@@ -0,0 +1,57 @@
#!python
import os
# platform= makes it in line with Godots scons file, keeping p for backwards compatibility
platform = ARGUMENTS.get("p", "linux")
platform = ARGUMENTS.get("platform", platform)
# 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 = Environment()
if platform == "windows":
env = Environment(ENV = os.environ)
godot_headers_path = ARGUMENTS.get("headers", os.getenv("GODOT_HEADERS", "godot-cpp/godot_headers"))
godot_bindings_path = ARGUMENTS.get("cpp_bindings", os.getenv("CPP_BINDINGS", "godot-cpp"))
# default to debug build, must be same setting as used for cpp_bindings
target = ARGUMENTS.get("target", "debug")
if ARGUMENTS.get("use_llvm", "no") == "yes":
env["CXX"] = "clang++"
# put stuff that is the same for all first, saves duplication
if platform == "osx":
env.Append(CCFLAGS = ['-g','-O3', '-std=c++14', '-arch', 'x86_64'])
env.Append(LINKFLAGS = ['-arch', 'x86_64', '-framework', 'Cocoa', '-Wl,-undefined,dynamic_lookup'])
elif platform == "linux":
env.Append(CCFLAGS = ['-g','-O3', '-std=c++14', '-Wno-writable-strings'])
env.Append(LINKFLAGS = ['-Wl,-R,\'$$ORIGIN\''])
elif platform == "windows":
# need to add detection of msvc vs mingw, this is for msvc...
env.Append(LINKFLAGS = ['/WX'])
if target == "debug":
env.Append(CCFLAGS = ['-EHsc', '-D_DEBUG', '/MDd'])
else:
env.Append(CCFLAGS = ['-O2', '-EHsc', '-DNDEBUG', '/MD'])
def add_sources(sources, dir):
for f in os.listdir(dir):
if f.endswith(".cpp"):
sources.append(dir + "/" + f)
env.Append(CPPPATH=[godot_headers_path, godot_bindings_path + '/include', godot_bindings_path + '/include/gen/', godot_bindings_path + '/include/core/'])
if target == "debug":
env.Append(LIBS=['libgodot-cpp.linux.debug.64'])
else:
env.Append(LIBS=['libgodot-cpp.linux.release.64'])
env.Append(LIBPATH=[ godot_bindings_path + '/bin/' ])
sources = []
add_sources(sources, "src")
library = env.SharedLibrary(target='bin/libconstructor', source=sources)
Default(library)

View File

@@ -1,21 +0,0 @@
[gd_resource type="GDNativeScript" load_steps=2 format=2]
[sub_resource type="GDNativeLibrary" id=1]
platform/unix = ""
platform/x11 = "res://lib/libtest.so"
platform/server = ""
platform/android = ""
platform/haiku = ""
platform/mac = ""
platform/ios = ""
platform/osx = ""
platform/html5 = ""
platform/windows = ""
platform/uwp = ""
[resource]
library = SubResource( 1 )
script_name = "TestClass"

View File

@@ -0,0 +1,16 @@
[entry]
X11.64="res://bin/libconstructor.so"
X11.32="res://bin/libconstructor.so"
[dependencies]
X11.64=[ ]
X11.32=[ ]
[general]
singleton=false
load_once=true
symbol_prefix="godot_"
reloadable=true

View File

@@ -0,0 +1,9 @@
[gd_resource type="NativeScript" load_steps=2 format=2]
[ext_resource path="res://bin/constructor.gdnlib" type="GDNativeLibrary" id=1]
[resource]
class_name = "TestClass"
library = ExtResource( 1 )

View File

@@ -0,0 +1,31 @@
[remap]
importer="texture"
type="StreamTexture"
path="res://.import/icon.png-487276ed1e3a0c39cad0279d744ee560.stex"
[deps]
source_file="res://icon.png"
dest_files=[ "res://.import/icon.png-487276ed1e3a0c39cad0279d744ee560.stex" ]
[params]
compress/mode=0
compress/lossy_quality=0.7
compress/hdr_mode=0
compress/bptc_ldr=0
compress/normal_map=0
flags/repeat=0
flags/filter=true
flags/mipmaps=false
flags/anisotropic=false
flags/srgb=2
process/fix_alpha_border=true
process/premult_alpha=false
process/HDR_as_SRGB=false
process/invert_color=false
stream=false
size_limit=0
detect_3d=true
svg/scale=1.0

View File

@@ -1,16 +1,23 @@
; Engine configuration file.
; It's best edited using the editor UI and not directly,
; since the parameters that go here are not all obvious.
;
; Format:
;
; Format:
; [section] ; section goes between []
; param=value ; assign values to parameters
config_version=4
_global_script_classes=[ ]
_global_script_class_icons={
}
[application]
name="cpp_constructors"
icon="res://icon.png"
[rendering]
viewport/default_environment="res://default_env.tres"

View File

@@ -1,36 +0,0 @@
#!python
import os
env = Environment()
if ARGUMENTS.get("use_llvm", "no") == "yes":
env["CXX"] = "clang++"
platform = ARGUMENTS.get("p", "linux")
def add_sources(sources, dir):
for f in os.listdir(dir):
if f.endswith(".cpp"):
sources.append(dir + "/" + f)
if platform == "linux":
env.Append(CCFLAGS = ['-g','-O3', '-std=c++14', '-Wno-writable-strings'])
env.Append(LINKFLAGS = ['-Wl,-R,\'$$ORIGIN\''])
env.Append(CPPPATH=['/usr/include/godot/', '/usr/include/godot/godot_cpp/', '/usr/include/godot/godot_cpp/core'])
env.Append(LIBS=['godot_cpp_core', 'godot_cpp_bindings'])
env.Append(LIBPATH=["../lib"])
if platform == "windows":
env.Append(LIBS=['godot.windows.tools.64'])
sources = ["init.cpp"]
library = env.SharedLibrary(target='../lib/test', source=sources)
Default(library)

View File

@@ -0,0 +1,52 @@
#include <Reference.hpp>
#include <Timer.hpp>
#include <File.hpp>
#include <Ref.hpp>
#include <stdio.h>
using namespace godot;
class TestClass : public Reference {
GODOT_CLASS(TestClass, Reference)
int count;
public:
void _init() {
count = 34;
}
int test_method(String s) {
Godot::print(s);
count++;
Timer *t = new Timer;
t->set_wait_time(13.36 * count);
Godot::print(String::num_real(t->get_wait_time()));
delete t;
// test Ref<T>
Ref<File> file;
file.instance();
file->open("res://test.txt", File::WRITE);
file->close();
// ref should free the memory automatically
return count;
}
static void _register_methods() {
register_method("test_method", &TestClass::test_method);
}
};

View File

@@ -1,60 +1,20 @@
#include <Godot.hpp>
#include <Reference.hpp>
#include <Timer.hpp>
#include <File.hpp>
#include "TestClass.hpp"
#include <Ref.hpp>
#include <stdio.h>
using namespace godot;
class TestClass : public GodotScript<Reference> {
GODOT_CLASS(TestClass)
int count;
public:
int test_method(const String s)
{
Godot::print(s);
count++;
Timer *t = new Timer;
t->set_wait_time(13.36 * count);
// sorry, no String::num() yet
char str[128];
snprintf(str, 128, "wait time: %f", t->get_wait_time());
Godot::print(String(str));
delete t;
// test Ref<T>
Ref<File> file = new File;
file->open("res://test.txt", File::WRITE);
file->close();
// ref should free the memory automatically
return count;
}
static void _register_methods()
{
register_method("test_method", &TestClass::test_method);
}
};
GODOT_NATIVE_INIT(godot_native_init_options *options)
extern "C" void GDN_EXPORT godot_gdnative_init(godot_gdnative_init_options *o)
{
register_class<TestClass>();
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<TestClass>();
}

View File