Skip to content
Snippets Groups Projects
load_config.rs 1.68 KiB
Newer Older
Louis's avatar
Louis committed
#[cfg(not(target_arch = "wasm32"))]
mod setup {
	pub fn get_asset_path_string() -> String {
		std::env::current_dir()
			.unwrap()
			.join("assets")
			.to_str()
			.unwrap()
			.to_string()
	}

	pub fn initial_size() -> (f32, f32) {
Louis's avatar
Louis committed
		(16.0 * 32.0, 16.0 * 18.0)
Louis's avatar
Louis committed
	}
}

#[cfg(target_arch = "wasm32")]
mod setup {
	pub fn get_asset_path_string() -> String {
		String::from("assets")
	}

	#[cfg(feature = "no_aspect")]
	pub fn initial_size() -> (f32, f32) {
Louis's avatar
Louis committed
		static default_width: f32 = 16.0 * 32.0;
		static default_height: f32 = 16.0 * 18.0;
Louis's avatar
Louis committed

		web_sys::window()
			.and_then(|window: web_sys::Window| {
				let w = window
					.inner_width()
					.ok()
					.and_then(|val| val.as_f64().map(|v| v as f32))
					.unwrap_or(default_width);
				let h = window
					.inner_height()
					.ok()
					.and_then(|val| val.as_f64().map(|v| v as f32))
					.unwrap_or(default_height);

				log::info!("Using w/h without ratio: {}/{}", w, h);
Louis's avatar
Louis committed
				Some((w, h))
			})
			.unwrap_or((default_width, default_height))
	}

	#[cfg(not(feature = "no_aspect"))]
	pub fn initial_size() -> (f32, f32) {
Louis's avatar
Louis committed
		static default_width: f32 = 16.0 * 32.0;
		static default_height: f32 = 16.0 * 18.0;
		static ratio: f32 = default_width / default_height;
Louis's avatar
Louis committed

		web_sys::window()
			.and_then(|window: web_sys::Window| {
				let w = window
					.inner_width()
					.ok()
					.and_then(|val| val.as_f64().map(|v| v as f32))
					.unwrap_or(default_width);
				let h = window
					.inner_height()
					.ok()
					.and_then(|val| val.as_f64().map(|v| v as f32))
					.unwrap_or(default_height);

				log::info!("Using w/h with ratio: {}/{}", w, h / ratio);
Louis's avatar
Louis committed
				Some((w, h / ratio))
			})
			.unwrap_or((default_width, default_height))
	}
}

pub use setup::*;