diff --git a/.gitmodules b/.gitmodules deleted file mode 100644 index 4df63d6..0000000 --- a/.gitmodules +++ /dev/null @@ -1,3 +0,0 @@ -[submodule "doomfire"] - path = doomfire - url = https://github.com/r-marques/doomfire.git diff --git a/Cargo.lock b/Cargo.lock index 655e983..22ae4ea 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -52,11 +52,11 @@ dependencies = [ [[package]] name = "blockishfire" -version = "0.1.0" +version = "0.2.0" dependencies = [ "blockish 0.0.8 (registry+https://github.com/rust-lang/crates.io-index)", "crossterm 0.15.0 (registry+https://github.com/rust-lang/crates.io-index)", - "doomfire 0.1.0", + "rand 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -181,14 +181,6 @@ dependencies = [ "byteorder 1.3.4 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "doomfire" -version = "0.1.0" -dependencies = [ - "rand 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)", - "rand_chacha 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "either" version = "1.6.0" diff --git a/Cargo.toml b/Cargo.toml index b802ae8..99e2e99 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,11 +1,12 @@ [package] name = "blockishfire" -version = "0.1.0" +version = "0.2.0" authors = ["yazgoo "] +description = "doomfire in the terminal" edition = "2018" license = "MIT" [dependencies] crossterm = "0.15" blockish = "0.0.8" -doomfire = { path = "doomfire", version = "0.1.0" } +rand = "0.7" diff --git a/doomfire b/doomfire deleted file mode 160000 index bb774f1..0000000 --- a/doomfire +++ /dev/null @@ -1 +0,0 @@ -Subproject commit bb774f11196a7e100dcb258378d8afbc3d68c3f9 diff --git a/src/main.rs b/src/main.rs index 2deb86d..34f51d3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,19 +1,56 @@ +extern crate rand; + +use rand::Rng; + use std::thread; use std::time::{Duration, Instant}; use crossterm::terminal; -use doomfire::{DoomFire, FIRE_HEIGHT, FIRE_WIDTH, TIME_PER_FRAME}; - -fn from_u8_rgb(r: u8, g: u8, b: u8) -> u32 { - let (r, g, b) = (r as u32, g as u32, b as u32); - (r << 16) | (g << 8) | b -} - fn main() { - let mut doom_fire = DoomFire::new(); + const COLOR_PALLET: [[u8; 3]; 37] = [ + [0x07, 0x07, 0x07], + [0x1F, 0x07, 0x07], + [0x2F, 0x0F, 0x07], + [0x47, 0x0F, 0x07], + [0x57, 0x17, 0x07], + [0x67, 0x1F, 0x07], + [0x77, 0x1F, 0x07], + [0x8F, 0x27, 0x07], + [0x9F, 0x2F, 0x07], + [0xAF, 0x3F, 0x07], + [0xBF, 0x47, 0x07], + [0xC7, 0x47, 0x07], + [0xDF, 0x4F, 0x07], + [0xDF, 0x57, 0x07], + [0xDF, 0x57, 0x07], + [0xD7, 0x5F, 0x07], + [0xD7, 0x5F, 0x07], + [0xD7, 0x67, 0x0F], + [0xCF, 0x6F, 0x0F], + [0xCF, 0x77, 0x0F], + [0xCF, 0x7F, 0x0F], + [0xCF, 0x87, 0x17], + [0xC7, 0x87, 0x17], + [0xC7, 0x8F, 0x17], + [0xC7, 0x97, 0x1F], + [0xBF, 0x9F, 0x1F], + [0xBF, 0x9F, 0x1F], + [0xBF, 0xA7, 0x27], + [0xBF, 0xA7, 0x27], + [0xBF, 0xAF, 0x2F], + [0xB7, 0xAF, 0x2F], + [0xB7, 0xB7, 0x2F], + [0xB7, 0xB7, 0x37], + [0xCF, 0xCF, 0x6F], + [0xDF, 0xDF, 0x9F], + [0xEF, 0xEF, 0xC7], + [0xFF, 0xFF, 0xFF], +]; + let fire_width = 320; + let fire_height = 168; + let time_per_frame = 1000/ 60; + let mut doom_fire = vec![0; fire_width * fire_height]; - let mut frame: Vec = vec![0; FIRE_WIDTH * FIRE_HEIGHT * 4]; - let mut buffer: Vec = vec![0; FIRE_WIDTH * FIRE_HEIGHT]; let mut term_width = 0 as u32; let mut term_height = 0 as u32; @@ -21,39 +58,50 @@ fn main() { match terminal::size() { Ok(res) => { term_width = res.0 as u32 * 8; - term_height = (res.1 - 1) as u32 * 8 * 2; + term_height = res.1 as u32 * 8 * 2; } Err(_) => {} } let mut engine = blockish::ThreadedEngine::new(term_width, term_height, false); + for i in 0..fire_width { + doom_fire[(fire_height - 1) * fire_width + i] = 36; + } + let mut rng = rand::thread_rng(); loop { let start_time = Instant::now(); - println!("\x1b[{};0f", 0); - doom_fire.draw(&mut frame); - - // DoomFire expects a &[u8] to write the pixels with a RGBA encoding but minifb - // expects a &[u32] with a 0RGB pixel encoding, where the upper 8 bits are ignored. - for (i, pixel) in frame.chunks_exact(4).enumerate() { - buffer[i] = from_u8_rgb(pixel[0], pixel[1], pixel[2]); + print!("\x1b[{};0f", 0); + for x in 0..fire_width { + for y in 1..fire_height { + let src = y * fire_width + x; + if doom_fire[src] > 0 { + let r = (rng.gen_range(0.0, 3.0) + 0.5) as usize & 3; + let dst = (src - r + 1) as usize; + let res = doom_fire[src] - (r & 1); + doom_fire[dst - fire_width] = res; + } + else { + doom_fire[src - fire_width] = 0; + } + } } engine.render(&|x, y| { - let start = (y * FIRE_HEIGHT as u32 / term_height * FIRE_WIDTH as u32 + (x * FIRE_WIDTH as u32 / term_width)) + let start = (y * fire_height as u32 / term_height * fire_width as u32 + (x * fire_width as u32 / term_width)) as usize; - let pixel = buffer[start]; + let pixel = doom_fire[start]; + let rgb = COLOR_PALLET[pixel]; ( - (pixel >> 16 & 0xff) as u8, - (pixel >> 8 & 0xff) as u8, - (pixel & 0xff) as u8, + rgb[0], + rgb[1], + rgb[2], ) }); - doom_fire.update(); let end_time = Instant::now(); let render_time = end_time - start_time; - if render_time < Duration::from_millis(TIME_PER_FRAME) { - let waste_time = Duration::from_millis(TIME_PER_FRAME) - render_time; + if render_time < Duration::from_millis(time_per_frame) { + let waste_time = Duration::from_millis(time_per_frame) - render_time; thread::sleep(waste_time); } }