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