Main.hx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package;
  2. import kha.Window;
  3. import kha.WindowOptions;
  4. import kha.WindowMode;
  5. import kha.System;
  6. import iron.object.Object;
  7. import iron.Scene;
  8. import iron.RenderPath;
  9. import arm.render.Inc;
  10. import arm.render.RenderPathDeferred;
  11. import arm.render.Uniforms;
  12. import arm.Config;
  13. #if arm_player
  14. using StringTools;
  15. #end
  16. class Main {
  17. static var tasks: Int;
  18. public static function main() {
  19. tasks = 1;
  20. tasks++; Config.load(function() { tasks--; start(); });
  21. #if arm_physics
  22. tasks++; loadPhysics();
  23. #end
  24. tasks--; start();
  25. }
  26. static function start() {
  27. if (tasks > 0) return;
  28. Config.create();
  29. var c = Config.raw;
  30. var windowMode = c.window_mode == 0 ? WindowMode.Windowed : WindowMode.Fullscreen;
  31. var windowFeatures = None;
  32. if (c.window_resizable) windowFeatures |= FeatureResizable;
  33. if (c.window_maximizable) windowFeatures |= FeatureMaximizable;
  34. if (c.window_minimizable) windowFeatures |= FeatureMinimizable;
  35. #if arm_player
  36. var title = Krom.getArg(0);
  37. title = title.replace("\\", "/");
  38. var lasti = title.lastIndexOf("/");
  39. if (lasti >= 0) title = title.substr(lasti + 1);
  40. if (title.endsWith(".exe")) title = title.substr(0, title.length - 4);
  41. #else
  42. var title = "untitled - ArmorPaint";
  43. #end
  44. var options: kha.SystemOptions = {
  45. title: title,
  46. window: {
  47. width: c.window_w,
  48. height: c.window_h,
  49. x: c.window_x,
  50. y: c.window_y,
  51. mode: windowMode,
  52. windowFeatures: windowFeatures
  53. },
  54. framebuffer: {
  55. samplesPerPixel: 1,
  56. verticalSync: c.window_vsync
  57. }
  58. };
  59. System.start(options, function(window: Window) {
  60. iron.App.init(function() {
  61. Scene.setActive("Scene", function(o: Object) {
  62. Config.init();
  63. Uniforms.init();
  64. var path = new RenderPath();
  65. Inc.init(path);
  66. RenderPathDeferred.init(path);
  67. path.commands = RenderPathDeferred.commands;
  68. RenderPath.setActive(path);
  69. #if arm_player
  70. new arm.Player();
  71. #else
  72. new arm.App();
  73. #end
  74. #if arm_physics
  75. o.addTrait(new arm.plugin.PhysicsWorld());
  76. #end
  77. });
  78. });
  79. });
  80. }
  81. #if arm_physics
  82. static function loadPhysics() {
  83. var b = haxe.io.Bytes.ofData(Krom.loadBlob("data/ammo.wasm.js"));
  84. var print = function(s: String) { trace(s); };
  85. var loaded = function() { tasks--; start(); };
  86. untyped __js__("(1, eval)({0})", b.toString());
  87. var instantiateWasm = function(imports, successCallback) {
  88. var wasmbin = Krom.loadBlob("data/ammo.wasm.wasm");
  89. var module = new js.lib.webassembly.Module(wasmbin);
  90. var inst = new js.lib.webassembly.Instance(module, imports);
  91. successCallback(inst);
  92. return inst.exports;
  93. };
  94. untyped __js__("Ammo({print:print, instantiateWasm:instantiateWasm}).then(loaded)");
  95. }
  96. #end
  97. }