///|
/// High-level game contract for examples and small apps that already model
/// their state as `update + draw`.
pub(open) trait EngineGame {
  fn update_game(Self, @core.InputSnapshot) -> Unit
  fn draw_game(Self, EngineContext) -> Array[@gfx.DrawTrianglesCommand]
}

///|
fn[T : EngineGame] engine_game_update(
  game : T,
  input : @core.InputSnapshot,
) -> Unit {
  game.update_game(input)
}

///|
fn[T : EngineGame] engine_game_draw(
  game : T,
  ctx : EngineContext,
) -> Array[@gfx.DrawTrianglesCommand] {
  game.draw_game(ctx)
}

///|
pub fn[T : EngineGame] run_game(
  game : T,
  on_frame? : () -> Unit,
  after_render? : (Double, Double) -> Unit,
  audio_ctx? : @audio.MixerAudioContext,
  audio_frames_per_tick~ : Int32 = 735,
  width~ : Int32 = 320,
  height~ : Int32 = 240,
  title~ : String = "kagura",
  canvas~ : String = "#app",
  on_error? : (RunFailure) -> Unit,
) -> Unit {
  run(
    update=fn(input) { engine_game_update(game, input) },
    draw=fn(ctx) { engine_game_draw(game, ctx) },
    on_frame?,
    after_render?,
    audio_ctx?,
    audio_frames_per_tick~,
    width~,
    height~,
    title~,
    canvas~,
    on_error?,
  )
}

///|
pub fn[T : EngineGame] start_game(
  game : T,
  on_frame? : () -> Unit,
  after_render? : (Double, Double) -> Unit,
  audio_ctx? : @audio.MixerAudioContext,
  audio_frames_per_tick~ : Int32 = 735,
  width~ : Int32 = 320,
  height~ : Int32 = 240,
  title~ : String = "kagura",
  canvas~ : String = "#app",
  on_error? : (RunFailure) -> Unit,
) -> RunSession {
  start(
    update=fn(input) { engine_game_update(game, input) },
    draw=fn(ctx) { engine_game_draw(game, ctx) },
    on_frame?,
    after_render?,
    audio_ctx?,
    audio_frames_per_tick~,
    width~,
    height~,
    title~,
    canvas~,
    on_error?,
  )
}