armory/Sources/armory/system/Starter.hx

129 lines
3.9 KiB
Haxe
Raw Normal View History

2018-08-26 18:42:10 +02:00
package armory.system;
2018-11-06 10:01:21 +01:00
import kha.WindowOptions;
2018-08-26 18:42:10 +02:00
class Starter {
#if arm_loadscreen
2019-12-19 23:54:08 +01:00
public static var drawLoading: kha.graphics2.Graphics->Int->Int->Void = null;
public static var numAssets: Int;
2018-08-26 18:42:10 +02:00
#end
2019-12-19 23:54:08 +01:00
public static function main(scene: String, mode: Int, resize: Bool, min: Bool, max: Bool, w: Int, h: Int, msaa: Int, vsync: Bool, getRenderPath: Void->iron.RenderPath) {
2019-02-10 20:37:38 +01:00
2020-07-06 23:44:04 +02:00
var tasks = 0;
2020-06-17 19:13:43 +02:00
2018-08-26 18:42:10 +02:00
function start() {
if (tasks > 0) return;
2019-11-16 14:11:32 +01:00
2018-08-26 18:42:10 +02:00
if (armory.data.Config.raw == null) armory.data.Config.raw = {};
2018-08-29 23:01:23 +02:00
var c = armory.data.Config.raw;
if (c.window_mode == null) c.window_mode = mode;
if (c.window_resizable == null) c.window_resizable = resize;
if (c.window_minimizable == null) c.window_minimizable = min;
if (c.window_maximizable == null) c.window_maximizable = max;
if (c.window_w == null) c.window_w = w;
if (c.window_h == null) c.window_h = h;
2019-03-06 21:54:05 +01:00
if (c.window_scale == null) c.window_scale = 1.0;
2018-08-29 23:01:23 +02:00
if (c.window_msaa == null) c.window_msaa = msaa;
if (c.window_vsync == null) c.window_vsync = vsync;
2019-11-16 14:11:32 +01:00
2018-08-26 18:42:10 +02:00
armory.object.Uniforms.register();
2019-11-16 14:11:32 +01:00
2018-09-05 08:49:44 +02:00
var windowMode = c.window_mode == 0 ? kha.WindowMode.Windowed : kha.WindowMode.Fullscreen;
2018-11-06 10:01:21 +01:00
var windowFeatures = None;
if (c.window_resizable) windowFeatures |= FeatureResizable;
if (c.window_maximizable) windowFeatures |= FeatureMaximizable;
if (c.window_minimizable) windowFeatures |= FeatureMinimizable;
2019-11-16 14:11:32 +01:00
2019-11-20 16:50:54 +01:00
#if (kha_webgl && (!arm_legacy) && (!kha_node))
2019-02-10 20:37:38 +01:00
try {
#end
2019-12-19 23:54:08 +01:00
kha.System.start({title: Main.projectName, width: c.window_w, height: c.window_h, window: {mode: windowMode, windowFeatures: windowFeatures}, framebuffer: {samplesPerPixel: c.window_msaa, verticalSync: c.window_vsync}}, function(window: kha.Window) {
2019-11-16 14:11:32 +01:00
2018-08-26 18:42:10 +02:00
iron.App.init(function() {
#if arm_loadscreen
2019-12-19 23:54:08 +01:00
function load(g: kha.graphics2.Graphics) {
2018-08-26 18:42:10 +02:00
if (iron.Scene.active != null && iron.Scene.active.ready) iron.App.removeRender2D(load);
else drawLoading(g, iron.data.Data.assetsLoaded, numAssets);
}
iron.App.notifyOnRender2D(load);
#end
2019-12-19 23:54:08 +01:00
iron.Scene.setActive(scene, function(object: iron.object.Object) {
2018-08-26 18:42:10 +02:00
iron.RenderPath.setActive(getRenderPath());
2019-02-10 11:47:42 +01:00
#if arm_patch
iron.Scene.getRenderPath = getRenderPath;
#end
2019-03-11 10:32:31 +01:00
#if arm_draworder_shader
iron.RenderPath.active.drawOrder = iron.RenderPath.DrawOrder.Shader;
#end // else Distance
2018-08-26 18:42:10 +02:00
});
});
});
2019-02-10 20:37:38 +01:00
2019-11-20 16:50:54 +01:00
#if (kha_webgl && (!arm_legacy) && (!kha_node))
2019-02-10 20:37:38 +01:00
}
2019-12-19 23:54:08 +01:00
catch (e: Dynamic) {
2019-02-10 20:37:38 +01:00
if (!kha.SystemImpl.gl2) {
trace("This project was not compiled with legacy shaders flag - please use WebGL 2 capable browser.");
}
}
#end
2018-08-26 18:42:10 +02:00
}
#if (js && arm_bullet)
2019-12-19 23:54:08 +01:00
function loadLibAmmo(name: String) {
kha.Assets.loadBlobFromPath(name, function(b: kha.Blob) {
2020-06-17 19:13:43 +02:00
js.Syntax.code("(1,eval)({0})", b.toString());
2019-11-16 14:11:32 +01:00
#if kha_krom
2020-06-17 19:13:43 +02:00
js.Syntax.code("Ammo({print:function(s){haxe.Log.trace(s);},instantiateWasm:function(imports,successCallback) {
var wasmbin = Krom.loadBlob('ammo.wasm.wasm');
var module = new WebAssembly.Module(wasmbin);
var inst = new WebAssembly.Instance(module,imports);
2019-11-16 14:11:32 +01:00
successCallback(inst);
return inst.exports;
2020-06-17 19:13:43 +02:00
}}).then(function(){ tasks--; start();})");
2019-11-16 14:11:32 +01:00
#else
2020-06-17 19:13:43 +02:00
js.Syntax.code("Ammo({print:function(s){haxe.Log.trace(s);}}).then(function(){ tasks--; start();})");
2019-11-16 14:11:32 +01:00
#end
2018-08-26 18:42:10 +02:00
});
}
#end
#if (js && arm_navigation)
2019-12-19 23:54:08 +01:00
function loadLib(name: String) {
kha.Assets.loadBlobFromPath(name, function(b: kha.Blob) {
2020-06-05 10:53:57 +02:00
js.Syntax.code("(1, eval)({0})", b.toString());
2018-08-26 18:42:10 +02:00
tasks--;
start();
});
}
#end
tasks = 1;
2019-11-16 14:11:32 +01:00
#if (js && arm_bullet)
tasks++;
#if kha_krom
loadLibAmmo("ammo.wasm.js");
#else
loadLibAmmo("ammo.js");
#end
#end
#if (js && arm_navigation)
tasks++;
loadLib("recast.js");
#end
#if (arm_config)
tasks++;
armory.data.Config.load(function() { tasks--; start(); });
#end
2018-08-26 18:42:10 +02:00
tasks--; start();
}
}