Skip to content

Commit

Permalink
Merge pull request #3 from AndriBaal/feature_component_storage
Browse files Browse the repository at this point in the history
0.5.0
  • Loading branch information
AndriBaal authored Jun 16, 2023
2 parents 9cb224e + 1c4bb8b commit 44aa722
Show file tree
Hide file tree
Showing 20 changed files with 1,608 additions and 596 deletions.
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "shura"
version = "0.4.3"
version = "0.5.0"
edition = "2021"
repository = "https://github.com/AndriBaal/shura"
documentation = "https://docs.rs/shura"
Expand Down Expand Up @@ -62,7 +62,6 @@ naga = "0.12"
egui-wgpu = { version = "0.22", features = ["winit"], optional = true }
egui = { version = "0.22", default-features = false, optional = true, features = [
"bytemuck",
# "tracing",
"default_fonts",
"mint",
] }
Expand Down
11 changes: 6 additions & 5 deletions examples/bunnymark/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ impl ComponentController for Bunny {

let frame = ctx.frame.frame_time();
let fov = ctx.world_camera.fov();
for bunny in ctx.components.iter_mut::<Self>() {
ctx.components.for_each_mut::<Self>(|bunny| {
let mut linvel = bunny.linvel;
let mut translation = bunny.base.translation();

Expand All @@ -133,14 +133,15 @@ impl ComponentController for Bunny {
}
bunny.linvel = linvel;
bunny.base.set_translation(translation);
}
});
}

