Skip to content

Commit a09b55d

Browse files
committed
Make libgodot_project buildable on Windows.
1 parent 4943d52 commit a09b55d

File tree

6 files changed

+72
-5
lines changed

6 files changed

+72
-5
lines changed

README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,12 @@ git clone --recursive https://github.com/migeran/libgodot_project
3535
3636
cd libgodot_project
3737
38-
# Build for the host platform (Mac and Linux)
38+
# Build for Mac and Linux
3939
./build_libgodot.sh
4040
41+
# Or build for Windows
42+
build_libgodot.bat
43+
4144
# Build for iOS (Mac only)
4245
./build_libgodot.sh --target ios
4346
```
@@ -48,6 +51,7 @@ The C++ sample shows how Godot can be controlled by a host process, by displayin
4851

4952
After libgodot is compiled successfully, run the following commands to test the C++ sample:
5053

54+
On Linux and MacOS:
5155
```
5256
cd samples/cpp_sample
5357
mkdir build
@@ -56,6 +60,16 @@ cd build
5660
./sample
5761
```
5862

63+
On Windows:
64+
```
65+
cd samples/cpp_sample
66+
mkdir build
67+
cmake -S . -B build -G Ninja
68+
cd build
69+
ninja
70+
sample.exe
71+
```
72+
5973
### SwiftUI Sample
6074

6175
To test the SwiftUI sample, just open the ios_sample project in XCode 15.1+, build it and run on any iOS device. (iOS Simulator is not supported due to Godot limitations.)

build_libgodot.bat

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
set BASE_DIR=%~dp0
2+
cd %BASE_DIR%/godot
3+
4+
call scons platform=windows arch=x86_64 verbose=yes msvc=yes dev_build=yes debug_symbols=yes
5+
call scons platform=windows target=template_debug arch=x86_64 verbose=yes msvc=yes shared_library=yes dev_build=yes debug_symbols=yes
6+
7+
copy /y bin\libgodot.windows.editor.dev.x86_64.dll ..\build\libgodot.dll
8+
9+
if not exist ..\build\extension_api.json bin/godot.windows.editor.dev.x86_64.exe --dump-extension-api
10+
copy /y ..\build\extension_api.json ..\godot-cpp\gdextension
11+
12+
copy /y core\extension\gdextension_interface.h ..\godot-cpp\gdextension
13+
14+
cd ..

godot-cpp

samples/cpp_sample/CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ target_include_directories(${PROJECT_NAME} SYSTEM
2222
${GODOT_CPP_DIR}/gen/include
2323
${GODOT_CPP_DIR}/gdextension
2424
)
25-
target_link_libraries(${PROJECT_NAME} dl godot-cpp Threads::Threads)
25+
if (WIN32)
26+
target_link_libraries(${PROJECT_NAME} godot-cpp Threads::Threads)
27+
else()
28+
target_link_libraries(${PROJECT_NAME} dl godot-cpp Threads::Threads)
29+
endif (WIN32)
2630
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
2731
COMMAND ${CMAKE_COMMAND} -E copy
2832
${LIBGODOT_BUILD_DIR}/libgodot${CMAKE_SHARED_LIBRARY_SUFFIX}

samples/cpp_sample/src/main.cpp

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
#include <stdlib.h>
22
#include <stdio.h>
3+
#if defined(__APPLE__) || defined(__unix__)
34
#include <dlfcn.h>
5+
#elif defined(_WIN32)
6+
#include <windows.h>
7+
#endif
48
#include <string>
59
#include <vector>
610

@@ -13,8 +17,10 @@
1317

1418
#ifdef __APPLE__
1519
#define LIBGODOT_LIBRARY_NAME "libgodot.dylib"
16-
#else
20+
#elif defined(__unix__)
1721
#define LIBGODOT_LIBRARY_NAME "./libgodot.so"
22+
#elif defined(_WIN32)
23+
#define LIBGODOT_LIBRARY_NAME "libgodot.dll"
1824
#endif
1925

2026
extern "C" {
@@ -47,6 +53,7 @@ GDExtensionBool GDE_EXPORT gdextension_default_init(GDExtensionInterfaceGetProcA
4753
class LibGodot {
4854
public:
4955
LibGodot(std::string p_path = LIBGODOT_LIBRARY_NAME) {
56+
#if defined(__APPLE__) || defined(__unix__)
5057
handle = dlopen(p_path.c_str(), RTLD_LAZY);
5158
if (handle == nullptr) {
5259
fprintf(stderr, "Error opening libgodot: %s\n", dlerror());
@@ -66,16 +73,38 @@ class LibGodot {
6673
handle == nullptr;
6774
return;
6875
}
76+
#elif defined(_WIN32)
77+
LPCSTR libgodot_library_name = reinterpret_cast<LPCSTR>(LIBGODOT_LIBRARY_NAME);
78+
handle = LoadLibrary(libgodot_library_name);
79+
if (handle == NULL) {
80+
fprintf(stderr, "Error opening libgodot: %lu\n", GetLastError());
81+
return;
82+
}
83+
func_libgodot_create_godot_instance = (void *(*)(int, char **, GDExtensionInitializationFunction))GetProcAddress(handle, "gdextension_create_godot_instance");
84+
if (func_libgodot_create_godot_instance == NULL) {
85+
fprintf(stderr, "Error acquiring function: %lu\n", GetLastError());
86+
FreeLibrary(handle);
87+
return;
88+
}
89+
#endif
6990
}
7091

7192
~LibGodot() {
7293
if (is_open()) {
94+
#if defined(__APPLE__) || defined(__unix__)
7395
dlclose(handle);
96+
#elif defined(_WIN32)
97+
FreeLibrary(handle);
98+
#endif
7499
}
75100
}
76101

77102
bool is_open() {
103+
#if defined(__APPLE__) || defined(__unix__)
78104
return handle != nullptr && func_libgodot_create_godot_instance != nullptr;
105+
#elif defined(_WIN32)
106+
return handle != NULL && func_libgodot_create_godot_instance != NULL;
107+
#endif
79108
}
80109

81110
godot::GodotInstance *create_godot_instance(int p_argc, char *p_argv[], GDExtensionInitializationFunction p_init_func = gdextension_default_init) {
@@ -95,9 +124,15 @@ class LibGodot {
95124
}
96125

97126
private:
127+
#if defined(__APPLE__) || defined(__unix__)
98128
void *handle = nullptr;
99129
GDExtensionObjectPtr (*func_libgodot_create_godot_instance)(int, char *[], GDExtensionInitializationFunction) = nullptr;
100130
void (*func_libgodot_destroy_godot_instance)(GDExtensionObjectPtr) = nullptr;
131+
#elif defined(_WIN32)
132+
HINSTANCE handle = NULL;
133+
GDExtensionObjectPtr (*func_libgodot_create_godot_instance)(int, char *[], GDExtensionInitializationFunction) = NULL;
134+
void (*func_libgodot_destroy_godot_instance)(GDExtensionObjectPtr) = NULL;
135+
#endif
101136
};
102137

103138
int main(int argc, char **argv) {

0 commit comments

Comments
 (0)