Drawing static to a window

This commit is contained in:
2022-02-19 15:28:13 -06:00
parent fb8cee8d52
commit 99b39239ba
3 changed files with 1423 additions and 4 deletions
Generated
+1353 -1
View File
File diff suppressed because it is too large Load Diff
+4 -2
View File
@@ -1,9 +1,11 @@
[package]
name = "vulcan-emu"
version = "0.1.0"
edition = "2018"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
rand = "0.8.0"
rand = "0.8.0"
winit = "0.26.1"
pixels = "0.9.0"
+66 -1
View File
@@ -4,6 +4,71 @@ mod opcodes;
mod cpu;
mod bus;
use winit::{
event::{ Event, WindowEvent },
event_loop::{ EventLoop, ControlFlow },
window::WindowBuilder,
dpi::LogicalSize
};
use pixels::{Error, Pixels, SurfaceTexture};
use rand::RngCore;
use rand::prelude::ThreadRng;
use std::time::{Instant, Duration};
use pixels::wgpu::Instance;
use std::convert::TryInto;
fn main() {
println!("Hello, world!");
let event_loop = EventLoop::new();
let window = {
let size = LogicalSize::new(640, 480);
WindowBuilder::new()
.with_title("Vulcan")
.with_inner_size(size)
.with_min_inner_size(size)
.build(&event_loop)
.unwrap()
};
let mut pixels = {
let surface_texture = SurfaceTexture::new(640, 480, &window);
Pixels::new(640, 480, surface_texture).unwrap()
};
event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Poll;
match event {
Event::WindowEvent {
event: WindowEvent::CloseRequested,
window_id
} if window_id == window.id() => {
*control_flow = ControlFlow::Exit
}
Event::MainEventsCleared => {
let start = Instant::now();
draw(pixels.get_frame());
let draw_time = Instant::now() - start;
pixels.render();
let total_time = Instant::now() - start;
println!("Tick took {} total, {} to draw", total_time.as_micros(), draw_time.as_micros());
}
_ => {}
}
})
}
fn draw(frame: &mut [u8]) {
assert_eq!(frame.len(), 640 * 480 * 4);
let mut rng = rand::thread_rng();
for (i, pixel) in frame.chunks_exact_mut(4).enumerate() {
let p = rng.next_u32();
let [low, mid, high, _] = p.to_le_bytes();
pixel[0] = low;
pixel[1] = mid;
pixel[2] = high;
pixel[3] = 0xff;
}
}