Skip to content

Commit

Permalink
Add more clap flags for easier testing
Browse files Browse the repository at this point in the history
  • Loading branch information
iMilchshake committed Sep 2, 2024
1 parent 64948a6 commit ae1aabb
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 39 deletions.
37 changes: 37 additions & 0 deletions src/args.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use clap::{crate_version, Parser};

#[derive(Parser, Debug)]
#[command(name = "Random Gores Map Generator")]
#[command(version = crate_version!())]
#[command(about = "Visual editor for generating maps and customizing the generators presets", long_about = None)]
pub struct Args {
/// select initial generation config
pub gen_config: Option<String>,

/// select initial map config
pub map_config: Option<String>,

/// trigger map generation on startup
#[arg(short = 'g', long)]
pub generate: bool,

/// enable instant generation
#[arg(short = 'i', long)]
pub instant: bool,

/// enable fixed seed
#[arg(short = 'f', long)]
pub fixed_seed: bool,

/// enable auto generation
#[arg(short = 'a', long)]
pub auto_generation: bool,

/// disable all debug visualization calculations for improved performance
#[arg(short = 'd', long)]
pub disable_debug: bool,

/// comma seperated list of debug layers to enable on startup
#[arg(short = 'e', long, value_delimiter = ',', num_args = 1..)]
pub enable_layers: Option<Vec<String>>,
}
41 changes: 39 additions & 2 deletions src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::{path::PathBuf, str::FromStr};
const STEPS_PER_FRAME: usize = 50;

use crate::{
args::Args,
config::{GenerationConfig, MapConfig},
debug::DebugLayers,
generator::Generator,
Expand All @@ -11,6 +12,7 @@ use crate::{
random::Seed,
};
use egui::{epaint::Shadow, Color32, Frame, Margin};
use log::warn;
use std::env;

use macroquad::input::{
Expand Down Expand Up @@ -94,7 +96,7 @@ impl Editor {
gen_config: GenerationConfig,
map_config: MapConfig,
disable_debug: bool,
enable_layers: Option<Vec<String>>,
enable_layers: &Option<Vec<String>>,
) -> Editor {
let init_gen_configs: Vec<GenerationConfig> = GenerationConfig::get_all_configs();
let init_map_configs: Vec<MapConfig> = MapConfig::get_all_configs();
Expand Down Expand Up @@ -124,7 +126,7 @@ impl Editor {

editor.initialize_debug_layers();

if let Some(enable_layers) = enable_layers {
if let Some(ref enable_layers) = enable_layers {
for layer_name in enable_layers {
let layer = editor
.debug_layers
Expand All @@ -140,6 +142,28 @@ impl Editor {
editor
}

pub fn handle_cli_args(&mut self, args: &Args) {
self.instant = args.instant;
self.auto_generate = args.auto_generation;
self.fixed_seed = args.fixed_seed;

if let Some(config_name) = &args.gen_config {
if self.load_gen_config(config_name).is_err() {
warn!("Coulnt load gen config {}", config_name);
}
}

if let Some(config_name) = &args.map_config {
if self.load_map_config(config_name).is_err() {
warn!("Coulnt load map config {}", config_name);
}
}

if args.generate {
self.set_playing()
}
}

pub fn initialize_debug_layers(&mut self) {
if self.disable_debug_layers {
return;
Expand Down Expand Up @@ -300,4 +324,17 @@ impl Editor {
Err("Generation config not found!")
}
}

pub fn load_map_config(&mut self, config_name: &str) -> Result<(), &'static str> {
if let Some(config) = self
.init_map_configs
.iter()
.find(|&c| c.name == config_name)
{
self.map_config = config.clone();
Ok(())
} else {
Err("Generation config not found!")
}
}
}
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 args;
pub mod config;
pub mod debug;
pub mod editor;
Expand Down
42 changes: 5 additions & 37 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,42 +1,21 @@
#![cfg_attr(target_os = "windows", windows_subsystem = "windows")]

use clap::{crate_version, Parser};
use clap::Parser;
use gores_mapgen::{
args::Args,
config::{GenerationConfig, MapConfig},
editor::*,
fps_control::*,
map::*,
rendering::*,
};
use log::warn;
use macroquad::{color::*, miniquad, window::*};
use miniquad::conf::{Conf, Platform};
use simple_logger::SimpleLogger;
use std::panic::{self, AssertUnwindSafe};

const DISABLE_VSYNC: bool = true;

#[derive(Parser, Debug)]
#[command(name = "Random Gores Map Generator")]
#[command(version = crate_version!())]
#[command(about = "Visual editor for generating maps and customizing the generators presets", long_about = None)]
struct Args {
/// select initial generation config
config: Option<String>,

/// enable instant, auto generate and fixed seed
#[arg(short, long)]
testing: bool,

/// disable all debug visualization calculations for improved performance
#[arg(short, long)]
disable_debug: bool,

/// comma seperated list of debug layers to enable on startup
#[arg(short, long, value_delimiter = ',', num_args = 1..)]
enable_layers: Option<Vec<String>>,
}

fn window_conf() -> Conf {
Conf {
window_title: "egui with macroquad".to_owned(),
Expand All @@ -60,23 +39,12 @@ async fn main() {
GenerationConfig::get_initial_gen_config(),
MapConfig::get_initial_config(),
args.disable_debug,
args.enable_layers,
&args.enable_layers,
);
let mut fps_ctrl = FPSControl::new().with_max_fps(60);

// handle cli args TODO: move all to some editor function
if args.testing {
editor.instant = true;
editor.fixed_seed = true;
editor.auto_generate = true;
editor.edit_gen_config = true;
}

if let Some(config_name) = args.config {
if editor.load_gen_config(&config_name).is_err() {
warn!("Coulnt load config {}", config_name);
}
}
// handle cli args
editor.handle_cli_args(&args);

// main loop for gui (and step-wise map generation)
loop {
Expand Down

0 comments on commit ae1aabb

Please sign in to comment.