Files
lime/templates/haxe/ApplicationMain.hx
2016-11-01 07:40:10 -07:00

232 lines
4.8 KiB
Haxe

package;
@:access(lime.app.Application)
class ApplicationMain {
public static var config:lime.app.Config;
public static var preloader:lime.app.Preloader;
private static var app:lime.app.Application;
public static function main () {
config = {
build: "::meta.buildNumber::",
company: "::meta.company::",
file: "::APP_FILE::",
fps: ::WIN_FPS::,
name: "::meta.title::",
orientation: "::WIN_ORIENTATION::",
packageName: "::meta.packageName::",
version: "::meta.version::",
windows: [
::foreach windows::
{
allowHighDPI: ::allowHighDPI::,
antialiasing: ::antialiasing::,
background: ::background::,
borderless: ::borderless::,
depthBuffer: ::depthBuffer::,
display: ::display::,
fullscreen: ::fullscreen::,
hardware: ::hardware::,
height: ::height::,
hidden: #if munit true #else ::hidden:: #end,
maximized: ::maximized::,
minimized: ::minimized::,
parameters: "::parameters::",
resizable: ::resizable::,
stencilBuffer: ::stencilBuffer::,
title: "::title::",
vsync: ::vsync::,
width: ::width::,
x: ::x::,
y: ::y::
},::end::
]
};
#if (!html5 || munit)
create ();
#end
}
public static function create ():Void {
preloader = new ::if (PRELOADER_NAME != "")::::PRELOADER_NAME::::else::lime.app.Preloader::end:: ();
preloader.onComplete.add (registerLibrary);
#if !munit
app = new ::APP_MAIN:: ();
app.setPreloader (preloader);
app.create (config);
#end
preloader.onComplete.add (start);
preloader.create (config);
#if (js && html5)
var urls = [];
var types = [];
::foreach assets::::if (embed)::
urls.push ("::resourceName::");
::if (type == "image")::types.push (lime.Assets.AssetType.IMAGE);
::elseif (type == "binary")::types.push (lime.Assets.AssetType.BINARY);
::elseif (type == "text")::types.push (lime.Assets.AssetType.TEXT);
::elseif (type == "font")::types.push (lime.Assets.AssetType.FONT);
::elseif (type == "sound")::types.push (lime.Assets.AssetType.SOUND);
::elseif (type == "music")::types.push (lime.Assets.AssetType.MUSIC);
::else::types.push (null);::end::
::end::::end::
if (config.assetsPrefix != null) {
for (i in 0...urls.length) {
if (types[i] != lime.Assets.AssetType.FONT) {
urls[i] = config.assetsPrefix + urls[i];
}
}
}
preloader.load (urls, types);
#end
}
#if (js && html5)
@:keep @:expose("lime.embed")
public static function embed (element:Dynamic, width:Null<Int> = null, height:Null<Int> = null, background:String = null, assetsPrefix:String = null) {
var htmlElement:js.html.Element = null;
if (Std.is (element, String)) {
htmlElement = cast js.Browser.document.getElementById (cast (element, String));
} else if (element == null) {
htmlElement = cast js.Browser.document.createElement ("div");
} else {
htmlElement = cast element;
}
var color = null;
if (background != null && background != "") {
background = StringTools.replace (background, "#", "");
if (background.indexOf ("0x") > -1) {
color = Std.parseInt (background);
} else {
color = Std.parseInt ("0x" + background);
}
}
if (width == null) {
width = 0;
}
if (height == null) {
height = 0;
}
config.windows[0].background = color;
config.windows[0].element = htmlElement;
config.windows[0].width = width;
config.windows[0].height = height;
config.assetsPrefix = assetsPrefix;
create ();
}
#end
private static function registerLibrary ():Void {
lime.Assets.registerLibrary ("default", new DefaultAssetLibrary ());
}
public static function start ():Void {
#if !munit
var result = app.exec ();
#if (sys && !ios && !nodejs && !emscripten)
lime.system.System.exit (result);
#end
#else
new ::APP_MAIN:: ();
#end
}
#if neko
@:noCompletion @:dox(hide) public static function __init__ () {
// Copy from https://github.com/HaxeFoundation/haxe/blob/development/std/neko/_std/Sys.hx#L164
// since Sys.programPath () isn't available in __init__
var sys_program_path = {
var m = neko.vm.Module.local().name;
try {
sys.FileSystem.fullPath(m);
} catch (e:Dynamic) {
// maybe the neko module name was supplied without .n extension...
if (!StringTools.endsWith(m, ".n")) {
try {
sys.FileSystem.fullPath(m + ".n");
} catch (e:Dynamic) {
m;
}
} else {
m;
}
}
};
var loader = new neko.vm.Loader (untyped $loader);
loader.addPath (haxe.io.Path.directory (#if (haxe_ver >= 3.3) sys_program_path #else Sys.executablePath () #end));
loader.addPath ("./");
loader.addPath ("@executable_path/");
}
#end
}