Skip to content

Installing for Godot 3.x

Emmanuel edited this page Feb 20, 2022 · 9 revisions

Installing for Godot 3.x

Prerequisites

We will be following gdnative_cpp_example Godot guide with several modifications.

Direct install:

git clone --recursive https://github.com/EmmanuelMess/Godot-poly2tri.git

And import Godot-poly2tri/demo/project.godot in Godot

All steps

Start

Move your <project folder> into an empty folder "cpp_root".

If your project was a git repo, you can move the repo a folder up, see here how.

If it wasn't a git repo, simply run git init in cpp_root.

Clone the cpp submodule:

git submodule add -b 3.x https://github.com/godotengine/godot-cpp
git submodule update --init --recursive

Create an api.json:

cd godot-cpp
<path to godot> --gdnative-generate-json-api api.json
mv ./api.json ../

Create a few files:

Create cpp_root/godot-cpp/gdexample.h:

#ifndef GDEXAMPLE_H
#define GDEXAMPLE_H

#include <Godot.hpp>
#include <Sprite.hpp>

namespace godot {

class GDExample : public Sprite {
    GODOT_CLASS(GDExample, Sprite)

private:
    float time_passed;

public:
    static void _register_methods();

    GDExample();
    ~GDExample();

    void _init(); // our initializer called by Godot

    void _process(float delta);
};

}

#endif

Create cpp_root/godot-cpp/gdexample.cpp:

#include "gdexample.h"

#include <vector>
#include <poly2tri.h>

using namespace godot;

void GDExample::_register_methods() {
    register_method("_process", &GDExample::_process);
}

GDExample::GDExample() {
}

GDExample::~GDExample() {
    // add your cleanup here
}

void GDExample::_init() {
    // initialize any variables here
    time_passed = 0.0;
}

void GDExample::_process(float delta) {
    time_passed += delta;

    Vector2 new_position = Vector2(10.0 + (10.0 * sin(time_passed * 2.0)), 10.0 + (10.0 * cos(time_passed * 1.5)));

    set_position(new_position);

    std::vector<p2t::Point*> polyline{ new p2t::Point(0, 0), new p2t::Point(0, 1),
                                     new p2t::Point(1, 1), new p2t::Point(1, 0) };
    p2t::CDT cdt{ polyline };
    const auto result = cdt.GetTriangles();
    p2t::IsDelaunay(result);

}

Create cpp_root/godot-cpp/gdlibrary.cpp:

#include "gdexample.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::GDExample>();
}

Clone poly2tri

At cpp_root:

git submodule add  https://github.com/jhasse/poly2tri.git

Go to root_cpp/poly2tri/CMakeFiles.txt and add set(CMAKE_POSITION_INDEPENDENT_CODE ON) after set(CMAKE_CXX_STANDARD 14).

Then, in cpp_root, run:

mkdir poly2tri/build && cd poly2tri/build
cmake -GNinja ..
cmake --build .

Build stuff

In the cpp_root create SConstruct file:

#!python
import os

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.', 'demo/bin/'))
opts.Add(PathVariable('target_name', 'The library name.', 'libgdexample', PathVariable.PathAccept))

# Local dependency paths, adapt them to your setup
godot_headers_path = "godot-cpp/godot-headers/"
cpp_bindings_path = "godot-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();

# For the reference:
# - CCFLAGS are compilation flags shared between C and C++
# - CFLAGS are for C-specific compilation flags
# - CXXFLAGS are for C++-specific compilation flags
# - CPPFLAGS are for pre-processor flags
# - CPPDEFINES are for pre-processor defines
# - LINKFLAGS are for linking flags

# Check our platform specifics
if env['platform'] == "osx":
    env['target_path'] += 'osx/'
    cpp_library += '.osx'
    env.Append(CCFLAGS=['-arch', 'x86_64'])
    env.Append(CXXFLAGS=['-std=c++17'])
    env.Append(LINKFLAGS=['-arch', 'x86_64'])
    if env['target'] in ('debug', 'd'):
        env.Append(CCFLAGS=['-g', '-O2'])
    else:
        env.Append(CCFLAGS=['-g', '-O3'])

elif env['platform'] in ('x11', 'linux'):
    env['target_path'] += 'x11/'
    cpp_library += '.linux'
    env.Append(CCFLAGS=['-fPIC'])
    env.Append(CXXFLAGS=['-std=c++17'])
    if env['target'] in ('debug', 'd'):
        env.Append(CCFLAGS=['-g3', '-Og'])
    else:
        env.Append(CCFLAGS=['-g', '-O3'])

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(CPPDEFINES=['WIN32', '_WIN32', '_WINDOWS', '_CRT_SECURE_NO_WARNINGS'])
    env.Append(CCFLAGS=['-W3', '-GR'])
    env.Append(CXXFLAGS='/std:c++17')
    if env['target'] in ('debug', 'd'):
        env.Append(CPPDEFINES=['_DEBUG'])
        env.Append(CCFLAGS=['-EHsc', '-MDd', '-ZI'])
        env.Append(LINKFLAGS=['-DEBUG'])
    else:
        env.Append(CPPDEFINES=['NDEBUG'])
        env.Append(CCFLAGS=['-O2', '-EHsc', '-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/', 'poly2tri/poly2tri/'])
env.Append(LIBPATH=[cpp_bindings_path + 'bin/', 'poly2tri/build/'])
env.Append(LIBS=[cpp_library, 'libpoly2tri.a'])

# tweak this if you want to use different folders, or more folders, to store your source code in.
env.Append(CPPPATH=[cpp_bindings_path + 'src/'])
sources = Glob(cpp_bindings_path + '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))

Go to the SConstruct file and change demo/bin/ to <project folder>/bin.

In cpp_root run:

mkdir <project folder>/bin
cd godot-cpp
scons platform=linux generate_bindings=yes bits=64 use_custom_api_file=yes custom_api_file=../api.json -j$(nproc)
cd ..
scons platform=linux

Creating a sprite

Create <project folder>/bin/gdexample.gdnlib:

[general]

singleton=false
load_once=true
symbol_prefix="godot_"
reloadable=false

[entry]

X11.64="res://bin/x11/libgdexample.so"
Windows.64="res://bin/win64/libgdexample.dll"
OSX.64="res://bin/osx/libgdexample.dylib"

[dependencies]

X11.64=[]
Windows.64=[]
OSX.64=[]

Create <project folder>/bin/gdexample.gdns:

[gd_resource type="NativeScript" load_steps=2 format=2]

[ext_resource path="res://bin/gdexample.gdnlib" type="GDNativeLibrary" id=1]

[resource]

resource_name = "gdexample"
class_name = "GDExample"
library = ExtResource( 1 )

In your project

  1. Open Godot and load your project
  2. Create a new Spacial called "Main", and save the scene as "main.tscn"
  3. Create a new Sprite, set Centered to false and set texture to Godot's icon
  4. Load bin/gdexample.gdns as its script
  5. Run Main scene
  6. If the sprite moves, it worked. If it doesn't check all steps.