mirror of
https://github.com/godotengine/gdnative-demos.git
synced 2025-12-30 21:49:05 +03:00
Reworked C SimpleDemo to latest godot_headers
This commit is contained in:
2
.gitattributes
vendored
2
.gitattributes
vendored
@@ -4,3 +4,5 @@
|
||||
*.tscn eol=lf
|
||||
*.cfg eol=lf
|
||||
*.godot eol=lf
|
||||
*.gdns eol=lf
|
||||
*.gdnlib eol=lf
|
||||
|
||||
3
.gitmodules
vendored
Normal file
3
.gitmodules
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
[submodule "c/SimpleDemo/godot_headers"]
|
||||
path = c/SimpleDemo/godot_headers
|
||||
url = https://github.com/GodotNativeTools/godot_headers
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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))
|
||||
|
||||
@@ -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=[]
|
||||
2
c/SimpleDemo/demo/bin/osx/.gitignore
vendored
Normal file
2
c/SimpleDemo/demo/bin/osx/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
*
|
||||
!.gitignore
|
||||
18
c/SimpleDemo/demo/bin/simple.gdnlib
Normal file
18
c/SimpleDemo/demo/bin/simple.gdnlib
Normal file
@@ -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=[ ]
|
||||
2
c/SimpleDemo/demo/bin/win64/.gitignore
vendored
Normal file
2
c/SimpleDemo/demo/bin/win64/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
*
|
||||
!.gitignore
|
||||
2
c/SimpleDemo/demo/bin/x11/.gitignore
vendored
Normal file
2
c/SimpleDemo/demo/bin/x11/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
*
|
||||
!.gitignore
|
||||
|
Before Width: | Height: | Size: 6.2 KiB After Width: | Height: | Size: 6.2 KiB |
24
c/SimpleDemo/demo/main.tscn
Normal file
24
c/SimpleDemo/demo/main.tscn
Normal file
@@ -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"]
|
||||
@@ -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]
|
||||
|
||||
1
c/SimpleDemo/godot_headers
Submodule
1
c/SimpleDemo/godot_headers
Submodule
Submodule c/SimpleDemo/godot_headers added at 53c1f1c645
@@ -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"]
|
||||
|
||||
|
||||
Reference in New Issue
Block a user