#include "game.h"

#include <gfx/gfx_app.h>
#include <log/log.h>

#include <stdlib.h>

static bool init(const GfxAppDesc* desc, void** app_state) {
  Game* game = calloc(1, sizeof(Game));
  if (!game) {
    LOGE("Failed to allocate game state");
    return false;
  }
  if (!game_new(game, desc->argc, desc->argv)) {
    LOGE("Failed to initialize game");
    return false;
  }
  *app_state = game;
  return true;
}

static void shutdown(void* app_state) {
  assert(app_state);
  Game* game = (Game*)(app_state);
  game_end(game);
}

static void update(void* app_state, double t, double dt) {
  assert(app_state);
  Game* game = (Game*)(app_state);
  game_update(game, t, dt);
}

static void render(void* app_state) {
  assert(app_state);
  Game* game = (Game*)(app_state);
  game_render(game);
}

static void resize(void* app_state, int width, int height) {
  assert(app_state);
  Game* game = (Game*)(app_state);
  game_set_viewport(game, width, height);
}

int main(int argc, const char** argv) {
  const int initial_width  = 1350;
  const int initial_height = 900;
  const int max_fps        = 60;

  gfx_app_run(
      &(GfxAppDesc){
          .argc              = argc,
          .argv              = argv,
          .width             = initial_width,
          .height            = initial_height,
          .max_fps           = max_fps,
          .update_delta_time = max_fps > 0 ? 1.0 / (double)max_fps : 0.0},
      &(GfxAppCallbacks){
          .init     = init,
          .update   = update,
          .render   = render,
          .resize   = resize,
          .shutdown = shutdown});

  return 0;
}