1 module bindbc.bgfx.dynload;
2 
3 version(BindBgfx_Static)
4 {
5 	// these functions do nothing with statically-linked library
6 
7 	@disable bool loadBgfx();
8 	@disable bool loadBgfx(const(char)* libName);
9 	@disable void unloadBgfx();
10 	@disable bool isBgfxLoaded();
11 }
12 else:
13 
14 import bindbc.loader;
15 import bindbc.bgfx.funcs;
16 
17 private SharedLib lib;
18 
19 bool loadBgfx()
20 {
21 	version (Windows)
22 	{
23 		const(char)[][4] libNames =
24 		[
25 			"bgfx.dll",
26 			"bgfx-shared-libRelease.dll",
27 			"bgfx_debug.dll",
28 			"bgfx-shared-libDebug.dll"
29 		];
30 	}
31 	else version (OSX)
32 	{
33 		const(char)[][2] libNames =
34 		[
35 			"libbgfx-shared-libRelease.dylib",
36 			"libbgfx-shared-libDebug.dylib"
37 		];
38 	}
39 	else version (linux)
40 	{
41 		const(char)[][4] libNames =
42 		[
43 			"libbgfx-shared-libRelease.so",
44 			"libbgfx-shared-libDebug.so",
45 			"./libbgfx-shared-libRelease.so",
46 			"./libbgfx-shared-libDebug.so"
47 		];
48 	}
49 	else
50 		static assert(0, "bindbc-bgfx is not supported on this platform.");
51 
52 	foreach (libName; libNames)
53 	{
54 		bool success = loadBgfx(libName.ptr);
55 		if (success)
56 			return success;
57 	}
58 	return false;
59 }
60 
61 bool loadBgfx(const(char)* libName)
62 {
63 	lib = load(libName);
64 	if (lib == invalidHandle)
65 		return false;
66 
67 	size_t errors = errorCount();
68 
69 	import std.algorithm.searching : startsWith;
70 	static foreach (m; __traits(allMembers, bindbc.bgfx.funcs))
71 	{
72 		static if (m.startsWith("da_"))
73 			lib.bindSymbol(cast(void**)&__traits(getMember, bindbc.bgfx.funcs, m[3..$]), __traits(getMember, bindbc.bgfx.funcs, m[3..$]).stringof);
74 	}
75 
76 	return errors == errorCount();
77 }
78 
79 void unloadBgfx()
80 {
81 	if(lib != invalidHandle)
82 		lib.unload();
83 }
84 
85 bool isBgfxLoaded()
86 {
87 	return lib != invalidHandle;
88 }