146 lines
3.8 KiB
Rust
146 lines
3.8 KiB
Rust
use std::path::PathBuf;
|
|
|
|
pub(crate) mod syntax {
|
|
use anyhow::{bail, Result};
|
|
use serde::Deserialize;
|
|
use std::path::{Path, PathBuf};
|
|
|
|
// Raw serde syntax structs.
|
|
// These are never seen by code outside this crate.
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
pub struct Config {
|
|
pub fonts: Fonts,
|
|
pub sprite_root: PathBuf,
|
|
pub starfield: Starfield,
|
|
pub zoom_min: f32,
|
|
pub zoom_max: f32,
|
|
}
|
|
|
|
impl Config {
|
|
// TODO: clean up build trait
|
|
pub fn build(self, asset_root: &Path) -> Result<super::Config> {
|
|
for i in &self.fonts.files {
|
|
if !asset_root.join(i).exists() {
|
|
bail!("font file `{}` doesn't exist", i.display());
|
|
}
|
|
}
|
|
|
|
let starfield_density = 0.01;
|
|
let starfield_size = self.starfield.max_dist * self.zoom_max;
|
|
let starfield_count = (starfield_size * starfield_density) as i32;
|
|
|
|
// 12, because that should be enough to tile any screen.
|
|
// Starfield squares are tiled to cover the viewport, adapting to any screen ratio.
|
|
// An insufficient limit will result in some tiles not being drawn
|
|
let starfield_instance_limit = 12 * starfield_count as u64;
|
|
|
|
return Ok(super::Config {
|
|
sprite_root: asset_root.join(self.sprite_root),
|
|
font_files: self
|
|
.fonts
|
|
.files
|
|
.iter()
|
|
.map(|i| asset_root.join(i))
|
|
.collect(),
|
|
|
|
font_sans: self.fonts.sans,
|
|
font_serif: self.fonts.serif,
|
|
font_mono: self.fonts.mono,
|
|
//font_cursive: self.fonts.cursive,
|
|
//font_fantasy: self.fonts.fantasy,
|
|
starfield_max_dist: self.starfield.max_dist,
|
|
starfield_min_dist: self.starfield.min_dist,
|
|
starfield_max_size: self.starfield.max_size,
|
|
starfield_min_size: self.starfield.min_size,
|
|
starfield_sprite: self.starfield.sprite,
|
|
starfield_count,
|
|
starfield_density,
|
|
starfield_size,
|
|
starfield_instance_limit,
|
|
zoom_max: self.zoom_max,
|
|
zoom_min: self.zoom_min,
|
|
});
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
pub struct Fonts {
|
|
pub files: Vec<PathBuf>,
|
|
pub sans: String,
|
|
pub serif: String,
|
|
pub mono: String,
|
|
//pub cursive: String,
|
|
//pub fantasy: String,
|
|
}
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
pub struct Starfield {
|
|
pub min_size: f32,
|
|
pub max_size: f32,
|
|
pub min_dist: f32,
|
|
pub max_dist: f32,
|
|
pub sprite: String,
|
|
}
|
|
}
|
|
|
|
/// Content configuration
|
|
#[derive(Debug, Clone)]
|
|
pub struct Config {
|
|
/// The directory where all images are stored.
|
|
/// Image paths are always interpreted relative to this path.
|
|
/// This is a subdirectory of the asset root.
|
|
pub sprite_root: PathBuf,
|
|
|
|
/// List of font files to load
|
|
pub font_files: Vec<PathBuf>,
|
|
|
|
/// Sans Serif font family name
|
|
pub font_sans: String,
|
|
|
|
/// Serif font family name
|
|
pub font_serif: String,
|
|
|
|
/// Monospace font family name
|
|
pub font_mono: String,
|
|
//pub font_cursive: String,
|
|
//pub font_fantasy: String,
|
|
/// Min size of starfield sprite, in game units
|
|
pub starfield_min_size: f32,
|
|
|
|
/// Max size of starfield sprite, in game units
|
|
pub starfield_max_size: f32,
|
|
|
|
/// Minimum z-distance of starfield star, in game units
|
|
pub starfield_min_dist: f32,
|
|
|
|
/// Maximum z-distance of starfield star, in game units
|
|
pub starfield_max_dist: f32,
|
|
|
|
/// Name of starfield sprite
|
|
pub starfield_sprite: String,
|
|
|
|
/// Size of a square starfield tile, in game units.
|
|
/// A tile of size STARFIELD_Z_MAX * screen-size-in-game-units
|
|
/// will completely cover a (square) screen.
|
|
/// This should be big enough to cover the height of the screen at max zoom.
|
|
pub starfield_size: f32,
|
|
|
|
/// Average number of stars per game unit
|
|
pub starfield_density: f32,
|
|
|
|
/// Number of stars in one starfield tile
|
|
/// Must be positive
|
|
pub starfield_count: i32,
|
|
|
|
// TODO: this shouldn't be here, it depends on graphics implementation
|
|
/// The maximum number of starfield sprites we can create
|
|
pub starfield_instance_limit: u64,
|
|
|
|
/// Minimum zoom, in game units
|
|
pub zoom_min: f32,
|
|
|
|
/// Maximum zoom,in game units
|
|
pub zoom_max: f32,
|
|
}
|