Option to enable/disable debug drawing
This commit is contained in:
@@ -40,6 +40,12 @@ typedef struct Game {
|
|||||||
struct {
|
struct {
|
||||||
BzObjectPool *pathData;
|
BzObjectPool *pathData;
|
||||||
} pools;
|
} pools;
|
||||||
|
struct {
|
||||||
|
bool path;
|
||||||
|
bool entityColliders;
|
||||||
|
bool mapColliders;
|
||||||
|
bool spatialGrid;
|
||||||
|
} debugDraw;
|
||||||
f32 elapsed;
|
f32 elapsed;
|
||||||
} Game;
|
} Game;
|
||||||
|
|
||||||
|
|||||||
29
game/main.c
29
game/main.c
@@ -10,10 +10,15 @@
|
|||||||
|
|
||||||
#include "pathfinding.h"
|
#include "pathfinding.h"
|
||||||
|
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
ECS_COMPONENT_DECLARE(Game);
|
ECS_COMPONENT_DECLARE(Game);
|
||||||
|
|
||||||
ecs_world_t *ECS = NULL;
|
ecs_world_t *ECS = NULL;
|
||||||
|
|
||||||
|
static ecs_entity_t renderCollidersSystem;
|
||||||
|
static ecs_entity_t renderDebugPathSystem;
|
||||||
|
|
||||||
bool init(void *userData);
|
bool init(void *userData);
|
||||||
void deinit(void *userData);
|
void deinit(void *userData);
|
||||||
|
|
||||||
@@ -132,6 +137,9 @@ bool init(void *userData) {
|
|||||||
|
|
||||||
ECS_SYSTEM(ECS, renderColliders, EcsOnUpdate, Position, Size);
|
ECS_SYSTEM(ECS, renderColliders, EcsOnUpdate, Position, Size);
|
||||||
|
|
||||||
|
renderDebugPathSystem = renderDebugPath;
|
||||||
|
renderCollidersSystem = renderColliders;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
void deinit(void *userData) {
|
void deinit(void *userData) {
|
||||||
@@ -229,7 +237,6 @@ void render(float dt, void *userData) {
|
|||||||
BeginMode2D(game->camera);
|
BeginMode2D(game->camera);
|
||||||
|
|
||||||
bzTileMapDraw(&game->map);
|
bzTileMapDraw(&game->map);
|
||||||
bzTileMapDrawColliders(&game->map);
|
|
||||||
|
|
||||||
if (game->input.building) {
|
if (game->input.building) {
|
||||||
Color placeColor = game->input.buildingCanPlace ?
|
Color placeColor = game->input.buildingCanPlace ?
|
||||||
@@ -244,8 +251,6 @@ void render(float dt, void *userData) {
|
|||||||
game->input.buildingSize.sizeY * height, placeColor);
|
game->input.buildingSize.sizeY * height, placeColor);
|
||||||
}
|
}
|
||||||
|
|
||||||
bzSpatialGridDrawDebugGrid(game->entityGrid);
|
|
||||||
|
|
||||||
|
|
||||||
Vector2 worldPos = GetScreenToWorld2D(GetMousePosition(), game->camera);
|
Vector2 worldPos = GetScreenToWorld2D(GetMousePosition(), game->camera);
|
||||||
int tileX = (int) worldPos.x / 16;
|
int tileX = (int) worldPos.x / 16;
|
||||||
@@ -257,6 +262,7 @@ void render(float dt, void *userData) {
|
|||||||
heap = bzHeapCreate(PathNode, game->map.width * game->map.height);
|
heap = bzHeapCreate(PathNode, game->map.width * game->map.height);
|
||||||
if (!ecs_has(ECS, game->entity, Path) && IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) {
|
if (!ecs_has(ECS, game->entity, Path) && IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) {
|
||||||
Path path = {};
|
Path path = {};
|
||||||
|
clock_t begin = clock();
|
||||||
const Position *start = ecs_get(ECS, game->entity, Position);
|
const Position *start = ecs_get(ECS, game->entity, Position);
|
||||||
findPath(&(PathfindingDesc) {
|
findPath(&(PathfindingDesc) {
|
||||||
.start=(TilePosition) {
|
.start=(TilePosition) {
|
||||||
@@ -272,9 +278,19 @@ void render(float dt, void *userData) {
|
|||||||
if (path.paths) {
|
if (path.paths) {
|
||||||
ecs_set_ptr(ECS, game->entity, Path, &path);
|
ecs_set_ptr(ECS, game->entity, Path, &path);
|
||||||
}
|
}
|
||||||
|
clock_t end = clock();
|
||||||
|
double timeSpent = (double) (end - begin) / CLOCKS_PER_SEC;
|
||||||
|
timeSpent *= 1000;
|
||||||
|
bzLogInfo("A* took: %.3fms", timeSpent);
|
||||||
}
|
}
|
||||||
|
|
||||||
ecs_progress(ECS, dt);
|
ecs_progress(ECS, dt);
|
||||||
|
ecs_enable(ECS, renderDebugPathSystem, game->debugDraw.path);
|
||||||
|
ecs_enable(ECS, renderCollidersSystem, game->debugDraw.entityColliders);
|
||||||
|
if (game->debugDraw.mapColliders)
|
||||||
|
bzTileMapDrawColliders(&game->map);
|
||||||
|
if (game->debugDraw.spatialGrid)
|
||||||
|
bzSpatialGridDrawDebugGrid(game->entityGrid);
|
||||||
|
|
||||||
EndMode2D();
|
EndMode2D();
|
||||||
}
|
}
|
||||||
@@ -303,6 +319,13 @@ void imguiRender(float dt, void *userData) {
|
|||||||
if (game->input.building)
|
if (game->input.building)
|
||||||
game->input.state = INPUT_PLACING;
|
game->input.state = INPUT_PLACING;
|
||||||
|
|
||||||
|
}
|
||||||
|
if (igCollapsingHeader_TreeNodeFlags("DebugDraw", 0)) {
|
||||||
|
igCheckbox("map colliders", &game->debugDraw.mapColliders);
|
||||||
|
igCheckbox("entity colliders", &game->debugDraw.entityColliders);
|
||||||
|
igCheckbox("spatial grid", &game->debugDraw.spatialGrid);
|
||||||
|
igCheckbox("path", &game->debugDraw.path);
|
||||||
|
|
||||||
}
|
}
|
||||||
if (igCollapsingHeader_TreeNodeFlags("Entities", 0)) {
|
if (igCollapsingHeader_TreeNodeFlags("Entities", 0)) {
|
||||||
igSliderFloat("Frame duration", &game->frameDuration, 0.0f, 1.0f, NULL, 0);
|
igSliderFloat("Frame duration", &game->frameDuration, 0.0f, 1.0f, NULL, 0);
|
||||||
|
|||||||
@@ -173,9 +173,9 @@ void renderDebugPath(ecs_iter_t *it) {
|
|||||||
while (pathData) {
|
while (pathData) {
|
||||||
for (i32 iPath = 0; iPath < pathData->numWaypoints; iPath++) {
|
for (i32 iPath = 0; iPath < pathData->numWaypoints; iPath++) {
|
||||||
Color color = RED;
|
Color color = RED;
|
||||||
if (first && iPath < path[i].curWaypoint - 1)
|
if (first && iPath < path[i].curWaypoint)
|
||||||
color = GREEN;
|
color = GREEN;
|
||||||
else if (first && iPath == path[i].curWaypoint - 1)
|
else if (first && iPath == path[i].curWaypoint)
|
||||||
color = ORANGE;
|
color = ORANGE;
|
||||||
color.a = 180;
|
color.a = 180;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user