archivemuh
This commit is contained in:
13
archive/dusksdl2/display/spritebatch/CMakeLists.txt
Normal file
13
archive/dusksdl2/display/spritebatch/CMakeLists.txt
Normal file
@@ -0,0 +1,13 @@
|
||||
# Copyright (c) 2025 Dominic Masters
|
||||
#
|
||||
# This software is released under the MIT License.
|
||||
# https://opensource.org/licenses/MIT
|
||||
|
||||
# Sources
|
||||
target_sources(${DUSK_TARGET_NAME}
|
||||
PRIVATE
|
||||
spritebatch.c
|
||||
)
|
||||
|
||||
# Subdirs
|
||||
# add_subdirectory(draw)
|
75
archive/dusksdl2/display/spritebatch/spritebatch.c
Normal file
75
archive/dusksdl2/display/spritebatch/spritebatch.c
Normal file
@@ -0,0 +1,75 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "spritebatch.h"
|
||||
#include "assert/assert.h"
|
||||
#include "util/memory.h"
|
||||
#include "console/console.h"
|
||||
|
||||
spritebatch_t SPRITEBATCH;
|
||||
|
||||
void spriteBatchInit() {
|
||||
SPRITEBATCH.spriteCount = 0;
|
||||
SPRITEBATCH.currentTexture = NULL;
|
||||
|
||||
meshInit(
|
||||
&SPRITEBATCH.mesh,
|
||||
GL_TRIANGLES,
|
||||
SPRITEBATCH_VERTEX_COUNT,
|
||||
&SPRITEBATCH.vertices[0]
|
||||
);
|
||||
}
|
||||
|
||||
void spriteBatchPush(
|
||||
texture_t *texture,
|
||||
const float_t minX,
|
||||
const float_t minY,
|
||||
const float_t maxX,
|
||||
const float_t maxY,
|
||||
const uint8_t r,
|
||||
const uint8_t g,
|
||||
const uint8_t b,
|
||||
const uint8_t a,
|
||||
const float_t u0,
|
||||
const float_t v0,
|
||||
const float_t u1,
|
||||
const float_t v1
|
||||
) {
|
||||
// Need to flush?
|
||||
if(
|
||||
SPRITEBATCH.currentTexture != texture ||
|
||||
SPRITEBATCH.spriteCount >= SPRITEBATCH_SPRITES_MAX
|
||||
) {
|
||||
spriteBatchFlush();
|
||||
SPRITEBATCH.currentTexture = texture;
|
||||
}
|
||||
|
||||
quadBuffer(
|
||||
&SPRITEBATCH.vertices[SPRITEBATCH.spriteCount * QUAD_VERTEX_COUNT],
|
||||
minX, minY, maxX, maxY,
|
||||
r, g, b, a,
|
||||
u0, v0, u1, v1
|
||||
);
|
||||
|
||||
SPRITEBATCH.spriteCount++;
|
||||
}
|
||||
|
||||
void spriteBatchClear() {
|
||||
SPRITEBATCH.spriteCount = 0;
|
||||
SPRITEBATCH.currentTexture = NULL;
|
||||
}
|
||||
|
||||
void spriteBatchFlush() {
|
||||
if(SPRITEBATCH.spriteCount == 0) return;
|
||||
textureBind(SPRITEBATCH.currentTexture);
|
||||
meshDraw(&SPRITEBATCH.mesh, 0, QUAD_VERTEX_COUNT * SPRITEBATCH.spriteCount);
|
||||
spriteBatchClear();
|
||||
}
|
||||
|
||||
void spriteBatchDispose() {
|
||||
meshDispose(&SPRITEBATCH.mesh);
|
||||
}
|
52
archive/dusksdl2/display/spritebatch/spritebatch.h
Normal file
52
archive/dusksdl2/display/spritebatch/spritebatch.h
Normal file
@@ -0,0 +1,52 @@
|
||||
/**
|
||||
* Copyright (c) 2025 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"
|
||||
|
||||
#define SPRITEBATCH_SPRITES_MAX 1
|
||||
#define SPRITEBATCH_VERTEX_COUNT (SPRITEBATCH_SPRITES_MAX * QUAD_VERTEX_COUNT)
|
||||
|
||||
|
||||
typedef struct {
|
||||
mesh_t mesh;
|
||||
int32_t spriteCount;
|
||||
texture_t *currentTexture;
|
||||
meshvertex_t vertices[SPRITEBATCH_VERTEX_COUNT];
|
||||
} spritebatch_t;
|
||||
|
||||
extern spritebatch_t SPRITEBATCH;
|
||||
|
||||
/**
|
||||
* Initializes a sprite batch.
|
||||
*
|
||||
* @param spriteBatch The sprite batch to initialize.
|
||||
*/
|
||||
void spriteBatchInit();
|
||||
|
||||
void spriteBatchPush(
|
||||
texture_t *texture,
|
||||
const float_t minX,
|
||||
const float_t minY,
|
||||
const float_t maxX,
|
||||
const float_t maxY,
|
||||
const uint8_t r,
|
||||
const uint8_t g,
|
||||
const uint8_t b,
|
||||
const uint8_t a,
|
||||
const float_t u0,
|
||||
const float_t v0,
|
||||
const float_t u1,
|
||||
const float_t v1
|
||||
);
|
||||
|
||||
void spriteBatchClear();
|
||||
|
||||
void spriteBatchFlush();
|
||||
|
||||
void spriteBatchDispose();
|
Reference in New Issue
Block a user