Built sprite batch
This commit is contained in:
59
src/engine/display/spritebatch.c
Normal file
59
src/engine/display/spritebatch.c
Normal file
@ -0,0 +1,59 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "spritebatch.h"
|
||||
|
||||
spritebatch_t * spriteBatchCreate(int32_t maxSprites) {
|
||||
spritebatch_t *batch = malloc(sizeof(spritebatch_t));
|
||||
if(batch == NULL) return NULL;
|
||||
|
||||
batch->maxSprites = maxSprites;
|
||||
batch->currentSprite = 0;
|
||||
|
||||
batch->primitive = primitiveCreate(
|
||||
maxSprites*QUAD_VERTICE_COUNT, maxSprites*QUAD_INDICE_COUNT
|
||||
);
|
||||
if(batch == NULL) {
|
||||
free(batch);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return batch;
|
||||
}
|
||||
|
||||
void spriteBatchQuad(spritebatch_t *spriteBatch, int32_t index,
|
||||
float x, float y, float width, float height,
|
||||
float u0, float v0, float u1, float v1
|
||||
) {
|
||||
if(index == -1) {
|
||||
index = spriteBatch->currentSprite++;
|
||||
} else {
|
||||
spriteBatch->currentSprite = max(index, spriteBatch->currentSprite);
|
||||
}
|
||||
|
||||
quadBuffer(spriteBatch->primitive,
|
||||
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);
|
||||
free(spriteBatch);
|
||||
}
|
Reference in New Issue
Block a user