Skip to main content

libdqg/
lib.rs

1pub mod app;
2pub mod game;
3pub mod renderer;
4pub mod scene;
5pub mod types;
6pub mod input;
7pub mod titlebar;
8pub mod platform;
9pub mod util;
10pub mod camera;
11pub mod transform;
12pub mod ecs;
13pub mod scene_file;
14pub mod scripting;
15pub mod world;
16
17pub use transform::Transformable;
18
19/// Re-exported so downstream crates can build the vectors and matrices this API takes
20/// (e.g. [`renderer::Sprite::transform`]) without pinning `glam` themselves.
21pub use glam;
22
23pub fn add(left: u64, right: u64) -> u64 {
24    left + right
25}
26
27/// Copies everything matching `res/*` in the calling crate into a `res/` folder
28/// next to the built executable, so binaries can load assets through plain
29/// relative paths (e.g. `./res/textures/smug.png`) no matter how they're started.
30///
31/// Meant to be called from a crate's `build.rs`; it relies on the `CARGO_MANIFEST_DIR`
32/// and `OUT_DIR` variables Cargo sets for build scripts. Does nothing if the crate
33/// has no `res/` directory.
34pub fn copy_res_to_output_dir() -> Result<(), Box<dyn std::error::Error>> {
35    let manifest_dir = std::path::PathBuf::from(std::env::var("CARGO_MANIFEST_DIR")?);
36    let res_dir = manifest_dir.join("res");
37    if !res_dir.is_dir() {
38        return Ok(());
39    }
40    println!("cargo:rerun-if-changed={}", res_dir.display());
41
42    // `OUT_DIR` is `<target>/<profile>/build/<crate>-<hash>/out`, and the executable
43    // is written three levels up from it, in `<target>/<profile>`.
44    let out_dir = std::path::PathBuf::from(std::env::var("OUT_DIR")?);
45    let exe_dir = out_dir
46        .ancestors()
47        .nth(3)
48        .ok_or("OUT_DIR is not laid out as <target>/<profile>/build/<crate>/out")?;
49    let dest_dir = exe_dir.join("res");
50    std::fs::create_dir_all(&dest_dir)?;
51
52    let pattern = res_dir
53        .join("*")
54        .to_str()
55        .ok_or("resource path is not valid UTF-8")?
56        .replace('\\', "/");
57    let entries = glob::glob(&pattern)?.collect::<Result<Vec<_>, _>>()?;
58
59    let mut options = fs_extra::dir::CopyOptions::new();
60    options.overwrite = true;
61    fs_extra::copy_items(&entries, &dest_dir, &options)?;
62
63    Ok(())
64}
65
66#[cfg(test)]
67mod tests {
68    use super::*;
69
70    #[test]
71    fn it_works() {
72        let result = add(2, 2);
73        assert_eq!(result, 4);
74    }
75}