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

#include "game.h"

game_t GAME_STATE;

bool gameInit() {
  // Init the game
  GAME_STATE.name = GAME_NAME;

  logInit();
  logText("Starting Game");

  // Init
  gameTimeInit();
  renderInit();
  inputInit();

  // Load the world shader.
  GAME_STATE.shaderWorld = assetShaderLoad(
    "shaders/textured.vert", "shaders/textured.frag"
  );

  // Font
  GAME_STATE.fontTexture = assetTextureLoad("font.png");
  GAME_STATE.fontTileset = tilesetCreate(20, 20,
    GAME_STATE.fontTexture->width,
    GAME_STATE.fontTexture->height,
    1, 1, 1, 1
  );
  GAME_STATE.fontBatch = spriteBatchCreate(1024);

  // Prepare the renderer.
  holdemRenderFrameInit();
  holdemRenderWorldInit();
  holdemRenderPlayerInit();
  holdemRenderCardInit();

  // Prepare the action manager
  pokerActionInit();

  // Start the first action
  pokerActionAdd(actionStart());

  // Init the input manger.
  return true;
}

bool gameUpdate(float platformDelta) {
  gameTimeUpdate(platformDelta);
  renderFrameStart();
  inputUpdate();
  
  shaderUse(GAME_STATE.shaderWorld);// TODO: remove
  
  // Update the frame buffers and action queue
  holdemRenderFrameUpdate();
  pokerActionUpdate();

  // Render things on each frame, then render those frames.
  holdemRenderFrameUseLeft();
  holdemRenderWorld();
  holdemRenderFrameUseRight();
  holdemRenderWorld();
  holdemRenderFrameBack();

  if(inputIsPressed(INPUT_NULL)) return false;
  return true;
}

void gameDispose() {
  pokerActionDispose();
  shaderDispose(GAME_STATE.shaderWorld);
  inputDispose();
  renderDispose();
}