1 module polyplex;
2 
3 import bindbc.sdl;
4 import bindbc.opengl;
5 import openal;
6 
7 import polyplex.utils.logging;
8 import std.stdio;
9 import std.conv;
10 static import std.file;
11 static import std.process;
12 
13 public import polyplex.core;
14 public import polyplex.math;
15 public import polyplex.utils.logging;
16 
17 
18 public static GraphicsBackend ChosenBackend = GraphicsBackend.OpenGL;
19 private static bool core_init = false;
20 private static bool vk_init = false;
21 private static bool gl_init = false;
22 
23 
24 public enum GraphicsBackend {
25 	OpenGL
26 }
27 
28 private string get_arch() {
29 	version(X86) return "i386";
30 	version(X86_64) return "amd64";
31 	version(ARM) return "arm";
32 	version(AArch64) return "arm64";
33 }
34 
35 private string get_system_lib(string libname, bool s = true) {
36 	string lstr = "libs/"~get_arch()~"/lib"~libname~".so";
37 
38 	string plt = "linux/bsd";
39 	version(Windows) {
40 		lstr = "libs/"~get_arch()~"/"~libname~".dll";
41 		plt = "win32";
42 	}
43 	
44 	version(FreeBSD) {
45 		lstr = "libs/"~get_arch()~"/lib"~libname~"-freebsd.so";
46 		plt = "linux/bsd";
47 	}
48 	
49 	version(OpenBSD) {
50 		lstr = "libs/"~get_arch()~"/lib"~libname~"-openbsd.so";
51 		plt = "linux/bsd";
52 	}
53 
54 	version(OSX) {
55 		// lstr = "libs/"~get_arch()~"/lib"~libname~".dylib";
56 		// plt = "darwin/osx";
57 	}
58 	Logger.Info("Binding library {0}: [{1} on {2}] from {3}", libname, plt, get_arch(), lstr);
59 	return lstr;
60 }
61 
62 private string trimexe(string input) {
63 	string i = input;
64 	version(Windows) {
65 		while (i[i.length-1] != '\\') {
66 			i.length--;
67 		}
68 		return i;
69 	}
70 	else {
71 		while (i[i.length-1] != '/') {
72 			i.length--;
73 		}
74 		return i;
75 	}
76 }
77 
78 /**
79 	De-init libraries
80 */
81 public void UnInitLibraries() {
82 	unloadSDL();
83 	unloadOpenGL();
84 	unloadOAL();
85 	core_init = false;
86 }
87 
88 /*
89 	InitLibraries loads the Derelict libraries for Vulkan, SDL and OpenGL
90 */
91 public void InitLibraries() {
92 	/*if (!core_init) {
93 		if (std.file.exists("libs/")) {
94 			// Load bundled libraries.
95 			Logger.Info("Binding to runtime libraries...");
96 			string path_sep = ":";
97 			string sys_sep = "/";
98 			version(Windows) {
99 				path_sep = ";";
100 				sys_sep = "\\";
101 			}
102 			string path = std.process.environment["PATH"];
103 			string path_begin = std.file.thisExePath();
104 			path_begin = trimexe(path_begin);
105 			std.process.environment["PATH"] = path_begin ~ "libs" ~ sys_sep ~ get_arch() ~ path_sep ~ path;
106 			Logger.Debug("Updated PATH to {0}", std.process.environment["PATH"]);
107 			loadSDL(get_system_lib("SDL2").dup.ptr);
108 		} else {
109 			// Load system libraries
110 			Logger.Info("Binding to system libraries....");
111 			SDLSupport support = loadSDL();
112 			if (support == SDLSupport.noLibrary) Logger.Fatal("SDL2 not found!");
113 		}
114 		loadOAL();
115 		SDL_version linked;
116 		SDL_version compiled;
117 		SDL_GetVersion(&linked);
118 		SDL_VERSION(&compiled);
119 		Logger.Debug("SDL (compiled against): {0}.{1}.{2}", to!string(compiled.major), to!string(compiled.minor), to!string(compiled.patch));
120 		Logger.Debug("SDL (linked): {0}.{1}.{2}", to!string(linked.major), to!string(linked.minor), to!string(linked.patch));
121 		core_init = true;
122 	}*/
123 	loadOpenGL();
124 	//gl_init = true;
125 }
126 
127 version(OSX) {
128 	static assert(0, "macOS is NOT supported with Polyplex; and won't be for the forseeable future, sorry.");
129 }
130 
131 shared static this() {
132 	bool success = loadOAL();
133 	Logger.Info("Bound OpenAL {0}!", success ? "successfully" : "unsuccessfully");
134 	
135 
136 	import bindbc.sdl;
137 	SDLSupport support = loadSDL();
138 	if (support == SDLSupport.noLibrary) Logger.Fatal("SDL2 not found!");
139 	SDL_version linked;
140 	SDL_version compiled;
141 	SDL_GetVersion(&linked);
142 	SDL_VERSION(&compiled);
143 	Logger.Info("SDL (compiled against): {0}.{1}.{2}", to!string(compiled.major), to!string(compiled.minor), to!string(compiled.patch));
144 	Logger.Info("SDL (linked): {0}.{1}.{2}", to!string(linked.major), to!string(linked.minor), to!string(linked.patch));
145 }