1 /**
2  * Copyright:       Copyright (C) 2018 Gabriel Gheorghe, All Rights Reserved
3  * Authors:         $(Gabriel Gheorghe)
4  * License:         $(LINK2 https://www.gnu.org/licenses/gpl-3.0.txt, GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007)
5  * Source:          $(LINK2 https://github.com/GabyForceQ/LibertyEngine/blob/master/source/liberty/core/platform.d)
6  * Documentation:
7  * Coverage:
8 **/
9 module liberty.core.platform;
10 
11 import bindbc.glfw;
12 
13 import liberty.core.engine;
14 import liberty.input.event;
15 import liberty.logger;
16 import liberty.core.window;
17 
18 /**
19  * A platform represents the root system for window manager.
20  * Currently it supports only one window.
21  * GraphicsEngine service should be initialized before attemping to initialize the platform.
22 **/
23 final abstract class Platform {
24   private {
25     static Window window;
26   }
27 
28   /**
29    * Initialize current platform.
30    * It loads the GLFW library.
31    * Application fails if library coudn't be loaded.
32   **/
33   static void initialize() {
34     Logger.info(InfoMessage.Creating, typeof(this).stringof);
35 
36     // Load GLFW Library
37     const res = loadGLFW();
38     if (res != glfwSupport) {
39       if (res == GLFWSupport.noLibrary)
40         Logger.error("No GLFW library", typeof(this).stringof);
41       else if (res == glfwSupport.badLibrary)
42         Logger.error("Bad GLFW library", typeof(this).stringof);
43     }
44 
45     if (!glfwInit()) {
46       Logger.error("GLFW failed to init", typeof(this).stringof);
47       return;
48     }
49 
50     glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
51     glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 5);
52 
53     // Create main window
54     window = new Window(
55       1280,
56       720,
57       "Liberty Engine v0.0.16-alpha.dev"
58     );
59 
60     EventManager.initialize();
61 
62     glfwSetCursorPosCallback(window.getHandle(), &EventManager.mouseCallback);
63     glfwSetScrollCallback(window.getHandle(), &EventManager.scrollCallback);
64     glfwSetJoystickCallback(&EventManager.joystickCallback);
65 
66     Logger.info(InfoMessage.Created, typeof(this).stringof);
67   }
68 
69   /**
70    * Deinitialize current platform.
71   **/
72   static void deinitialize() {
73     Logger.info(InfoMessage.Destroying, typeof(this).stringof);
74     
75     // Check if window exists before destroying it
76     if (window !is null) {
77       window.destroy();
78       window = null;
79     }
80 
81     Logger.info(InfoMessage.Destroyed, typeof(this).stringof);
82   }
83 
84   /**
85    * Get current platform window.
86   **/
87   static Window getWindow()  {
88     return window;
89   }
90 }