rust/lib-hedgewars-engine/src/render/gear.rs
author alfadur
Sat, 06 Jul 2019 22:04:59 +0300
changeset 15223 b2c086629fb8
parent 15195 e2adb40c7988
child 15291 16bd389fc735
permissions -rw-r--r--
optimize 64-bit sqrt some more
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15125
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14734
diff changeset
     1
use super::atlas::AtlasCollection;
15195
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15125
diff changeset
     2
use crate::render::camera::Camera;
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15125
diff changeset
     3
15125
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14734
diff changeset
     4
use integral_geometry::Size;
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14734
diff changeset
     5
15195
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15125
diff changeset
     6
use png::{ColorType, Decoder, DecodingError};
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15125
diff changeset
     7
use std::{
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15125
diff changeset
     8
    fs::{read_dir, File},
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15125
diff changeset
     9
    io,
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15125
diff changeset
    10
    io::BufReader,
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15125
diff changeset
    11
    path::Path,
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15125
diff changeset
    12
};
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15125
diff changeset
    13
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15125
diff changeset
    14
pub struct GearRenderer {
15125
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14734
diff changeset
    15
    atlas: AtlasCollection,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14734
diff changeset
    16
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14734
diff changeset
    17
15195
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15125
diff changeset
    18
const ATLAS_SIZE: Size = Size::square(1024);
15125
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14734
diff changeset
    19
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14734
diff changeset
    20
impl GearRenderer {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14734
diff changeset
    21
    pub fn new() -> Self {
15195
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15125
diff changeset
    22
        let mut atlas = AtlasCollection::new(ATLAS_SIZE);
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15125
diff changeset
    23
        let sprites = load_sprites(Path::new("../../share/hedgewars/Data/Graphics/"))
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15125
diff changeset
    24
            .expect("Unable to load Graphics");
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15125
diff changeset
    25
        for sprite in &sprites {
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15125
diff changeset
    26
            atlas.insert_sprite(*sprite);
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15125
diff changeset
    27
        }
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15125
diff changeset
    28
        println!(
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15125
diff changeset
    29
            "Filled atlas with {} sprites:\n{}",
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15125
diff changeset
    30
            sprites.len(),
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15125
diff changeset
    31
            atlas.used_space()
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15125
diff changeset
    32
        );
15125
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14734
diff changeset
    33
        Self { atlas }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14734
diff changeset
    34
    }
15195
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15125
diff changeset
    35
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15125
diff changeset
    36
    pub fn render(&mut self, camera: &Camera) {
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15125
diff changeset
    37
        let projection = camera.projection();
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15125
diff changeset
    38
    }
15125
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14734
diff changeset
    39
}
15195
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15125
diff changeset
    40
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15125
diff changeset
    41
fn load_sprite(path: &Path) -> io::Result<Size> {
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15125
diff changeset
    42
    let decoder = Decoder::new(BufReader::new(File::open(path)?));
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15125
diff changeset
    43
    let (info, mut reader) = decoder.read_info()?;
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15125
diff changeset
    44
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15125
diff changeset
    45
    let size = Size::new(info.width as usize, info.height as usize);
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15125
diff changeset
    46
    Ok(size)
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15125
diff changeset
    47
}
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15125
diff changeset
    48
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15125
diff changeset
    49
fn load_sprites(path: &Path) -> io::Result<Vec<Size>> {
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15125
diff changeset
    50
    let mut result = vec![];
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15125
diff changeset
    51
    for file in read_dir(path)? {
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15125
diff changeset
    52
        let file = file?;
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15125
diff changeset
    53
        if let Some(extension) = file.path().extension() {
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15125
diff changeset
    54
            if extension == "png" {
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15125
diff changeset
    55
                let sprite = load_sprite(&file.path())?;
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15125
diff changeset
    56
                result.push(sprite);
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15125
diff changeset
    57
            }
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15125
diff changeset
    58
        }
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15125
diff changeset
    59
    }
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15125
diff changeset
    60
    Ok(result)
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15125
diff changeset
    61
}