Skip to content

Commit

Permalink
feat: config files
Browse files Browse the repository at this point in the history
  • Loading branch information
voidcoefficient committed Nov 9, 2022
1 parent 8abd3f9 commit ee351c8
Show file tree
Hide file tree
Showing 7 changed files with 197 additions and 9 deletions.
99 changes: 99 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ edition = "2021"
[dependencies]
bracket-lib = "0.8.7"
specs = "0.18.0"
specs-derive = "0.4.1"
specs-derive = "0.4.1"
knuffel = "2.0.0"
62 changes: 62 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use std::env;
use std::fmt::{Display, Formatter, Write};
use std::fs::{read_to_string, File};

use knuffel::Decode;
use knuffel::Error;

#[derive(Decode, Copy, Clone)]
pub enum Config {
Performance(Performance),
}

#[derive(Decode, Copy, Clone)]
pub struct Performance {
#[knuffel(property)]
pub show_fps: bool,

#[knuffel(property)]
pub fps_cap: u8,
}

impl Display for Performance {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.write_str(&*format!(
"Performance(show_fps={}, fps_cap={})",
self.show_fps, self.fps_cap
))
}
}

pub fn load_config() -> Config {
let mut current_path =
env::current_exe().expect("could not load current directory for configuration loading");
current_path.pop(); // removes the binary
current_path.push("config.kdl");

if !current_path.as_path().exists() {
File::create(&current_path).unwrap();
}

let config = parse_config(
&current_path
.to_str()
.expect("could not parse config")
.to_string(),
)
.unwrap();

*config
.iter()
.nth(0)
.unwrap_or(&Config::Performance(Performance {
show_fps: true,
fps_cap: 144,
}))
}

fn parse_config(path: &String) -> Result<Vec<Config>, Error> {
let text = read_to_string(path).unwrap();

knuffel::parse::<Vec<Config>>(&*path, &text)
}
6 changes: 4 additions & 2 deletions src/gui/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use specs::hibitset::BitSetLike;
use specs::shred::Fetch;
use specs::{Entity, Join, ReadStorage, WorldExt};

use MenuMode::*;

use crate::map::{xy_to_idx, TileType};
use crate::systems::craft::RECIPES;
use crate::{
Expand Down Expand Up @@ -46,8 +48,8 @@ pub fn draw_menu(world: &World, ctx: &mut BTerm) {
ctx.draw_box(60, 0, 19, height, RGB::named(WHITE), RGB::named(BLACK));

match ui.menu_mode {
MenuMode::Default | MenuMode::Inventory | MenuMode::Craft => show_options(ctx, 62, 2),
MenuMode::Interact => show_interact(world, ctx, 62, 2),
Default | Inventory | Craft => show_options(ctx, 62, 2),
Interact => show_interact(world, ctx, 62, 2),
}
}

Expand Down
11 changes: 11 additions & 0 deletions src/gui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ pub struct UserInterfaceState {
pub menu_mode: MenuMode,
pub control_mode: ControlMode,
pub selected_option: usize,
pub show_performance_info: bool,
}

impl UserInterfaceState {
pub fn new(show_performance_info: bool) -> Self {
Self {
show_performance_info,
..Self::default()
}
}
}

impl Default for UserInterfaceState {
Expand All @@ -31,6 +41,7 @@ impl Default for UserInterfaceState {
menu_mode: MenuMode::default(),
control_mode: ControlMode::default(),
selected_option: 0,
show_performance_info: true,
}
}
}
Expand Down
18 changes: 16 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
use std::env;
use std::fmt::{Display, Formatter};
use std::fs::{read_to_string, File};
use std::process::exit;

use bracket_lib::color::{BLACK, RGB, YELLOW};
use bracket_lib::prelude::{
main_loop, to_cp437, BError, BTerm, BTermBuilder, FontCharType, GameState, VirtualKeyCode,
};
use bracket_lib::random::RandomNumberGenerator;
use knuffel::{Decode, Error};
use specs::DenseVecStorage;
use specs::{Builder, World, WorldExt};
use specs::{Component, Entity};
Expand All @@ -14,6 +18,7 @@ use crate::components::items::{
Axe, BlocksMovement, Bush, CraftQueue, FirePit, Flint, InBackpack, Item, PickupQueue, Rose,
Three, Tier, WoodenStick,
};
use crate::config::{load_config, Config};
use crate::gui::{MenuMode, UserInterfaceState};
use crate::logs::Log;
use crate::map::new_map;
Expand All @@ -23,6 +28,7 @@ use crate::state::State;
use crate::systems::pickup::PickupSystem;

mod components;
mod config;
mod gui;
mod logs;
mod map;
Expand Down Expand Up @@ -74,10 +80,18 @@ impl Display for Name {
}

fn main() -> BError {
let config = {
let inner = load_config();
match inner {
Config::Performance(perf) => perf,
}
};
println!("{}", config);

let context = BTermBuilder::simple80x50()
.with_title("PipeLain")
.with_dimensions(160, 100)
.with_fps_cap(144.)
.with_fps_cap(f32::from(config.fps_cap))
.build()?;
let mut state = State::default();

Expand Down Expand Up @@ -114,7 +128,7 @@ fn main() -> BError {
"press tab to show/hide the right side menu".to_string(),
],
});
state.world.insert(UserInterfaceState::default());
state.world.insert(UserInterfaceState::new(config.show_fps));

let player = player(&mut state.world, 40, 25);
state.world.insert(player);
Expand Down
7 changes: 3 additions & 4 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ impl GameState for State {
}
}

let mode = {
let (mode, show_perf) = {
let ui = self.world.fetch::<UserInterfaceState>();

ui.menu_mode
(ui.menu_mode, ui.show_performance_info)
};

match mode {
Expand All @@ -90,8 +90,7 @@ impl GameState for State {
draw_log(&self.world, ctx);
draw_menu(&self.world, ctx);

let show_performance_info = true;
if show_performance_info {
if show_perf {
ctx.print_color(
0,
0,
Expand Down

0 comments on commit ee351c8

Please sign in to comment.