Use stack alloc for sorting draw data
This commit is contained in:
@@ -82,7 +82,6 @@ typedef struct Game {
|
||||
f32 elapsed;
|
||||
|
||||
ecs_query_t *drawQuery;
|
||||
DrawData *drawData;
|
||||
} Game;
|
||||
|
||||
static void setScreen(Game *game, GameScreen newScreen) {
|
||||
|
||||
18
game/main.c
18
game/main.c
@@ -107,7 +107,6 @@ bool init(void *userData) {
|
||||
Game *game = ecs_singleton_get_mut(ECS, Game);
|
||||
setScreen(game, SCREEN_MAIN_MENU);
|
||||
game->font = LoadFontEx("assets/fonts/CompassPro.ttf", 92, NULL, 0);
|
||||
game->drawData = bzArrayCreate(DrawData, 1000);
|
||||
game->drawQuery = ecs_query(ECS, {
|
||||
.filter.terms = {
|
||||
{ ecs_id(Position) },
|
||||
@@ -291,8 +290,6 @@ void deinit(void *userData) {
|
||||
bzObjectPoolDestroy(game->pools.btNode);
|
||||
bzObjectPoolDestroy(game->pools.btNodeState);
|
||||
|
||||
bzArrayDestroy(game->drawData);
|
||||
|
||||
soundsUnloadAll(sounds);
|
||||
|
||||
bzUIDestroy(UI);
|
||||
@@ -316,7 +313,6 @@ void update(float dt, void *userData) {
|
||||
if (game->screen != game->nextScreen)
|
||||
game->screen = game->nextScreen;
|
||||
|
||||
BZ_ASSERT(game->stackAlloc.allocated == 0);
|
||||
bzStackAllocReset(&game->stackAlloc);
|
||||
|
||||
ImGuiIO *io = igGetIO();
|
||||
@@ -358,9 +354,11 @@ static void renderGame(Game *game, float dt) {
|
||||
// Ground UI
|
||||
drawPlayerInputUIGround();
|
||||
// Entities
|
||||
bzArrayClear(game->drawData);
|
||||
i32 numDraws = ecs_query_entity_count(game->drawQuery);
|
||||
DrawData *drawData = bzStackAlloc(&game->stackAlloc, numDraws * sizeof(*drawData));
|
||||
ecs_iter_t it = ecs_query_iter(ECS, game->drawQuery);
|
||||
ecs_entity_t worker = 0;
|
||||
i32 drawIdx = 0;
|
||||
while (ecs_iter_next(&it)) {
|
||||
Position *p = ecs_field(&it, Position, 1);
|
||||
Size *s = ecs_field(&it, Size, 2);
|
||||
@@ -382,19 +380,19 @@ static void renderGame(Game *game, float dt) {
|
||||
src.height -= 0.02f;
|
||||
if (t[i].flipX) src.width *= -1.0f;
|
||||
if (t[i].flipY) src.height *= -1.0f;
|
||||
bzArrayPush(game->drawData, (DrawData) {
|
||||
drawData[drawIdx++] = (DrawData) {
|
||||
.tex = t[i].texture,
|
||||
.src = src,
|
||||
.dst = dst,
|
||||
.origin = origin,
|
||||
.rotation = r[i]
|
||||
});
|
||||
};
|
||||
}
|
||||
}
|
||||
qsort(game->drawData, bzArraySize(game->drawData), sizeof(*game->drawData), cmpDrawData);
|
||||
qsort(drawData, numDraws, sizeof(*drawData), cmpDrawData);
|
||||
|
||||
for (i32 i = 0; i < bzArraySize(game->drawData); i++) {
|
||||
DrawData draw = game->drawData[i];
|
||||
for (i32 i = 0; i < numDraws; i++) {
|
||||
DrawData draw = drawData[i];
|
||||
DrawTexturePro(draw.tex, draw.src, draw.dst, draw.origin, draw.rotation, WHITE);
|
||||
}
|
||||
Vector2 target = GetMousePosition();
|
||||
|
||||
Reference in New Issue
Block a user