App constructor

App([
  1. AppConfig? config
])

Creates a new App instance.

  • config: Optional AppConfig to customize application behavior, such as the canvas element ID and graphics batch capacity. If null, default configuration is used.

The constructor initializes all core systems (graphics, input, audio, resources) and sets up the game loop. The onCreate method is called once initialization is complete.

Implementation

App([AppConfig? config]) {
  instance = this;

  _config = config ??= AppConfig();

  var root = document.querySelector(_config.canvasElement) as HTMLCanvasElement?;
  if (root == null) {
    die("Could not find canvas: $_config.canvasElement");
    return;
  }

  onError(ErrorEvent event) {
    _killApp = true;
  }

  window.addEventListener('error', onError.toJS);

  canvas = root;
  loader = Loader();
  gamepad = Gamepad();
  keyboard = Keyboard(canvas);
  mouse = Mouse(canvas);
  accel = Accelerometer(canvas, mouse, config.autoRequestAccelerometerPermission);
  gfx = Graphics(canvas, batchCapacityInBytes: _config.gfxBatchCapacityInBytes);
  audio = Audio();
  resources = ResourceManager(gfx.gl, audio, loader);

  canvas.addEventListener(
    'focus',
    (FocusEvent e) {
      if (!_config.autoSuspend || !_suspended) {
        return;
      }
      _suspended = false;
      audio.resume();
      onResume();
      _validateUpdateTimer();
    }.toJS,
  );

  canvas.addEventListener(
    'blur',
    (FocusEvent e) {
      if (!_config.autoSuspend || _suspended) {
        return;
      }
      _suspended = true;
      onSuspend();
      audio.suspend();
      keyboard.suspend();
      mouse.suspend();
      gamepad.suspend();
    }.toJS,
  );


  canvas.focus();
  window.addEventListener('load', (Event e) {
    canvas.focus();
  }.toJS);

  updateRate = 60;

  gfx.setViewport(0, 0, canvas.width, canvas.height);
  onCreate();
  _renderGame();
  _validateUpdateTimer();
}