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)] = &[
|
15954
|
74 |
(SpriteId::Mine, "Graphics/MineOn.png"),
|
|
75 |
(SpriteId::Grenade, "Graphics/Bomb.png"),
|
|
76 |
(SpriteId::Cheese, "Graphics/cheese.png"),
|
|
77 |
(SpriteId::Cleaver, "Graphics/cleaver.png"),
|
15781
|
78 |
];
|
|
79 |
|
|
80 |
const MAX_SPRITES: usize = SpriteId::MaxSprite as usize + 1;
|
|
81 |
|
15787
|
82 |
type SpriteTexCoords = (u32, [[f32; 2]; 4]);
|
|
83 |
|
15784
|
84 |
pub struct GearEntry {
|
|
85 |
position: [f32; 2],
|
|
86 |
size: Size,
|
|
87 |
}
|
|
88 |
|
|
89 |
impl GearEntry {
|
|
90 |
pub fn new(x: f32, y: f32, size: Size) -> Self {
|
|
91 |
Self {
|
|
92 |
position: [x, y],
|
|
93 |
size,
|
|
94 |
}
|
|
95 |
}
|
|
96 |
}
|
|
97 |
|
15211
|
98 |
pub struct GearRenderer {
|
15141
|
99 |
atlas: AtlasCollection,
|
15786
|
100 |
texture: Texture2D,
|
15787
|
101 |
allocation: Box<[SpriteTexCoords; MAX_SPRITES]>,
|
15784
|
102 |
shader: Shader,
|
|
103 |
layout: InputLayout,
|
|
104 |
vertex_buffer: Buffer,
|
15141
|
105 |
}
|
|
106 |
|
15307
|
107 |
struct SpriteData {
|
|
108 |
size: Size,
|
|
109 |
filename: PathBuf,
|
|
110 |
}
|
|
111 |
|
15311
|
112 |
const ATLAS_SIZE: Size = Size::square(2048);
|
15141
|
113 |
|
|
114 |
impl GearRenderer {
|
15918
|
115 |
pub fn new(data_path: &Path) -> Self {
|
15211
|
116 |
let mut atlas = AtlasCollection::new(ATLAS_SIZE);
|
15307
|
117 |
|
15782
|
118 |
let texture = Texture2D::new(
|
|
119 |
ATLAS_SIZE,
|
|
120 |
TextureInternalFormat::Rgba8,
|
|
121 |
TextureFilter::Linear,
|
|
122 |
);
|
15307
|
123 |
|
15787
|
124 |
let mut allocation = Box::new([Default::default(); MAX_SPRITES]);
|
15311
|
125 |
|
15781
|
126 |
for (sprite, file) in SPRITE_LOAD_LIST {
|
15918
|
127 |
let path = data_path.join(Path::new(file));
|
|
128 |
let size = load_sprite_size(path.as_path()).expect(&format!("Unable to open {}", file));
|
15781
|
129 |
let index = atlas
|
|
130 |
.insert_sprite(size)
|
|
131 |
.expect(&format!("Could not store sprite {:?}", sprite));
|
|
132 |
let (texture_index, rect) = atlas.get_rect(index).unwrap();
|
15311
|
133 |
|
15786
|
134 |
let mut pixels = vec![255u8; size.area() * 4].into_boxed_slice();
|
15918
|
135 |
load_sprite_pixels(path.as_path(), &mut pixels).expect("Unable to load Graphics");
|
15311
|
136 |
|
15781
|
137 |
texture.update(
|
|
138 |
rect,
|
15786
|
139 |
&pixels,
|
15782
|
140 |
None,
|
|
141 |
TextureFormat::Rgba,
|
|
142 |
TextureDataType::UnsignedByte,
|
15781
|
143 |
);
|
|
144 |
|
15787
|
145 |
let mut tex_coords = [
|
|
146 |
[rect.left() as f32, rect.bottom() as f32 + 1.0],
|
|
147 |
[rect.right() as f32 + 1.0, rect.bottom() as f32 + 1.0],
|
|
148 |
[rect.left() as f32, rect.top() as f32],
|
|
149 |
[rect.right() as f32 + 1.0, rect.top() as f32],
|
|
150 |
]; //.map(|n| n as f32 / ATLAS_SIZE as f32);
|
|
151 |
|
|
152 |
for coords in &mut tex_coords {
|
|
153 |
coords[0] /= ATLAS_SIZE.width as f32;
|
|
154 |
coords[1] /= ATLAS_SIZE.height as f32;
|
|
155 |
}
|
|
156 |
|
|
157 |
allocation[*sprite as usize] = (texture_index, tex_coords);
|
15307
|
158 |
}
|
|
159 |
|
15786
|
160 |
let shader = Shader::new(
|
|
161 |
VERTEX_SHADER,
|
|
162 |
Some(PIXEL_SHADER),
|
|
163 |
&[VariableBinding::Sampler("texture", 0)],
|
|
164 |
)
|
|
165 |
.unwrap();
|
15784
|
166 |
|
|
167 |
let layout = InputLayout::new(vec![
|
|
168 |
InputElement {
|
|
169 |
shader_slot: 0,
|
|
170 |
buffer_slot: 0,
|
|
171 |
format: InputFormat::Float(gl::FLOAT, false),
|
|
172 |
components: 2,
|
|
173 |
stride: size_of::<Vertex>() as u32,
|
|
174 |
offset: 0,
|
|
175 |
},
|
15786
|
176 |
InputElement {
|
|
177 |
shader_slot: 1,
|
|
178 |
buffer_slot: 0,
|
|
179 |
format: InputFormat::Float(gl::FLOAT, false),
|
|
180 |
components: 2,
|
|
181 |
stride: size_of::<Vertex>() as u32,
|
|
182 |
offset: size_of::<[f32; 2]>() as u32,
|
|
183 |
},
|
15784
|
184 |
]);
|
|
185 |
|
|
186 |
let vertex_buffer = Buffer::empty(BufferType::Array, BufferUsage::DynamicDraw);
|
|
187 |
|
|
188 |
Self {
|
|
189 |
atlas,
|
15786
|
190 |
texture,
|
15784
|
191 |
allocation,
|
|
192 |
shader,
|
|
193 |
layout,
|
|
194 |
vertex_buffer,
|
|
195 |
}
|
15141
|
196 |
}
|
15211
|
197 |
|
15784
|
198 |
pub fn render(&mut self, camera: &Camera, entries: &[GearEntry]) {
|
15786
|
199 |
let mut data = Vec::with_capacity(entries.len() * 6);
|
|
200 |
|
15787
|
201 |
for (index, entry) in entries.iter().enumerate() {
|
|
202 |
let sprite_id = match index & 0b11 {
|
|
203 |
0 => SpriteId::Mine,
|
|
204 |
1 => SpriteId::Grenade,
|
|
205 |
2 => SpriteId::Cheese,
|
|
206 |
_ => SpriteId::Cleaver,
|
|
207 |
};
|
|
208 |
let sprite_coords = &self.allocation[sprite_id as usize].1;
|
|
209 |
|
15786
|
210 |
let v = [
|
|
211 |
Vertex {
|
|
212 |
position: [
|
|
213 |
entry.position[0] - entry.size.width as f32 / 2.0,
|
|
214 |
entry.position[1] + entry.size.height as f32 / 2.0,
|
|
215 |
],
|
15787
|
216 |
tex_coords: sprite_coords[0],
|
15786
|
217 |
},
|
|
218 |
Vertex {
|
|
219 |
position: [
|
|
220 |
entry.position[0] + entry.size.width as f32 / 2.0,
|
|
221 |
entry.position[1] + entry.size.height as f32 / 2.0,
|
|
222 |
],
|
15787
|
223 |
tex_coords: sprite_coords[1],
|
15786
|
224 |
},
|
|
225 |
Vertex {
|
|
226 |
position: [
|
|
227 |
entry.position[0] - entry.size.width as f32 / 2.0,
|
|
228 |
entry.position[1] - entry.size.height as f32 / 2.0,
|
|
229 |
],
|
15787
|
230 |
tex_coords: sprite_coords[2],
|
15786
|
231 |
},
|
|
232 |
Vertex {
|
|
233 |
position: [
|
|
234 |
entry.position[0] + entry.size.width as f32 / 2.0,
|
|
235 |
entry.position[1] - entry.size.height as f32 / 2.0,
|
|
236 |
],
|
15787
|
237 |
tex_coords: sprite_coords[3],
|
15786
|
238 |
},
|
|
239 |
];
|
|
240 |
|
|
241 |
data.extend_from_slice(&[v[0], v[1], v[2], v[1], v[3], v[2]]);
|
|
242 |
}
|
|
243 |
|
15211
|
244 |
let projection = camera.projection();
|
15784
|
245 |
self.shader.bind();
|
|
246 |
self.shader.set_matrix("projection", projection.as_ptr());
|
15786
|
247 |
self.shader.bind_texture_2d(0, &self.texture);
|
15784
|
248 |
|
|
249 |
self.vertex_buffer.write_typed(&data);
|
|
250 |
let _buffer_bind = self.layout.bind(&[(0, &self.vertex_buffer)], None);
|
15786
|
251 |
|
15784
|
252 |
let _state = PipelineState::new().with_blend();
|
|
253 |
unsafe {
|
15785
|
254 |
gl::DrawArrays(gl::TRIANGLES, 0, entries.len() as i32 * 6);
|
15784
|
255 |
}
|
15211
|
256 |
}
|
15141
|
257 |
}
|
15211
|
258 |
|
15311
|
259 |
fn load_sprite_pixels(path: &Path, buffer: &mut [u8]) -> io::Result<Size> {
|
15307
|
260 |
let decoder = Decoder::new(BufReader::new(File::open(path)?));
|
|
261 |
let (info, mut reader) = decoder.read_info()?;
|
|
262 |
|
|
263 |
let size = Size::new(info.width as usize, info.height as usize);
|
|
264 |
reader.next_frame(buffer)?;
|
15311
|
265 |
Ok(size)
|
15307
|
266 |
}
|
|
267 |
|
|
268 |
fn load_sprite_size(path: &Path) -> io::Result<Size> {
|
15211
|
269 |
let decoder = Decoder::new(BufReader::new(File::open(path)?));
|
|
270 |
let (info, mut reader) = decoder.read_info()?;
|
|
271 |
|
|
272 |
let size = Size::new(info.width as usize, info.height as usize);
|
|
273 |
Ok(size)
|
|
274 |
}
|
|
275 |
|
15307
|
276 |
fn load_sprites(path: &Path) -> io::Result<Vec<SpriteData>> {
|
15211
|
277 |
let mut result = vec![];
|
|
278 |
for file in read_dir(path)? {
|
|
279 |
let file = file?;
|
|
280 |
if let Some(extension) = file.path().extension() {
|
|
281 |
if extension == "png" {
|
15307
|
282 |
let path = file.path();
|
|
283 |
let sprite = load_sprite_size(&path)?;
|
|
284 |
result.push(SpriteData {
|
|
285 |
size: sprite,
|
|
286 |
filename: path,
|
|
287 |
});
|
15211
|
288 |
}
|
|
289 |
}
|
|
290 |
}
|
|
291 |
Ok(result)
|
|
292 |
}
|