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

#include "scene.h"

void sceneInit(scene_t *scene) {
  scene->itemCount = 0;

  cameraLookAt(&scene->camera, 3,3,3, 0,0,0);
  cameraPerspective(&scene->camera, 75, 16.0f/9.0f, 0.01f, 100.0f);
}

sceneitem_t * sceneAdd(scene_t *scene) {
  sceneitem_t *item = scene->items + scene->itemCount;
  scene->itemCount++;
  return item;
}

void sceneUpdate(scene_t *scene, engine_t *engine) {
  int32_t i;
  sceneitem_t *item;

  for(i = 0; i < scene->itemCount; i++) {
    item = scene->items + i;
    if(item->onUpdate == NULL) continue;
    item->onUpdate(scene, i, engine);
  }
}

void sceneRender(scene_t *scene, engine_t *engine, shader_t *shader) {
  int32_t i;
  sceneitem_t *item;

  shaderUse(shader);
  shaderUseCamera(shader, &scene->camera);

  for(i = 0; i < scene->itemCount; i++) {
    item = scene->items + i;
    if(item->onRender == NULL) continue;
    item->onRender(scene, i, engine);
  }
}

void sceneDispose(scene_t *scene) {
  int32_t i;
  sceneitem_t *item;

  for(i = 0; i < scene->itemCount; i++) {
    item = scene->items + i;
    if(item->onDispose == NULL) continue;
    item->onDispose(scene, i, NULL);
  }
}