restructure control flow of uw8-window to hopefully make it work on MacOS

This commit is contained in:
2022-07-21 08:51:17 +02:00
parent 57a92ba79a
commit 499bb02f2c
6 changed files with 250 additions and 267 deletions

View File

@@ -1,24 +1,44 @@
use anyhow::Result;
use std::time::Instant;
mod cpu;
mod gpu;
pub fn run<F: 'static + FnMut(&mut dyn Framebuffer, u32, bool) -> Instant>(
gpu: bool,
update: F,
) -> ! {
if gpu {
match gpu::Window::new() {
Ok(window) => window.run(Box::new(update)),
Err(err) => eprintln!(
"Failed to create gpu window: {}\nFalling back to cpu window",
err
),
pub struct Window(Box<dyn WindowImpl>);
impl Window {
pub fn new(gpu: bool) -> Result<Window> {
if gpu {
match gpu::Window::new() {
Ok(window) => return Ok(Window(Box::new(window))),
Err(err) => eprintln!(
"Failed to create gpu window: {}\nFalling back tp cpu window",
err
),
}
}
cpu::Window::new().map(|window| Window(Box::new(window)))
}
pub fn begin_frame(&mut self) -> Input {
self.0.begin_frame()
}
pub fn end_frame(&mut self, framebuffer: &[u8], palette: &[u8], next_frame: Instant) {
self.0.end_frame(framebuffer, palette, next_frame)
}
pub fn is_open(&self) -> bool {
self.0.is_open()
}
cpu::run(Box::new(update));
}
pub trait Framebuffer {
fn update(&mut self, pixels: &[u8], palette: &[u8]);
pub struct Input {
pub gamepads: [u8; 4],
pub reset: bool,
}
trait WindowImpl {
fn begin_frame(&mut self) -> Input;
fn end_frame(&mut self, framebuffer: &[u8], palette: &[u8], next_frame: Instant);
fn is_open(&self) -> bool;
}