Skip to content
This repository was archived by the owner on Dec 12, 2022. It is now read-only.

Commit 30ffc92

Browse files
committed
add spinning-cubes example, try method binding _physics_update and calling rotateY currently crashes
1 parent 5a02f7a commit 30ffc92

13 files changed

Lines changed: 217 additions & 7 deletions

File tree

examples/hello-world/src/main.zig

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,11 @@ pub const HelloWorld = struct {
1717
_ = self;
1818
std.log.info("Hello World is ready!\n", .{});
1919
}
20-
21-
pub fn update(self: *Self, delta: f32) void {
22-
_ = self;
23-
std.log.info("Update {}\n", .{delta});
24-
}
2520
};
2621

2722
fn init() !void {
2823
try godot.registerClass(HelloWorld);
2924
try godot.registerMethod(HelloWorld, HelloWorld._ready, "_ready");
30-
try godot.registerMethod(HelloWorld, HelloWorld.update, "_physics_process");
3125
}
3226

3327
export fn godot_nativescript_init(handle: *anyopaque) void {

examples/spinning-cube/Main.tscn

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
[gd_scene load_steps=5 format=2]
2+
3+
[ext_resource path="res://spinning_cube_library.gdnlib" type="GDNativeLibrary" id=1]
4+
5+
[sub_resource type="CubeMesh" id=1]
6+
7+
[sub_resource type="SpatialMaterial" id=2]
8+
roughness = 0.0
9+
10+
[sub_resource type="NativeScript" id=3]
11+
resource_name = "SpinningCube"
12+
class_name = "SpinningCube"
13+
library = ExtResource( 1 )
14+
15+
[node name="Main" type="Spatial"]
16+
17+
[node name="Cube" type="MeshInstance" parent="."]
18+
mesh = SubResource( 1 )
19+
material/0 = SubResource( 2 )
20+
script = SubResource( 3 )
21+
22+
[node name="Camera" type="Camera" parent="."]
23+
transform = Transform( 0.572229, -0.327396, 0.751909, 0, 0.916856, 0.399217, -0.820094, -0.228443, 0.524651, 4.71648, 2.5, 3.45846 )
24+
current = true

examples/spinning-cube/build.zig

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const std = @import("std");
2+
const deps = @import("deps.zig");
3+
4+
pub fn build(b: *std.build.Builder) void {
5+
// Standard release options allow the person running `zig build` to select
6+
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
7+
const mode = b.standardReleaseOptions();
8+
9+
const lib = b.addSharedLibrary("spinning-cube", "src/main.zig", b.version(1, 0, 0));
10+
lib.setBuildMode(mode);
11+
lib.linkLibC();
12+
lib.install();
13+
deps.addAllTo(lib);
14+
15+
const main_tests = b.addTest("src/main.zig");
16+
main_tests.setBuildMode(mode);
17+
18+
const test_step = b.step("test", "Run library tests");
19+
test_step.dependOn(&main_tests.step);
20+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[gd_resource type="Environment" load_steps=2 format=2]
2+
3+
[sub_resource type="ProceduralSky" id=1]
4+
sky_top_color = Color( 0.0470588, 0.454902, 0.976471, 1 )
5+
sky_horizon_color = Color( 0.556863, 0.823529, 0.909804, 1 )
6+
sky_curve = 0.25
7+
ground_bottom_color = Color( 0.101961, 0.145098, 0.188235, 1 )
8+
ground_horizon_color = Color( 0.482353, 0.788235, 0.952941, 1 )
9+
ground_curve = 0.01
10+
sun_energy = 16.0
11+
12+
[resource]
13+
background_mode = 2
14+
background_sky = SubResource( 1 )

examples/spinning-cube/icon.png

3.42 KB
Loading
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
[remap]
2+
3+
importer="texture"
4+
type="StreamTexture"
5+
path="res://.import/icon.png-487276ed1e3a0c39cad0279d744ee560.stex"
6+
metadata={
7+
"vram_texture": false
8+
}
9+
10+
[deps]
11+
12+
source_file="res://icon.png"
13+
dest_files=[ "res://.import/icon.png-487276ed1e3a0c39cad0279d744ee560.stex" ]
14+
15+
[params]
16+
17+
compress/mode=0
18+
compress/lossy_quality=0.7
19+
compress/hdr_mode=0
20+
compress/bptc_ldr=0
21+
compress/normal_map=0
22+
flags/repeat=0
23+
flags/filter=true
24+
flags/mipmaps=false
25+
flags/anisotropic=false
26+
flags/srgb=2
27+
process/fix_alpha_border=true
28+
process/premult_alpha=false
29+
process/HDR_as_SRGB=false
30+
process/invert_color=false
31+
process/normal_map_invert_y=false
32+
stream=false
33+
size_limit=0
34+
detect_3d=true
35+
svg/scale=1.0
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
; Engine configuration file.
2+
; It's best edited using the editor UI and not directly,
3+
; since the parameters that go here are not all obvious.
4+
;
5+
; Format:
6+
; [section] ; section goes between []
7+
; param=value ; assign values to parameters
8+
9+
config_version=4
10+
11+
_global_script_classes=[ ]
12+
_global_script_class_icons={
13+
}
14+
15+
[application]
16+
17+
config/name="spinning-cube"
18+
run/main_scene="res://Main.tscn"
19+
config/icon="res://icon.png"
20+
21+
[rendering]
22+
23+
environment/default_environment="res://default_env.tres"
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[entry]
2+
3+
X11.64="res://zig-out/lib/spinning-cube.so"
4+
OSX.64="res://zig-out/lib/spinning-cube.dylib"
5+
Windows.64="res://zig-out/lib/spinning-cube.dll"
6+
7+
[dependencies]
8+
9+
X11.64=[ ]
10+
OSX.64=[ ]
11+
Windows.64=[ ]
12+
13+
[general]
14+
15+
singleton=false
16+
load_once=true
17+
symbol_prefix="godot_"
18+
reloadable=true
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
const std = @import("std");
2+
const godot = @import("godot-zig");
3+
4+
const SpinningCube = struct {
5+
const Self = @This();
6+
pub const BaseClass = godot.MeshInstance;
7+
8+
base: *BaseClass,
9+
// start: godot.Vector3,
10+
time: f32,
11+
rotate_speed: f32 = 1.0, // TODO: this should be a property...
12+
13+
pub fn ready(self: *Self) void {
14+
_ = self;
15+
}
16+
17+
pub fn update(self: *Self, delta: f32) void {
18+
_ = delta;
19+
// TODO: still crashes for reasons
20+
// following change was necessary to get this to compile
21+
// spatial.zig:750: @ptrCast(*const anyopaque, &arg_angle),
22+
godot.callUp(BaseClass, self.base, "rotateY", .{self.rotate_speed * 0.0001}) catch |err| {
23+
std.log.err("godot-zig: SpinningCube update: {s}\n", .{err});
24+
};
25+
}
26+
};
27+
28+
fn init() !void {
29+
try godot.registerClass(SpinningCube);
30+
try godot.registerMethod(SpinningCube, SpinningCube.ready, "_ready");
31+
try godot.registerMethod(SpinningCube, SpinningCube.update, "_physics_process");
32+
}
33+
34+
export fn godot_nativescript_init(handle: *anyopaque) void {
35+
std.log.info("godot-zig: godot_nativescript_init()\n", .{});
36+
godot.initNativeScript(handle);
37+
38+
init() catch |err| {
39+
std.log.err("godot-zig: godot_nativescript_init: spinning-cube: {s}\n", .{err});
40+
};
41+
}
42+
43+
export fn godot_gdnative_init(options: *godot.c_api.godot_gdnative_init_options) void {
44+
std.log.info("godot-zig: godot_gdnative_init()\n", .{});
45+
godot.init(std.heap.c_allocator, options);
46+
}
47+
48+
export fn godot_gdnative_terminate(options: *godot.c_api.godot_gdnative_terminate_options) void {
49+
_ = options;
50+
std.log.info("godot-zig: godot_gdnative_terminate()\n", .{});
51+
godot.terminate();
52+
}

examples/spinning-cube/zig.mod

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
id: 6yy4q9t1o3c819o5w6lxhb9lgzuowouvq3340dqu70wjkeuf
2+
name: spinning-cube
3+
main: src/main.zig
4+
license: None
5+
description: Spinning Cube example for godot-zig
6+
root_dependencies:
7+
- src: git https://github.com/atomicptr/godot-zig.git
8+
c_include_dirs:
9+
- godot-headers

0 commit comments

Comments
 (0)