Built sprite batch

This commit is contained in:
2021-04-05 08:48:17 +10:00
parent ac63eb6b6a
commit 5c54a10298
6 changed files with 162 additions and 2 deletions

View File

@ -10,12 +10,39 @@
#define QUAD_VERTICE_COUNT 4 #define QUAD_VERTICE_COUNT 4
#define QUAD_INDICE_COUNT 6 #define QUAD_INDICE_COUNT 6
/**
* Buffers the vertices of a quad onto a primitive.
*
* @param x0 The X lower coordinate.
* @param y0 The Y lower coordinate.
* @param u0 The X lower texture coordinate.
* @param v0 The Y lower texture coordinate.
* @param x1 The X higher coordinate.
* @param y1 The Y higher coordinate.
* @param u1 The X higher texture coordinate.
* @param v1 The Y higher texture coordinate.
* @param verticeStart Start vertice to buffer to.
* @param indiceStart Start indice to buffer to.
*/
void quadBuffer(primitive_t *primitive, void quadBuffer(primitive_t *primitive,
float x0, float y0, float u0, float v0, float x0, float y0, float u0, float v0,
float x1, float y1, float u1, float v1, float x1, float y1, float u1, float v1,
int32_t verticeStart, int32_t indiceStart int32_t verticeStart, int32_t indiceStart
); );
/**
* Creates a new quad primitive.
*
* @param x0 The X lower coordinate.
* @param y0 The Y lower coordinate.
* @param u0 The X lower texture coordinate.
* @param v0 The Y lower texture coordinate.
* @param x1 The X higher coordinate.
* @param y1 The Y higher coordinate.
* @param u1 The X higher texture coordinate.
* @param v1 The Y higher texture coordinate.
* @return The quad primitive.
*/
primitive_t * quadCreate( primitive_t * quadCreate(
float x0, float y0, float u0, float v0, float x0, float y0, float u0, float v0,
float x1, float y1, float u1, float v1 float x1, float y1, float u1, float v1

View 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);
}

View File

@ -0,0 +1,70 @@
// Copyright (c) 2021 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include <stdint.h>
#include "primitive.h"
#include "primitives/quad.h"
#include "../util/math.h"
typedef struct {
/** Maximum sprites the batch can hold. */
int32_t maxSprites;
/** The current/next sprite index. */
int32_t currentSprite;
/** Internal primitive */
primitive_t *primitive;
} spritebatch_t;
/**
* Creates a new Sprite Batch made of standard quads.
*
* @param maxSprites The maxiumum number of sprites the batch can hold.
* @returns A new quad Sprite Batch.
*/
spritebatch_t * spriteBatchCreate(int32_t maxSprites);
/**
* Renders a sprite onto a given Sprite Batch.
*
* @param spriteBatch The sprite batch to render to.
* @param index The index within the sprite batch. Set to -1 to select "next".
* @param x X coordinate of the sprite.
* @param y Y coordinate of the sprite.
* @param width Width of the sprite.
* @param height Height of the sprite.
* @param u0 Texture U coordinate (min).
* @param v0 Texture V coordinate (min).
* @param u1 Texture U coordinate (max).
* @param v1 Texture V coordinate (max).
*/
void spriteBatchQuad(spritebatch_t *spriteBatch, int32_t index,
float x, float y, float width, float height,
float u0, float v0, float u1, float v1
);
/**
* Flushes a sprite batch to reset the indexes.
* @param spriteBatch The batch.
*/
void spriteBatchFlush(spritebatch_t *spriteBatch);
/**
* Draws the Sprite Batch.
*
* @param spriteBatch The sprite batch to render.
* @param start Start index to render from.
* @param count Count of sprites to render. Set to -1 to render to the current.
*/
void spriteBatchDraw(spritebatch_t *spriteBatch, int32_t start, int32_t count);
/**
* Disposes a previously created Sprite Batch.
*
* @param spriteBatch The sprite batch to dispose.
*/
void spriteBatchDispose(spritebatch_t *spriteBatch);

View File

@ -10,6 +10,7 @@
camera_t *camera; camera_t *camera;
shader_t *shader; shader_t *shader;
world_t *world; world_t *world;
spritebatch_t *batch;
engine_t * engineInit(platform_t *platform, char *name, uint32_t inputCount) { engine_t * engineInit(platform_t *platform, char *name, uint32_t inputCount) {
// Create the engine instance. // Create the engine instance.
@ -37,7 +38,7 @@ engine_t * engineInit(platform_t *platform, char *name, uint32_t inputCount) {
shader = assetShaderLoad("shaders/test.vert", "shaders/test.frag"); shader = assetShaderLoad("shaders/test.vert", "shaders/test.frag");
camera = cameraCreate(); camera = cameraCreate();
cameraLookAt(camera, cameraLookAt(camera,
30, 30, 30, 5, 5, 5,
0, 0, 0 0, 0, 0
); );
cameraPerspective(camera, 45.0f, 1920.0f/1080.0f, 0.5f, 100.0f); cameraPerspective(camera, 45.0f, 1920.0f/1080.0f, 0.5f, 100.0f);

View File

@ -14,6 +14,7 @@
#include "display/camera.h" #include "display/camera.h"
#include "world/world.h" #include "world/world.h"
#include "world/chunklist.h" #include "world/chunklist.h"
#include "display/spritebatch.h"
/** Information about the current engine context. */ /** Information about the current engine context. */

View File

@ -5,4 +5,6 @@
#pragma once #pragma once
#define mod(a,b) (a%b+b)%b #define mod(a,b) (a%b+b)%b
#define max(a,b) (a<b?b:a)
#define min(a,b) (a>b?b:a)