-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.rs
109 lines (91 loc) · 3.11 KB
/
lib.rs
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
104
105
106
107
108
109
pub mod prelude {
pub use crate::{SoftBufferOptions, SoftBufferPlugin, SoftBufferResource, SoftBufferStage};
}
pub use softbuffer;
use bevy::{
diagnostic::{Diagnostic, DiagnosticId, Diagnostics},
prelude::*,
window::{WindowBackendScaleFactorChanged, WindowId, WindowResized},
winit::WinitWindows,
};
use softbuffer::GraphicsContext;
use std::time::Instant;
#[derive(Debug, Hash, PartialEq, Eq, Clone, StageLabel)]
pub enum SoftBufferStage {
Draw,
Render,
}
#[derive(Debug, Clone)]
pub struct SoftBufferOptions {
/// Width of the pixel buffer
pub width: u32,
/// Height of the pixel buffer
pub height: u32,
}
impl Default for SoftBufferOptions {
fn default() -> Self {
SoftBufferOptions {
width: 180,
height: 120,
}
}
}
pub struct SoftBufferResource {
pub buffer: Vec<u32>,
pub window_id: WindowId,
graphics_context: GraphicsContext,
}
pub struct SoftBufferPlugin;
impl Plugin for SoftBufferPlugin {
fn build(&self, app: &mut App) {
app.add_stage_after(
CoreStage::PostUpdate,
SoftBufferStage::Draw,
SystemStage::parallel(),
)
.add_stage_after(
SoftBufferStage::Draw,
SoftBufferStage::Render,
SystemStage::parallel(),
)
.init_resource::<SoftBufferOptions>()
.add_startup_system_to_stage(StartupStage::PreStartup, Self::setup)
.add_system_to_stage(SoftBufferStage::Render, Self::render);
}
}
impl SoftBufferPlugin {
pub const RENDER_TIME: DiagnosticId =
DiagnosticId::from_u128(1187582084072339577959028643519383692);
pub fn setup(
mut commands: Commands,
mut diagnostics: ResMut<Diagnostics>,
options: Res<SoftBufferOptions>,
windows: Res<Windows>,
winit_windows: NonSend<WinitWindows>,
) {
diagnostics.add(Diagnostic::new(Self::RENDER_TIME, "render_time", 20).with_suffix("s"));
let window_id = windows
.get_primary()
.expect("primary window not found")
.id();
let winit_window = winit_windows
.get_window(window_id)
.expect("failed to get primary winit window");
//let window_size = winit_window.inner_size();
let buffer = vec![0u32; options.width as usize * options.height as usize];
let graphics_context = unsafe { GraphicsContext::new(winit_window) }.unwrap();
commands.insert_resource(SoftBufferResource { buffer, window_id, graphics_context });
}
pub fn render(
resource: Res<SoftBufferResource>,
options: Res<SoftBufferOptions>,
mut diagnostics: ResMut<Diagnostics>
) {
let start = Instant::now();
//resource.pixels.render().expect("failed to render pixels");
resource.graphics_context.set_buffer(&resource.buffer, options.width as u16, options.height as u16);
let end = Instant::now();
let render_time = end.duration_since(start);
diagnostics.add_measurement(Self::RENDER_TIME, || render_time.as_secs_f64());
}
}