fn render(ctx: &Context, encoder: &mut RenderEncoder) {
let scene = ctx.scene_states.get::<BunnyState>();
encoder.render_all::<Self>(ctx, RenderConfig::WORLD, |r, instances| {
r.render_sprite(instances, &scene.bunny_model, &scene.bunny_sprite)
});
ctx.components
.render_all::<Self>(encoder, RenderConfig::WORLD, |r, instances| {
r.render_sprite(instances, &scene.bunny_model, &scene.bunny_sprite)
});
if let Some(screenshot) = &scene.screenshot {
encoder.copy_to_target(&ctx.defaults.world_target, &screenshot);
}
Expand Down
11 changes: 6 additions & 5 deletions examples/bunnymark_text/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ impl ComponentController for Bunny {

let frame = ctx.frame.frame_time();
let fov = ctx.world_camera.fov();
for bunny in ctx.components.iter_mut::<Self>() {
ctx.components.for_each_mut::<Self>(|bunny| {
let mut linvel = bunny.linvel;
let mut translation = bunny.base.translation();

Expand All @@ -124,14 +124,15 @@ impl ComponentController for Bunny {
}
bunny.linvel = linvel;
bunny.base.set_translation(translation);
}
});
}

fn render(ctx: &Context, encoder: &mut RenderEncoder) {
let scene = ctx.scene_states.get::<BunnyState>();
encoder.render_all::<Self>(ctx, RenderConfig::WORLD, |r, instances| {
r.render_sprite(instances, &scene.bunny_model, &scene.bunny_sprite);
});
ctx.components
.render_all::<Self>(encoder, RenderConfig::WORLD, |r, instances| {
r.render_sprite(instances, &scene.bunny_model, &scene.bunny_sprite);
});

scene.font.queue(
ctx.defaults,
Expand Down
45 changes: 31 additions & 14 deletions examples/flappy_bird/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,20 @@ impl Bird {
}

impl ComponentController for Bird {
const CONFIG: ComponentConfig = ComponentConfig {
storage: ComponentStorage::Single,
..ComponentConfig::DEFAULT
};

fn render(ctx: &Context, encoder: &mut RenderEncoder) {
encoder.render_each::<Self>(ctx, RenderConfig::default(), |r, bird, instance| {
let index = (ctx.frame.total_time() * 7.0 % 3.0) as usize;
r.render_sprite(instance, &bird.model, &bird.sprite[index])
});
ctx.components.render_each::<Self>(
encoder,
RenderConfig::default(),
|r, bird, instance| {
let index = (ctx.frame.total_time() * 7.0 % 3.0) as usize;
r.render_sprite(instance, &bird.model, &bird.sprite[index])
},
);
}

fn update(ctx: &mut Context) {
Expand Down Expand Up @@ -229,9 +238,11 @@ impl ComponentController for Ground {
..ComponentConfig::DEFAULT
};
fn render(ctx: &Context, encoder: &mut RenderEncoder) {
encoder.render_each::<Self>(ctx, RenderConfig::default(), |r, ground, instance| {
r.render_sprite(instance, &ground.model, &ground.sprite)
});
ctx.components.render_each::<Self>(
encoder,
RenderConfig::default(),
|r, ground, instance| r.render_sprite(instance, &ground.model, &ground.sprite),
);
}
}

Expand Down Expand Up @@ -260,12 +271,17 @@ impl ComponentController for Background {
const CONFIG: ComponentConfig = ComponentConfig {
priority: 1,
buffer: BufferOperation::Manual,
storage: ComponentStorage::Single,
..ComponentConfig::DEFAULT
};
fn render(ctx: &Context, encoder: &mut RenderEncoder) {
encoder.render_each::<Self>(ctx, RenderConfig::default(), |r, background, instance| {
r.render_sprite(instance, &background.model, &background.sprite)
});
ctx.components.render_each::<Self>(
encoder,
RenderConfig::default(),
|r, background, instance| {
r.render_sprite(instance, &background.model, &background.sprite)
},
);
}
}

Expand Down Expand Up @@ -335,9 +351,10 @@ impl ComponentController for Pipe {

fn render(ctx: &Context, encoder: &mut RenderEncoder) {
let scene = ctx.scene_states.get::<FlappyState>();
encoder.render_all::<Self>(ctx, RenderConfig::default(), |r, instances| {
r.render_sprite(instances.clone(), &scene.top_pipe_model, &scene.pipe_sprite);
r.render_sprite(instances, &scene.bottom_pipe_model, &scene.pipe_sprite);
});
ctx.components
.render_all::<Self>(encoder, RenderConfig::default(), |r, instances| {
r.render_sprite(instances.clone(), &scene.top_pipe_model, &scene.pipe_sprite);
r.render_sprite(instances, &scene.bottom_pipe_model, &scene.pipe_sprite);
});
}
}
46 changes: 27 additions & 19 deletions examples/flappy_bird_ai/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,7 @@ impl ComponentController for Bird {
let mut new_birds = Vec::with_capacity(amount);
for _ in 0..amount {
let index = gene_pool.sample(&mut rng);
let rand_bird = ctx
.components
.index_mut::<Bird>(GroupHandle::DEFAULT_GROUP, index)
.unwrap();
let rand_bird = ctx.components.index_mut::<Bird>(index).unwrap();

let mut new_bird = Bird::with_brain(&rand_bird);
new_bird.brain.mutate();
Expand Down Expand Up @@ -244,11 +241,15 @@ impl ComponentController for Bird {

fn render(ctx: &Context, encoder: &mut RenderEncoder) {
let scene = ctx.scene_states.get::<BirdSimulation>();
encoder.render_each::<Self>(ctx, RenderConfig::default(), |r, bird, instance| {
if !bird.dead {
r.render_sprite(instance, &scene.bird_model, &scene.bird_sprite)
}
});
ctx.components.render_each::<Self>(
encoder,
RenderConfig::default(),
|r, bird, instance| {
if !bird.dead {
r.render_sprite(instance, &scene.bird_model, &scene.bird_sprite)
}
},
);
}
}

Expand Down Expand Up @@ -282,9 +283,11 @@ impl ComponentController for Ground {
..ComponentConfig::DEFAULT
};
fn render(ctx: &Context, encoder: &mut RenderEncoder) {
encoder.render_each::<Self>(ctx, RenderConfig::default(), |r, ground, instance| {
r.render_sprite(instance, &ground.model, &ground.sprite)
});
ctx.components.render_each::<Self>(
encoder,
RenderConfig::default(),
|r, ground, instance| r.render_sprite(instance, &ground.model, &ground.sprite),
);
}
}

Expand Down Expand Up @@ -316,9 +319,13 @@ impl ComponentController for Background {
..ComponentConfig::DEFAULT
};
fn render(ctx: &Context, encoder: &mut RenderEncoder) {
encoder.render_each::<Self>(ctx, RenderConfig::default(), |r, background, instance| {
r.render_sprite(instance, &background.model, &background.sprite)
});
ctx.components.render_each::<Self>(
encoder,
RenderConfig::default(),
|r, background, instance| {
r.render_sprite(instance, &background.model, &background.sprite)
},
);
}
}

Expand Down Expand Up @@ -367,10 +374,11 @@ impl ComponentController for Pipe {

fn render(ctx: &Context, encoder: &mut RenderEncoder) {
let scene = ctx.scene_states.get::<BirdSimulation>();
encoder.render_all::<Self>(ctx, RenderConfig::default(), |r, instances| {
r.render_sprite(instances.clone(), &scene.top_pipe_model, &scene.pipe_sprite);
r.render_sprite(instances, &scene.bottom_pipe_model, &scene.pipe_sprite);
});
ctx.components
.render_all::<Self>(encoder, RenderConfig::default(), |r, instances| {
r.render_sprite(instances.clone(), &scene.top_pipe_model, &scene.pipe_sprite);
r.render_sprite(instances, &scene.bottom_pipe_model, &scene.pipe_sprite);
});
}
}

Expand Down
29 changes: 14 additions & 15 deletions examples/lighting/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,10 @@ impl ComponentController for Obstacle {
};

fn render(ctx: &Context, encoder: &mut RenderEncoder) {
encoder.render_each::<Self>(ctx, RenderConfig::WORLD, |renderer, o, i| {
renderer.render_color(i, &o.model, &o.color)
});
ctx.components
.render_each::<Self>(encoder, RenderConfig::WORLD, |renderer, o, i| {
renderer.render_color(i, &o.model, &o.color)
});
}
}

Expand Down Expand Up @@ -353,19 +354,17 @@ impl ComponentController for Light {
clear_color: Some(Color::TRANSPARENT),
..RenderConfig::WORLD
});
for (buffer, lights) in ctx.components.iter_render::<Self>() {
for (buffer, instance, light) in ctx.components.iter_render::<Self>() {
renderer.use_instances(buffer);
for (i, light) in lights {
renderer.use_shader(&state.light_shader);
renderer.use_model(&light.light_model);
renderer.use_uniform(&light.light_color, 1);
renderer.draw(i);

for shadow in &light.shadows {
renderer.use_shader(&state.shadow_shader);
renderer.use_model(shadow);
renderer.draw(i);
}
renderer.use_shader(&state.light_shader);
renderer.use_model(&light.light_model);
renderer.use_uniform(&light.light_color, 1);
renderer.draw(instance);

for shadow in &light.shadows {
renderer.use_shader(&state.shadow_shader);
renderer.use_model(shadow);
renderer.draw(instance);
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions examples/models/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,9 @@ impl ComponentController for ModelTest {
..ComponentConfig::DEFAULT
};
fn render(ctx: &Context, encoder: &mut RenderEncoder) {
encoder.render_each::<Self>(ctx, RenderConfig::WORLD, |r, model, index| {
r.render_color(index, &model.model, &model.color)
});
ctx.components
.render_each::<Self>(encoder, RenderConfig::WORLD, |r, model, index| {
r.render_color(index, &model.model, &model.color)
});
}
}
50 changes: 23 additions & 27 deletions examples/physics_serde/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ impl Player {
}

impl ComponentController for Player {
const CONFIG: ComponentConfig = ComponentConfig {
storage: ComponentStorage::Single,
..ComponentConfig::DEFAULT
};

fn update(ctx: &mut Context) {
let scroll = ctx.input.wheel_delta();
let fov = ctx.world_camera.fov();
Expand Down Expand Up @@ -203,9 +208,10 @@ impl ComponentController for Player {
}

fn render(ctx: &Context, encoder: &mut RenderEncoder) {
encoder.render_each::<Self>(ctx, RenderConfig::WORLD, |r, player, index| {
r.render_sprite(index, &player.model, &player.sprite)
});
ctx.components
.render_each::<Self>(encoder, RenderConfig::WORLD, |r, player, index| {
r.render_sprite(index, &player.model, &player.sprite)
});
}

fn collision(
Expand Down Expand Up @@ -258,9 +264,10 @@ impl Floor {

impl ComponentController for Floor {
fn render(ctx: &Context, encoder: &mut RenderEncoder) {
encoder.render_each::<Self>(ctx, RenderConfig::WORLD, |r, floor, index| {
r.render_color(index, &floor.model, &floor.color)
});
ctx.components
.render_each::<Self>(encoder, RenderConfig::WORLD, |r, floor, index| {
r.render_color(index, &floor.model, &floor.color)
});
}
}

Expand Down Expand Up @@ -294,28 +301,17 @@ impl PhysicsBox {

impl ComponentController for PhysicsBox {
fn render(ctx: &Context, encoder: &mut RenderEncoder) {
let mut renderer = encoder.renderer(RenderConfig::WORLD);
let state = ctx.scene_states.get::<PhysicsState>();
for (buffer, boxes) in ctx.components.iter_render::<Self>() {
let mut ranges = vec![];
let mut last = 0;
for (i, b) in boxes.clone() {
if b.collided {
ranges.push((&state.default_color, last..i.index));
ranges.push((&state.collision_color, i.index..i.index + 1));
last = i.index + 1;
} else if b.hovered {
ranges.push((&state.default_color, last..i.index));
ranges.push((&state.hover_color, i.index..i.index + 1));
last = i.index + 1;
}
}
ranges.push((&state.default_color, last..buffer.len()));
renderer.use_instances(buffer);
for (color, r) in ranges {
renderer.render_color(r, &state.box_model, color)
}
}
ctx.components.render_each::<Self>(encoder, RenderConfig::WORLD, |renderer, b, instance| {
let sprite = if b.hovered {
&state.hover_color
} else if b.collided {
&state.collision_color
} else {
&state.default_color
};
renderer.render_color(instance, &state.box_model, sprite);
}) ;
}

fn update(ctx: &mut Context) {
Expand Down
Loading

0 comments on commit 44aa722

Please sign in to comment.