15781
|
1 |
use crate::render::{
|
|
2 |
atlas::{AtlasCollection, SpriteIndex, SpriteLocation},
|
|
3 |
camera::Camera,
|
|
4 |
gl::Texture2D,
|
|
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)]
|
|
21 |
enum SpriteId {
|
|
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 |
|
15311
|
57 |
let texture = Texture2D::new(ATLAS_SIZE, gl::RGBA8, gl::LINEAR);
|
15307
|
58 |
|
15781
|
59 |
let mut allocation = Box::new([(0, Rect::at_origin(Size::EMPTY)); MAX_SPRITES]);
|
15311
|
60 |
|
15781
|
61 |
for (sprite, file) in SPRITE_LOAD_LIST {
|
|
62 |
let path = Path::new(file);
|
|
63 |
let size = load_sprite_size(path).expect(&format!("Unable to open {}", file));
|
|
64 |
let index = atlas
|
|
65 |
.insert_sprite(size)
|
|
66 |
.expect(&format!("Could not store sprite {:?}", sprite));
|
|
67 |
let (texture_index, rect) = atlas.get_rect(index).unwrap();
|
15311
|
68 |
|
15781
|
69 |
let mut pixels = vec![0; size.area()].into_boxed_slice();
|
|
70 |
load_sprite_pixels(path, mapgen::theme::slice_u32_to_u8_mut(&mut pixels[..]))
|
|
71 |
.expect("Unable to load Graphics");
|
15311
|
72 |
|
15781
|
73 |
texture.update(
|
|
74 |
rect,
|
|
75 |
mapgen::theme::slice_u32_to_u8_mut(&mut pixels[..]),
|
|
76 |
0,
|
|
77 |
gl::RGBA,
|
|
78 |
gl::UNSIGNED_BYTE,
|
|
79 |
);
|
|
80 |
|
|
81 |
allocation[*sprite as usize] = (texture_index, rect);
|
15307
|
82 |
}
|
|
83 |
|
15781
|
84 |
Self { atlas, allocation }
|
15141
|
85 |
}
|
15211
|
86 |
|
|
87 |
pub fn render(&mut self, camera: &Camera) {
|
|
88 |
let projection = camera.projection();
|
|
89 |
}
|
15141
|
90 |
}
|
15211
|
91 |
|
15311
|
92 |
fn load_sprite_pixels(path: &Path, buffer: &mut [u8]) -> io::Result<Size> {
|
15307
|
93 |
let decoder = Decoder::new(BufReader::new(File::open(path)?));
|
|
94 |
let (info, mut reader) = decoder.read_info()?;
|
|
95 |
|
|
96 |
let size = Size::new(info.width as usize, info.height as usize);
|
|
97 |
reader.next_frame(buffer)?;
|
15311
|
98 |
Ok(size)
|
15307
|
99 |
}
|
|
100 |
|
|
101 |
fn load_sprite_size(path: &Path) -> io::Result<Size> {
|
15211
|
102 |
let decoder = Decoder::new(BufReader::new(File::open(path)?));
|
|
103 |
let (info, mut reader) = decoder.read_info()?;
|
|
104 |
|
|
105 |
let size = Size::new(info.width as usize, info.height as usize);
|
|
106 |
Ok(size)
|
|
107 |
}
|
|
108 |
|
15307
|
109 |
fn load_sprites(path: &Path) -> io::Result<Vec<SpriteData>> {
|
15211
|
110 |
let mut result = vec![];
|
|
111 |
for file in read_dir(path)? {
|
|
112 |
let file = file?;
|
|
113 |
if let Some(extension) = file.path().extension() {
|
|
114 |
if extension == "png" {
|
15307
|
115 |
let path = file.path();
|
|
116 |
let sprite = load_sprite_size(&path)?;
|
|
117 |
result.push(SpriteData {
|
|
118 |
size: sprite,
|
|
119 |
filename: path,
|
|
120 |
});
|
15211
|
121 |
}
|
|
122 |
}
|
|
123 |
}
|
|
124 |
Ok(result)
|
|
125 |
}
|