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