mirror of
https://github.com/exoticorn/microw8.git
synced 2026-01-20 11:16:42 +01:00
restructure control flow of uw8-window to hopefully make it work on MacOS
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user