commit 1d5ac58783ee1c7c9931973e169962010fcf0d78 Author: zhangjiahao Date: Wed Aug 5 14:15:45 2026 +0800 Godot 项目初始化:C++ GDExtension 骨架 diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..f28239b --- /dev/null +++ b/.editorconfig @@ -0,0 +1,4 @@ +root = true + +[*] +charset = utf-8 diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..8ad74f7 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +# Normalize EOL for all files that Git considers text files. +* text=auto eol=lf diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0b7cc5a --- /dev/null +++ b/.gitignore @@ -0,0 +1,13 @@ +# Godot 4+ specific ignores +.godot/ +/android/ + +# C++ 扩展编译产物 +/build/ +/bin/ +*.user +*.suo + +# 其他 +.DS_Store +Thumbs.db diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..34bccae --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,41 @@ +cmake_minimum_required(VERSION 3.24) +project(marry) + +# 标准 C++17 +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_CXX_EXTENSIONS OFF) + +# 引用 godot-cpp(CMake 方式) +set(GODOT_CPP_PATH "${CMAKE_CURRENT_SOURCE_DIR}/godot-cpp") +add_subdirectory("${GODOT_CPP_PATH}") + +# 扩展库本体 +add_library(marry SHARED + src/register_types.cpp + src/example.cpp + src/example.h + src/register_types.h +) + +target_include_directories(marry PRIVATE src) + +# 链接 godot-cpp +target_link_libraries(marry PRIVATE godot-cpp) + +# 输出到 bin/ 目录(Godot 从那里加载;VS 里编译后 dll 直接落到 res://bin/) +set_target_properties(marry PROPERTIES + LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/bin" + RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/bin" + ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/bin" + PREFIX "" +) + +# VS 多配置生成器会输出到 bin/Release/ 等子目录,这里复制到 bin/ 根目录 +# 保证 .gdextension 里 res://bin/marry.dll 始终能找到 +add_custom_command(TARGET marry POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy_if_different + "$" + "${CMAKE_CURRENT_SOURCE_DIR}/bin/marry.dll" + COMMENT "复制 marry.dll 到 bin/ 根目录" +) diff --git a/build-marry.bat b/build-marry.bat new file mode 100644 index 0000000..4d4a2b5 --- /dev/null +++ b/build-marry.bat @@ -0,0 +1,9 @@ +@echo off +call "D:\Visual Studio\IDE\VC\Auxiliary\Build\vcvars64.bat" >nul 2>&1 +cd /d E:\newgame\marry +echo === CMake Configure (VS 2026) === +"D:\Visual Studio\IDE\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin\cmake.exe" -S . -B build -G "Visual Studio 18 2026" -A x64 +echo === Build === +"D:\Visual Studio\IDE\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin\cmake.exe" --build build --config Release +echo === Result === +dir E:\newgame\marry\bin diff --git a/godot-cpp b/godot-cpp new file mode 160000 index 0000000..60b5a41 --- /dev/null +++ b/godot-cpp @@ -0,0 +1 @@ +Subproject commit 60b5a4196de8442b43b32ba68ebe1e79cfcb762f diff --git a/icon.svg b/icon.svg new file mode 100644 index 0000000..c6bbb7d --- /dev/null +++ b/icon.svg @@ -0,0 +1 @@ + diff --git a/icon.svg.import b/icon.svg.import new file mode 100644 index 0000000..172030e --- /dev/null +++ b/icon.svg.import @@ -0,0 +1,43 @@ +[remap] + +importer="texture" +type="CompressedTexture2D" +uid="uid://oya8wpn6jmc8" +path="res://.godot/imported/icon.svg-218a8f2b3041327d8a5756f3a245f83b.ctex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://icon.svg" +dest_files=["res://.godot/imported/icon.svg-218a8f2b3041327d8a5756f3a245f83b.ctex"] + +[params] + +compress/mode=0 +compress/high_quality=false +compress/lossy_quality=0.7 +compress/uastc_level=0 +compress/rdo_quality_loss=0.0 +compress/hdr_compression=1 +compress/normal_map=0 +compress/channel_pack=0 +mipmaps/generate=false +mipmaps/limit=-1 +roughness/mode=0 +roughness/src_normal="" +process/channel_remap/red=0 +process/channel_remap/green=1 +process/channel_remap/blue=2 +process/channel_remap/alpha=3 +process/fix_alpha_border=true +process/premult_alpha=false +process/normal_map_invert_y=false +process/hdr_as_srgb=false +process/hdr_clamp_exposure=false +process/size_limit=0 +detect_3d/compress_to=1 +svg/scale=1.0 +editor/scale_with_editor_scale=false +editor/convert_colors_with_editor_theme=false diff --git a/main/.editorconfig b/main/.editorconfig new file mode 100644 index 0000000..f28239b --- /dev/null +++ b/main/.editorconfig @@ -0,0 +1,4 @@ +root = true + +[*] +charset = utf-8 diff --git a/main/.gitattributes b/main/.gitattributes new file mode 100644 index 0000000..8ad74f7 --- /dev/null +++ b/main/.gitattributes @@ -0,0 +1,2 @@ +# Normalize EOL for all files that Git considers text files. +* text=auto eol=lf diff --git a/main/.gitignore b/main/.gitignore new file mode 100644 index 0000000..0af181c --- /dev/null +++ b/main/.gitignore @@ -0,0 +1,3 @@ +# Godot 4+ specific ignores +.godot/ +/android/ diff --git a/main/icon.svg b/main/icon.svg new file mode 100644 index 0000000..c6bbb7d --- /dev/null +++ b/main/icon.svg @@ -0,0 +1 @@ + diff --git a/main/icon.svg.import b/main/icon.svg.import new file mode 100644 index 0000000..1a012f1 --- /dev/null +++ b/main/icon.svg.import @@ -0,0 +1,43 @@ +[remap] + +importer="texture" +type="CompressedTexture2D" +uid="uid://bgf1ec0y2dis7" +path="res://.godot/imported/icon.svg-218a8f2b3041327d8a5756f3a245f83b.ctex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://icon.svg" +dest_files=["res://.godot/imported/icon.svg-218a8f2b3041327d8a5756f3a245f83b.ctex"] + +[params] + +compress/mode=0 +compress/high_quality=false +compress/lossy_quality=0.7 +compress/uastc_level=0 +compress/rdo_quality_loss=0.0 +compress/hdr_compression=1 +compress/normal_map=0 +compress/channel_pack=0 +mipmaps/generate=false +mipmaps/limit=-1 +roughness/mode=0 +roughness/src_normal="" +process/channel_remap/red=0 +process/channel_remap/green=1 +process/channel_remap/blue=2 +process/channel_remap/alpha=3 +process/fix_alpha_border=true +process/premult_alpha=false +process/normal_map_invert_y=false +process/hdr_as_srgb=false +process/hdr_clamp_exposure=false +process/size_limit=0 +detect_3d/compress_to=1 +svg/scale=1.0 +editor/scale_with_editor_scale=false +editor/convert_colors_with_editor_theme=false diff --git a/main/project.godot b/main/project.godot new file mode 100644 index 0000000..a34766d --- /dev/null +++ b/main/project.godot @@ -0,0 +1,23 @@ +; Engine configuration file. +; It's best edited using the editor UI and not directly, +; since the parameters that go here are not all obvious. +; +; Format: +; [section] ; section goes between [] +; param=value ; assign values to parameters + +config_version=5 + +[application] + +config/name="main" +config/features=PackedStringArray("4.6", "Forward Plus") +config/icon="res://icon.svg" + +[physics] + +3d/physics_engine="Jolt Physics" + +[rendering] + +rendering_device/driver.windows="d3d12" diff --git a/marry.gdextension b/marry.gdextension new file mode 100644 index 0000000..bd40da4 --- /dev/null +++ b/marry.gdextension @@ -0,0 +1,8 @@ +[configuration] +entry_symbol = "marry_library_init" +compatibility_minimum = "4.5" + +[libraries] +windows.x86_64 = "res://bin/marry.dll" +linux.x86_64 = "res://bin/libmarry.so" +macos.universal = "res://bin/libmarry.dylib" diff --git a/project.godot b/project.godot new file mode 100644 index 0000000..46cb952 --- /dev/null +++ b/project.godot @@ -0,0 +1,23 @@ +; Engine configuration file. +; It's best edited using the editor UI and not directly, +; since the parameters that go here are not all obvious. +; +; Format: +; [section] ; section goes between [] +; param=value ; assign values to parameters + +config_version=5 + +[application] + +config/name="新建游戏项目" +config/features=PackedStringArray("4.6", "Forward Plus") +config/icon="res://icon.svg" + +[physics] + +3d/physics_engine="Jolt Physics" + +[rendering] + +rendering_device/driver.windows="d3d12" diff --git a/src/example.cpp b/src/example.cpp new file mode 100644 index 0000000..ef7353d --- /dev/null +++ b/src/example.cpp @@ -0,0 +1,22 @@ +#include "example.h" + +#include +#include +#include + +using namespace godot; + +void MyMath::_bind_methods() { + // 把 C++ 方法注册给 GDScript 调用 + ClassDB::bind_method(D_METHOD("add", "a", "b"), &MyMath::add); +} + +MyMath::MyMath() { +} + +MyMath::~MyMath() { +} + +int MyMath::add(int a, int b) { + return a + b; +} diff --git a/src/example.h b/src/example.h new file mode 100644 index 0000000..f1594aa --- /dev/null +++ b/src/example.h @@ -0,0 +1,26 @@ +#ifndef MARRY_EXAMPLE_H +#define MARRY_EXAMPLE_H + +#include + +namespace godot { + +// 示例 C++ 类:在 GDScript 里可以直接用 MyMath.new() 创建 +// 之后把你的 C++ 逻辑类都加在这里 +class MyMath : public RefCounted { + GDCLASS(MyMath, RefCounted) + +protected: + static void _bind_methods(); + +public: + MyMath(); + ~MyMath(); + + // 示例方法:加法(GDScript 调用: m.add(3, 4) -> 7) + int add(int a, int b); +}; + +} + +#endif // MARRY_EXAMPLE_H diff --git a/src/register_types.cpp b/src/register_types.cpp new file mode 100644 index 0000000..02a11a1 --- /dev/null +++ b/src/register_types.cpp @@ -0,0 +1,38 @@ +#include "register_types.h" + +#include "example.h" + +#include +#include +#include + +using namespace godot; + +void initialize_marry_module(ModuleInitializationLevel p_level) { + if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) { + return; + } + // 在这里注册所有 C++ 类 + GDREGISTER_CLASS(MyMath); +} + +void uninitialize_marry_module(ModuleInitializationLevel p_level) { + if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) { + return; + } + // godot-cpp 在库卸载时自动清理类,无需手动注销 +} + +// Godot 加载扩展的入口(dll 导出符号) +extern "C" { +// Initialization. +GDExtensionBool GDE_EXPORT marry_library_init(GDExtensionInterfaceGetProcAddress p_get_proc_address, GDExtensionClassLibraryPtr p_library, GDExtensionInitialization *r_initialization) { + godot::GDExtensionBinding::InitObject init_obj(p_get_proc_address, p_library, r_initialization); + + init_obj.register_initializer(initialize_marry_module); + init_obj.register_terminator(uninitialize_marry_module); + init_obj.set_minimum_library_initialization_level(MODULE_INITIALIZATION_LEVEL_SCENE); + + return init_obj.init(); +} +} diff --git a/src/register_types.h b/src/register_types.h new file mode 100644 index 0000000..22d12ce --- /dev/null +++ b/src/register_types.h @@ -0,0 +1,11 @@ +#ifndef MARRY_REGISTER_TYPES_H +#define MARRY_REGISTER_TYPES_H + +#include + +using namespace godot; + +void initialize_marry_module(ModuleInitializationLevel p_level); +void uninitialize_marry_module(ModuleInitializationLevel p_level); + +#endif // MARRY_REGISTER_TYPES_H