first wrong flake draw

This commit is contained in:
nek0 2024-10-12 03:45:16 +02:00
parent daf99064d5
commit 5a759ea096
4 changed files with 88 additions and 3 deletions

7
Cargo.lock generated
View file

@ -8,6 +8,12 @@ version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "c_vec"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fdd7a427adc0135366d99db65b36dae9237130997e560ed61118041fb72be6e8"
[[package]] [[package]]
name = "cfg-if" name = "cfg-if"
version = "1.0.0" version = "1.0.0"
@ -40,6 +46,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3b498da7d14d1ad6c839729bd4ad6fc11d90a57583605f3b4df2cd709a9cd380" checksum = "3b498da7d14d1ad6c839729bd4ad6fc11d90a57583605f3b4df2cd709a9cd380"
dependencies = [ dependencies = [
"bitflags", "bitflags",
"c_vec",
"lazy_static", "lazy_static",
"libc", "libc",
"sdl2-sys", "sdl2-sys",

View file

@ -6,4 +6,4 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
sdl2 = "0.37" sdl2 = { version = "0.37", features = ["gfx"]}

View file

@ -16,6 +16,7 @@
packageBuildInputs = with pkgs; [ packageBuildInputs = with pkgs; [
SDL2 SDL2
SDL2_gfx
]; ];
inherit (import "${crate2nix}/tools.nix" { inherit pkgs; }) inherit (import "${crate2nix}/tools.nix" { inherit pkgs; })

View file

@ -3,17 +3,22 @@ extern crate sdl2;
use sdl2::pixels::Color; use sdl2::pixels::Color;
use sdl2::event::Event; use sdl2::event::Event;
use sdl2::keyboard::Keycode; use sdl2::keyboard::Keycode;
use sdl2::render::Canvas;
use sdl2::video::Window;
use sdl2::gfx::primitives::DrawRenderer;
use std::time::Duration; use std::time::Duration;
struct Base { struct Base {
lines: Vec<Line>, lines: Vec<Line>,
} }
#[derive(Clone, Debug)]
struct Position { struct Position {
x: u32, x: u32,
y: u32, y: u32,
} }
#[derive(Clone, Debug)]
struct Line { struct Line {
start: Position, start: Position,
end: Position, end: Position,
@ -50,6 +55,10 @@ pub fn main() {
, end: Position{x: width / 4, y: 3 * height / 4}} , end: Position{x: width / 4, y: 3 * height / 4}}
]}; ]};
canvas.set_draw_color(Color::RGB(0, 0, 0));
canvas.clear();
canvas.present();
'running: loop { 'running: loop {
if i % 255 == 0 { if i % 255 == 0 {
flip = !flip; flip = !flip;
@ -59,14 +68,18 @@ pub fn main() {
} else { } else {
i = i + 1; i = i + 1;
} }
canvas.set_draw_color(Color::RGB(i, 64, 255 - i)); //canvas.set_draw_color(Color::RGB(i, 64, 255 - i));
canvas.clear(); //canvas.clear();
draw_lines(&canvas, Color::RGB(255 - i, 64, i), & base.lines);
for event in event_pump.poll_iter() { for event in event_pump.poll_iter() {
match event { match event {
Event::Quit { .. } | Event::Quit { .. } |
Event::KeyDown { keycode: Some(Keycode::Escape), .. } => { Event::KeyDown { keycode: Some(Keycode::Escape), .. } => {
break 'running break 'running
}, },
Event::KeyDown { keycode: Some(Keycode::Return), .. } => {
base.lines = step(base.lines);
},
_ => {} _ => {}
} }
} }
@ -75,3 +88,67 @@ pub fn main() {
::std::thread::sleep(Duration::from_millis(1000 / 60)); ::std::thread::sleep(Duration::from_millis(1000 / 60));
} }
} }
fn step (lines: Vec<Line>) -> Vec<Line> {
println!("breaking {:?}", lines);
lines.iter().fold(vec![], break_line)
}
fn break_line (mut acc: Vec<Line>, line: & Line) -> Vec<Line> {
let dx = line.end.x as i32 - line.start.x as i32;
let dy = line.end.y as i32 - line.start.y as i32;
let first = Line{
start: Position {
x : line.start.x,
y : line.start.y,
},
end: Position {
x : (line.start.x as i32 + dx / 3) as u32,
y : (line.start.y as i32 + dy / 3) as u32,
},
};
let second = Line{
start: Position {
x : (line.start.x as i32 + dx / 3) as u32,
y : (line.start.y as i32 + dy / 3) as u32,
},
end: Position {
x : line.start.x,
y : (line.start.y as i32 + 2 * (dy / 3)) as u32,
},
};
let third = Line{
start: Position {
x : line.start.x,
y : (line.start.y as i32 + 2 * (dy / 3)) as u32,
},
end: Position {
x : (line.start.x as i32 + 2 * (dx / 3)) as u32,
y : (line.start.y as i32 + 2 * (dy / 3)) as u32,
},
};
let fourth = Line{
start: Position {
x : (line.start.x as i32 + 2 * (dx / 3)) as u32,
y : (line.start.y as i32 + 2 * (dy / 3)) as u32,
},
end: Position {
x : (line.start.x as i32 + dx) as u32,
y : (line.start.y as i32 + dy) as u32,
},
};
acc.append(& mut vec![first, second, third, fourth]);
acc.to_vec()
}
fn draw_lines (canvas: & Canvas<Window>, color: Color, lines: & Vec<Line>) {
for line in lines {
canvas.line(
line.start.x as i16,
line.start.y as i16,
line.end.x as i16,
line.end.y as i16,
color
).unwrap();
}
}