This commit is contained in:
2026-06-18 20:25:54 -05:00
parent 730a5b2b10
commit 57b2cdb9d1
111 changed files with 865 additions and 3328 deletions
@@ -0,0 +1,107 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "display/mesh/quad.h"
#include "display/texture/texture.h"
#include "display/shader/shadermaterial.h"
#define SPRITEBATCH_SPRITES_MAX 512
#define SPRITEBATCH_VERTEX_COUNT (SPRITEBATCH_SPRITES_MAX * QUAD_VERTEX_COUNT)
#define SPRITEBATCH_FLUSH_COUNT 16
#define SPRITEBATCH_SPRITES_MAX_PER_FLUSH (\
SPRITEBATCH_SPRITES_MAX / SPRITEBATCH_FLUSH_COUNT \
)
typedef struct {
vec3 min;
vec3 max;
vec2 uvMin;
vec2 uvMax;
} spritebatchsprite_t;
typedef struct {
mesh_t mesh;
int32_t spriteCount;
int32_t spriteFlush;
shader_t *shader;
shadermaterial_t material;
} spritebatch_t;
// Have to define these separately because of alignment on certain platforms.
extern meshvertex_t SPRITEBATCH_VERTICES[SPRITEBATCH_VERTEX_COUNT];
extern spritebatch_t SPRITEBATCH;
/**
* Initializes the global sprite batch and its internal mesh buffer.
*
* @return Error state.
*/
errorret_t spriteBatchInit();
/**
* Lowest-level buffer function. Writes sprites into the internal vertex buffer.
* Flushes automatically when the per-flush capacity is reached. Does not
* modify material state - call spriteBatchSetState or use a high-level push
* function before buffering.
*
* @param sprites Pointer to the sprite array.
* @param count Number of sprites to buffer.
* @param shader Shader to use when flushing.
* @param material Material information passed to the shader when flushing.
* @return Error state.
*/
errorret_t spriteBatchBuffer(
const spritebatchsprite_t *sprites,
const uint32_t count,
shader_t *shader,
const shadermaterial_t material
);
/**
* Buffers an array of sprites to a given array of mesh vertices. This is the
* internal method that is used to buffer to the internal spritebatch mesh, but
* you can use it to achieve sprite buffering to a mesh you own.
*
* verticesSize is the size of the vertices array, we use this to ensure no
* buffer overflows.
*
* @param sprites Pointer to the sprite array.
* @param count Number of sprites to buffer.
* @param vertices Pointer to the vertex array to write to.
* @param verticesSize Size of the vertex array, in number of vertices.
*/
void spriteBatchBufferToMesh(
const spritebatchsprite_t *sprites,
const uint32_t count,
meshvertex_t *vertices,
const uint32_t verticesSize
);
/**
* Resets sprite and flush counters and clears the current material state.
* Calling spriteBatchFlush after this renders nothing.
*/
void spriteBatchClear();
/**
* Uploads and draws all buffered sprites. If a material type has been set via
* spriteBatchSetState or spriteBatchCheckState, the shader is bound and the
* material is applied first. If matType is NULL the caller is responsible for
* having the correct shader already bound. Does nothing if the buffer is empty.
*
* @return Error state.
*/
errorret_t spriteBatchFlush();
/**
* Disposes of the sprite batch and frees its internal mesh buffer.
*
* @return Error state.
*/
errorret_t spriteBatchDispose();