1 /*
2     Copyright © 2020, Luna Nielsen
3     Distributed under the 2-Clause BSD License, see LICENSE file.
4     
5     Authors: Luna Nielsen
6 */
7 module engine;
8 public import engine.core;
9 public import engine.input;
10 public import engine.render;
11 public import engine.math;
12 public import engine.audio;
13 public import engine.net;
14 public import engine.ui;
15 public import engine.game;
16 public import engine.i18n;
17 
18 import bindbc.glfw;
19 import bindbc.openal;
20 import bindbc.freetype;
21 
22 /**
23     Initialize the game engine
24 */
25 void initEngine() {
26     // Initialize logger if needed
27     if (AppLog is null) AppLog = new Logger();
28 
29     // Initialize GLFW
30     initGLFW();
31     glfwInit();
32     AppLog.info("Engine", "GLFW initialized...");
33 
34     // Initialize OpenAL
35     initOAL();
36     initAudioEngine();
37     AppLog.info("Engine", "Audio Engine initialized...");
38 
39     // Create window
40     GameWindow = new Window();
41     GameWindow.makeCurrent();
42     AppLog.info("Engine", "Window initialized...");
43 
44     // Initialize OpenGL and make context current
45     initOGL();
46     initRender();
47     AppLog.info("Engine", "Renderer initialized...");
48 
49     // Initialize Font system
50     initFT();
51     initFontSystem();
52     AppLog.info("Engine", "Font system initialized...");
53 
54     // Initialize input
55     initInput(GameWindow.winPtr);
56     AppLog.info("Engine", "Input system initialized...");
57 
58     // Initialize atlasser
59     GameAtlas = new AtlasCollection();
60     AppLog.info("Engine", "Texture atlassing initialized...");
61 
62     // Initialize subsystems
63     initTileMesh();
64     AppLog.info("Engine", "Intialized internal state for renderer...");
65 
66     initPlaylist();
67     AppLog.info("Engine", "Initialized smaller subsystems...");
68 }
69 
70 /**
71     Closes the engine and relases libraries, etc.
72 */
73 void closeEngine() {
74     import core.memory : GC;
75     destroy(GamePlaylist);
76     destroy(GameWindow);
77 	destroy(AppLog);
78 
79     // Collect the stuff before we terminate all this other stuff
80     // We let OpenGL, OpenAL and GLFW be terminated by the closing of the program
81     GC.collect();
82 }
83 
84 private void initOAL() {
85     auto support = loadOpenAL();
86     if (support == ALSupport.badLibrary) {
87         AppLog.fatal("Engine", "Could not load OpenAL, bad library!");
88     } else if (support == ALSupport.noLibrary) {
89         AppLog.fatal("Engine", "Could not load OpenAL, no library found!");
90     }
91 }
92 
93 private void initGLFW() {
94     auto support = loadGLFW();
95     if (support == GLFWSupport.badLibrary) {
96         AppLog.fatal("Engine", "Could not load GLFW, bad library!");
97     } else if (support == GLFWSupport.noLibrary) {
98         AppLog.fatal("Engine", "Could not load GLFW, no library found!");
99     }
100 }
101 
102 private void initOGL() {
103     auto support = loadOpenGL();
104     if (support == GLSupport.badLibrary) {
105         AppLog.fatal("Engine", "Could not load OpenGL, bad library!");
106     } else if (support == GLSupport.noLibrary) {
107         AppLog.fatal("Engine", "Could not load OpenGL, no library found!");
108     } else if (support == GLSupport.noContext) {
109         AppLog.fatal("Engine", "OpenGL context was not created before loading OpenGL.");
110     }
111 }
112 
113 private void initFT() {
114     auto support = loadFreeType();
115     if (support == FTSupport.badLibrary) {
116         AppLog.fatal("Engine", "Could not load FreeType, bad library!");
117     } else if (support == FTSupport.noLibrary) {
118         AppLog.fatal("Engine", "Could not load FreeType, no library found!");
119     }
120 }