// Copyright (c) 2022 Dominic Masters
// 
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT

#include "DawnGame.hpp"
#include "scenes/Scene_1.hpp"

using namespace Dawn;

DawnGame::DawnGame(DawnHost *host) :
  host(host),
  renderManager(this),
  inputManager(this),
  localeManager(this),
  saveManager(this),
  audioManager(this)
{
}

int32_t DawnGame::init() {
  this->assetManager.init();
  this->localeManager.init();
  this->renderManager.init();
  this->audioManager.init();

  this->scene = new Scene_1(this);
  
  return DAWN_GAME_INIT_RESULT_SUCCESS;
}

int32_t DawnGame::update(float_t delta) {
  if(this->sceneToCutTo != nullptr) {
    if(this->sceneToCutTo == this->scene) {
      delete this->scene;
      this->scene = nullptr;
    } else {
      delete this->scene;
      this->scene = this->sceneToCutTo;
    }
    this->sceneToCutTo = nullptr;
  }

  this->assetManager.update();
  this->inputManager.update();
  this->timeManager.update(delta);

  if(this->scene != nullptr) this->scene->update();
  
  this->renderManager.update();

  return DAWN_GAME_UPDATE_RESULT_SUCCESS;
}


void DawnGame::sceneCutover(Scene *scene) {
  if(scene == nullptr) scene = this->scene;
  this->sceneToCutTo = scene;
}