diff --git a/.gitattributes b/.gitattributes index 9abe13e..cfc7154 100644 --- a/.gitattributes +++ b/.gitattributes @@ -4,3 +4,5 @@ *.tscn eol=lf *.cfg eol=lf *.godot eol=lf +*.gdns eol=lf +*.gdnlib eol=lf diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..5efb8ca --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "c/SimpleDemo/godot_headers"] + path = c/SimpleDemo/godot_headers + url = https://github.com/GodotNativeTools/godot_headers diff --git a/c/SimpleDemo/README.md b/c/SimpleDemo/README.md index d20f66f..c9b0081 100644 --- a/c/SimpleDemo/README.md +++ b/c/SimpleDemo/README.md @@ -5,40 +5,53 @@ This is a small example using C to create a GDNative script that just showcases ## Compiling Dependencies: - * You need to have the [Godot headers](https://github.com/GodotNativeTools/godot_headers) saved somewhere on your system + + * You need [Godot headers](https://github.com/GodotNativeTools/godot_headers), this is now a submodule of this repo * clang or any decent C compiler that's C11 or C99 compatible ### Scons (cross platform) You can use scons to compile the library if you have it installed: - scons platform=PLATFORM +``` +scons platform=PLATFORM +``` Where platform is: x11, osx or windows -Optionally you can specify: - - headers=/PATH/TO/GODOT/HEADERS ### Linux To compile the library on Linux, do - cd src - clang -std=c11 -fPIC -c -I/PATH/TO/GODOT/HEADERS simple.c -o simple.os - clang -shared simple.os -o ../bin/libsimple.so +``` +cd src +clang -std=c11 -fPIC -c -I../godot_headers simple.c -o simple.os +clang -shared simple.os -o ../demo/bin/x11/libsimple.so +``` -This creates the file `libsimple.so` in your `src` directory. +This creates the file `libsimple.so` in your `demo/bin/x11` directory. For windows you need to find out what compiler flags need to be used, I don't know which ones. (If you do, feel free to fork and update this project and README) ### Mac OS X On Mac OS X: - cd src - clang -std=c11 -fPIC -c -I/PATH/TO/GODOT/HEADERS simple.c -o simple.os -arch i386 -arch x86_64 - clang -dynamiclib simple.os -o ../bin/libsimple.dylib -arch i386 -arch x86_64 +``` +cd src +clang -std=c11 -fPIC -c -I../godot_headers simple.c -o simple.os -arch i386 -arch x86_64 +clang -dynamiclib simple.os -o ../demo/bin/osx/libsimple.dylib -arch i386 -arch x86_64 +``` This creates the file 'libsimple.dylib' as a universal binary (or alternatively remove one of the -arch options from both commands if you want to just compile for one architecture). ### Windows -To be added +On Windows: + +``` +cd src +cl /Fosimple.obj /c simple.c /nologo -EHsc -DNDEBUG /MD /I. /I../godot_headers +link /nologo /dll /out:..\demo\bin\win64\libsimple.dll /implib:..\demo\bin\win64\libsimple.lib simple.obj +``` + +This creates the file `libsimple.dll` in your `demo/bin/win64` directory. + ## Usage diff --git a/c/SimpleDemo/SConstruct b/c/SimpleDemo/SConstruct index 82b75b6..e7ead64 100644 --- a/c/SimpleDemo/SConstruct +++ b/c/SimpleDemo/SConstruct @@ -1,47 +1,79 @@ #!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.', 'demo/bin/')) +opts.Add(PathVariable('target_name', 'The library name.', 'libsimple', PathVariable.PathAccept)) + # Local dependency paths, adapt them to your setup -godot_headers_path = ARGUMENTS.get("headers", os.getenv("GODOT_HEADERS", "../../../godot_headers/")) +godot_headers_path = "godot_headers/" -target = ARGUMENTS.get("target", "debug") +# only support 64 at this time.. +bits = 64 -# platform= makes it in line with Godots scons file, keeping p for backwards compatibility -platform = ARGUMENTS.get("p", "linux") -platform = ARGUMENTS.get("platform", platform) +# Updates the environment with the option variables. +opts.Update(env) -# 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) +# Process some arguments +if env['use_llvm']: + env['CC'] = 'clang' + env['CXX'] = 'clang++' -if ARGUMENTS.get("use_llvm", "no") == "yes": - env["CC"] = "clang" +if env['p'] != '': + env['platform'] = env['p'] -def add_sources(sources, directory): - for file in os.listdir(directory): - if file.endswith('.c'): - sources.append(directory + '/' + file) +if env['platform'] == '': + print("No valid target platform selected.") + quit(); -if platform == "osx": - env.Append(CCFLAGS = ['-g','-O3', '-arch', 'x86_64']) - env.Append(LINKFLAGS = ['-arch', 'x86_64']) - -if platform == "linux": - env.Append(CCFLAGS = ['-fPIC', '-g','-O3', '-std=c11']) - -if platform == "windows": - if target == "debug": - env.Append(CCFLAGS = ['-EHsc', '-D_DEBUG', '/MDd']) +# Check our platform specifics +if env['platform'] == "osx": + env['target_path'] += 'osx/' + if env['target'] in ('debug', 'd'): + env.Append(CCFLAGS = ['-g','-O2', '-arch', 'x86_64']) + env.Append(LINKFLAGS = ['-arch', 'x86_64']) else: - env.Append(CCFLAGS = ['-O2', '-EHsc', '-DNDEBUG', '/MD']) + env.Append(CCFLAGS = ['-g','-O3', '-arch', 'x86_64']) + env.Append(LINKFLAGS = ['-arch', 'x86_64']) -# , 'include', 'include/core' +elif env['platform'] in ('x11', 'linux'): + env['target_path'] += 'x11/' + 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/' + # 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']) + +# make sure our binding library is properly includes env.Append(CPPPATH=['.', godot_headers_path]) -sources = [] -add_sources(sources, "src") +# 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/*.c') + +library = env.SharedLibrary(target=env['target_path'] + env['target_name'] , source=sources) -library = env.SharedLibrary(target='bin/simple', source=sources) Default(library) + +# Generates help for the -h scons option. +Help(opts.GenerateHelpText(env)) diff --git a/c/SimpleDemo/bin/simple.gdnlib b/c/SimpleDemo/bin/simple.gdnlib deleted file mode 100644 index c7ae452..0000000 --- a/c/SimpleDemo/bin/simple.gdnlib +++ /dev/null @@ -1,18 +0,0 @@ - -[general] - -singleton=false -load_once=true -symbol_prefix="godot_" - -[entry] - -X11.64="res://bin/libsimple.so" -Windows.64="res://bin/simple.dll" -OSX.64="res://bin/libsimple.dylib" - -[dependencies] - -X11.64=[] -Windows=[] -OSX.64=[] diff --git a/c/SimpleDemo/demo/bin/osx/.gitignore b/c/SimpleDemo/demo/bin/osx/.gitignore new file mode 100644 index 0000000..d6b7ef3 --- /dev/null +++ b/c/SimpleDemo/demo/bin/osx/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore diff --git a/c/SimpleDemo/demo/bin/simple.gdnlib b/c/SimpleDemo/demo/bin/simple.gdnlib new file mode 100644 index 0000000..84734e3 --- /dev/null +++ b/c/SimpleDemo/demo/bin/simple.gdnlib @@ -0,0 +1,18 @@ +[general] + +singleton=false +load_once=true +symbol_prefix="godot_" +reloadable=true + +[entry] + +X11.64="res://bin/x11/libsimple.so" +Windows.64="res://bin/win64/libsimple.dll" +OSX.64="res://bin/osx/libsimple.dylib" + +[dependencies] + +X11.64=[ ] +Windows=[ ] +OSX.64=[ ] diff --git a/c/SimpleDemo/bin/simple.gdns b/c/SimpleDemo/demo/bin/simple.gdns similarity index 100% rename from c/SimpleDemo/bin/simple.gdns rename to c/SimpleDemo/demo/bin/simple.gdns diff --git a/c/SimpleDemo/demo/bin/win64/.gitignore b/c/SimpleDemo/demo/bin/win64/.gitignore new file mode 100644 index 0000000..d6b7ef3 --- /dev/null +++ b/c/SimpleDemo/demo/bin/win64/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore diff --git a/c/SimpleDemo/demo/bin/x11/.gitignore b/c/SimpleDemo/demo/bin/x11/.gitignore new file mode 100644 index 0000000..d6b7ef3 --- /dev/null +++ b/c/SimpleDemo/demo/bin/x11/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore diff --git a/c/SimpleDemo/default_env.tres b/c/SimpleDemo/demo/default_env.tres similarity index 100% rename from c/SimpleDemo/default_env.tres rename to c/SimpleDemo/demo/default_env.tres diff --git a/c/SimpleDemo/icon.png b/c/SimpleDemo/demo/icon.png similarity index 100% rename from c/SimpleDemo/icon.png rename to c/SimpleDemo/demo/icon.png diff --git a/c/SimpleDemo/main.gd b/c/SimpleDemo/demo/main.gd similarity index 100% rename from c/SimpleDemo/main.gd rename to c/SimpleDemo/demo/main.gd diff --git a/c/SimpleDemo/demo/main.tscn b/c/SimpleDemo/demo/main.tscn new file mode 100644 index 0000000..c5bf9cb --- /dev/null +++ b/c/SimpleDemo/demo/main.tscn @@ -0,0 +1,24 @@ +[gd_scene load_steps=2 format=2] + +[ext_resource path="res://main.gd" type="Script" id=1] + +[node name="main" type="Control"] +anchor_right = 1.0 +anchor_bottom = 1.0 +script = ExtResource( 1 ) + +[node name="Button" type="Button" parent="."] +margin_left = 384.0 +margin_top = 233.0 +margin_right = 630.0 +margin_bottom = 289.0 +text = "Hello" + +[node name="Label" type="Label" parent="."] +margin_left = 392.0 +margin_top = 311.0 +margin_right = 629.0 +margin_bottom = 368.0 +size_flags_vertical = 0 + +[connection signal="pressed" from="Button" to="." method="_on_Button_pressed"] diff --git a/c/SimpleDemo/project.godot b/c/SimpleDemo/demo/project.godot similarity index 87% rename from c/SimpleDemo/project.godot rename to c/SimpleDemo/demo/project.godot index 0d55cdc..b661c6b 100644 --- a/c/SimpleDemo/project.godot +++ b/c/SimpleDemo/demo/project.godot @@ -6,7 +6,12 @@ ; [section] ; section goes between [] ; param=value ; assign values to parameters -config_version=3 +config_version=4 + +_global_script_classes=[ ] +_global_script_class_icons={ + +} [application] diff --git a/c/SimpleDemo/godot_headers b/c/SimpleDemo/godot_headers new file mode 160000 index 0000000..53c1f1c --- /dev/null +++ b/c/SimpleDemo/godot_headers @@ -0,0 +1 @@ +Subproject commit 53c1f1c645161b60a4f33bc52686cc872fbf5770 diff --git a/c/SimpleDemo/main.tscn b/c/SimpleDemo/main.tscn deleted file mode 100644 index baf6621..0000000 --- a/c/SimpleDemo/main.tscn +++ /dev/null @@ -1,63 +0,0 @@ -[gd_scene load_steps=2 format=2] - -[ext_resource path="res://main.gd" type="Script" id=1] - -[node name="main" type="Control" index="0"] - -anchor_left = 0.0 -anchor_top = 0.0 -anchor_right = 1.0 -anchor_bottom = 1.0 -rect_pivot_offset = Vector2( 0, 0 ) -mouse_filter = 0 -mouse_default_cursor_shape = 0 -size_flags_horizontal = 1 -size_flags_vertical = 1 -script = ExtResource( 1 ) - -[node name="Button" type="Button" parent="." index="0"] - -anchor_left = 0.0 -anchor_top = 0.0 -anchor_right = 0.0 -anchor_bottom = 0.0 -margin_left = 384.0 -margin_top = 233.0 -margin_right = 630.0 -margin_bottom = 289.0 -rect_pivot_offset = Vector2( 0, 0 ) -focus_mode = 2 -mouse_filter = 0 -mouse_default_cursor_shape = 0 -size_flags_horizontal = 1 -size_flags_vertical = 1 -toggle_mode = false -enabled_focus_mode = 2 -shortcut = null -group = null -text = "Hello" -flat = false -align = 1 - -[node name="Label" type="Label" parent="." index="1"] - -anchor_left = 0.0 -anchor_top = 0.0 -anchor_right = 0.0 -anchor_bottom = 0.0 -margin_left = 392.0 -margin_top = 311.0 -margin_right = 629.0 -margin_bottom = 368.0 -rect_pivot_offset = Vector2( 0, 0 ) -mouse_filter = 2 -mouse_default_cursor_shape = 0 -size_flags_horizontal = 1 -size_flags_vertical = 0 -percent_visible = 1.0 -lines_skipped = 0 -max_lines_visible = -1 - -[connection signal="pressed" from="Button" to="." method="_on_Button_pressed"] - -