remove doomfire dependency

This commit is contained in:
yazgoo 2020-08-19 23:00:42 +02:00
parent 3e3de2ba7b
commit 60dbc0165e
5 changed files with 79 additions and 42 deletions

3
.gitmodules vendored
View File

@ -1,3 +0,0 @@
[submodule "doomfire"]
path = doomfire
url = https://github.com/r-marques/doomfire.git

12
Cargo.lock generated
View File

@ -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"

View File

@ -1,11 +1,12 @@
[package]
name = "blockishfire"
version = "0.1.0"
version = "0.2.0"
authors = ["yazgoo <yazgoo@gmail.com>"]
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"

@ -1 +0,0 @@
Subproject commit bb774f11196a7e100dcb258378d8afbc3d68c3f9

View File

@ -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<u8> = vec![0; FIRE_WIDTH * FIRE_HEIGHT * 4];
let mut buffer: Vec<u32> = 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);
}
}