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 
17 import bindbc.glfw;
18 import bindbc.openal;
19 import bindbc.freetype;
20 
21 /**
22     Initialize the game engine
23 */
24 void initEngine() {
25     // Initialize logger if needed
26     if (AppLog is null) AppLog = new Logger();
27 
28     // Initialize GLFW
29     initGLFW();
30     glfwInit();
31     AppLog.info("Engine", "GLFW initialized...");
32 
33     // Initialize OpenAL
34     initOAL();
35     initAudioEngine();
36     AppLog.info("Engine", "Audio Engine initialized...");
37 
38     // Create window
39     GameWindow = new Window();
40     GameWindow.makeCurrent();
41     AppLog.info("Engine", "Window initialized...");
42 
43     // Initialize OpenGL and make context current
44     initOGL();
45     initRender();
46     AppLog.info("Engine", "Renderer initialized...");
47 
48     // Initialize Font system
49     initFT();
50     initFontSystem();
51     AppLog.info("Engine", "Font system initialized...");
52 
53     // Initialize input
54     initInput(GameWindow.winPtr);
55     AppLog.info("Engine", "Input system initialized...");
56 
57     // Initialize atlasser
58     GameAtlas = new AtlasCollection();
59     AppLog.info("Engine", "Texture atlassing initialized...");
60 
61     // Initialize subsystems
62     initTileMesh();
63     AppLog.info("Engine", "Intialized internal state for renderer...");
64 
65     initPlaylist();
66     initUI("assets/fonts/KosugiMaru.ttf");
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 }