Skip to content

Commit

Permalink
Added: cjk character support
Browse files Browse the repository at this point in the history
  • Loading branch information
Wayoung7 committed Apr 12, 2024
1 parent d3201fd commit 03843f9
Show file tree
Hide file tree
Showing 11 changed files with 194 additions and 42 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
push:
branches:
- master
- v0.3.0
schedule:
- cron: '00 01 * * *'

Expand Down
8 changes: 5 additions & 3 deletions examples/fountain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crossterm::{
execute, terminal,
};
use firework_rs::{
config::Config,
fireworks::{ExplosionForm, Firework, FireworkConfig, FireworkManager},
particle::ParticleConfig,
term::Terminal,
Expand All @@ -23,6 +24,7 @@ fn main() -> Result<()> {
let mut stdout = stdout();
let (_width, _height) = terminal::size()?;
let mut is_running = true;
let cfg = Config::default();

terminal::enable_raw_mode()?;
execute!(stdout, terminal::EnterAlternateScreen, cursor::Hide)?;
Expand All @@ -44,7 +46,7 @@ fn main() -> Result<()> {
}
event::Event::Resize(_, _) => {
fm.reset();
term.reinit();
term.reinit(&cfg);
}
_ => {}
};
Expand All @@ -54,8 +56,8 @@ fn main() -> Result<()> {
fm.update(time, delta_time);
time = SystemTime::now();

term.render(&fm);
term.print(&mut stdout);
term.render(&fm, &cfg);
term.print(&mut stdout, &cfg);

if delta_time < Duration::from_secs_f32(0.05) {
let rem = Duration::from_secs_f32(0.05) - delta_time;
Expand Down
8 changes: 5 additions & 3 deletions examples/heart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crossterm::{
execute, terminal,
};
use firework_rs::{
config::Config,
fireworks::{ExplosionForm, Firework, FireworkConfig, FireworkManager},
particle::ParticleConfig,
term::Terminal,
Expand All @@ -23,6 +24,7 @@ fn main() -> Result<()> {
let mut stdout = stdout();
let (_width, _height) = terminal::size()?;
let mut is_running = true;
let cfg = Config::default();

terminal::enable_raw_mode()?;
execute!(stdout, terminal::EnterAlternateScreen, cursor::Hide)?;
Expand All @@ -44,7 +46,7 @@ fn main() -> Result<()> {
}
event::Event::Resize(_, _) => {
fm.reset();
term.reinit();
term.reinit(&cfg);
}
_ => {}
};
Expand All @@ -54,8 +56,8 @@ fn main() -> Result<()> {
fm.update(time, delta_time);
time = SystemTime::now();

term.render(&fm);
term.print(&mut stdout);
term.render(&fm, &cfg);
term.print(&mut stdout, &cfg);

if delta_time < Duration::from_secs_f32(0.05) {
let rem = Duration::from_secs_f32(0.05) - delta_time;
Expand Down
8 changes: 5 additions & 3 deletions examples/vortex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crossterm::{
execute, terminal,
};
use firework_rs::{
config::Config,
fireworks::{ExplosionForm, Firework, FireworkConfig, FireworkManager},
particle::ParticleConfig,
term::Terminal,
Expand All @@ -22,6 +23,7 @@ fn main() -> Result<()> {
let mut stdout = stdout();
let (_width, _height) = terminal::size()?;
let mut is_running = true;
let cfg = Config::default();

terminal::enable_raw_mode()?;
execute!(stdout, terminal::EnterAlternateScreen, cursor::Hide)?;
Expand All @@ -43,7 +45,7 @@ fn main() -> Result<()> {
}
event::Event::Resize(_, _) => {
fm.reset();
term.reinit();
term.reinit(&cfg);
}
_ => {}
};
Expand All @@ -53,8 +55,8 @@ fn main() -> Result<()> {
fm.update(time, delta_time);
time = SystemTime::now();

term.render(&fm);
term.print(&mut stdout);
term.render(&fm, &cfg);
term.print(&mut stdout, &cfg);

if delta_time < Duration::from_secs_f32(0.05) {
let rem = Duration::from_secs_f32(0.05) - delta_time;
Expand Down
6 changes: 6 additions & 0 deletions src/bin/firework/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,10 @@ pub struct Cli {
/// If this is not specified, the default fps is 12
#[arg(long, value_name = "FRAME-RATE")]
pub fps: Option<u8>,

/// Set whether to enable cjk character
///
/// If enabled, each character will take up two Latin character space
#[arg(long)]
pub cjk: bool,
}
25 changes: 22 additions & 3 deletions src/bin/firework/gen.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
use std::time::Duration;

use firework_rs::{demo::demo_firework_0, fireworks::FireworkManager};
use firework_rs::{config::Config, demo::demo_firework_0, fireworks::FireworkManager};
use glam::Vec2;
use rand::{seq::IteratorRandom, thread_rng, Rng};

pub fn dyn_gen(fm: &mut FireworkManager, width: u16, height: u16, enable_gradient: bool) {
pub fn dyn_gen(
fm: &mut FireworkManager,
width: u16,
height: u16,
enable_gradient: bool,
cfg: &Config,
) {
let colors = [
vec![
(255, 102, 75),
Expand Down Expand Up @@ -47,15 +53,28 @@ pub fn dyn_gen(fm: &mut FireworkManager, width: u16, height: u16, enable_gradien
(255, 155, 84),
],
vec![(0, 29, 61), (0, 53, 102), (255, 195, 0), (255, 214, 10)],
vec![
(61, 52, 139),
(118, 120, 237),
(247, 184, 1),
(241, 135, 1),
(243, 91, 4),
],
];
if fm.fireworks.len() < (width as usize * height as usize) / 1300 + 3 {
let limit = if cfg.enable_cjk {
(width as usize * height as usize) / 1800 + 3
} else {
(width as usize * height as usize) / 1300 + 3
};
if fm.fireworks.len() < limit {
let x: isize = thread_rng().gen_range(-3..(width as isize + 3));
let y: isize = thread_rng().gen_range(-1..(height as isize + 1));
fm.add_firework(demo_firework_0(
Vec2::new(x as f32, y as f32),
Duration::from_secs_f32(thread_rng().gen_range(0.0..2.0)),
enable_gradient,
colors.iter().choose(&mut thread_rng()).unwrap().to_owned(),
cfg,
));
}
}
26 changes: 20 additions & 6 deletions src/bin/firework/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ use crossterm::{
event::{self, KeyCode},
execute, terminal,
};
use firework_rs::fireworks::FireworkManager;
use firework_rs::term::Terminal;
use firework_rs::{config::Config, fireworks::FireworkManager};
use firework_rs::{
demo::{
demo_firework_2, demo_firework_comb_0, demo_firework_comb_1, demo_firework_comb_2,
Expand All @@ -29,9 +29,13 @@ use gen::dyn_gen;
use glam::Vec2;

fn main() -> Result<()> {
let mut cfg = Config::default();
let mut fps: u8 = 20;
let mut is_running = true;
let cli = Cli::parse();
if cli.cjk {
cfg = Config { enable_cjk: true };
}
if let Some(f) = cli.fps {
if !(5..=30).contains(&f) {
return Err(Error::new(
Expand Down Expand Up @@ -84,7 +88,7 @@ fn main() -> Result<()> {
execute!(stdout, terminal::EnterAlternateScreen, cursor::Hide)?;

let mut time = SystemTime::now();
let mut term = Terminal::default();
let mut term = Terminal::new(&cfg);

while is_running {
if event::poll(Duration::ZERO)? {
Expand All @@ -96,7 +100,7 @@ fn main() -> Result<()> {
}
event::Event::Resize(_, _) => {
fm.reset();
term.reinit();
term.reinit(&cfg);
}
_ => {}
};
Expand All @@ -105,12 +109,22 @@ fn main() -> Result<()> {
(_width, _height) = terminal::size()?;
let delta_time = SystemTime::now().duration_since(time).unwrap();
if fm.install_form == FireworkInstallForm::DynamicInstall {
dyn_gen(&mut fm, _width, _height, cli.gradient);
dyn_gen(
&mut fm,
if cfg.enable_cjk {
(_width - 1) / 2
} else {
_width
},
_height,
cli.gradient,
&cfg,
);
}
fm.update(time, delta_time);
time = SystemTime::now();
term.render(&fm);
term.print(&mut stdout);
term.render(&fm, &cfg);
term.print(&mut stdout, &cfg);

if delta_time < Duration::from_secs_f32(1. / fps as f32) {
let rem = Duration::from_secs_f32(1. / fps as f32) - delta_time;
Expand Down
10 changes: 10 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/// Configuration of the program
pub struct Config {
pub enable_cjk: bool,
}

impl Default for Config {

Check failure on line 6 in src/config.rs

View workflow job for this annotation

GitHub Actions / Lints

this `impl` can be derived
fn default() -> Self {
Self { enable_cjk: false }
}
}
10 changes: 8 additions & 2 deletions src/demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use glam::Vec2;
use rand::{seq::IteratorRandom, thread_rng, Rng};

use crate::{
config::Config,
fireworks::{ExplosionForm, Firework, FireworkConfig},
particle::ParticleConfig,
utils::{
Expand All @@ -23,11 +24,16 @@ pub fn demo_firework_0(
spawn_after: Duration,
enable_gradient: bool,
colors: Vec<(u8, u8, u8)>,
cfg: &Config,
) -> Firework {
let mut particles = Vec::new();
for v in gen_points_circle_normal(
thread_rng().gen_range(230.0..400.0),
thread_rng().gen_range(33..47),
thread_rng().gen_range(if cfg.enable_cjk {
400.0..600.0
} else {
230.0..400.0
}),
thread_rng().gen_range(if cfg.enable_cjk { 20..35 } else { 33..47 }),
)
.iter()
{
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod config;
pub mod demo;
pub mod fireworks;
pub mod particle;
Expand Down
Loading

0 comments on commit 03843f9

Please sign in to comment.