Skip to content

Commit

Permalink
clippy suggestions, ported examples over to use macros
Browse files Browse the repository at this point in the history
  • Loading branch information
DavJCosby committed Sep 27, 2024
1 parent 833c47f commit bfea481
Show file tree
Hide file tree
Showing 12 changed files with 99 additions and 193 deletions.
4 changes: 1 addition & 3 deletions examples/calibration.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
mod resources;

use resources::tui::SledTerminalDisplay;

use glam::Vec2;
use resources::tui::SledTerminalDisplay;
use sled::{color::Rgb, Sled, SledError};

fn main() -> Result<(), SledError> {
Expand Down
144 changes: 39 additions & 105 deletions examples/drivers/comet.rs
Original file line number Diff line number Diff line change
@@ -1,122 +1,56 @@
use rand::Rng;
use sled::driver::{BufferContainer, Driver, Filters, TimeInfo};
use sled::{color::Rgb, Sled, SledError, Vec2};
use driver_macros::*;
use sled::driver::{Driver, TimeInfo};
use sled::SledResult;
use sled::{color::Rgb, Sled};

const NUM_STARS: usize = 5000;
const VELOCITY: f32 = 6.0;
const DIRECTION: Vec2 = Vec2::new(-0.7071, -0.7071);
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;

#[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);

return driver;
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();
#[draw_commands]
fn draw(sled: &mut Sled, time_info: &TimeInfo) -> SledResult {
let elapsed = time_info.elapsed.as_secs_f32();

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

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);
// 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);
}

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;
}
}
// 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);
}

Ok(())
}

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

let fade_amount = 1.0 - (delta * 25.0);

sled.for_each(|led| led.color *= fade_amount);

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(())
}
29 changes: 10 additions & 19 deletions examples/drivers/ripples.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use driver_macros::*;
use rand::Rng;
use sled::driver::{BufferContainer, Driver, Filters, TimeInfo};
use sled::{color::Rgb, Sled, SledError, Vec2};
use sled::driver::{BufferContainer, Driver, TimeInfo};
use sled::SledResult;
use sled::{color::Rgb, Sled, Vec2};
use std::ops::Range;

const MAX_RIPPLES: usize = 12;
Expand All @@ -18,11 +20,8 @@ pub fn build_driver() -> Driver {
return driver;
}

