15141
|
1 |
use super::atlas::AtlasCollection;
|
15211
|
2 |
use crate::render::camera::Camera;
|
|
3 |
|
15141
|
4 |
use integral_geometry::Size;
|
|
5 |
|
15211
|
6 |
use png::{ColorType, Decoder, DecodingError};
|
|
7 |
use std::{
|
|
8 |
fs::{read_dir, File},
|
|
9 |
io,
|
|
10 |
io::BufReader,
|
|
11 |
path::Path,
|
|
12 |
};
|
|
13 |
|
|
14 |
pub struct GearRenderer {
|
15141
|
15 |
atlas: AtlasCollection,
|
|
16 |
}
|
|
17 |
|
15211
|
18 |
const ATLAS_SIZE: Size = Size::square(1024);
|
15141
|
19 |
|
|
20 |
impl GearRenderer {
|
|
21 |
pub fn new() -> Self {
|
15211
|
22 |
let mut atlas = AtlasCollection::new(ATLAS_SIZE);
|
|
23 |
let sprites = load_sprites(Path::new("../../share/hedgewars/Data/Graphics/"))
|
|
24 |
.expect("Unable to load Graphics");
|
|
25 |
for sprite in &sprites {
|
|
26 |
atlas.insert_sprite(*sprite);
|
|
27 |
}
|
|
28 |
println!(
|
|
29 |
"Filled atlas with {} sprites:\n{}",
|
|
30 |
sprites.len(),
|
|
31 |
atlas.used_space()
|
|
32 |
);
|
15141
|
33 |
Self { atlas }
|
|
34 |
}
|
15211
|
35 |
|
|
36 |
pub fn render(&mut self, camera: &Camera) {
|
|
37 |
let projection = camera.projection();
|
|
38 |
}
|
15141
|
39 |
}
|
15211
|
40 |
|
|
41 |
fn load_sprite(path: &Path) -> io::Result<Size> {
|
|
42 |
let decoder = Decoder::new(BufReader::new(File::open(path)?));
|
|
43 |
let (info, mut reader) = decoder.read_info()?;
|
|
44 |
|
|
45 |
let size = Size::new(info.width as usize, info.height as usize);
|
|
46 |
Ok(size)
|
|
47 |
}
|
|
48 |
|
|
49 |
fn load_sprites(path: &Path) -> io::Result<Vec<Size>> {
|
|
50 |
let mut result = vec![];
|
|
51 |
for file in read_dir(path)? {
|
|
52 |
let file = file?;
|
|
53 |
if let Some(extension) = file.path().extension() {
|
|
54 |
if extension == "png" {
|
|
55 |
let sprite = load_sprite(&file.path())?;
|
|
56 |
result.push(sprite);
|
|
57 |
}
|
|
58 |
}
|
|
59 |
}
|
|
60 |
Ok(result)
|
|
61 |
}
|