15781
|
1 |
use crate::render::{
|
15787
|
2 |
atlas::{AtlasCollection, SpriteIndex},
|
15781
|
3 |
camera::Camera,
|
15784
|
4 |
gl::{
|
|
5 |
Buffer, BufferType, BufferUsage, InputElement, InputFormat, InputLayout, PipelineState,
|
|
6 |
Shader, Texture2D, TextureDataType, TextureFilter, TextureFormat, TextureInternalFormat,
|
15786
|
7 |
VariableBinding,
|
15784
|
8 |
},
|
15781
|
9 |
};
|
15211
|
10 |
|
15307
|
11 |
use integral_geometry::{Rect, Size};
|
15141
|
12 |
|
15211
|
13 |
use png::{ColorType, Decoder, DecodingError};
|
15781
|
14 |
|
15211
|
15 |
use std::{
|
15307
|
16 |
collections::HashMap,
|
|
17 |
ffi::OsString,
|
15211
|
18 |
fs::{read_dir, File},
|
|
19 |
io,
|
|
20 |
io::BufReader,
|
15784
|
21 |
mem::size_of,
|
15781
|
22 |
path::{Path, PathBuf},
|
15211
|
23 |
};
|
|
24 |
|
15784
|
25 |
const VERTEX_SHADER: &'static str = r#"
|
|
26 |
#version 330 core
|
|
27 |
|
|
28 |
uniform mat4 projection;
|
|
29 |
|
15786
|
30 |
layout(location = 0) in vec2 position;
|
|
31 |
layout(location = 1) in vec2 texCoords;
|
|
32 |
|
|
33 |
out vec2 varTexCoords;
|
|
34 |
|
15784
|
35 |
void main() {
|
15786
|
36 |
varTexCoords = texCoords;
|
15784
|
37 |
gl_Position = projection * vec4(position, 0.0, 1.0);
|
|
38 |
}
|
|
39 |
"#;
|
|
40 |
|
|
41 |
const PIXEL_SHADER: &'static str = r#"
|
|
42 |
#version 330 core
|
|
43 |
|
15786
|
44 |
uniform sampler2D texture;
|
|
45 |
|
|
46 |
in vec2 varTexCoords;
|
|
47 |
|
15784
|
48 |
out vec4 outColor;
|
|
49 |
|
|
50 |
void main() {
|
15786
|
51 |
outColor = texture2D(texture, varTexCoords);
|
15784
|
52 |
}
|
|
53 |
"#;
|
|
54 |
|
|
55 |
#[repr(C)]
|
|
56 |
#[derive(Copy, Clone)]
|
|
57 |
struct Vertex {
|
15786
|
58 |
position: [f32; 2],
|
|
59 |
tex_coords: [f32; 2],
|
15784
|
60 |
}
|
|
61 |
|
15781
|
62 |
#[derive(PartialEq, Debug, Clone, Copy)]
|
15787
|
63 |
#[repr(u32)]
|
15782
|
64 |
pub enum SpriteId {
|
15781
|
65 |
Mine = 0,
|
|
66 |
Grenade,
|
15787
|
67 |
Cheese,
|
|
68 |
Cleaver,
|
15781
|
69 |
|
|
70 |
MaxSprite,
|
|
71 |
}
|
|
72 |
|
|
73 |
const SPRITE_LOAD_LIST: &[(SpriteId, &str)] = &[
|
|
74 |
(
|
|
75 |
SpriteId::Mine,
|
|
76 |
"../../share/hedgewars/Data/Graphics/MineOn.png",
|
|
77 |
),
|
|
78 |
(
|
|
79 |
SpriteId::Grenade,
|
|
80 |
"../../share/hedgewars/Data/Graphics/Bomb.png",
|
|
81 |
),
|
15787
|
82 |
(
|
|
83 |
SpriteId::Cheese,
|
|
84 |
"../../share/hedgewars/Data/Graphics/cheese.png",
|
|
85 |
),
|
|
86 |
(
|
|
87 |
SpriteId::Cleaver,
|
|
88 |
"../../share/hedgewars/Data/Graphics/cleaver.png",
|
|
89 |
),
|
15781
|
90 |
];
|
|
91 |
|
|
92 |
const MAX_SPRITES: usize = SpriteId::MaxSprite as usize + 1;
|
|
93 |
|
15787
|
94 |
type SpriteTexCoords = (u32, [[f32; 2]; 4]);
|
|
95 |
|
15784
|
96 |
pub struct GearEntry {
|
|
97 |
position: [f32; 2],
|
|
98 |
size: Size,
|
|
99 |
}
|
|
100 |
|
|
101 |
impl GearEntry {
|
|
102 |
pub fn new(x: f32, y: f32, size: Size) -> Self {
|
|
103 |
Self {
|
|
104 |
position: [x, y],
|
|
105 |
size,
|
|
106 |
}
|
|
107 |
}
|
|
108 |
}
|
|
109 |
|
15211
|
110 |
pub struct GearRenderer {
|
15141
|
111 |
atlas: AtlasCollection,
|
15786
|
112 |
texture: Texture2D,
|
15787
|
113 |
allocation: Box<[SpriteTexCoords; MAX_SPRITES]>,
|
15784
|
114 |
shader: Shader,
|
|
115 |
layout: InputLayout,
|
|
116 |
vertex_buffer: Buffer,
|
15141
|
117 |
}
|
|
118 |
|
15307
|
119 |
struct SpriteData {
|
|
120 |
size: Size,
|
|
121 |
filename: PathBuf,
|
|
122 |
}
|
|
123 |
|
15311
|
124 |
const ATLAS_SIZE: Size = Size::square(2048);
|
15141
|
125 |
|
|
126 |
impl GearRenderer {
|
|
127 |
pub fn new() -> Self {
|
15211
|
128 |
let mut atlas = AtlasCollection::new(ATLAS_SIZE);
|
15307
|
129 |
|
15782
|
130 |
let texture = Texture2D::new(
|
|
131 |
ATLAS_SIZE,
|
|
132 |
TextureInternalFormat::Rgba8,
|
|
133 |
TextureFilter::Linear,
|
|
134 |
);
|
15307
|
135 |
|
15787
|
136 |
let mut allocation = Box::new([Default::default(); MAX_SPRITES]);
|
15311
|
137 |
|
15781
|
138 |
for (sprite, file) in SPRITE_LOAD_LIST {
|
|
139 |
let path = Path::new(file);
|
|
140 |
let size = load_sprite_size(path).expect(&format!("Unable to open {}", file));
|
|
141 |
let index = atlas
|
|
142 |
.insert_sprite(size)
|
|
143 |
.expect(&format!("Could not store sprite {:?}", sprite));
|
|
144 |
let (texture_index, rect) = atlas.get_rect(index).unwrap();
|
15311
|
145 |
|
15786
|
146 |
let mut pixels = vec![255u8; size.area() * 4].into_boxed_slice();
|
|
147 |
load_sprite_pixels(path, &mut pixels).expect("Unable to load Graphics");
|
15311
|
148 |
|
15781
|
149 |
texture.update(
|
|
150 |
rect,
|
15786
|
151 |
&pixels,
|
15782
|
152 |
None,
|
|
153 |
TextureFormat::Rgba,
|
|
154 |
TextureDataType::UnsignedByte,
|
15781
|
155 |
);
|
|
156 |
|
15787
|
157 |
let mut tex_coords = [
|
|
158 |
[rect.left() as f32, rect.bottom() as f32 + 1.0],
|
|
159 |
[rect.right() as f32 + 1.0, rect.bottom() as f32 + 1.0],
|
|
160 |
[rect.left() as f32, rect.top() as f32],
|
|
161 |
[rect.right() as f32 + 1.0, rect.top() as f32],
|
|
162 |
]; //.map(|n| n as f32 / ATLAS_SIZE as f32);
|
|
163 |
|
|
164 |
for coords in &mut tex_coords {
|
|
165 |
coords[0] /= ATLAS_SIZE.width as f32;
|
|
166 |
coords[1] /= ATLAS_SIZE.height as f32;
|
|
167 |
}
|
|
168 |
|
|
169 |
allocation[*sprite as usize] = (texture_index, tex_coords);
|
15307
|
170 |
}
|
|
171 |
|
15786
|
172 |
let shader = Shader::new(
|
|
173 |
VERTEX_SHADER,
|
|
174 |
Some(PIXEL_SHADER),
|
|
175 |
&[VariableBinding::Sampler("texture", 0)],
|
|
176 |
)
|
|
177 |
.unwrap();
|
15784
|
178 |
|
|
179 |
let layout = InputLayout::new(vec![
|
|
180 |
InputElement {
|
|
181 |
shader_slot: 0,
|
|
182 |
buffer_slot: 0,
|
|
183 |
format: InputFormat::Float(gl::FLOAT, false),
|
|
184 |
components: 2,
|
|
185 |
stride: size_of::<Vertex>() as u32,
|
|
186 |
offset: 0,
|
|
187 |
},
|
15786
|
188 |
InputElement {
|
|
189 |
shader_slot: 1,
|
|
190 |
buffer_slot: 0,
|
|
191 |
format: InputFormat::Float(gl::FLOAT, false),
|
|
192 |
components: 2,
|
|
193 |
stride: size_of::<Vertex>() as u32,
|
|
194 |
offset: size_of::<[f32; 2]>() as u32,
|
|
195 |
},
|
15784
|
196 |
]);
|
|
197 |
|
|
198 |
let vertex_buffer = Buffer::empty(BufferType::Array, BufferUsage::DynamicDraw);
|
|
199 |
|
|
200 |
Self {
|
|
201 |
atlas,
|
15786
|
202 |
texture,
|
15784
|
203 |
allocation,
|
|
204 |
shader,
|
|
205 |
layout,
|
|
206 |
vertex_buffer,
|
|
207 |
}
|
15141
|
208 |
}
|
15211
|
209 |
|
15784
|
210 |
pub fn render(&mut self, camera: &Camera, entries: &[GearEntry]) {
|
15786
|
211 |
let mut data = Vec::with_capacity(entries.len() * 6);
|
|
212 |
|
15787
|
213 |
for (index, entry) in entries.iter().enumerate() {
|
|
214 |
let sprite_id = match index & 0b11 {
|
|
215 |
0 => SpriteId::Mine,
|
|
216 |
1 => SpriteId::Grenade,
|
|
217 |
2 => SpriteId::Cheese,
|
|
218 |
_ => SpriteId::Cleaver,
|
|
219 |
};
|
|
220 |
let sprite_coords = &self.allocation[sprite_id as usize].1;
|
|
221 |
|
15786
|
222 |
let v = [
|
|
223 |
Vertex {
|
|
224 |
position: [
|
|
225 |
entry.position[0] - entry.size.width as f32 / 2.0,
|
|
226 |
entry.position[1] + entry.size.height as f32 / 2.0,
|
|
227 |
],
|
15787
|
228 |
tex_coords: sprite_coords[0],
|
15786
|
229 |
},
|
|
230 |
Vertex {
|
|
231 |
position: [
|
|
232 |
entry.position[0] + entry.size.width as f32 / 2.0,
|
|
233 |
entry.position[1] + entry.size.height as f32 / 2.0,
|
|
234 |
],
|
15787
|
235 |
tex_coords: sprite_coords[1],
|
15786
|
236 |
},
|
|
237 |
Vertex {
|
|
238 |
position: [
|
|
239 |
entry.position[0] - entry.size.width as f32 / 2.0,
|
|
240 |
entry.position[1] - entry.size.height as f32 / 2.0,
|
|
241 |
],
|
15787
|
242 |
tex_coords: sprite_coords[2],
|
15786
|
243 |
},
|
|
244 |
Vertex {
|
|
245 |
position: [
|
|
246 |
entry.position[0] + entry.size.width as f32 / 2.0,
|
|
247 |
entry.position[1] - entry.size.height as f32 / 2.0,
|
|
248 |
],
|
15787
|
249 |
tex_coords: sprite_coords[3],
|
15786
|
250 |
},
|
|
251 |
];
|
|
252 |
|
|
253 |
data.extend_from_slice(&[v[0], v[1], v[2], v[1], v[3], v[2]]);
|
|
254 |
}
|
|
255 |
|
15211
|
256 |
let projection = camera.projection();
|
15784
|
257 |
self.shader.bind();
|
|
258 |
self.shader.set_matrix("projection", projection.as_ptr());
|
15786
|
259 |
self.shader.bind_texture_2d(0, &self.texture);
|
15784
|
260 |
|
|
261 |
self.vertex_buffer.write_typed(&data);
|
|
262 |
let _buffer_bind = self.layout.bind(&[(0, &self.vertex_buffer)], None);
|
15786
|
263 |
|
15784
|
264 |
let _state = PipelineState::new().with_blend();
|
|
265 |
|
|
266 |
unsafe {
|
15785
|
267 |
gl::DrawArrays(gl::TRIANGLES, 0, entries.len() as i32 * 6);
|
15784
|
268 |
}
|
15211
|
269 |
}
|
15141
|
270 |
}
|
15211
|
271 |
|
15311
|
272 |
fn load_sprite_pixels(path: &Path, buffer: &mut [u8]) -> io::Result<Size> {
|
15307
|
273 |
let decoder = Decoder::new(BufReader::new(File::open(path)?));
|
|
274 |
let (info, mut reader) = decoder.read_info()?;
|
|
275 |
|
|
276 |
let size = Size::new(info.width as usize, info.height as usize);
|
|
277 |
reader.next_frame(buffer)?;
|
15311
|
278 |
Ok(size)
|
15307
|
279 |
}
|
|
280 |
|
|
281 |
fn load_sprite_size(path: &Path) -> io::Result<Size> {
|
15211
|
282 |
let decoder = Decoder::new(BufReader::new(File::open(path)?));
|
|
283 |
let (info, mut reader) = decoder.read_info()?;
|
|
284 |
|
|
285 |
let size = Size::new(info.width as usize, info.height as usize);
|
|
286 |
Ok(size)
|
|
287 |
}
|
|
288 |
|
15307
|
289 |
fn load_sprites(path: &Path) -> io::Result<Vec<SpriteData>> {
|
15211
|
290 |
let mut result = vec![];
|
|
291 |
for file in read_dir(path)? {
|
|
292 |
let file = file?;
|
|
293 |
if let Some(extension) = file.path().extension() {
|
|
294 |
if extension == "png" {
|
15307
|
295 |
let path = file.path();
|
|
296 |
let sprite = load_sprite_size(&path)?;
|
|
297 |
result.push(SpriteData {
|
|
298 |
size: sprite,
|
|
299 |
filename: path,
|
|
300 |
});
|
15211
|
301 |
}
|
|
302 |
}
|
|
303 |
}
|
|
304 |
Ok(result)
|
|
305 |
}
|