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