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

#include "spritebatch.h"

void spriteBatchInit(spritebatch_t *batch, int32_t maxSprites) {
  batch->maxSprites = maxSprites;
  batch->currentSprite = 0;

  primitiveInit(&batch->primitive,
    maxSprites*QUAD_VERTICE_COUNT, maxSprites*QUAD_INDICE_COUNT
  );
}

void spriteBatchQuad(spritebatch_t *spriteBatch, int32_t index,
  float x, float y, float z, float width, float height,
  float u0, float v0, float u1, float v1
) {
  if(index == -1) {
    index = spriteBatch->currentSprite++;
  } else {
    spriteBatch->currentSprite = mathMax(index, spriteBatch->currentSprite);
  }
  
  quadBuffer(&spriteBatch->primitive, z,
    x, y, u0, v0,
    x+width, y+height, u1, v1,
    index*QUAD_VERTICE_COUNT, index*QUAD_INDICE_COUNT
  );
}

void spriteBatchFlush(spritebatch_t *spriteBatch) {
  spriteBatch->currentSprite = 0;
}

void spriteBatchDraw(spritebatch_t *spriteBatch, int32_t index, int32_t count) {
  if(count == -1) count = spriteBatch->currentSprite;
  primitiveDraw(&spriteBatch->primitive,
    index*QUAD_INDICE_COUNT, count*QUAD_INDICE_COUNT
  );
}

void spriteBatchDispose(spritebatch_t *spriteBatch) {
  primitiveDispose(&spriteBatch->primitive);
}