Skip to content

Commit

Permalink
Some more clean up on the road to less code
Browse files Browse the repository at this point in the history
  • Loading branch information
Honeybunch committed Dec 31, 2023
1 parent 4fcd2ae commit e23b611
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 34 deletions.
3 changes: 3 additions & 0 deletions include/cameracomponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#include "simd.h"

#include <flecs.h>

typedef struct TbWorld TbWorld;
typedef uint32_t TbViewId;

Expand All @@ -16,5 +18,6 @@ typedef struct TbCameraComponent {
float width;
float height;
} TbCameraComponent;
extern ECS_COMPONENT_DECLARE(TbCameraComponent);

void tb_register_camera_component(TbWorld *world);
29 changes: 22 additions & 7 deletions source/cameracomponent.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include "viewsystem.h"
#include "world.h"

#include <flecs.h>
ECS_COMPONENT_DECLARE(TbCameraComponent);

bool create_camera_component(ecs_world_t *ecs, ecs_entity_t e,
const char *source_path, const cgltf_node *node,
Expand All @@ -19,7 +19,6 @@ bool create_camera_component(ecs_world_t *ecs, ecs_entity_t e,
(void)extra;

ECS_COMPONENT(ecs, TbViewSystem);
ECS_COMPONENT(ecs, TbCameraComponent);

bool ret = true;
if (node->camera) {
Expand All @@ -34,10 +33,8 @@ bool create_camera_component(ecs_world_t *ecs, ecs_entity_t e,
.fov = persp->yfov,
.near = persp->znear,
.far = persp->zfar,
.width =
(float)view_sys->rnd_sys->render_thread->swapchain.width,
.height =
(float)view_sys->rnd_sys->render_thread->swapchain.height,
.width = (float)view_sys->rnd_sys->render_thread->swapchain.width,
.height = (float)view_sys->rnd_sys->render_thread->swapchain.height,
};
ecs_set_ptr(ecs, e, TbCameraComponent, &comp);
} else {
Expand All @@ -51,7 +48,6 @@ bool create_camera_component(ecs_world_t *ecs, ecs_entity_t e,

void destroy_camera_components(ecs_world_t *ecs) {
ECS_COMPONENT(ecs, TbViewSystem);
ECS_COMPONENT(ecs, TbCameraComponent);

// Remove camera component from entities
ecs_filter_t *filter =
Expand All @@ -76,8 +72,27 @@ void destroy_camera_components(ecs_world_t *ecs) {
void tb_register_camera_component(TbWorld *world) {
ecs_world_t *ecs = world->ecs;
ECS_COMPONENT(ecs, TbAssetSystem);
ECS_COMPONENT_DEFINE(ecs, TbCameraComponent);
ECS_TAG(ecs, TbCameraSystem);

// Metadata for transform component
{
ecs_struct(ecs,
{
.entity = ecs_id(TbCameraComponent),
.members =
{
{.name = "view_id", .type = ecs_id(ecs_i32_t)},
{.name = "aspect_ratio", .type = ecs_id(ecs_f32_t)},
{.name = "fov", .type = ecs_id(ecs_f32_t)},
{.name = "near", .type = ecs_id(ecs_f32_t)},
{.name = "far", .type = ecs_id(ecs_f32_t)},
{.name = "width", .type = ecs_id(ecs_f32_t)},
{.name = "height", .type = ecs_id(ecs_f32_t)},
},
});
}

// Add an asset system to handle loading cameras
TbAssetSystem asset = {
.add_fn = create_camera_component,
Expand Down
4 changes: 1 addition & 3 deletions source/camerasystem.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,12 @@ void camera_update_tick(ecs_iter_t *it) {

void tb_register_camera_sys(TbWorld *world) {
ecs_world_t *ecs = world->ecs;
ECS_COMPONENT(ecs, TbCameraComponent);

ECS_COMPONENT(ecs, TbTransformComponent);
ECS_COMPONENT(ecs, TbAssetSystem);

ECS_SYSTEM(ecs, camera_update_tick, EcsOnUpdate, TbCameraComponent,
TbTransformComponent);

tb_register_camera_component(world);
}

void tb_unregister_camera_sys(TbWorld *world) { (void)world; }
1 change: 0 additions & 1 deletion source/lightsystem.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ void tb_register_light_sys(TbWorld *world) {
ECS_COMPONENT(ecs, TbAssetSystem);
ECS_COMPONENT(ecs, TbDirectionalLightComponent);
ECS_COMPONENT(ecs, TbTransformComponent);
ECS_COMPONENT(ecs, TbCameraComponent);

TbLightSystem sys = {
.dir_light_query =
Expand Down
2 changes: 0 additions & 2 deletions source/meshsystem.c
Original file line number Diff line number Diff line change
Expand Up @@ -1166,7 +1166,6 @@ void mesh_draw_tick(ecs_iter_t *it) {
TracyCZoneNC(ctx, "Mesh Draw Tick", TracyCategoryColorRendering, true);
ecs_world_t *ecs = it->world;

ECS_COMPONENT(ecs, TbCameraComponent);
ECS_COMPONENT(ecs, TbMeshComponent);
ECS_COMPONENT(ecs, TbDirectionalLightComponent);
ECS_COMPONENT(ecs, TbMeshSystem);
Expand Down Expand Up @@ -1489,7 +1488,6 @@ void tb_register_mesh_sys(TbWorld *world) {
ECS_COMPONENT(ecs, TbMeshComponent);
ECS_COMPONENT(ecs, TbTransformComponent);
ECS_COMPONENT(ecs, TbDirectionalLightComponent);
ECS_COMPONENT(ecs, TbCameraComponent);
ECS_COMPONENT(ecs, TbMeshSystem);
ECS_COMPONENT(ecs, TbAssetSystem);

Expand Down
1 change: 0 additions & 1 deletion source/oceansystem.c
Original file line number Diff line number Diff line change
Expand Up @@ -963,7 +963,6 @@ void tb_register_ocean_sys(TbWorld *world) {
ECS_COMPONENT(ecs, TbAudioSystem);
ECS_COMPONENT(ecs, TbOceanSystem);
ECS_COMPONENT(ecs, TbOceanComponent);
ECS_COMPONENT(ecs, TbCameraComponent);

TbRenderSystem *rnd_sys = ecs_singleton_get_mut(ecs, TbRenderSystem);
TbRenderPipelineSystem *rp_sys =
Expand Down
2 changes: 0 additions & 2 deletions source/skysystem.c
Original file line number Diff line number Diff line change
Expand Up @@ -1005,7 +1005,6 @@ void sky_draw_tick(ecs_iter_t *it) {
ECS_COMPONENT(ecs, TbRenderSystem);
ECS_COMPONENT(ecs, TbRenderPipelineSystem);
ECS_COMPONENT(ecs, TbSkyComponent);
ECS_COMPONENT(ecs, TbCameraComponent);
ECS_COMPONENT(ecs, TbTransformComponent);

TbSkySystem *sky_sys = ecs_singleton_get_mut(ecs, TbSkySystem);
Expand Down Expand Up @@ -1287,7 +1286,6 @@ void tb_register_sky_sys(TbWorld *world) {
ECS_COMPONENT(ecs, TbAssetSystem);
ECS_COMPONENT(ecs, TbSkyComponent);
ECS_COMPONENT(ecs, TbDirectionalLightComponent);
ECS_COMPONENT(ecs, TbCameraComponent);
ECS_COMPONENT(ecs, TbTransformComponent);

TbRenderSystem *rnd_sys = ecs_singleton_get_mut(ecs, TbRenderSystem);
Expand Down
1 change: 0 additions & 1 deletion source/thirdpersoncomponents.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ void post_load_tp_movement(ecs_world_t *ecs, ecs_entity_t e) {
ECS_COMPONENT(ecs, TbThirdPersonMovementComponent);
ECS_TAG(ecs, TbThirdPersonCameraComponent);
ECS_COMPONENT(ecs, TbTransformComponent);
ECS_COMPONENT(ecs, TbCameraComponent);
ECS_COMPONENT(ecs, TbRigidbodyComponent);
tb_auto movement = ecs_get_mut(ecs, e, TbThirdPersonMovementComponent);
tb_auto trans = ecs_get(ecs, e, TbTransformComponent);
Expand Down
27 changes: 10 additions & 17 deletions source/visualloggingsystem.c
Original file line number Diff line number Diff line change
Expand Up @@ -339,29 +339,25 @@ TbVisualLoggingSystem create_visual_logging_system(
view_sys->set_layout,
},
};
err = tb_rnd_create_pipeline_layout(rnd_sys, &create_info,
"Primitive Pipeline Layout",
&sys.pipe_layout);
err = tb_rnd_create_pipeline_layout(
rnd_sys, &create_info, "Primitive Pipeline Layout", &sys.pipe_layout);
TB_VK_CHECK(err, "Failed to create primitive pipeline layout");
}

{
uint32_t attach_count = 0;
VkFormat color_format = VK_FORMAT_UNDEFINED;
VkFormat depth_format = VK_FORMAT_UNDEFINED;
tb_render_pipeline_get_attachments(
rp_sys, rp_sys->transparent_color_pass,
&attach_count, NULL);
tb_render_pipeline_get_attachments(rp_sys, rp_sys->transparent_color_pass,
&attach_count, NULL);
TB_CHECK(attach_count == 2, "Unexpected");
TbPassAttachment attach_info[2] = {0};
tb_render_pipeline_get_attachments(
rp_sys, rp_sys->transparent_color_pass,
&attach_count, attach_info);
tb_render_pipeline_get_attachments(rp_sys, rp_sys->transparent_color_pass,
&attach_count, attach_info);

for (uint32_t attach_idx = 0; attach_idx < attach_count; ++attach_idx) {
VkFormat format =
tb_render_target_get_format(rp_sys->rt_sys,
attach_info[attach_idx].attachment);
VkFormat format = tb_render_target_get_format(
rp_sys->rt_sys, attach_info[attach_idx].attachment);
if (format == VK_FORMAT_D32_SFLOAT) {
depth_format = format;
} else {
Expand All @@ -380,8 +376,7 @@ TbVisualLoggingSystem create_visual_logging_system(
.draw_fn = vlog_draw_record,
.pass_id = rp_sys->transparent_color_pass,
};
sys.draw_ctx =
tb_render_pipeline_register_draw_context(rp_sys, &desc);
sys.draw_ctx = tb_render_pipeline_register_draw_context(rp_sys, &desc);
}

return sys;
Expand Down Expand Up @@ -452,8 +447,7 @@ void vlog_draw_tick(ecs_iter_t *it) {
((VLogShape *)batch->draws)[i].location = frame->loc_draws[i];
}

tb_render_pipeline_issue_draw_batch(sys->rp_sys,
sys->draw_ctx, 1, batch);
tb_render_pipeline_issue_draw_batch(sys->rp_sys, sys->draw_ctx, 1, batch);
}
}

Expand Down Expand Up @@ -538,7 +532,6 @@ void tb_register_visual_logging_sys(TbWorld *world) {
ECS_COMPONENT(ecs, TbRenderPipelineSystem);
ECS_COMPONENT(ecs, TbMeshSystem);
ECS_COMPONENT(ecs, TbCoreUISystem);
ECS_COMPONENT(ecs, TbCameraComponent);
ECS_COMPONENT(ecs, TbVisualLoggingSystem);

TbRenderSystem *rnd_sys = ecs_singleton_get_mut(ecs, TbRenderSystem);
Expand Down
2 changes: 2 additions & 0 deletions source/world.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ ECS_COMPONENT_DECLARE(TbTransformComponent);

TbCreateWorldSystemsFn tb_create_default_world =
^(TbWorld *world, TbRenderThread *thread, SDL_Window *window) {
tb_register_camera_component(world);

tb_register_physics_sys(world);
tb_register_light_sys(world);
tb_register_audio_sys(world);
Expand Down

0 comments on commit e23b611

Please sign in to comment.