15781
|
1 |
use crate::render::{
|
|
2 |
atlas::{AtlasCollection, SpriteIndex, SpriteLocation},
|
|
3 |
camera::Camera,
|
15782
|
4 |
gl::{Texture2D, TextureDataType, TextureFilter, TextureFormat, TextureInternalFormat},
|
15781
|
5 |
};
|
15211
|
6 |
|
15307
|
7 |
use integral_geometry::{Rect, Size};
|
15141
|
8 |
|
15211
|
9 |
use png::{ColorType, Decoder, DecodingError};
|
15781
|
10 |
|
15211
|
11 |
use std::{
|
15307
|
12 |
collections::HashMap,
|
|
13 |
ffi::OsString,
|
15211
|
14 |
fs::{read_dir, File},
|
|
15 |
io,
|
|
16 |
io::BufReader,
|
15781
|
17 |
path::{Path, PathBuf},
|
15211
|
18 |
};
|
|
19 |
|
15781
|
20 |
#[derive(PartialEq, Debug, Clone, Copy)]
|
15782
|
21 |
pub enum SpriteId {
|
15781
|
22 |
Mine = 0,
|
|
23 |
Grenade,
|
|
24 |
|
|
25 |
MaxSprite,
|
|
26 |
}
|
|
27 |
|
|
28 |
const SPRITE_LOAD_LIST: &[(SpriteId, &str)] = &[
|
|
29 |
(
|
|
30 |
SpriteId::Mine,
|
|
31 |
"../../share/hedgewars/Data/Graphics/MineOn.png",
|
|
32 |
),
|
|
33 |
(
|
|
34 |
SpriteId::Grenade,
|
|
35 |
"../../share/hedgewars/Data/Graphics/Bomb.png",
|
|
36 |
),
|
|
37 |
];
|
|
38 |
|
|
39 |
const MAX_SPRITES: usize = SpriteId::MaxSprite as usize + 1;
|
|
40 |
|
15211
|
41 |
pub struct GearRenderer {
|
15141
|
42 |
atlas: AtlasCollection,
|
15781
|
43 |
allocation: Box<[SpriteLocation; MAX_SPRITES]>,
|
15141
|
44 |
}
|
|
45 |
|
15307
|
46 |
struct SpriteData {
|
|
47 |
size: Size,
|
|
48 |
filename: PathBuf,
|
|
49 |
}
|
|
50 |
|
15311
|
51 |
const ATLAS_SIZE: Size = Size::square(2048);
|
15141
|
52 |
|
|
53 |
impl GearRenderer {
|
|
54 |
pub fn new() -> Self {
|
15211
|
55 |
let mut atlas = AtlasCollection::new(ATLAS_SIZE);
|
15307
|
56 |
|
15782
|
57 |
let texture = Texture2D::new(
|
|
58 |
ATLAS_SIZE,
|
|
59 |
TextureInternalFormat::Rgba8,
|
|
60 |
TextureFilter::Linear,
|
|
61 |
);
|
15307
|
62 |
|
15781
|
63 |
let mut allocation = Box::new([(0, Rect::at_origin(Size::EMPTY)); MAX_SPRITES]);
|
15311
|
64 |
|
15781
|
65 |
for (sprite, file) in SPRITE_LOAD_LIST {
|
|
66 |
let path = Path::new(file);
|
|
67 |
let size = load_sprite_size(path).expect(&format!("Unable to open {}", file));
|
|
68 |
let index = atlas
|
|
69 |
.insert_sprite(size)
|
|
70 |
.expect(&format!("Could not store sprite {:?}", sprite));
|
|
71 |
let (texture_index, rect) = atlas.get_rect(index).unwrap();
|
15311
|
72 |
|
15781
|
73 |
let mut pixels = vec![0; size.area()].into_boxed_slice();
|
|
74 |
load_sprite_pixels(path, mapgen::theme::slice_u32_to_u8_mut(&mut pixels[..]))
|
|
75 |
.expect("Unable to load Graphics");
|
15311
|
76 |
|
15781
|
77 |
texture.update(
|
|
78 |
rect,
|
|
79 |
mapgen::theme::slice_u32_to_u8_mut(&mut pixels[..]),
|
15782
|
80 |
None,
|
|
81 |
TextureFormat::Rgba,
|
|
82 |
TextureDataType::UnsignedByte,
|
15781
|
83 |
);
|
|
84 |
|
|
85 |
allocation[*sprite as usize] = (texture_index, rect);
|
15307
|
86 |
}
|
|
87 |
|
15781
|
88 |
Self { atlas, allocation }
|
15141
|
89 |
}
|
15211
|
90 |
|
|
91 |
pub fn render(&mut self, camera: &Camera) {
|
|
92 |
let projection = camera.projection();
|
|
93 |
}
|
15141
|
94 |
}
|
15211
|
95 |
|
15311
|
96 |
fn load_sprite_pixels(path: &Path, buffer: &mut [u8]) -> io::Result<Size> {
|
15307
|
97 |
let decoder = Decoder::new(BufReader::new(File::open(path)?));
|
|
98 |
let (info, mut reader) = decoder.read_info()?;
|
|
99 |
|
|
100 |
let size = Size::new(info.width as usize, info.height as usize);
|
|
101 |
reader.next_frame(buffer)?;
|
15311
|
102 |
Ok(size)
|
15307
|
103 |
}
|
|
104 |
|
|
105 |
fn load_sprite_size(path: &Path) -> io::Result<Size> {
|
15211
|
106 |
let decoder = Decoder::new(BufReader::new(File::open(path)?));
|
|
107 |
let (info, mut reader) = decoder.read_info()?;
|
|
108 |
|
|
109 |
let size = Size::new(info.width as usize, info.height as usize);
|
|
110 |
Ok(size)
|
|
111 |
}
|
|
112 |
|
15307
|
113 |
fn load_sprites(path: &Path) -> io::Result<Vec<SpriteData>> {
|
15211
|
114 |
let mut result = vec![];
|
|
115 |
for file in read_dir(path)? {
|
|
116 |
let file = file?;
|
|
117 |
if let Some(extension) = file.path().extension() {
|
|
118 |
if extension == "png" {
|
15307
|
119 |
let path = file.path();
|
|
120 |
let sprite = load_sprite_size(&path)?;
|
|
121 |
result.push(SpriteData {
|
|
122 |
size: sprite,
|
|
123 |
filename: path,
|
|
124 |
});
|
15211
|
125 |
}
|
|
126 |
}
|
|
127 |
}
|
|
128 |
Ok(result)
|
|
129 |
}
|