15307
|
1 |
use super::{atlas::AtlasCollection, gl::Texture2D};
|
15211
|
2 |
use crate::render::camera::Camera;
|
|
3 |
|
15307
|
4 |
use integral_geometry::{Rect, Size};
|
15141
|
5 |
|
15307
|
6 |
use crate::render::atlas::SpriteIndex;
|
15211
|
7 |
use png::{ColorType, Decoder, DecodingError};
|
15307
|
8 |
use std::path::PathBuf;
|
15211
|
9 |
use std::{
|
15307
|
10 |
collections::HashMap,
|
|
11 |
ffi::OsString,
|
15211
|
12 |
fs::{read_dir, File},
|
|
13 |
io,
|
|
14 |
io::BufReader,
|
|
15 |
path::Path,
|
|
16 |
};
|
|
17 |
|
|
18 |
pub struct GearRenderer {
|
15141
|
19 |
atlas: AtlasCollection,
|
|
20 |
}
|
|
21 |
|
15307
|
22 |
struct SpriteData {
|
|
23 |
size: Size,
|
|
24 |
filename: PathBuf,
|
|
25 |
}
|
|
26 |
|
|
27 |
const ATLAS_SIZE: Size = Size::square(2024);
|
15141
|
28 |
|
|
29 |
impl GearRenderer {
|
|
30 |
pub fn new() -> Self {
|
15307
|
31 |
let mut lookup = Vec::with_capacity(2048);
|
|
32 |
|
15211
|
33 |
let mut atlas = AtlasCollection::new(ATLAS_SIZE);
|
15307
|
34 |
let mut sprites = load_sprites(Path::new("../../share/hedgewars/Data/Graphics/"))
|
15211
|
35 |
.expect("Unable to load Graphics");
|
15307
|
36 |
let max_size = sprites
|
|
37 |
.iter()
|
|
38 |
.fold(Size::EMPTY, |size, sprite| size.join(sprite.size));
|
|
39 |
for sprite in sprites.drain(..) {
|
|
40 |
lookup.push((sprite.filename, atlas.insert_sprite(sprite.size).unwrap()));
|
15211
|
41 |
}
|
15307
|
42 |
|
15211
|
43 |
println!(
|
|
44 |
"Filled atlas with {} sprites:\n{}",
|
|
45 |
sprites.len(),
|
|
46 |
atlas.used_space()
|
|
47 |
);
|
15307
|
48 |
|
|
49 |
let texture = Texture2D::new(max_size, gl::RGBA8, gl::LINEAR);
|
|
50 |
|
|
51 |
let mut pixels = Vec::with_capacity(max_size.area()).into_boxed_slice();
|
|
52 |
for (path, sprite_index) in lookup.drain(..) {
|
|
53 |
if let Some((atlas_index, rect)) = atlas.get_rect(sprite_index) {
|
|
54 |
load_sprite_pixels(&path, &mut pixels[..]).expect("Unable to load Graphics");
|
|
55 |
texture.update(rect, &pixels, 0, gl::RGBA, gl::UNSIGNED_BYTE);
|
|
56 |
}
|
|
57 |
}
|
|
58 |
|
15141
|
59 |
Self { atlas }
|
|
60 |
}
|
15211
|
61 |
|
|
62 |
pub fn render(&mut self, camera: &Camera) {
|
|
63 |
let projection = camera.projection();
|
|
64 |
}
|
15141
|
65 |
}
|
15211
|
66 |
|
15307
|
67 |
fn load_sprite_pixels(path: &Path, buffer: &mut [u8]) -> io::Result<()> {
|
|
68 |
let decoder = Decoder::new(BufReader::new(File::open(path)?));
|
|
69 |
let (info, mut reader) = decoder.read_info()?;
|
|
70 |
|
|
71 |
let size = Size::new(info.width as usize, info.height as usize);
|
|
72 |
reader.next_frame(buffer)?;
|
|
73 |
Ok(())
|
|
74 |
}
|
|
75 |
|
|
76 |
fn load_sprite_size(path: &Path) -> io::Result<Size> {
|
15211
|
77 |
let decoder = Decoder::new(BufReader::new(File::open(path)?));
|
|
78 |
let (info, mut reader) = decoder.read_info()?;
|
|
79 |
|
|
80 |
let size = Size::new(info.width as usize, info.height as usize);
|
|
81 |
Ok(size)
|
|
82 |
}
|
|
83 |
|
15307
|
84 |
fn load_sprites(path: &Path) -> io::Result<Vec<SpriteData>> {
|
15211
|
85 |
let mut result = vec![];
|
|
86 |
for file in read_dir(path)? {
|
|
87 |
let file = file?;
|
|
88 |
if let Some(extension) = file.path().extension() {
|
|
89 |
if extension == "png" {
|
15307
|
90 |
let path = file.path();
|
|
91 |
let sprite = load_sprite_size(&path)?;
|
|
92 |
result.push(SpriteData {
|
|
93 |
size: sprite,
|
|
94 |
filename: path,
|
|
95 |
});
|
15211
|
96 |
}
|
|
97 |
}
|
|
98 |
}
|
|
99 |
Ok(result)
|
|
100 |
}
|