Use built-in qsort instead of flecs (better perf)
This commit is contained in:
@@ -38,17 +38,17 @@ i32 _bzArrayPop(void *arr);
|
|||||||
#define bzArrayDel(arr, idx) _bzArrayDelN(arr, idx, 1)
|
#define bzArrayDel(arr, idx) _bzArrayDelN(arr, idx, 1)
|
||||||
#define bzArrayDelN(arr, idx, n) _bzArrayDelN(arr, idx, n)
|
#define bzArrayDelN(arr, idx, n) _bzArrayDelN(arr, idx, n)
|
||||||
|
|
||||||
#define bzArrayPush(arr, e) \
|
#define bzArrayPush(arr, ...) \
|
||||||
do { \
|
do { \
|
||||||
(arr) = bzArrayEnsureCapacity(arr, bzArraySize(arr) + 1); \
|
(arr) = bzArrayEnsureCapacity(arr, bzArraySize(arr) + 1); \
|
||||||
(arr)[_bzArrayPush(arr)] = (e); \
|
(arr)[_bzArrayPush(arr)] = (__VA_ARGS__); \
|
||||||
} while (0)
|
} while (0)
|
||||||
#define bzArrayIns(arr, idx, e) \
|
#define bzArrayIns(arr, idx, ...) \
|
||||||
do { \
|
do { \
|
||||||
(arr) = bzArrayEnsureCapacity(arr, bzArraySize(arr) + 1); \
|
(arr) = bzArrayEnsureCapacity(arr, bzArraySize(arr) + 1); \
|
||||||
(arr)[_bzArrayIns(arr, idx)] = (e); \
|
(arr)[_bzArrayIns(arr, idx)] = (__VA_ARGS__); \
|
||||||
} while (0)
|
} while (0)
|
||||||
#define bzArraySet(arr, idx, e) (arr)[_bzArraySet(arr, idx)] = e
|
#define bzArraySet(arr, idx, ...) (arr)[_bzArraySet(arr, idx)] = (__VA_ARGS__)
|
||||||
#define bzArrayPop(arr) (arr)[_bzArrayPop(arr)]
|
#define bzArrayPop(arr) (arr)[_bzArrayPop(arr)]
|
||||||
|
|
||||||
#define bzArrayFor(arr, it) for (i32 it = 0; it < bzArraySize(arr); it++)
|
#define bzArrayFor(arr, it) for (i32 it = 0; it < bzArraySize(arr); it++)
|
||||||
|
|||||||
@@ -10,6 +10,14 @@ typedef enum GameScreen {
|
|||||||
SCREEN_SETTINGS,
|
SCREEN_SETTINGS,
|
||||||
} GameScreen;
|
} GameScreen;
|
||||||
|
|
||||||
|
typedef struct DrawData {
|
||||||
|
Texture tex;
|
||||||
|
Rectangle src;
|
||||||
|
Rectangle dst;
|
||||||
|
Vector2 origin;
|
||||||
|
f32 rotation;
|
||||||
|
} DrawData;
|
||||||
|
|
||||||
typedef struct Game {
|
typedef struct Game {
|
||||||
GameScreen screen;
|
GameScreen screen;
|
||||||
Camera2D camera;
|
Camera2D camera;
|
||||||
@@ -41,6 +49,7 @@ typedef struct Game {
|
|||||||
f32 elapsed;
|
f32 elapsed;
|
||||||
|
|
||||||
ecs_query_t *drawQuery;
|
ecs_query_t *drawQuery;
|
||||||
|
DrawData *drawData;
|
||||||
} Game;
|
} Game;
|
||||||
|
|
||||||
extern ecs_world_t *ECS;
|
extern ecs_world_t *ECS;
|
||||||
|
|||||||
38
game/main.c
38
game/main.c
@@ -1,5 +1,8 @@
|
|||||||
#include <rlImGui.h>
|
#include <rlImGui.h>
|
||||||
#include <raygui.h>
|
#include <raygui.h>
|
||||||
|
#include <time.h>
|
||||||
|
#include <raymath.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
#include "systems.h"
|
#include "systems.h"
|
||||||
#include "components.h"
|
#include "components.h"
|
||||||
@@ -15,8 +18,6 @@
|
|||||||
|
|
||||||
#include "pathfinding.h"
|
#include "pathfinding.h"
|
||||||
|
|
||||||
#include <time.h>
|
|
||||||
#include <raymath.h>
|
|
||||||
|
|
||||||
ECS_COMPONENT_DECLARE(Game);
|
ECS_COMPONENT_DECLARE(Game);
|
||||||
ECS_COMPONENT_DECLARE(InputState);
|
ECS_COMPONENT_DECLARE(InputState);
|
||||||
@@ -148,12 +149,11 @@ ECS_DTOR(Path, path, {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
int cmpPos(ecs_entity_t e1, const void *v1, ecs_entity_t e2, const void *v2) {
|
int cmpDrawData(const void *a, const void *b) {
|
||||||
const Position *pos1 = v1;
|
const DrawData *lhs = (DrawData *) a;
|
||||||
const Position *pos2 = v2;
|
const DrawData *rhs = (DrawData *) b;
|
||||||
|
|
||||||
// NOTE: entities with lower Y are in front
|
f32 dif = (rhs->dst.y) - (lhs->dst.y);
|
||||||
f32 dif = pos2->y - pos1->y;
|
|
||||||
int cmpVal = 0;
|
int cmpVal = 0;
|
||||||
if (dif < 0) cmpVal = 1;
|
if (dif < 0) cmpVal = 1;
|
||||||
else if (dif > 0) cmpVal = -1;
|
else if (dif > 0) cmpVal = -1;
|
||||||
@@ -179,6 +179,7 @@ bool init(void *userData) {
|
|||||||
Game *game = ecs_singleton_get_mut(ECS, Game);
|
Game *game = ecs_singleton_get_mut(ECS, Game);
|
||||||
game->screen = SCREEN_MAIN_MENU;
|
game->screen = 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) },
|
||||||
@@ -186,8 +187,6 @@ bool init(void *userData) {
|
|||||||
{ ecs_id(Rotation) },
|
{ ecs_id(Rotation) },
|
||||||
{ ecs_id(TextureRegion) }
|
{ ecs_id(TextureRegion) }
|
||||||
},
|
},
|
||||||
.order_by_component = ecs_id(Position),
|
|
||||||
.order_by = cmpPos
|
|
||||||
});
|
});
|
||||||
|
|
||||||
ECS_COMPONENT_DEFINE(ECS, InputState);
|
ECS_COMPONENT_DEFINE(ECS, InputState);
|
||||||
@@ -250,10 +249,6 @@ bool init(void *userData) {
|
|||||||
|
|
||||||
ECS_SYSTEM(ECS, renderDebugPath, EcsOnUpdate, Path);
|
ECS_SYSTEM(ECS, renderDebugPath, EcsOnUpdate, Path);
|
||||||
|
|
||||||
//ECS_SYSTEM(ECS, renderTerrain, EcsOnUpdate, Position, Size, Rotation, TextureRegion, TextureTerrain);
|
|
||||||
//ECS_SYSTEM(ECS, renderBuildings, EcsOnUpdate, Position, Size, Rotation, TextureRegion, TextureBuildings);
|
|
||||||
//ECS_SYSTEM(ECS, renderEntities, EcsOnUpdate, Position, Size, Rotation, TextureRegion, TextureEntities);
|
|
||||||
|
|
||||||
ECS_SYSTEM(ECS, renderColliders, EcsOnUpdate, Position, Size);
|
ECS_SYSTEM(ECS, renderColliders, EcsOnUpdate, Position, Size);
|
||||||
ECS_SYSTEM(ECS, renderRotationDirection, EcsOnUpdate, Position, Rotation);
|
ECS_SYSTEM(ECS, renderRotationDirection, EcsOnUpdate, Position, Rotation);
|
||||||
|
|
||||||
@@ -294,6 +289,8 @@ void deinit(void *userData) {
|
|||||||
bzObjectPoolDestroy(game->pools.pathData);
|
bzObjectPoolDestroy(game->pools.pathData);
|
||||||
bzObjectPoolDestroy(game->pools.actions);
|
bzObjectPoolDestroy(game->pools.actions);
|
||||||
|
|
||||||
|
bzArrayDestroy(game->drawData);
|
||||||
|
|
||||||
bzUIDestroy(UI);
|
bzUIDestroy(UI);
|
||||||
UI = NULL;
|
UI = NULL;
|
||||||
|
|
||||||
@@ -342,6 +339,7 @@ static void renderGame(Game *game, float dt) {
|
|||||||
// Ground UI
|
// Ground UI
|
||||||
drawPlayerInputUIGround();
|
drawPlayerInputUIGround();
|
||||||
// Entities
|
// Entities
|
||||||
|
bzArrayClear(game->drawData);
|
||||||
ecs_iter_t it = ecs_query_iter(ECS, game->drawQuery);
|
ecs_iter_t it = ecs_query_iter(ECS, game->drawQuery);
|
||||||
while (ecs_iter_next(&it)) {
|
while (ecs_iter_next(&it)) {
|
||||||
Position *p = ecs_field(&it, Position, 1);
|
Position *p = ecs_field(&it, Position, 1);
|
||||||
@@ -354,9 +352,21 @@ static void renderGame(Game *game, float dt) {
|
|||||||
Rectangle src = t[i].rec;
|
Rectangle src = t[i].rec;
|
||||||
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;
|
||||||
DrawTexturePro(t[i].texture, src, dst, origin, t[i].rotation, WHITE);
|
bzArrayPush(game->drawData, (DrawData) {
|
||||||
|
.tex = t[i].texture,
|
||||||
|
.src = src,
|
||||||
|
.dst = dst,
|
||||||
|
.origin = origin,
|
||||||
|
.rotation = t[i].rotation
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
qsort(game->drawData, bzArraySize(game->drawData), sizeof(*game->drawData), cmpDrawData);
|
||||||
|
|
||||||
|
for (i32 i = 0; i < bzArraySize(game->drawData); i++) {
|
||||||
|
DrawData draw = game->drawData[i];
|
||||||
|
DrawTexturePro(draw.tex, draw.src, draw.dst, draw.origin, draw.rotation, WHITE);
|
||||||
|
}
|
||||||
|
|
||||||
ecs_progress(ECS, dt);
|
ecs_progress(ECS, dt);
|
||||||
ecs_enable(ECS, renderDebugPathSystem, game->debugDraw.path);
|
ecs_enable(ECS, renderDebugPathSystem, game->debugDraw.path);
|
||||||
|
|||||||
Reference in New Issue
Block a user