Add raining effect of cubes
This commit is contained in:
parent
6d43f30c09
commit
1852cf3a7f
1 changed files with 92 additions and 87 deletions
179
main.odin
179
main.odin
|
|
@ -32,7 +32,8 @@ Globals :: struct {
|
|||
physics_system: ^ecs.PhysicsSystem,
|
||||
camera_system: ^ecs.CameraSystem,
|
||||
render_system: ^ecs.RenderSystem,
|
||||
entities: []ecs.EntityID,
|
||||
|
||||
time_since_cube: f32,
|
||||
}
|
||||
g: ^Globals
|
||||
|
||||
|
|
@ -41,15 +42,15 @@ default_context: runtime.Context
|
|||
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)
|
||||
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
|
||||
|
||||
sa.run({
|
||||
window_title = "ECS",
|
||||
window_title = "ECS Test",
|
||||
|
||||
allocator = sa.Allocator(sh.allocator(&default_context)),
|
||||
logger = sa.Logger(sh.logger(&default_context)),
|
||||
|
|
@ -63,7 +64,7 @@ main :: proc() {
|
|||
|
||||
init_cb :: proc "c" () {
|
||||
context = default_context
|
||||
|
||||
|
||||
sa.show_mouse(false)
|
||||
sa.lock_mouse(true)
|
||||
|
||||
|
|
@ -75,9 +76,15 @@ init_cb :: proc "c" () {
|
|||
|
||||
frame_cb:: proc "c" () {
|
||||
context = default_context
|
||||
|
||||
|
||||
dt := f32(sa.frame_duration())
|
||||
|
||||
|
||||
g.time_since_cube += dt
|
||||
if g.time_since_cube >= 0.2 {
|
||||
create_cube()
|
||||
g.time_since_cube = 0
|
||||
}
|
||||
|
||||
ecs.physics_system_update(g.physics_system, &g.coordinator, dt)
|
||||
ecs.camera_system_update(g.camera_system, &g.coordinator, dt)
|
||||
ecs.render_system_update(g.render_system, &g.coordinator, dt)
|
||||
|
|
@ -89,8 +96,7 @@ cleanup_cb :: proc "c" () {
|
|||
context = default_context
|
||||
|
||||
ecs.render_system_delete(g.render_system, &g.coordinator)
|
||||
|
||||
delete(g.entities)
|
||||
|
||||
ecs.coordinator_destroy(&g.coordinator)
|
||||
free(g)
|
||||
}
|
||||
|
|
@ -101,6 +107,65 @@ event_cb :: proc "c" (event: ^sa.Event) {
|
|||
ecs.input_system_update(g.input_system, &g.coordinator, event)
|
||||
}
|
||||
|
||||
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, -2.5, 0.0 }
|
||||
})
|
||||
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 },
|
||||
})
|
||||
ecs.coordinator_add_component(
|
||||
ecs.TransformComponent,
|
||||
&g.coordinator,
|
||||
entity,
|
||||
ecs.TransformComponent{
|
||||
position = Vec3{
|
||||
f32(rand.int_max(50)),
|
||||
10,
|
||||
f32(rand.int_max(50)),
|
||||
},
|
||||
rotation = ecs.Vec3{0.0, 0.0, 0.0},
|
||||
scale = ecs.Vec3{1.0, 1.0, 1.0},
|
||||
})
|
||||
ecs.coordinator_add_component(
|
||||
ecs.ColorComponent,
|
||||
&g.coordinator,
|
||||
entity,
|
||||
ecs.ColorComponent{
|
||||
color = Vec4{
|
||||
rand.float32_uniform(0,1),
|
||||
rand.float32_uniform(0,1),
|
||||
rand.float32_uniform(0,1),
|
||||
rand.float32_uniform(0,1),
|
||||
},
|
||||
})
|
||||
ecs.coordinator_add_component(
|
||||
ecs.MeshComponent,
|
||||
&g.coordinator,
|
||||
entity,
|
||||
ecs.MeshComponent{
|
||||
mesh_id = .Cube
|
||||
})
|
||||
ecs.coordinator_add_component(
|
||||
ecs.MaterialComponent,
|
||||
&g.coordinator,
|
||||
entity,
|
||||
ecs.MaterialComponent{
|
||||
material_id = .Grid
|
||||
})
|
||||
}
|
||||
|
||||
create_scene :: proc() {
|
||||
ecs.coordinator_register_component(ecs.GravityComponent, &g.coordinator)
|
||||
ecs.coordinator_register_component(ecs.RigidBodyComponent, &g.coordinator)
|
||||
|
|
@ -110,7 +175,7 @@ create_scene :: proc() {
|
|||
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)
|
||||
signature := ecs.signature_create()
|
||||
ecs.signature_set(&signature, ecs.coordinator_get_component_type(ecs.GravityComponent, &g.coordinator))
|
||||
|
|
@ -137,11 +202,11 @@ create_scene :: proc() {
|
|||
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)
|
||||
user_entity := ecs.coordinator_create_entity(&g.coordinator)
|
||||
ecs.coordinator_add_component(
|
||||
ecs.CameraComponent,
|
||||
&g.coordinator,
|
||||
camera_entity,
|
||||
user_entity,
|
||||
ecs.CameraComponent{
|
||||
position = { 30, 0, 60 },
|
||||
target = { 0, 0, 1 },
|
||||
|
|
@ -154,87 +219,27 @@ create_scene :: proc() {
|
|||
ecs.coordinator_add_component(
|
||||
ecs.InputComponent,
|
||||
&g.coordinator,
|
||||
camera_entity,
|
||||
user_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.GravityComponent,
|
||||
&g.coordinator,
|
||||
entity,
|
||||
ecs.GravityComponent{
|
||||
ecs.Vec3{0.0, -2.82, 0.0}
|
||||
})
|
||||
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},
|
||||
})
|
||||
ecs.coordinator_add_component(
|
||||
ecs.TransformComponent,
|
||||
&g.coordinator,
|
||||
entity,
|
||||
ecs.TransformComponent{
|
||||
position = Vec3{
|
||||
f32(rand.int_max(50)),
|
||||
f32(rand.int_max(10)),
|
||||
f32(rand.int_max(50)),
|
||||
},
|
||||
rotation = ecs.Vec3{0.0, 0.0, 0.0},
|
||||
scale = ecs.Vec3{1.0, 1.0, 1.0},
|
||||
})
|
||||
ecs.coordinator_add_component(
|
||||
ecs.ColorComponent,
|
||||
&g.coordinator,
|
||||
entity,
|
||||
ecs.ColorComponent{
|
||||
color = Vec4{
|
||||
rand.float32_uniform(0,1),
|
||||
rand.float32_uniform(0,1),
|
||||
rand.float32_uniform(0,1),
|
||||
rand.float32_uniform(0,1),
|
||||
},
|
||||
})
|
||||
ecs.coordinator_add_component(
|
||||
ecs.MeshComponent,
|
||||
&g.coordinator,
|
||||
entity,
|
||||
ecs.MeshComponent{
|
||||
mesh_id = .Cube
|
||||
})
|
||||
ecs.coordinator_add_component(
|
||||
ecs.MaterialComponent,
|
||||
&g.coordinator,
|
||||
entity,
|
||||
ecs.MaterialComponent{
|
||||
material_id = .Grid
|
||||
})
|
||||
}
|
||||
ecs.render_system_init(g.render_system, user_entity)
|
||||
}
|
||||
|
||||
reset_tracking_allocator :: proc(track: ^mem.Tracking_Allocator) {
|
||||
if len(track.allocation_map) > 0 {
|
||||
fmt.eprintf("=== %v allocations not freed: ===\n", len(track.allocation_map))
|
||||
for _, entry in track.allocation_map {
|
||||
fmt.eprintf("- %v bytes @ %v\n", entry.size, entry.location)
|
||||
}
|
||||
fmt.eprintf("=== %v allocations not freed: ===\n", len(track.allocation_map))
|
||||
for _, entry in track.allocation_map {
|
||||
fmt.eprintf("- %v bytes @ %v\n", entry.size, entry.location)
|
||||
}
|
||||
if len(track.bad_free_array) > 0 {
|
||||
fmt.eprintf("=== %v incorrect frees: ===\n", len(track.bad_free_array))
|
||||
for entry in track.bad_free_array {
|
||||
fmt.eprintf("- %p @ %v\n", entry.memory, entry.location)
|
||||
}
|
||||
}
|
||||
if len(track.bad_free_array) > 0 {
|
||||
fmt.eprintf("=== %v incorrect frees: ===\n", len(track.bad_free_array))
|
||||
for entry in track.bad_free_array {
|
||||
fmt.eprintf("- %p @ %v\n", entry.memory, entry.location)
|
||||
}
|
||||
mem.tracking_allocator_destroy(track)
|
||||
}
|
||||
mem.tracking_allocator_destroy(track)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue