1 /**
2  * Retrograde Engine
3  *
4  * Authors:
5  *  Mike Bierlee, m.bierlee@lostmoment.com
6  * Copyright: 2014-2021 Mike Bierlee
7  * License:
8  *  This software is licensed under the terms of the MIT license.
9  *  The full terms of the license can be found in the LICENSE.txt file.
10  */
11 
12 module retrograde.derelict;
13 
14 version(Have_derelict_assimp3) {
15     import derelict.assimp3.assimp;
16 }
17 
18 version(Have_derelict_sdl2) {
19     import derelict.sdl2.sdl;
20     import derelict.sdl2.image;
21     import derelict.sdl2.ttf;
22 }
23 
24 version(Have_derelict_gl3) {
25     import derelict.opengl3.gl3;
26 }
27 
28 struct DllLoadContext {
29     version(Have_derelict_sdl2) {
30         bool loadSdl2Image = true;
31         bool loadSdl2Ttf = true;
32     }
33 
34     version(Have_derelict_gl3) {
35         bool loadOpenGl3 = true;
36     }
37 
38     version(Have_derelict_assimp3) {
39         bool loadAssimp3 = true;
40     }
41 }
42 
43 version(Have_derelict_sdl2) {
44     struct InitializationContext {
45         bool initializeVideo = true;
46         bool initializeJoystick = false;
47     }
48 }
49 
50 class DerelictLibrary {
51     private static bool loadedTtfLib = false;
52     private static bool loadedDlls = false;
53     private static bool _initializedSdl = false;
54 
55     public static bool dllsAreLoaded() {
56         return loadedDlls;
57     }
58 
59     public static bool sdlIsInitialized() {
60         return _initializedSdl;
61     }
62 
63     public static bool loadedAndInitialized() {
64         return dllsAreLoaded() && sdlIsInitialized();
65     }
66 
67     public static void loadDerelictDlls(DllLoadContext loadContext = DllLoadContext()) {
68         version(Have_derelict_sdl2) {
69             DerelictSDL2.load();
70             loadContext.loadSdl2Image && DerelictSDL2Image.load();
71             loadContext.loadSdl2Ttf && DerelictSDL2ttf.load();
72             loadedTtfLib = loadContext.loadSdl2Ttf;
73         }
74 
75         version(Have_derelict_gl3) {
76             loadContext.loadOpenGl3 && DerelictGL3.load();
77         }
78 
79         version(Have_derelict_assimp3) {
80             loadContext.loadAssimp3 && DerelictASSIMP3.load();
81         }
82 
83         loadedDlls = true;
84     }
85 
86     version(Have_derelict_sdl2) {
87         public static bool initializeSdl(InitializationContext context = InitializationContext()) {
88             Uint32 initFlags = 0;
89 
90             if (context.initializeVideo) initFlags |= SDL_INIT_VIDEO;
91             if (context.initializeJoystick) initFlags |= SDL_INIT_JOYSTICK;
92 
93             if (SDL_Init(initFlags) < 0) {
94                 return false;
95             }
96 
97             if (loadedTtfLib && (TTF_Init() < 0)) {
98                 return false;
99             }
100 
101             _initializedSdl = true;
102             return true;
103         }
104 
105         public static void quitSdl() {
106             if (loadedTtfLib) {
107                 TTF_Quit();
108             }
109 
110             SDL_Quit();
111         }
112     }
113 }