1 /* 2 * Copyright 2016 Mango-Engine Team 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 module mango_engine.mango; 17 18 import mango_engine.logging; 19 20 import std.conv; 21 22 version(mango_GLBackend) { 23 import mango_engine.graphics.opengl.gl_backend; 24 25 private shared GLBackend glBackend; 26 } 27 28 version(mango_VKBackend) { 29 import mango_engine.graphics.vulkan.vk_backend; 30 31 private shared VKBackend vkBackend; 32 } 33 34 immutable string MANGO_VERSION = "v1.0.0-SNAPSHOT"; 35 36 /// Enum to represent different Graphics APIs used by the backend. 37 enum GraphicsBackendType { 38 /// The OpenGL API. 39 API_OPENGL, 40 /// The Vulkan API. 41 API_VULKAN 42 } 43 44 /++ 45 Determine if the engine has compiled with 46 OpenGL support enabled. 47 48 Returns: If the engine has support for 49 OpenGL. 50 +/ 51 bool mango_hasGLSupport() @safe nothrow { 52 version(mango_GLBackend) { 53 return true; 54 } else return false; 55 } 56 57 /++ 58 Determine if the engine has compiled with 59 Vulkan support enabled. 60 61 Returns: If the engine has support for 62 Vulkan. 63 +/ 64 bool mango_hasVKSupport() @safe nothrow { 65 version(mango_VKBackend) { 66 return true; 67 } else return false; 68 } 69 70 private shared bool hasInit = false; 71 72 /++ 73 Determine if the engine has been initalized. 74 75 Returns: If the engine has been initalized. 76 +/ 77 bool mango_hasInitialized() @safe nothrow { 78 return hasInit; 79 } 80 81 /++ 82 Initalize the engine. This will 83 load system libraries and set up 84 the engine for usage. 85 +/ 86 void mango_init(GraphicsBackendType backendType) @system { 87 Logger logger = new ConsoleLogger("BackendInit"); 88 89 logger.logInfo("Mango Engine version " ~ MANGO_VERSION ~ " built on \"" ~ __DATE__ ~ " " ~ __TIME__ ~ "\""); 90 logger.logDebug("Compiled with OpenGL support: " ~ to!string(mango_hasGLSupport())); 91 logger.logDebug("Compiled with Vulkan support: " ~ to!string(mango_hasVKSupport())); 92 93 debug(mango_debug_sysInfo) { 94 import std.stdio : writeln; 95 logger.logDebug("Initializing engine..."); 96 } 97 98 final switch(backendType) { 99 case GraphicsBackendType.API_OPENGL: 100 version(mango_GLBackend) { 101 logger.logInfo("Initializing OpenGL backend."); 102 GLBackend _glBackend = new GLBackend(logger); 103 _glBackend.loadLibraries(); // TODO: ARGS 104 logger.logDebug("Loaded libraries."); 105 106 _glBackend.doInit(); 107 glBackend = cast(shared) _glBackend; 108 } else throw new Exception("Mango-Engine was not compiled with OpenGL support!"); 109 break; 110 case GraphicsBackendType.API_VULKAN: 111 version(mango_VKBackend) { 112 logger.logInfo("Initializing Vulkan backend."); 113 vkBackend = new VKBackend(); 114 vkBackend.loadLibraries(); 115 logger.logDebug("Loaded libraries."); 116 117 vkBackend.doInit(); 118 } else throw new Exception("Mango-Engine was not compiled with Vulkan support!"); 119 } 120 } 121 122 /++ 123 Destroy the engine. This will 124 free resources used by the library. 125 +/ 126 void mango_destroy() @system { 127 version(mango_GLBackend) { 128 if(glBackend !is null) 129 (cast(GLBackend) glBackend).doDestroy(); 130 } 131 132 version(mango_VKBackend) { 133 if(vkBackend !is null) 134 vkBackend.doDestroy(); 135 } 136 }