Standardise and make ECS more robust

This commit is contained in:
Hugo Mårdbrink 2025-08-28 14:31:55 +02:00
parent b9aaeb62c9
commit 2ba1022f79
13 changed files with 245 additions and 232 deletions

View file

@ -43,14 +43,14 @@ Material :: struct {
}
RenderSystem :: struct {
using base: SystemBase,
camera_entity: EntityID,
using _: SystemBase,
user_entity: EntityID,
materials: map[MaterialID]Material,
meshes : map[MeshID]Mesh,
}
@(private)
@(private="file")
init_outline_material :: proc(render_system: ^RenderSystem) {
shader := sg.make_shader(shaders.outline_shader_desc(sg.query_backend()))
pipeline := sg.make_pipeline({
@ -79,7 +79,7 @@ init_outline_material :: proc(render_system: ^RenderSystem) {
}
}
@(private)
@(private="file")
init_cube_material :: proc(render_system: ^RenderSystem) {
shader := sg.make_shader(shaders.cube_shader_desc(sg.query_backend()))
pipeline := sg.make_pipeline({
@ -108,7 +108,7 @@ init_cube_material :: proc(render_system: ^RenderSystem) {
}
}
@(private)
@(private="file")
init_cube_mesh :: proc (render_system: ^RenderSystem) {
cube_vertices := []CubeVertexData {
{ pos = { -0.5, -0.5, 0.5 } },
@ -171,7 +171,7 @@ init_cube_mesh :: proc (render_system: ^RenderSystem) {
}
}
render_system_init :: proc(render_system: ^RenderSystem, camera_entity: EntityID) {
render_system_init :: proc(render_system: ^RenderSystem, user_entity: EntityID) {
default_context := runtime.default_context()
sg.setup({
@ -182,7 +182,7 @@ render_system_init :: proc(render_system: ^RenderSystem, camera_entity: EntityID
render_system.materials = make(map[MaterialID]Material)
render_system.meshes = make(map[MeshID]Mesh)
render_system.camera_entity = camera_entity
render_system.user_entity = user_entity
init_cube_mesh(render_system)
init_cube_material(render_system)
@ -209,7 +209,7 @@ render_system_delete :: proc(render_system: ^RenderSystem, coordinator: ^Coordin
sg.shutdown()
}
@(private)
@(private="file")
CLEAR_SCREEN_ACTION :: sg.Pass_Action{
colors = {
0 = {
@ -219,10 +219,11 @@ CLEAR_SCREEN_ACTION :: sg.Pass_Action{
}
}
@(private="file")
outline_pass :: proc(render_system: ^RenderSystem, coordinator: ^Coordinator, proj: Mat4, view: Mat4) {
for entity in render_system.entities {
transform := coordinator_get_component(TransformComponent, coordinator, entity)
mesh_id := coordinator_get_component(MeshComponent, coordinator, entity).mesh_id
for entity_id in render_system.entities {
transform := coordinator_get_component(coordinator, TransformComponent, entity_id)
mesh_id := coordinator_get_component(coordinator, MeshComponent, entity_id).mesh_id
mesh := render_system.meshes[mesh_id]
scale_mat := linalg.matrix4_scale_f32(transform.scale * 1.2)
@ -257,17 +258,18 @@ outline_pass :: proc(render_system: ^RenderSystem, coordinator: ^Coordinator, pr
}
@(private="file")
cube_pass :: proc(render_system: ^RenderSystem, coordinator: ^Coordinator, proj: Mat4, view: Mat4) {
for entity in render_system.entities {
transform := coordinator_get_component(TransformComponent, coordinator, entity)
for entity_id in render_system.entities {
transform := coordinator_get_component(coordinator, TransformComponent, entity_id)
mesh_id := coordinator_get_component(MeshComponent, coordinator, entity).mesh_id
mesh_id := coordinator_get_component(coordinator, MeshComponent, entity_id).mesh_id
mesh := render_system.meshes[mesh_id]
material_id := coordinator_get_component(MaterialComponent, coordinator, entity).material_id
material_id := coordinator_get_component(coordinator, MaterialComponent, entity_id).material_id
material := render_system.materials[material_id]
color := coordinator_get_component(ColorComponent, coordinator, entity)
color := coordinator_get_component(coordinator, ColorComponent, entity_id)
scale_mat := linalg.matrix4_scale_f32(transform.scale)
rot_mat := linalg.matrix4_from_yaw_pitch_roll_f32(
@ -295,7 +297,7 @@ cube_pass :: proc(render_system: ^RenderSystem, coordinator: ^Coordinator, proj:
}
render_system_update :: proc(render_system: ^RenderSystem, coordinator: ^Coordinator) {
camera := coordinator_get_component(CameraComponent, coordinator, render_system.camera_entity)
camera := coordinator_get_component(coordinator, CameraComponent, render_system.user_entity)
proj := linalg.matrix4_perspective_f32(70, sa.widthf() / sa.heightf(), 0.001, 1000)
view := linalg.matrix4_look_at_f32(camera.position, camera.target, { 0, 1, 0 } )
@ -308,11 +310,13 @@ render_system_update :: proc(render_system: ^RenderSystem, coordinator: ^Coordin
sg.commit()
}
@(private="file")
sg_range :: proc {
sg_range_from_slice,
sg_range_from_struct,
}
@(private="file")
sg_range_from_slice :: proc(s: []$T) -> sg.Range {
return {
ptr = raw_data(s),
@ -320,6 +324,7 @@ sg_range_from_slice :: proc(s: []$T) -> sg.Range {
}
}
@(private="file")
sg_range_from_struct :: proc(s: ^$T) -> sg.Range where intrinsics.type_is_struct(T) {
return {
ptr = s,