Skip to content

Commit

Permalink
upgrade dependancies
Browse files Browse the repository at this point in the history
  • Loading branch information
DavJCosby committed Jul 25, 2024
1 parent 2ced04b commit 0198d7f
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 41 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@ scheduler = ["spin_sleep"]
named_colors = []

[dependencies]
glam = { version = "0.27", features = ["serde"] }
glam = { version = "0.28", features = ["serde"] }
serde = { version = "1.0", features = ["derive"] }
palette = { version = "0.7", default-features = false, features = [
"std",
"approx",
] }
toml = "0.8"
smallvec = "1.13"
compact_str = { version = "0.7", optional = true }
compact_str = { version = "0.8", optional = true }
spin_sleep = { version = "1.2", optional = true }

[dev-dependencies]
criterion = {version = "0.5.1", default-features = false, features = ["cargo_bench_support"]}
ratatui = "0.26"
ratatui = "0.27"
crossterm = {version = "0.27.0"}
rand = {version = "0.8.5", default-features = false, features = ["std", "std_rng"]}

Expand Down
138 changes: 101 additions & 37 deletions examples/drivers/comet.rs
Original file line number Diff line number Diff line change
@@ -1,58 +1,122 @@
use rand::Rng;
use sled::driver::{BufferContainer, Driver, Filters, TimeInfo};
use sled::{color::Rgb, Sled, SledError};
use sled::{color::Rgb, Sled, SledError, Vec2};

use std::f32::consts::TAU;
const INV_TAU: f32 = 1.0 / TAU;

const GREEN_RADIUS: f32 = 2.33;
const GREEN_COUNT: usize = 64;
const GREEN: Rgb = Rgb::new(0.6, 0.93, 0.762);

const BLUE_RADIUS: f32 = 3.0;
const BLUE_COUNT: usize = 96;
const BLUE: Rgb = Rgb::new(0.4, 0.51, 0.93);

const TRAIL_RADIUS: f32 = 1.2;
const NUM_STARS: usize = 5000;
const VELOCITY: f32 = 6.0;
const DIRECTION: Vec2 = Vec2::new(-0.7071, -0.7071);

#[allow(dead_code)]
pub fn build_driver() -> Driver {
let mut driver = Driver::new();

driver.set_startup_commands(startup);
driver.set_compute_commands(compute);
driver.set_draw_commands(draw);
driver

return driver;
}

fn startup(
sled: &mut Sled,
buffers: &mut BufferContainer,
_filters: &mut Filters,
) -> Result<(), SledError> {
let stars = buffers.create_buffer::<Vec2>("stars");
let center = sled.center_point();
let mut rng = rand::thread_rng();

let orth = DIRECTION.perp();

for _ in 0..NUM_STARS {
let sign = match rng.gen_bool(0.5) {
true => 1.0,
false => -1.0,
};

let spawn_pos = center
+ (DIRECTION * rng.gen_range(40.0..300.0))
+ (orth * rng.gen_range(1.45..35.0) * sign);

stars.push(spawn_pos);
}

let colors = buffers.create_buffer::<Rgb>("colors");
colors.extend([
Rgb::new(0.15, 0.5, 1.0),
Rgb::new(0.25, 0.3, 1.0),
Rgb::new(0.05, 0.4, 0.8),
Rgb::new(0.7, 0.0, 0.6),
Rgb::new(0.05, 0.75, 1.0),
Rgb::new(0.1, 0.8, 0.6),
Rgb::new(0.6, 0.05, 0.2),
Rgb::new(0.85, 0.15, 0.3),
Rgb::new(0.0, 0.0, 1.0),
Rgb::new(1.0, 0.71, 0.705),
]);

Ok(())
}

fn compute(
sled: &Sled,
buffers: &mut BufferContainer,
_filters: &mut Filters,
time_info: &TimeInfo,
) -> Result<(), SledError> {
let mut rng = rand::thread_rng();
let delta = time_info.delta.as_secs_f32();
let stars = buffers.get_buffer_mut::<Vec2>("stars")?;
let center = sled.center_point();

let orth = DIRECTION.perp();

for star in stars {
*star -= DIRECTION * VELOCITY * delta;
if star.x.signum() != DIRECTION.x.signum() && star.y.signum() != DIRECTION.y.signum() {
let dq = (*star - center).length_squared();
if dq > 1000.0 {
let sign = match rng.gen_bool(0.5) {
true => 1.0,
false => -1.0,
};

let spawn_pos = center
+ (DIRECTION * rng.gen_range(40.0..300.0))
+ (orth * rng.gen_range(1.5..35.0) * sign);

*star = spawn_pos;
}
}
}

Ok(())
}

fn draw(
sled: &mut Sled,
_buffers: &BufferContainer,
buffers: &BufferContainer,
_filters: &Filters,
time_info: &TimeInfo,
) -> Result<(), SledError> {
let elapsed = time_info.elapsed.as_secs_f32();
let stars = buffers.get_buffer::<Vec2>("stars")?;
let center = sled.center_point();
let delta = time_info.delta.as_secs_f32();

let inner_time_scale = elapsed / GREEN_RADIUS;
let outer_time_scale = elapsed / BLUE_RADIUS;
let fade_amount = 1.0 - (delta * 25.0);

// speckle in swirling green points
for i in 0..GREEN_COUNT {
let angle = inner_time_scale + (TAU / GREEN_COUNT as f32) * i as f32 % TAU;
sled.modulate_at_angle(angle, |led| led.color + GREEN);
}
sled.for_each(|led| led.color *= fade_amount);

// speckle in swirling blue points
for i in 0..BLUE_COUNT {
let angle = outer_time_scale + (TAU / BLUE_COUNT as f32) * i as f32 % TAU;
sled.modulate_at_angle(angle, |led| led.color + BLUE);
let mut i = 0;
for star in stars {
let d = Vec2::new(star.x - center.x, star.y - center.y);
let c = *buffers.get_buffer_item::<Rgb>("colors", i % 10)?;
sled.modulate_at_dir(d, |led| {
let d_sq = (d.length() - led.distance()).powi(2);
led.color + (c * 1.0 / d_sq)
});
i += 1;
}

// brighten or darken points depending on time and angle to simulate a sweeping
// trail thing.
let radar_time_scale = elapsed / TRAIL_RADIUS;
let angle = (radar_time_scale % TAU) + TAU;
sled.map(|led| {
let da = (led.angle() + angle) % TAU;
let fac = 1.0 - (da * INV_TAU).powf(1.25);
led.color * fac
});

Ok(())
}
2 changes: 1 addition & 1 deletion examples/resources/tui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ where
{
Canvas::default()
.block(Block::default().borders(Borders::ALL).title(title))
.marker(Marker::HalfBlock)
.marker(symbols::Marker::HalfBlock)
.background_color(Color::Black)
.x_bounds(x_bounds)
.y_bounds(y_bounds)
Expand Down

0 comments on commit 0198d7f

Please sign in to comment.