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

#include "dawngame.h"

shader_t shader;
primitive_t cube;
texture_t texture;
renderitem_t items[32];
int32_t itemCount;

void _renderTest(
  renderlist_t *list, renderpass_t *pass, engine_t *engine, int32_t i
) {
  shaderUsePosition(pass->shader, 0,0,0, 0,engine->time.current,0);
  shaderUseTexture(pass->shader, &texture);
  primitiveDraw(&cube, 0, -1);
}

bool dawnGameInit(dawngame_t *game) {
  renderListInit(&game->renderList, 512, 512);

  assetShaderLoad(&shader, "shaders/textured.vert", "shaders/textured.frag");
  assetTextureLoad(&texture, "test_texture.png");
  cubeInit(&cube, 1, 1, 1);

  items[itemCount++].onRender = &_renderTest;

  int32_t passNumber = renderPassAdd(&game->renderList);
  renderpass_t *pass = renderListGetPass(&game->renderList, passNumber);
  pass->shader = &shader;

  return true;
}

void dawnGameUpdate(dawngame_t *game) {
  camera_t camera;

  cameraPerspective(&camera, 45.0f, 512.0f/512.0f, 0.01f, 100.0f);
  cameraLookAt(&camera, 3,3,3, 0,0,0);
  
  // Render First Pass
  renderListRenderPass(
    &game->renderList, &game->engine, &camera, 0, items, itemCount
  );

  // Render the list itself.
  renderListRender(&game->renderList, &shader);

  // Render that to the backbuffer.
  renderListAsBackbuffer(&game->renderList, &game->engine, &shader);
}

void dawnGameDispose(dawngame_t *game) {

}