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