Standardise and make ECS more robust
This commit is contained in:
parent
b9aaeb62c9
commit
2ba1022f79
13 changed files with 245 additions and 232 deletions
130
main.odin
130
main.odin
|
|
@ -40,14 +40,13 @@ Globals :: struct {
|
|||
g: ^Globals
|
||||
|
||||
default_context: runtime.Context
|
||||
tracking_allocator: mem.Tracking_Allocator
|
||||
|
||||
main :: proc() {
|
||||
context.logger = log.create_console_logger()
|
||||
|
||||
tracking_allocator: mem.Tracking_Allocator
|
||||
mem.tracking_allocator_init(&tracking_allocator, context.allocator)
|
||||
context.allocator = mem.tracking_allocator(&tracking_allocator)
|
||||
defer reset_tracking_allocator(&tracking_allocator)
|
||||
|
||||
default_context = context
|
||||
|
||||
|
|
@ -71,7 +70,7 @@ init_cb :: proc "c" () {
|
|||
sa.lock_mouse(true)
|
||||
|
||||
g = new(Globals)
|
||||
g.coordinator = ecs.coordinator_create()
|
||||
ecs.coordinator_init(&g.coordinator)
|
||||
|
||||
create_scene()
|
||||
}
|
||||
|
|
@ -99,8 +98,10 @@ cleanup_cb :: proc "c" () {
|
|||
|
||||
ecs.render_system_delete(g.render_system, &g.coordinator)
|
||||
|
||||
ecs.coordinator_destroy(&g.coordinator)
|
||||
ecs.coordinator_delete(&g.coordinator)
|
||||
free(g)
|
||||
|
||||
reset_tracking_allocator(&tracking_allocator)
|
||||
}
|
||||
|
||||
event_cb :: proc "c" (event: ^sa.Event) {
|
||||
|
|
@ -113,24 +114,22 @@ create_cube :: proc() {
|
|||
entity := ecs.coordinator_create_entity(&g.coordinator)
|
||||
|
||||
ecs.coordinator_add_component(
|
||||
ecs.GravityComponent,
|
||||
&g.coordinator,
|
||||
entity,
|
||||
ecs.GravityComponent{
|
||||
ecs.Vec3{ 0.0, program_config.GRAVITY_CONSTANT, 0.0 }
|
||||
})
|
||||
},
|
||||
entity
|
||||
)
|
||||
ecs.coordinator_add_component(
|
||||
ecs.RigidBodyComponent,
|
||||
&g.coordinator,
|
||||
entity,
|
||||
ecs.RigidBodyComponent{
|
||||
velocity = ecs.Vec3{ 0.0, 0.0, 0.0 },
|
||||
acceleration = ecs.Vec3{ 0.0, 0.0, 0.0 },
|
||||
})
|
||||
},
|
||||
entity
|
||||
)
|
||||
ecs.coordinator_add_component(
|
||||
ecs.TransformComponent,
|
||||
&g.coordinator,
|
||||
entity,
|
||||
ecs.TransformComponent{
|
||||
position = Vec3{
|
||||
f32(rand.int_max(program_config.SPAWN_AREA)),
|
||||
|
|
@ -139,108 +138,115 @@ create_cube :: proc() {
|
|||
},
|
||||
rotation = ecs.Vec3{ 0.0, 0.0, 0.0 },
|
||||
scale = ecs.Vec3{ 1.0, 1.0, 1.0 },
|
||||
})
|
||||
|
||||
},
|
||||
entity
|
||||
)
|
||||
ecs.coordinator_add_component(
|
||||
ecs.ColorComponent,
|
||||
&g.coordinator,
|
||||
entity,
|
||||
ecs.ColorComponent{
|
||||
color = program_config.CUBE_COLORS[g.current_color]
|
||||
})
|
||||
},
|
||||
entity
|
||||
)
|
||||
g.current_color += 1
|
||||
g.current_color %= i32(len(program_config.CUBE_COLORS))
|
||||
|
||||
ecs.coordinator_add_component(
|
||||
ecs.MeshComponent,
|
||||
&g.coordinator,
|
||||
entity,
|
||||
ecs.MeshComponent{
|
||||
mesh_id = .Cube
|
||||
})
|
||||
},
|
||||
entity
|
||||
)
|
||||
ecs.coordinator_add_component(
|
||||
ecs.MaterialComponent,
|
||||
&g.coordinator,
|
||||
entity,
|
||||
ecs.MaterialComponent{
|
||||
material_id = .Cube
|
||||
})
|
||||
},
|
||||
entity
|
||||
)
|
||||
}
|
||||
|
||||
create_scene :: proc() {
|
||||
ecs.coordinator_register_component(ecs.GravityComponent, &g.coordinator)
|
||||
ecs.coordinator_register_component(ecs.RigidBodyComponent, &g.coordinator)
|
||||
ecs.coordinator_register_component(ecs.TransformComponent, &g.coordinator)
|
||||
ecs.coordinator_register_component(ecs.ColorComponent, &g.coordinator)
|
||||
ecs.coordinator_register_component(ecs.CameraComponent, &g.coordinator)
|
||||
ecs.coordinator_register_component(ecs.MaterialComponent, &g.coordinator)
|
||||
ecs.coordinator_register_component(ecs.MeshComponent, &g.coordinator)
|
||||
ecs.coordinator_register_component(ecs.InputComponent, &g.coordinator)
|
||||
coordinator := &g.coordinator
|
||||
|
||||
g.physics_system = ecs.coordinator_register_system(ecs.PhysicsSystem, &g.coordinator)
|
||||
signature := ecs.signature_create()
|
||||
ecs.signature_set(&signature, ecs.coordinator_get_component_type(ecs.GravityComponent, &g.coordinator))
|
||||
ecs.signature_set(&signature, ecs.coordinator_get_component_type(ecs.RigidBodyComponent, &g.coordinator))
|
||||
ecs.signature_set(&signature, ecs.coordinator_get_component_type(ecs.TransformComponent, &g.coordinator))
|
||||
ecs.coordinator_set_system_signature(ecs.PhysicsSystem, &g.coordinator, signature)
|
||||
ecs.coordinator_register_component(coordinator, ecs.GravityComponent)
|
||||
ecs.coordinator_register_component(coordinator, ecs.RigidBodyComponent)
|
||||
ecs.coordinator_register_component(coordinator, ecs.TransformComponent)
|
||||
ecs.coordinator_register_component(coordinator, ecs.ColorComponent)
|
||||
ecs.coordinator_register_component(coordinator, ecs.CameraComponent)
|
||||
ecs.coordinator_register_component(coordinator, ecs.MaterialComponent)
|
||||
ecs.coordinator_register_component(coordinator, ecs.MeshComponent)
|
||||
ecs.coordinator_register_component(coordinator, ecs.InputComponent)
|
||||
|
||||
g.render_system = ecs.coordinator_register_system(ecs.RenderSystem, &g.coordinator)
|
||||
g.physics_system = ecs.coordinator_register_system(coordinator, ecs.PhysicsSystem)
|
||||
signature := ecs.signature_make()
|
||||
ecs.signature_set(&signature, ecs.coordinator_get_component_id(coordinator, ecs.GravityComponent))
|
||||
ecs.signature_set(&signature, ecs.coordinator_get_component_id(coordinator, ecs.RigidBodyComponent))
|
||||
ecs.signature_set(&signature, ecs.coordinator_get_component_id(coordinator, ecs.TransformComponent))
|
||||
ecs.coordinator_set_system_signature(coordinator, ecs.PhysicsSystem, signature)
|
||||
|
||||
g.render_system = ecs.coordinator_register_system(coordinator, ecs.RenderSystem)
|
||||
ecs.signature_clear(&signature)
|
||||
ecs.signature_set(&signature, ecs.coordinator_get_component_type(ecs.TransformComponent, &g.coordinator))
|
||||
ecs.signature_set(&signature, ecs.coordinator_get_component_type(ecs.ColorComponent, &g.coordinator))
|
||||
ecs.signature_set(&signature, ecs.coordinator_get_component_type(ecs.MeshComponent, &g.coordinator))
|
||||
ecs.signature_set(&signature, ecs.coordinator_get_component_type(ecs.MaterialComponent, &g.coordinator))
|
||||
ecs.coordinator_set_system_signature(ecs.RenderSystem, &g.coordinator, signature)
|
||||
ecs.signature_set(&signature, ecs.coordinator_get_component_id(coordinator, ecs.TransformComponent))
|
||||
ecs.signature_set(&signature, ecs.coordinator_get_component_id(coordinator, ecs.ColorComponent))
|
||||
ecs.signature_set(&signature, ecs.coordinator_get_component_id(coordinator, ecs.MeshComponent))
|
||||
ecs.signature_set(&signature, ecs.coordinator_get_component_id(coordinator, ecs.MaterialComponent))
|
||||
ecs.coordinator_set_system_signature(coordinator, ecs.RenderSystem, signature)
|
||||
|
||||
g.camera_system = ecs.coordinator_register_system(ecs.CameraSystem, &g.coordinator)
|
||||
g.camera_system = ecs.coordinator_register_system(coordinator, ecs.CameraSystem)
|
||||
ecs.signature_clear(&signature)
|
||||
ecs.signature_set(&signature, ecs.coordinator_get_component_type(ecs.CameraComponent, &g.coordinator))
|
||||
ecs.signature_set(&signature, ecs.coordinator_get_component_type(ecs.InputComponent, &g.coordinator))
|
||||
ecs.coordinator_set_system_signature(ecs.CameraSystem, &g.coordinator, signature)
|
||||
ecs.signature_set(&signature, ecs.coordinator_get_component_id(coordinator, ecs.CameraComponent))
|
||||
ecs.signature_set(&signature, ecs.coordinator_get_component_id(coordinator, ecs.InputComponent))
|
||||
ecs.coordinator_set_system_signature(coordinator, ecs.CameraSystem, signature)
|
||||
|
||||
g.input_system = ecs.coordinator_register_system(ecs.InputSystem, &g.coordinator)
|
||||
g.input_system = ecs.coordinator_register_system(coordinator, ecs.InputSystem)
|
||||
ecs.signature_clear(&signature)
|
||||
ecs.signature_set(&signature, ecs.coordinator_get_component_type(ecs.InputComponent, &g.coordinator))
|
||||
ecs.coordinator_set_system_signature(ecs.InputSystem, &g.coordinator, signature)
|
||||
ecs.signature_set(&signature, ecs.coordinator_get_component_id(coordinator, ecs.InputComponent))
|
||||
ecs.coordinator_set_system_signature(coordinator, ecs.InputSystem, signature)
|
||||
|
||||
user_entity := ecs.coordinator_create_entity(&g.coordinator)
|
||||
user_entity := ecs.coordinator_create_entity(coordinator)
|
||||
ecs.coordinator_add_component(
|
||||
ecs.CameraComponent,
|
||||
&g.coordinator,
|
||||
user_entity,
|
||||
coordinator,
|
||||
ecs.CameraComponent{
|
||||
position = program_config.START_POSITION,
|
||||
target = { 0, 0, 1 },
|
||||
look = { 0, 0 },
|
||||
|
||||
movement_speed = program_config.MOVEMENT_SPEED,
|
||||
look_sensitivity = program_config.LOOK_SENSITIVITY,
|
||||
})
|
||||
},
|
||||
user_entity
|
||||
)
|
||||
|
||||
ecs.coordinator_add_component(
|
||||
ecs.InputComponent,
|
||||
&g.coordinator,
|
||||
user_entity,
|
||||
coordinator,
|
||||
ecs.InputComponent{
|
||||
key_down = {},
|
||||
mouse_movement = Vec2{ 0, 0 }
|
||||
})
|
||||
},
|
||||
user_entity
|
||||
)
|
||||
|
||||
ecs.render_system_init(g.render_system, user_entity)
|
||||
}
|
||||
|
||||
reset_tracking_allocator :: proc(track: ^mem.Tracking_Allocator) {
|
||||
clean := true
|
||||
if len(track.allocation_map) > 0 {
|
||||
fmt.eprintf("=== %v allocations not freed: ===\n", len(track.allocation_map))
|
||||
fmt.eprintfln("=== %v allocations not freed: ===", len(track.allocation_map))
|
||||
for _, entry in track.allocation_map {
|
||||
fmt.eprintf("- %v bytes @ %v\n", entry.size, entry.location)
|
||||
}
|
||||
clean = false
|
||||
}
|
||||
if len(track.bad_free_array) > 0 {
|
||||
fmt.eprintf("=== %v incorrect frees: ===\n", len(track.bad_free_array))
|
||||
fmt.eprintfln("=== %v incorrect frees: ===", len(track.bad_free_array))
|
||||
for entry in track.bad_free_array {
|
||||
fmt.eprintf("- %p @ %v\n", entry.memory, entry.location)
|
||||
}
|
||||
clean = false
|
||||
}
|
||||
if clean do fmt.printfln("=== No memory leaked ===")
|
||||
|
||||
mem.tracking_allocator_destroy(track)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue