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

#include "frame.h"

void holdemRenderFrameInit() {
  int32_t lWidth, rWidth, height;

  // Prepare the two frame buffers.
  lWidth = HOLDEM_GAME_FRAME_LEFT_WIDTH, rWidth = HOLDEM_GAME_FRAME_RIGHT_WIDTH;
  height = HOLDEM_GAME_FRAME_HEIGHT;
  GAME_STATE.frameLeft = frameBufferCreate(lWidth, height);
  GAME_STATE.frameRight = frameBufferCreate(rWidth, height);
  GAME_STATE.quadLeft = quadCreate(0, 0, 0, 0, 0, lWidth, height, 1, 1);
  GAME_STATE.quadRight = quadCreate(0, 0, 0, 0, 0, rWidth, height, 1, 1);
}

void holdemRenderFrameUpdate() {
  int32_t lWidth, rWidth, height;

  lWidth = HOLDEM_GAME_FRAME_LEFT_WIDTH, rWidth = HOLDEM_GAME_FRAME_RIGHT_WIDTH;
  height = HOLDEM_GAME_FRAME_HEIGHT;
  if((
    GAME_STATE.frameLeft->texture->width == lWidth &&
    GAME_STATE.frameLeft->texture->height == height
  )) return;

  // Recreate frame buffers.
  frameBufferDispose(GAME_STATE.frameLeft);
  frameBufferDispose(GAME_STATE.frameRight);
  GAME_STATE.frameLeft = frameBufferCreate(lWidth, height);
  GAME_STATE.frameRight = frameBufferCreate(rWidth, height);
  quadBuffer(GAME_STATE.quadLeft, 0,
    0, 0, 0, 1,
    lWidth, height, 1, 0,
    0, 0
  );
  quadBuffer(GAME_STATE.quadRight, 0,
    0, 0, 0, 1,
    rWidth, height, 1, 0,
    0, 0
  );
}

void holdemRenderFrameUseLeft() {
  glClearColor(0.3, 0, 0, 1);
  frameBufferUse(GAME_STATE.frameLeft, true);
  cameraPerspective(&GAME_STATE.cameraLeft, 35,
    (
      (float)GAME_STATE.frameLeft->texture->width /
      (float)GAME_STATE.frameLeft->texture->height
    ), 0.2f, 1000.0f
  );
  shaderUseCamera(GAME_STATE.shaderWorld, &GAME_STATE.cameraLeft);
}

void holdemRenderFrameUseRight() {
  glClearColor(0.3, 0.3, 0, 1);
  frameBufferUse(GAME_STATE.frameRight, true);

  cameraPerspective(&GAME_STATE.cameraRight, 45,
    (
      (float)GAME_STATE.frameRight->texture->width /
      (float)GAME_STATE.frameRight->texture->height
    ), 0.2f, 1000.0f
  );
  cameraLookAt(&GAME_STATE.cameraRight, 0, 3, 3, 0, 0, 0);

  // cameraPerspective(&GAME_STATE.cameraRight, 15,
  //   (
  //     (float)GAME_STATE.frameRight->texture->width /
  //     (float)GAME_STATE.frameRight->texture->height
  //   ), 0.2f, 1000.0f
  // );
  // cameraLookAt(&GAME_STATE.cameraRight, 0, 1, -8, 0, 0, 0);

  shaderUseCamera(GAME_STATE.shaderWorld, &GAME_STATE.cameraRight);
}

void holdemRenderFrameBack() {
  glClearColor(0, 0, 0, 1);
  frameBufferUse(NULL, true);
  cameraOrtho(&GAME_STATE.cameraMain, 0,
    RENDER_STATE.width, RENDER_STATE.height, 1, 0, 1
  );
  cameraLookAt(&GAME_STATE.cameraMain, 0, 0, 0.5f, 0, 0, 0);
  shaderUseCamera(GAME_STATE.shaderWorld, &GAME_STATE.cameraMain);
  shaderUsePosition(GAME_STATE.shaderWorld,
    0, 0, 0, 0, 0, 0
  );
  shaderUseTexture(GAME_STATE.shaderWorld, GAME_STATE.frameLeft->texture);
  primitiveDraw(GAME_STATE.quadLeft, 0, -1);
  shaderUsePosition(GAME_STATE.shaderWorld,
    RENDER_STATE.width - GAME_STATE.frameRight->texture->width,
    0, 0, 0, 0, 0
  );
  shaderUseTexture(GAME_STATE.shaderWorld, GAME_STATE.frameRight->texture);
  primitiveDraw(GAME_STATE.quadRight, 0, -1);
}