Separate input component
This commit is contained in:
parent
29e7b5e499
commit
705f498daa
9 changed files with 318 additions and 178 deletions
126
main.odin
126
main.odin
|
|
@ -28,15 +28,14 @@ Vec4 :: [4]f32
|
|||
|
||||
Globals :: struct {
|
||||
coordinator: ecs.Coordinator,
|
||||
input_system: ^ecs.InputSystem,
|
||||
physics_system: ^ecs.PhysicsSystem,
|
||||
camera_system: ^ecs.CameraSystem,
|
||||
render_system: ^ecs.RenderSystem,
|
||||
entities: []ecs.EntityID,
|
||||
}
|
||||
g: ^Globals
|
||||
|
||||
mouse_move: Vec2
|
||||
key_down: #sparse[sa.Keycode]bool
|
||||
|
||||
default_context: runtime.Context
|
||||
|
||||
main :: proc() {
|
||||
|
|
@ -50,7 +49,7 @@ main :: proc() {
|
|||
default_context = context
|
||||
|
||||
sa.run({
|
||||
window_title = "Ecs Test",
|
||||
window_title = "ECS",
|
||||
|
||||
allocator = sa.Allocator(sh.allocator(&default_context)),
|
||||
logger = sa.Logger(sh.logger(&default_context)),
|
||||
|
|
@ -77,23 +76,19 @@ init_cb :: proc "c" () {
|
|||
frame_cb:: proc "c" () {
|
||||
context = default_context
|
||||
|
||||
if key_down[.ESCAPE] {
|
||||
sa.quit()
|
||||
return
|
||||
}
|
||||
|
||||
dt := f32(sa.frame_duration())
|
||||
|
||||
ecs.physics_system_update(g.physics_system, &g.coordinator, dt)
|
||||
ecs.render_system_update(g.render_system, &g.coordinator, dt, key_down, mouse_move)
|
||||
ecs.camera_system_update(g.camera_system, &g.coordinator, dt)
|
||||
ecs.render_system_update(g.render_system, &g.coordinator, dt)
|
||||
|
||||
mouse_move = {}
|
||||
ecs.input_system_mouse_reset(g.input_system, &g.coordinator)
|
||||
}
|
||||
|
||||
cleanup_cb :: proc "c" () {
|
||||
context = default_context
|
||||
|
||||
ecs.delete_render_system(g.render_system)
|
||||
ecs.render_system_delete(g.render_system, &g.coordinator)
|
||||
|
||||
delete(g.entities)
|
||||
ecs.coordinator_destroy(&g.coordinator)
|
||||
|
|
@ -103,61 +98,94 @@ cleanup_cb :: proc "c" () {
|
|||
event_cb :: proc "c" (event: ^sa.Event) {
|
||||
context = default_context
|
||||
|
||||
#partial switch event.type {
|
||||
case .MOUSE_MOVE:
|
||||
mouse_move += {event.mouse_dx, event.mouse_dy}
|
||||
case .KEY_DOWN:
|
||||
key_down[event.key_code] = true
|
||||
case .KEY_UP:
|
||||
key_down[event.key_code] = false
|
||||
}
|
||||
ecs.input_system_update(g.input_system, &g.coordinator, event)
|
||||
}
|
||||
|
||||
create_scene :: proc() {
|
||||
ecs.coordinator_register_component(ecs.Gravity, &g.coordinator)
|
||||
ecs.coordinator_register_component(ecs.RigidBody, &g.coordinator)
|
||||
ecs.coordinator_register_component(ecs.Transform, &g.coordinator)
|
||||
ecs.coordinator_register_component(ecs.Color, &g.coordinator)
|
||||
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)
|
||||
|
||||
g.physics_system = ecs.coordinator_register_system(ecs.PhysicsSystem, &g.coordinator)
|
||||
g.render_system = ecs.coordinator_register_system(ecs.RenderSystem, &g.coordinator)
|
||||
ecs.init_render_system(g.render_system)
|
||||
|
||||
signature := ecs.signature_create()
|
||||
ecs.signature_set(&signature, ecs.coordinator_get_component_type(ecs.Gravity, &g.coordinator))
|
||||
ecs.signature_set(&signature, ecs.coordinator_get_component_type(ecs.RigidBody, &g.coordinator))
|
||||
ecs.signature_set(&signature, ecs.coordinator_get_component_type(ecs.Transform, &g.coordinator))
|
||||
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)
|
||||
|
||||
signature = ecs.signature_create()
|
||||
ecs.signature_set(&signature, ecs.coordinator_get_component_type(ecs.Transform, &g.coordinator))
|
||||
ecs.signature_set(&signature, ecs.coordinator_get_component_type(ecs.Color, &g.coordinator))
|
||||
g.render_system = ecs.coordinator_register_system(ecs.RenderSystem, &g.coordinator)
|
||||
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)
|
||||
|
||||
g.entities = make([]ecs.EntityID, ecs.ENTITY_MAX)
|
||||
g.camera_system = ecs.coordinator_register_system(ecs.CameraSystem, &g.coordinator)
|
||||
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)
|
||||
|
||||
g.input_system = ecs.coordinator_register_system(ecs.InputSystem, &g.coordinator)
|
||||
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)
|
||||
|
||||
camera_entity := ecs.coordinator_create_entity(&g.coordinator)
|
||||
ecs.coordinator_add_component(
|
||||
ecs.CameraComponent,
|
||||
&g.coordinator,
|
||||
camera_entity,
|
||||
ecs.CameraComponent{
|
||||
position = { 30, 0, 60 },
|
||||
target = { 0, 0, 1 },
|
||||
look = { 0, 0 },
|
||||
|
||||
movement_speed = 3.0,
|
||||
look_sensitivity = 0.15,
|
||||
})
|
||||
|
||||
ecs.coordinator_add_component(
|
||||
ecs.InputComponent,
|
||||
&g.coordinator,
|
||||
camera_entity,
|
||||
ecs.InputComponent{
|
||||
key_down = {},
|
||||
mouse_movement = Vec2{ 0, 0 }
|
||||
})
|
||||
|
||||
ecs.render_system_init(g.render_system, camera_entity)
|
||||
|
||||
g.entities = make([]ecs.EntityID, 1000)
|
||||
for &entity in g.entities {
|
||||
entity = ecs.coordinator_create_entity(&g.coordinator)
|
||||
|
||||
ecs.coordinator_add_component(
|
||||
ecs.Gravity,
|
||||
ecs.GravityComponent,
|
||||
&g.coordinator,
|
||||
entity,
|
||||
ecs.Gravity{
|
||||
ecs.GravityComponent{
|
||||
ecs.Vec3{0.0, -2.82, 0.0}
|
||||
})
|
||||
ecs.coordinator_add_component(
|
||||
ecs.RigidBody,
|
||||
ecs.RigidBodyComponent,
|
||||
&g.coordinator,
|
||||
entity,
|
||||
ecs.RigidBody{
|
||||
ecs.RigidBodyComponent{
|
||||
velocity = ecs.Vec3{0.0, 0.0, 0.0},
|
||||
acceleration = ecs.Vec3{0.0, 0.0, 0.0},
|
||||
})
|
||||
ecs.coordinator_add_component(
|
||||
ecs.Transform,
|
||||
ecs.TransformComponent,
|
||||
&g.coordinator,
|
||||
entity,
|
||||
ecs.Transform{
|
||||
ecs.TransformComponent{
|
||||
position = Vec3{
|
||||
f32(rand.int_max(50)),
|
||||
f32(rand.int_max(10)),
|
||||
|
|
@ -167,10 +195,10 @@ create_scene :: proc() {
|
|||
scale = ecs.Vec3{1.0, 1.0, 1.0},
|
||||
})
|
||||
ecs.coordinator_add_component(
|
||||
ecs.Color,
|
||||
ecs.ColorComponent,
|
||||
&g.coordinator,
|
||||
entity,
|
||||
ecs.Color{
|
||||
ecs.ColorComponent{
|
||||
color = Vec4{
|
||||
rand.float32_uniform(0,1),
|
||||
rand.float32_uniform(0,1),
|
||||
|
|
@ -178,6 +206,20 @@ create_scene :: proc() {
|
|||
rand.float32_uniform(0,1),
|
||||
},
|
||||
})
|
||||
ecs.coordinator_add_component(
|
||||
ecs.MeshComponent,
|
||||
&g.coordinator,
|
||||
entity,
|
||||
ecs.MeshComponent{
|
||||
mesh_id = .CubeMesh
|
||||
})
|
||||
ecs.coordinator_add_component(
|
||||
ecs.MaterialComponent,
|
||||
&g.coordinator,
|
||||
entity,
|
||||
ecs.MaterialComponent{
|
||||
material_id = .GridTexture
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue