-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrender.rs
More file actions
103 lines (90 loc) · 3.21 KB
/
render.rs
File metadata and controls
103 lines (90 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
use std::sync::Arc;
use winit::{dpi::PhysicalSize, event_loop::ActiveEventLoop, window::Window};
use crate::{
coordinates::WINDOW_SIZE,
palettes::{Palette, RGB8},
};
#[cfg(all(feature = "gpu_scaling", feature = "soft_scaling"))]
compile_error!("it's impossible to have both gpu and software scaling enabled");
#[cfg(not(any(feature = "gpu_scaling", feature = "soft_scaling")))]
compile_error!("at least one of gpu_scaling or soft_scaling needs to be active");
#[cfg(feature = "gpu_scaling")]
pub struct RenderBackend {
backend: crate::gpu::GPUState,
}
#[cfg(feature = "gpu_scaling")]
impl RenderBackend {
pub fn new(window: Arc<Window>, palette: Palette<RGB8>) -> Self {
let mut backend = smol::block_on(crate::gpu::GPUState::new(window));
backend.queue_palette_update(palette.into());
Self { backend }
}
pub fn resize(&mut self, size: PhysicalSize<u32>) {
self.backend.resize(size);
}
pub fn render(
&mut self,
frame_buffer: &[[u8; WINDOW_SIZE.0]; WINDOW_SIZE.1],
event_loop: &ActiveEventLoop,
) {
match self.backend.render(frame_buffer) {
Ok(_) => {}
Err(wgpu::SurfaceError::Lost) => self.backend.reinit_surface(),
Err(wgpu::SurfaceError::OutOfMemory) => event_loop.exit(),
Err(e) => eprint!("{:?}", e),
}
}
}
#[cfg(feature = "soft_scaling")]
pub struct RenderBackend {
backend: softbuffer::Surface<Arc<Window>, Arc<Window>>,
width: u32,
height: u32,
palette: Palette<crate::palettes::ZRGB>,
}
#[cfg(feature = "soft_scaling")]
impl RenderBackend {
pub fn new(window: Arc<Window>, palette: Palette<RGB8>) -> Self {
let size = window.inner_size();
let context = softbuffer::Context::new(window.clone()).unwrap();
Self {
backend: softbuffer::Surface::new(&context, window).unwrap(),
width: size.width,
height: size.height,
palette: palette.into(),
}
}
pub fn resize(&mut self, size: PhysicalSize<u32>) {
self.width = size.width;
self.height = size.height;
self.backend
.resize(
std::num::NonZeroU32::new(size.width).unwrap(),
std::num::NonZeroU32::new(size.height).unwrap(),
)
.unwrap()
}
pub fn render(
&mut self,
frame_buffer: &[[u8; WINDOW_SIZE.0]; WINDOW_SIZE.1],
_: &ActiveEventLoop,
) {
let mut buffer = self.backend.buffer_mut().unwrap();
assert!(buffer.len() == usize::try_from(self.width * self.height).unwrap());
let x_step = WINDOW_SIZE.0 as f32 / self.width as f32;
let y_step = WINDOW_SIZE.1 as f32 / self.height as f32;
for (y_idx, row) in buffer
.chunks_exact_mut(self.width.try_into().unwrap())
.enumerate()
{
for (x_idx, pixel) in row.iter_mut().enumerate() {
let x_idx = x_idx as f32 * x_step;
let y_idx = y_idx as f32 * y_step;
*pixel = self
.palette
.get_raw(frame_buffer[y_idx.floor() as usize][x_idx.floor() as usize]);
}
}
buffer.present().unwrap();
}
}