fn startup(
sled: &mut Sled,
buffers: &mut BufferContainer,
_filters: &mut Filters,
) -> Result<(), SledError> {
#[startup_commands]
fn startup(sled: &mut Sled, buffers: &mut BufferContainer) -> SledResult {
let sled_bounds = sled.domain();

let radii = buffers.create_buffer("radii");
Expand Down Expand Up @@ -52,12 +51,8 @@ fn startup(
Ok(())
}

fn compute(
sled: &Sled,
buffers: &mut BufferContainer,
_filters: &mut Filters,
time_info: &TimeInfo,
) -> Result<(), SledError> {
#[compute_commands]
fn compute(sled: &Sled, buffers: &mut BufferContainer, time_info: &TimeInfo) -> SledResult {
let delta = time_info.delta.as_secs_f32();
let bounds = sled.domain();
for i in 0..MAX_RIPPLES {
Expand Down Expand Up @@ -90,12 +85,8 @@ fn rand_init_radius() -> f32 {
rng.gen_range(-32.0..0.0)
}

fn draw(
sled: &mut Sled,
buffers: &BufferContainer,
_filters: &Filters,
_time_info: &TimeInfo,
) -> Result<(), SledError> {
#[draw_commands]
fn draw(sled: &mut Sled, buffers: &BufferContainer) -> SledResult {
sled.set_all(Rgb::new(0.0, 0.0, 0.0));
let colors = buffers.get_buffer("colors")?;
let positions = buffers.get_buffer("positions")?;
Expand Down
29 changes: 10 additions & 19 deletions examples/drivers/warpspeed.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use driver_macros::*;
use rand::Rng;
use sled::driver::{BufferContainer, Driver, Filters, TimeInfo};
use sled::{color::Rgb, Sled, SledError, Vec2};
use sled::driver::{BufferContainer, Driver, TimeInfo};
use sled::SledResult;
use sled::{color::Rgb, Sled, Vec2};

const NUM_STARS: usize = 5000;
const VELOCITY: f32 = 6.0;
Expand All @@ -17,11 +19,8 @@ pub fn build_driver() -> Driver {
return driver;
}

fn startup(
sled: &mut Sled,
buffers: &mut BufferContainer,
_filters: &mut Filters,
) -> Result<(), SledError> {
#[startup_commands]
fn startup(sled: &mut Sled, buffers: &mut BufferContainer) -> SledResult {
let stars = buffers.create_buffer::<Vec2>("stars");
let center = sled.center_point();
let mut rng = rand::thread_rng();
Expand Down Expand Up @@ -58,12 +57,8 @@ fn startup(
Ok(())
}

fn compute(
sled: &Sled,
buffers: &mut BufferContainer,
_filters: &mut Filters,
time_info: &TimeInfo,
) -> Result<(), SledError> {
#[compute_commands]
fn compute(sled: &Sled, buffers: &mut BufferContainer, time_info: &TimeInfo) -> SledResult {
let mut rng = rand::thread_rng();
let delta = time_info.delta.as_secs_f32();
let stars = buffers.get_buffer_mut::<Vec2>("stars")?;
Expand Down Expand Up @@ -93,12 +88,8 @@ fn compute(
Ok(())
}

fn draw(
sled: &mut Sled,
buffers: &BufferContainer,
_filters: &Filters,
time_info: &TimeInfo,
) -> Result<(), SledError> {
#[draw_commands]
fn draw(sled: &mut Sled, buffers: &BufferContainer, time_info: &TimeInfo) -> SledResult {
let stars = buffers.get_buffer::<Vec2>("stars")?;
let center = sled.center_point();
let delta = time_info.delta.as_secs_f32();
Expand Down
2 changes: 1 addition & 1 deletion examples/resources/config.yap
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
center: (0, 0.5)
density: 30
--segments--
(-2, 0) --> (0.5, -1) --> (3.5, 0) |
(-2, 0) --> (0.5, -1) --> (3.5, 0) -->
(2, 2) --> (-2, 2) --> (-2, 0)
46 changes: 19 additions & 27 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,29 @@ fn extract_center_and_density_from_lines(lines: &mut Lines) -> (Option<Vec2>, Op
let mut center: Option<Vec2> = None;
let mut density: Option<f32> = None;
let mut segment_marker_found = false;
loop {
if let Some(top_line) = lines.next() {
let trimmed = top_line.trim();
if trimmed.starts_with("--segments--") {
segment_marker_found = true;
break;
} else if trimmed.starts_with("center:") {
center = Some(get_center_from_line(top_line));
} else if trimmed.starts_with("density:") {
density = Some(get_density_from_line(top_line));
}
} else {

for line in lines.by_ref() {
let trimmed = line.trim();
if trimmed.starts_with("--segments--") {
segment_marker_found = true;
break;
} else if trimmed.starts_with("center:") {
center = Some(get_center_from_line(line));
} else if trimmed.starts_with("density:") {
density = Some(get_density_from_line(line));
}
}

if !segment_marker_found {
panic!("Error parsing config file: no segment marker of form `--segments--` found.")
}

return (center, density);
(center, density)
}

fn get_center_from_line(line: &str) -> Vec2 {
let colon_pos = line.find(':').unwrap();
parse_string_to_vec2(&line[(colon_pos + 1)..line.len()].trim())
parse_string_to_vec2(line[(colon_pos + 1)..line.len()].trim())
}

fn get_density_from_line(line: &str) -> f32 {
Expand All @@ -52,16 +49,15 @@ fn parse_string_to_vec2(s: &str) -> Vec2 {
let nums: Vec<f32> = sub
.split(',')
.map(|s| {
s.trim().parse().expect(&format!(
"Error parsing config file: malformed Vec2: `{}`",
s
))
s.trim().parse().unwrap_or_else(|_| {
panic!("Error parsing config file: malformed Vec2: `{}`", s)
})
})
.collect();
if !nums.len() == 2 {
panic!("Error parsing config file: malformed Vec2: {}", s);
}
return Vec2::new(nums[0], nums[1]);
Vec2::new(nums[0], nums[1])
} else {
panic!("Error parsing config file: malformed Vec2: `{}`", s);
}
Expand All @@ -70,18 +66,14 @@ fn parse_string_to_vec2(s: &str) -> Vec2 {
fn lines_to_string(lines: &mut Lines) -> String {
let mut composite = String::from("");

loop {
if let Some(top_line) = lines.next() {
composite += top_line.trim();
} else {
break;
}
for line in lines.by_ref() {
composite += line.trim();
}

composite
}

fn extract_segments_from_string(s: &String) -> Vec<LineSegment> {
fn extract_segments_from_string(s: &str) -> Vec<LineSegment> {
let connected: Vec<&str> = s.split("|").collect();
let mut segments: Vec<LineSegment> = vec![];
for sequence in connected {
Expand All @@ -99,7 +91,7 @@ fn extract_segments_from_string(s: &String) -> Vec<LineSegment> {
}
}

return segments;
segments
}

impl Config {
Expand Down
Loading

0 comments on commit bfea481

Please sign in to comment.