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 |
|
15311
|
27 |
const ATLAS_SIZE: Size = Size::square(2048);
|
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 |
|
15311
|
49 |
let texture = Texture2D::new(ATLAS_SIZE, gl::RGBA8, gl::LINEAR);
|
15307
|
50 |
|
15311
|
51 |
let mut pixels = vec![0; max_size.area()].into_boxed_slice();
|
|
52 |
let mut pixels_transposed = vec![0; max_size.area()].into_boxed_slice();
|
|
53 |
|
15307
|
54 |
for (path, sprite_index) in lookup.drain(..) {
|
|
55 |
if let Some((atlas_index, rect)) = atlas.get_rect(sprite_index) {
|
15311
|
56 |
let size = load_sprite_pixels(&path, mapgen::theme::slice_u32_to_u8_mut(&mut pixels[..])).expect("Unable to load Graphics");
|
|
57 |
|
15312
|
58 |
let used_pixels = if size.width != rect.width() {
|
15311
|
59 |
for y in 0..rect.height() {
|
|
60 |
for x in 0..rect.width() {
|
|
61 |
pixels_transposed[y * rect.width() + x] = pixels[x * rect.height() + y];
|
|
62 |
}
|
|
63 |
}
|
|
64 |
&mut pixels_transposed[..]
|
|
65 |
} else {
|
|
66 |
&mut pixels[..]
|
|
67 |
};
|
|
68 |
|
|
69 |
texture.update(rect, mapgen::theme::slice_u32_to_u8_mut(used_pixels), 0, gl::RGBA, gl::UNSIGNED_BYTE);
|
15307
|
70 |
}
|
|
71 |
}
|
|
72 |
|
15141
|
73 |
Self { atlas }
|
|
74 |
}
|
15211
|
75 |
|
|
76 |
pub fn render(&mut self, camera: &Camera) {
|
|
77 |
let projection = camera.projection();
|
|
78 |
}
|
15141
|
79 |
}
|
15211
|
80 |
|
15311
|
81 |
fn load_sprite_pixels(path: &Path, buffer: &mut [u8]) -> io::Result<Size> {
|
15307
|
82 |
let decoder = Decoder::new(BufReader::new(File::open(path)?));
|
|
83 |
let (info, mut reader) = decoder.read_info()?;
|
|
84 |
|
|
85 |
let size = Size::new(info.width as usize, info.height as usize);
|
|
86 |
reader.next_frame(buffer)?;
|
15311
|
87 |
Ok(size)
|
15307
|
88 |
}
|
|
89 |
|
|
90 |
fn load_sprite_size(path: &Path) -> io::Result<Size> {
|
15211
|
91 |
let decoder = Decoder::new(BufReader::new(File::open(path)?));
|
|
92 |
let (info, mut reader) = decoder.read_info()?;
|
|
93 |
|
|
94 |
let size = Size::new(info.width as usize, info.height as usize);
|
|
95 |
Ok(size)
|
|
96 |
}
|
|
97 |
|
15307
|
98 |
fn load_sprites(path: &Path) -> io::Result<Vec<SpriteData>> {
|
15211
|
99 |
let mut result = vec![];
|
|
100 |
for file in read_dir(path)? {
|
|
101 |
let file = file?;
|
|
102 |
if let Some(extension) = file.path().extension() {
|
|
103 |
if extension == "png" {
|
15307
|
104 |
let path = file.path();
|
|
105 |
let sprite = load_sprite_size(&path)?;
|
|
106 |
result.push(SpriteData {
|
|
107 |
size: sprite,
|
|
108 |
filename: path,
|
|
109 |
});
|
15211
|
110 |
}
|
|
111 |
}
|
|
112 |
}
|
|
113 |
Ok(result)
|
|
114 |
}
|