Compare commits

...

2 Commits

Author SHA1 Message Date
MartinOpat f9ec2e855d Switched 'intensive' functions to cpp 2025-11-11 00:23:12 +01:00
MartinOpat c92c6d4432 godot-cpp init. 2025-11-11 00:22:57 +01:00
7 changed files with 136 additions and 3 deletions

4
.gitmodules vendored Normal file
View File

@ -0,0 +1,4 @@
[submodule "game-of-life-test/thirdparty/godot-cpp"]
path = game-of-life-test/thirdparty/godot-cpp
url = https://github.com/godotengine/godot-cpp.git
branch = 4.5

View File

@ -1,15 +1,17 @@
extends Sprite2D
var n: int = 64
var n: int = 256
var arr := []
var data_img: Image
var data_tex: ImageTexture
var gol := GoL.new()
@onready var mat: ShaderMaterial = material as ShaderMaterial
# sim. consts
var T := 0.1
var T := 0.01
var t := 0.0
# Called when the node enters the scene tree for the first time.
@ -38,7 +40,8 @@ func _process(_delta: float) -> void:
t += _delta
if t >= T:
t = 0.0
_game_of_life_step()
#_game_of_life_step()
arr = gol.step_once(arr, n) # cpp step
_upload_arr()
func _game_of_life_step() -> void:

10
game-of-life-test/thirdparty/.gitignore vendored Normal file
View File

@ -0,0 +1,10 @@
bin
*.uid
*sconsign.dblite
**.os
game-of-life-test/thirdparty/godot-cpp/bin/
game-of-life-test/thirdparty/bin/
*.o
*.so

31
game-of-life-test/thirdparty/SConstruct vendored Normal file
View File

@ -0,0 +1,31 @@
import os
from SCons.Script import DefaultEnvironment, ARGUMENTS, Glob, Dir
env = DefaultEnvironment()
platform = ARGUMENTS.get("platform", "linux")
target = ARGUMENTS.get("target", "template_debug") # or template_release
arch = ARGUMENTS.get("arch", "x86_64")
HERE = Dir(".").abspath
GODOTCPP = os.path.join(HERE, "godot-cpp")
inc_paths = [
os.path.join(GODOTCPP, "include"),
os.path.join(GODOTCPP, "gen", "include"),
]
for extra in ("godot-headers", "gdextension"):
p = os.path.join(GODOTCPP, extra)
if os.path.isdir(p):
inc_paths.append(p)
env.Append(CPPPATH=inc_paths)
env.Append(LIBPATH=[os.path.join(GODOTCPP, "bin")])
env.Append(CXXFLAGS=["-std=c++17", "-fPIC"])
env.Append(LIBS=[f"godot-cpp.{platform}.{target}.{arch}"])
outdir = os.path.join(HERE, "bin")
os.makedirs(outdir, exist_ok=True)
soname = f"gol.{platform}.{'debug' if target == 'template_debug' else 'release'}.{arch}"
env.SharedLibrary(target=os.path.join(outdir, soname), source=Glob("src/*.cpp"))

@ -0,0 +1 @@
Subproject commit abe94570a16e38eec442d1c9acc0c518c479e725

View File

@ -0,0 +1,9 @@
[configuration]
entry_symbol = "gol_library_init"
compatibility_minimum = "4.5" ; required in 4.5+
reloadable = true
[libraries]
linux.debug.x86_64 = "res://thirdparty/bin/libgol.linux.debug.x86_64"

View File

@ -0,0 +1,75 @@
// src/gol.cpp
#include <gdextension_interface.h>
#include <godot_cpp/classes/ref_counted.hpp>
#include <godot_cpp/core/class_db.hpp>
#include <godot_cpp/core/defs.hpp>
#include <godot_cpp/godot.hpp>
#include <godot_cpp/variant/array.hpp>
using namespace godot;
class GoL : public RefCounted {
GDCLASS(GoL, RefCounted);
static void _bind_methods() {
ClassDB::bind_method(D_METHOD("step_once", "arr", "n"), &GoL::step_once);
}
static int neighs(const Array &a, int n, int x, int y) {
int s = 0;
for (int dy = -1; dy <= 1; ++dy) {
for (int dx = -1; dx <= 1; ++dx) {
if (dx == 0 && dy == 0)
continue;
const int nx = ((x + dx) % n + n) % n;
const int ny = ((y + dy) % n + n) % n;
const Array row = a[ny];
s += (int)row[nx];
}
}
return s;
}
public:
Array step_once(const Array &arr, int n) const {
Array next;
next.resize(n);
for (int y = 0; y < n; ++y) {
Array row;
row.resize(n);
const Array crow = arr[y];
for (int x = 0; x < n; ++x) {
const bool alive = ((int)crow[x]) == 1;
const int cn = neighs(arr, n, x, y);
row.set(x, alive ? int(cn == 2 || cn == 3) : int(cn == 3));
}
next.set(y, row);
}
return next;
}
};
extern "C" GDExtensionBool GDE_EXPORT
gol_library_init(GDExtensionInterfaceGetProcAddress get_proc_address,
GDExtensionClassLibraryPtr library,
GDExtensionInitialization *r_initialization) {
static GDExtensionBinding::InitObject init(get_proc_address, library,
r_initialization);
init.register_initializer([](ModuleInitializationLevel p_level) {
if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE)
return;
ClassDB::register_class<GoL>();
});
init.register_terminator([](ModuleInitializationLevel /*p_level*/) {
// no-op
});
init.set_minimum_library_initialization_level(
MODULE_INITIALIZATION_LEVEL_SCENE);
return init.init();
}