chore: refactor gui code
This commit is contained in:
parent
41bc4f7afe
commit
77af5453f0
517
src/gui.rs
517
src/gui.rs
|
@ -1,18 +1,19 @@
|
|||
use egui::{ClippedPrimitive, Context, TextureId};
|
||||
use egui_wgpu_backend::{BackendError, RenderPass, ScreenDescriptor};
|
||||
use egui::{Context, TextureId};
|
||||
use egui_wgpu_backend::{RenderPass, ScreenDescriptor};
|
||||
use egui_winit_platform::Platform;
|
||||
use wgpu::{
|
||||
Adapter, CommandEncoder, Device, Extent3d, FilterMode, Instance, Queue, RequestDeviceError,
|
||||
Surface, SurfaceConfiguration, SurfaceTexture, Texture, TextureFormat, TextureUsages,
|
||||
TextureView,
|
||||
Adapter, Backends, Color, CommandEncoder, Device, Extent3d, FilterMode, Instance, Queue,
|
||||
RequestDeviceError, Surface, SurfaceConfiguration, Texture, TextureFormat, TextureUsages,
|
||||
TextureView, TextureViewDescriptor,
|
||||
};
|
||||
use winit::dpi::PhysicalSize;
|
||||
use winit::error::OsError;
|
||||
use winit::event::{ElementState, KeyboardInput};
|
||||
use winit::event_loop::EventLoop;
|
||||
use winit::event::{ElementState, Event, KeyboardInput};
|
||||
use winit::event_loop::{ControlFlow, EventLoop};
|
||||
use winit::window::Window;
|
||||
|
||||
use crate::cpu::Cpu;
|
||||
use crate::{GB_HEIGHT, GB_WIDTH};
|
||||
use crate::{emu, GB_HEIGHT, GB_WIDTH};
|
||||
|
||||
const EGUI_DIMENSIONS: (usize, usize) = (1280, 720);
|
||||
const FILTER_MODE: FilterMode = FilterMode::Nearest;
|
||||
|
@ -20,219 +21,156 @@ const WINDOW_TITLE: &str = "DMG-01 Emulator";
|
|||
|
||||
const SCALE: f32 = 3.0;
|
||||
|
||||
/// Holds GUI State
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct GuiState {
|
||||
/// When true, egui winit should exit the application
|
||||
pub quit: bool,
|
||||
pub struct Gui {
|
||||
pub should_quit: bool,
|
||||
pub title: String,
|
||||
pub mode: EmuMode,
|
||||
|
||||
window: Window,
|
||||
platform: Platform,
|
||||
|
||||
surface: Surface,
|
||||
device: Device,
|
||||
queue: Queue,
|
||||
surface_config: SurfaceConfiguration,
|
||||
render_pass: RenderPass,
|
||||
|
||||
texture: Texture,
|
||||
texture_size: Extent3d,
|
||||
texture_id: TextureId,
|
||||
}
|
||||
|
||||
impl GuiState {
|
||||
pub fn new(title: String) -> Self {
|
||||
Self {
|
||||
title,
|
||||
quit: Default::default(),
|
||||
mode: EmuMode::Running,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum EmuMode {
|
||||
Running,
|
||||
StepFrame,
|
||||
Stopped,
|
||||
Step,
|
||||
}
|
||||
|
||||
/// To avoid using an [Option<KeyboardInput>] to keep track of user input from winit,
|
||||
/// we can use a "default" value. However, in order for this to work the chosen "default"
|
||||
/// value must be an **unused** key, so that it is ignored by the emulator.
|
||||
pub fn unused_key() -> KeyboardInput {
|
||||
#![allow(deprecated)]
|
||||
|
||||
KeyboardInput {
|
||||
scancode: Default::default(),
|
||||
state: ElementState::Released,
|
||||
virtual_keycode: Default::default(),
|
||||
modifiers: Default::default(), // this argument is deprecated
|
||||
}
|
||||
}
|
||||
|
||||
pub fn build_window<T>(event_loop: &EventLoop<T>) -> Result<Window, OsError> {
|
||||
use winit::dpi::PhysicalSize;
|
||||
use winit::window::WindowBuilder;
|
||||
|
||||
WindowBuilder::new()
|
||||
.with_decorations(true)
|
||||
.with_resizable(true)
|
||||
.with_transparent(false)
|
||||
.with_title(WINDOW_TITLE)
|
||||
.with_inner_size(PhysicalSize {
|
||||
width: EGUI_DIMENSIONS.0 as f32,
|
||||
height: EGUI_DIMENSIONS.1 as f32,
|
||||
})
|
||||
.build(event_loop)
|
||||
}
|
||||
|
||||
pub fn create_surface(window: &Window) -> (Instance, Surface) {
|
||||
use wgpu::Backends;
|
||||
impl Gui {
|
||||
pub fn new<T>(title: String, event_loop: &EventLoop<T>, cpu: &Cpu) -> Self {
|
||||
let window = build_window(event_loop).expect("build window");
|
||||
|
||||
let instance = Instance::new(Backends::PRIMARY);
|
||||
let surface = unsafe { instance.create_surface(window) };
|
||||
(instance, surface)
|
||||
}
|
||||
let surface = unsafe { instance.create_surface(&window) };
|
||||
|
||||
pub fn request_adapter(instance: &Instance, surface: &Surface) -> Option<Adapter> {
|
||||
use wgpu::{PowerPreference, RequestAdapterOptions};
|
||||
let adapter = request_adapter(&instance, &surface).expect("request adaptor");
|
||||
let (device, queue) = request_device(&adapter).expect("request device");
|
||||
let texture_format = surface.get_supported_formats(&adapter)[0]; // First is preferred
|
||||
|
||||
pollster::block_on(instance.request_adapter(&RequestAdapterOptions {
|
||||
power_preference: PowerPreference::HighPerformance,
|
||||
force_fallback_adapter: false, // TODO: What do I want to do with this?
|
||||
compatible_surface: Some(surface),
|
||||
}))
|
||||
}
|
||||
let surface_config = surface_config(&window, texture_format);
|
||||
surface.configure(&device, &surface_config);
|
||||
let platform = platform(&window);
|
||||
let mut render_pass = RenderPass::new(&device, texture_format, 1);
|
||||
|
||||
pub fn request_device(adapter: &Adapter) -> Result<(Device, Queue), RequestDeviceError> {
|
||||
use wgpu::{DeviceDescriptor, Features, Limits};
|
||||
let texture_size = texture_size();
|
||||
let texture = create_texture(&device, texture_size);
|
||||
write_to_texture(&queue, &texture, emu::pixel_buf(&cpu), texture_size);
|
||||
|
||||
pollster::block_on(adapter.request_device(
|
||||
&DeviceDescriptor {
|
||||
label: None,
|
||||
features: Features::default(),
|
||||
limits: Limits::default(),
|
||||
},
|
||||
None,
|
||||
))
|
||||
}
|
||||
let view = texture.create_view(&TextureViewDescriptor::default());
|
||||
let texture_id = expose_texture_to_egui(&mut render_pass, &device, &view);
|
||||
|
||||
pub fn surface_config(window: &Window, format: TextureFormat) -> SurfaceConfiguration {
|
||||
use wgpu::PresentMode;
|
||||
Self {
|
||||
should_quit: Default::default(),
|
||||
title,
|
||||
mode: EmuMode::Running,
|
||||
|
||||
let size = window.inner_size();
|
||||
SurfaceConfiguration {
|
||||
usage: TextureUsages::RENDER_ATTACHMENT,
|
||||
format,
|
||||
width: size.width as u32,
|
||||
height: size.height as u32,
|
||||
present_mode: PresentMode::Mailbox,
|
||||
}
|
||||
}
|
||||
window,
|
||||
platform,
|
||||
|
||||
pub fn platform_desc(window: &Window) -> Platform {
|
||||
use egui::FontDefinitions;
|
||||
use egui_winit_platform::PlatformDescriptor;
|
||||
surface,
|
||||
device,
|
||||
queue,
|
||||
surface_config,
|
||||
render_pass,
|
||||
|
||||
let size = window.inner_size();
|
||||
Platform::new(PlatformDescriptor {
|
||||
physical_width: size.width as u32,
|
||||
physical_height: size.height as u32,
|
||||
scale_factor: window.scale_factor(),
|
||||
font_definitions: FontDefinitions::default(),
|
||||
..Default::default()
|
||||
})
|
||||
}
|
||||
|
||||
pub fn texture_size() -> Extent3d {
|
||||
Extent3d {
|
||||
width: GB_WIDTH as u32,
|
||||
height: GB_HEIGHT as u32,
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn create_texture(device: &Device, size: Extent3d) -> Texture {
|
||||
use wgpu::{TextureDescriptor, TextureDimension};
|
||||
|
||||
device.create_texture(&TextureDescriptor {
|
||||
size,
|
||||
mip_level_count: 1,
|
||||
sample_count: 1,
|
||||
dimension: TextureDimension::D2,
|
||||
format: TextureFormat::Rgba8UnormSrgb,
|
||||
usage: TextureUsages::COPY_DST | TextureUsages::TEXTURE_BINDING,
|
||||
label: Some("gb_pixel_buffer"),
|
||||
})
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn write_to_texture(
|
||||
queue: &Queue,
|
||||
texture: &Texture,
|
||||
data: &[u8; GB_WIDTH * 4 * GB_HEIGHT],
|
||||
size: Extent3d,
|
||||
) {
|
||||
use std::num::NonZeroU32;
|
||||
use wgpu::{ImageCopyTexture, ImageDataLayout, Origin3d, TextureAspect};
|
||||
|
||||
queue.write_texture(
|
||||
ImageCopyTexture {
|
||||
texture,
|
||||
mip_level: 0,
|
||||
origin: Origin3d::ZERO,
|
||||
aspect: TextureAspect::All,
|
||||
},
|
||||
data,
|
||||
ImageDataLayout {
|
||||
offset: 0,
|
||||
bytes_per_row: NonZeroU32::new(4 * GB_WIDTH as u32),
|
||||
rows_per_image: NonZeroU32::new(GB_HEIGHT as u32),
|
||||
},
|
||||
size,
|
||||
);
|
||||
texture_size,
|
||||
texture_id,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn expose_texture_to_egui(
|
||||
render_pass: &mut RenderPass,
|
||||
device: &Device,
|
||||
view: &TextureView,
|
||||
) -> TextureId {
|
||||
render_pass.egui_texture_from_wgpu_texture(device, view, FILTER_MODE)
|
||||
pub fn maybe_quit(self: &Self, cpu: &Cpu, control_flow: &mut ControlFlow) {
|
||||
if self.should_quit {
|
||||
emu::save_and_exit(cpu, control_flow);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn request_redraw(self: &Self) {
|
||||
self.window.request_redraw();
|
||||
}
|
||||
|
||||
pub fn handle_event<T>(self: &mut Self, event: &Event<T>) {
|
||||
self.platform.handle_event(event);
|
||||
}
|
||||
|
||||
pub fn update_time(self: &mut Self, elapsed_seconds: f64) {
|
||||
self.platform.update_time(elapsed_seconds);
|
||||
}
|
||||
|
||||
pub fn resize(self: &mut Self, size: PhysicalSize<u32>) {
|
||||
// See: https://github.com/rust-windowing/winit/issues/208
|
||||
if size.width > 0 && size.height > 0 {
|
||||
self.surface_config.width = size.width;
|
||||
self.surface_config.height = size.height;
|
||||
self.surface.configure(&self.device, &self.surface_config);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn paint(self: &mut Self, cpu: &Cpu) {
|
||||
use wgpu::SurfaceError;
|
||||
|
||||
let data = emu::pixel_buf(cpu);
|
||||
write_to_texture(&self.queue, &self.texture, data, self.texture_size);
|
||||
|
||||
let output_frame = match self.surface.get_current_texture() {
|
||||
Ok(frame) => frame,
|
||||
Err(SurfaceError::Outdated) => return, // Occurs on minimization on Windows
|
||||
Err(e) => {
|
||||
eprintln!("Dropped frame with error: {}", e);
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
let output_view = output_frame
|
||||
.texture
|
||||
.create_view(&TextureViewDescriptor::default());
|
||||
|
||||
// Begin to draw Egui components
|
||||
self.platform.begin_frame();
|
||||
self.draw_egui(cpu, &self.platform.context(), self.texture_id);
|
||||
|
||||
// End the UI frame. We could now handle the output and draw the UI with the backend.
|
||||
let full_output = self.platform.end_frame(Some(&self.window));
|
||||
let paint_jobs = self.platform.context().tessellate(full_output.shapes);
|
||||
|
||||
let mut encoder = create_command_encoder(&self.device);
|
||||
let screen_descriptor = create_screen_descriptor(&self.window, &self.surface_config);
|
||||
let tdelta = full_output.textures_delta;
|
||||
// Upload all resources for the GPU.
|
||||
self.render_pass
|
||||
.add_textures(&self.device, &self.queue, &tdelta)
|
||||
.expect("add texture ok");
|
||||
self.render_pass
|
||||
.update_buffers(&self.device, &self.queue, &paint_jobs, &screen_descriptor);
|
||||
|
||||
// Record all render passes.
|
||||
self.render_pass
|
||||
.execute(
|
||||
&mut encoder,
|
||||
&output_view,
|
||||
&paint_jobs,
|
||||
&screen_descriptor,
|
||||
Some(Color::BLACK),
|
||||
)
|
||||
.expect("execute render pass");
|
||||
|
||||
// Submit the commands.
|
||||
self.queue.submit(std::iter::once(encoder.finish()));
|
||||
|
||||
// Redraw egui
|
||||
output_frame.present();
|
||||
|
||||
self.render_pass
|
||||
.remove_textures(tdelta)
|
||||
.expect("remove texture delta");
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn create_view(frame: &SurfaceTexture) -> TextureView {
|
||||
use wgpu::TextureViewDescriptor;
|
||||
|
||||
frame.texture.create_view(&TextureViewDescriptor::default())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn create_command_encoder(device: &Device) -> CommandEncoder {
|
||||
use wgpu::CommandEncoderDescriptor;
|
||||
|
||||
device.create_command_encoder(&CommandEncoderDescriptor {
|
||||
label: Some("encoder"),
|
||||
})
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn create_screen_descriptor(
|
||||
window: &Window,
|
||||
config: &SurfaceConfiguration,
|
||||
) -> ScreenDescriptor {
|
||||
ScreenDescriptor {
|
||||
physical_width: config.width,
|
||||
physical_height: config.height,
|
||||
scale_factor: window.scale_factor() as f32,
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn execute_render_pass(
|
||||
render_pass: &mut RenderPass,
|
||||
encoder: &mut CommandEncoder,
|
||||
view: &TextureView,
|
||||
jobs: Vec<ClippedPrimitive>,
|
||||
descriptor: &ScreenDescriptor,
|
||||
) -> Result<(), BackendError> {
|
||||
render_pass.execute(encoder, view, &jobs, descriptor, Some(wgpu::Color::BLACK))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn draw_egui(cpu: &Cpu, app: &mut GuiState, ctx: &Context, texture_id: TextureId) {
|
||||
pub fn draw_egui(self: &mut Self, cpu: &Cpu, ctx: &Context, texture_id: TextureId) {
|
||||
use crate::{cpu, instruction, ppu};
|
||||
|
||||
fn selectable_text(ui: &mut egui::Ui, mut text: &str) -> egui::Response {
|
||||
|
@ -242,11 +180,11 @@ pub fn draw_egui(cpu: &Cpu, app: &mut GuiState, ctx: &Context, texture_id: Textu
|
|||
egui::TopBottomPanel::top("top_panel").show(ctx, |ui| {
|
||||
ui.menu_button("File", |ui| {
|
||||
if ui.button("Quit").clicked() {
|
||||
app.quit = true;
|
||||
self.should_quit = true;
|
||||
}
|
||||
});
|
||||
|
||||
egui::Window::new(&app.title).show(ctx, |ui| {
|
||||
egui::Window::new(&self.title).show(ctx, |ui| {
|
||||
ui.image(
|
||||
texture_id,
|
||||
[GB_WIDTH as f32 * SCALE, GB_HEIGHT as f32 * SCALE],
|
||||
|
@ -259,12 +197,12 @@ pub fn draw_egui(cpu: &Cpu, app: &mut GuiState, ctx: &Context, texture_id: Textu
|
|||
|
||||
egui::Window::new("Settings").show(ctx, |ui| {
|
||||
egui::ComboBox::from_label("Emulation Mode")
|
||||
.selected_text(format!("{:?}", app.mode))
|
||||
.selected_text(format!("{:?}", self.mode))
|
||||
.show_ui(ui, |ui| {
|
||||
ui.selectable_value(&mut app.mode, EmuMode::Running, "Running");
|
||||
ui.selectable_value(&mut app.mode, EmuMode::Stopped, "Stopped");
|
||||
ui.selectable_value(&mut app.mode, EmuMode::StepFrame, "Step Frame");
|
||||
ui.selectable_value(&mut app.mode, EmuMode::Step, "Step");
|
||||
ui.selectable_value(&mut self.mode, EmuMode::Running, "Running");
|
||||
ui.selectable_value(&mut self.mode, EmuMode::Stopped, "Stopped");
|
||||
ui.selectable_value(&mut self.mode, EmuMode::StepFrame, "Step Frame");
|
||||
ui.selectable_value(&mut self.mode, EmuMode::Step, "Step");
|
||||
})
|
||||
});
|
||||
|
||||
|
@ -349,6 +287,169 @@ pub fn draw_egui(cpu: &Cpu, app: &mut GuiState, ctx: &Context, texture_id: Textu
|
|||
})
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum EmuMode {
|
||||
Running,
|
||||
StepFrame,
|
||||
Stopped,
|
||||
Step,
|
||||
}
|
||||
|
||||
/// To avoid using an [Option<KeyboardInput>] to keep track of user input from winit,
|
||||
/// we can use a "default" value. However, in order for this to work the chosen "default"
|
||||
/// value must be an **unused** key, so that it is ignored by the emulator.
|
||||
pub fn unused_key() -> KeyboardInput {
|
||||
#![allow(deprecated)]
|
||||
|
||||
KeyboardInput {
|
||||
scancode: Default::default(),
|
||||
state: ElementState::Released,
|
||||
virtual_keycode: Default::default(),
|
||||
modifiers: Default::default(), // this argument is deprecated
|
||||
}
|
||||
}
|
||||
|
||||
fn build_window<T>(event_loop: &EventLoop<T>) -> Result<Window, OsError> {
|
||||
use winit::window::WindowBuilder;
|
||||
|
||||
WindowBuilder::new()
|
||||
.with_decorations(true)
|
||||
.with_resizable(true)
|
||||
.with_transparent(false)
|
||||
.with_title(WINDOW_TITLE)
|
||||
.with_inner_size(PhysicalSize {
|
||||
width: EGUI_DIMENSIONS.0 as f32,
|
||||
height: EGUI_DIMENSIONS.1 as f32,
|
||||
})
|
||||
.build(event_loop)
|
||||
}
|
||||
|
||||
fn request_adapter(instance: &Instance, surface: &Surface) -> Option<Adapter> {
|
||||
use wgpu::{PowerPreference, RequestAdapterOptions};
|
||||
|
||||
pollster::block_on(instance.request_adapter(&RequestAdapterOptions {
|
||||
power_preference: PowerPreference::HighPerformance,
|
||||
compatible_surface: Some(surface),
|
||||
force_fallback_adapter: false, // TODO: What do I want to do with this?
|
||||
}))
|
||||
}
|
||||
|
||||
fn request_device(adapter: &Adapter) -> Result<(Device, Queue), RequestDeviceError> {
|
||||
use wgpu::{DeviceDescriptor, Features, Limits};
|
||||
|
||||
pollster::block_on(adapter.request_device(
|
||||
&DeviceDescriptor {
|
||||
features: Features::default(),
|
||||
limits: Limits::default(),
|
||||
label: None,
|
||||
},
|
||||
None,
|
||||
))
|
||||
}
|
||||
|
||||
fn surface_config(window: &Window, format: TextureFormat) -> SurfaceConfiguration {
|
||||
use wgpu::PresentMode;
|
||||
|
||||
let size = window.inner_size();
|
||||
SurfaceConfiguration {
|
||||
usage: TextureUsages::RENDER_ATTACHMENT,
|
||||
format,
|
||||
width: size.width as u32,
|
||||
height: size.height as u32,
|
||||
present_mode: PresentMode::Mailbox,
|
||||
}
|
||||
}
|
||||
|
||||
fn platform(window: &Window) -> Platform {
|
||||
use egui::FontDefinitions;
|
||||
use egui_winit_platform::PlatformDescriptor;
|
||||
|
||||
let size = window.inner_size();
|
||||
Platform::new(PlatformDescriptor {
|
||||
physical_width: size.width as u32,
|
||||
physical_height: size.height as u32,
|
||||
scale_factor: window.scale_factor(),
|
||||
font_definitions: FontDefinitions::default(),
|
||||
..Default::default()
|
||||
})
|
||||
}
|
||||
|
||||
fn texture_size() -> Extent3d {
|
||||
Extent3d {
|
||||
width: GB_WIDTH as u32,
|
||||
height: GB_HEIGHT as u32,
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
fn create_texture(device: &Device, size: Extent3d) -> Texture {
|
||||
use wgpu::{TextureDescriptor, TextureDimension};
|
||||
|
||||
device.create_texture(&TextureDescriptor {
|
||||
size,
|
||||
mip_level_count: 1,
|
||||
sample_count: 1,
|
||||
dimension: TextureDimension::D2,
|
||||
format: TextureFormat::Rgba8UnormSrgb,
|
||||
usage: TextureUsages::COPY_DST | TextureUsages::TEXTURE_BINDING,
|
||||
label: Some("gb_pixel_buffer"),
|
||||
})
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn write_to_texture(
|
||||
queue: &Queue,
|
||||
texture: &Texture,
|
||||
data: &[u8; GB_WIDTH * 4 * GB_HEIGHT],
|
||||
size: Extent3d,
|
||||
) {
|
||||
use std::num::NonZeroU32;
|
||||
use wgpu::{ImageCopyTexture, ImageDataLayout, Origin3d, TextureAspect};
|
||||
|
||||
queue.write_texture(
|
||||
ImageCopyTexture {
|
||||
texture,
|
||||
mip_level: 0,
|
||||
origin: Origin3d::ZERO,
|
||||
aspect: TextureAspect::All,
|
||||
},
|
||||
data,
|
||||
ImageDataLayout {
|
||||
offset: 0,
|
||||
bytes_per_row: NonZeroU32::new(4 * GB_WIDTH as u32),
|
||||
rows_per_image: NonZeroU32::new(GB_HEIGHT as u32),
|
||||
},
|
||||
size,
|
||||
);
|
||||
}
|
||||
|
||||
fn expose_texture_to_egui(
|
||||
render_pass: &mut RenderPass,
|
||||
device: &Device,
|
||||
view: &TextureView,
|
||||
) -> TextureId {
|
||||
render_pass.egui_texture_from_wgpu_texture(device, view, FILTER_MODE)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn create_command_encoder(device: &Device) -> CommandEncoder {
|
||||
use wgpu::CommandEncoderDescriptor;
|
||||
|
||||
device.create_command_encoder(&CommandEncoderDescriptor {
|
||||
label: Some("encoder"),
|
||||
})
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn create_screen_descriptor(window: &Window, config: &SurfaceConfiguration) -> ScreenDescriptor {
|
||||
ScreenDescriptor {
|
||||
physical_width: config.width,
|
||||
physical_height: config.height,
|
||||
scale_factor: window.scale_factor() as f32,
|
||||
}
|
||||
}
|
||||
|
||||
pub mod kbd {
|
||||
use winit::event::{ElementState, KeyboardInput, VirtualKeyCode};
|
||||
|
|
102
src/main.rs
102
src/main.rs
|
@ -1,14 +1,11 @@
|
|||
use std::time::Instant;
|
||||
|
||||
use clap::{crate_authors, crate_description, crate_name, crate_version, Arg, Command};
|
||||
use egui_wgpu_backend::RenderPass;
|
||||
use gb::gui::EmuMode;
|
||||
use gb::gui::{EmuMode, Gui};
|
||||
use gb::{emu, gui};
|
||||
use gilrs::Gilrs;
|
||||
use gui::GuiState;
|
||||
use rodio::{OutputStream, Sink};
|
||||
use tracing_subscriber::EnvFilter;
|
||||
use wgpu::TextureViewDescriptor;
|
||||
use winit::event::{Event, WindowEvent};
|
||||
use winit::event_loop::{EventLoop, EventLoopBuilder};
|
||||
|
||||
|
@ -47,22 +44,7 @@ fn main() {
|
|||
.with_env_filter(EnvFilter::from_default_env())
|
||||
.init();
|
||||
|
||||
// --Here lies a lot of winit + wgpu Boilerplate--
|
||||
let event_loop: EventLoop<Event<()>> = EventLoopBuilder::with_user_event().build();
|
||||
let window = gui::build_window(&event_loop).expect("build window");
|
||||
|
||||
let (instance, surface) = gui::create_surface(&window);
|
||||
let adapter = gui::request_adapter(&instance, &surface).expect("request adaptor");
|
||||
let (device, queue) = gui::request_device(&adapter).expect("request device");
|
||||
let format = surface.get_supported_formats(&adapter)[0]; // First is preferred
|
||||
|
||||
let mut config = gui::surface_config(&window, format);
|
||||
surface.configure(&device, &config);
|
||||
let mut platform = gui::platform_desc(&window);
|
||||
let mut render_pass = RenderPass::new(&device, format, 1);
|
||||
|
||||
// We interrupt your boiler plate to initialize the emulator so that
|
||||
// we can copy it's empty pixel buffer to the GPU
|
||||
// Init CPU
|
||||
let mut cpu = match m.value_of("boot") {
|
||||
Some(path) => {
|
||||
tracing::info!("User-provided boot ROM");
|
||||
|
@ -74,22 +56,12 @@ fn main() {
|
|||
}
|
||||
};
|
||||
|
||||
// Set up the wgpu (and then EGUI) texture we'll be working with.
|
||||
let texture_size = gui::texture_size();
|
||||
let texture = gui::create_texture(&device, texture_size);
|
||||
gui::write_to_texture(&queue, &texture, emu::pixel_buf(&cpu), texture_size);
|
||||
|
||||
let view = texture.create_view(&TextureViewDescriptor::default());
|
||||
let texture_id = gui::expose_texture_to_egui(&mut render_pass, &device, &view);
|
||||
|
||||
// Load ROM if filepath was provided
|
||||
if let Some(path) = m.value_of("rom") {
|
||||
tracing::info!("User-provided cartridge ROM");
|
||||
emu::read_game_rom(&mut cpu, path).expect("read game rom from path");
|
||||
}
|
||||
|
||||
emu::load_save(&mut cpu);
|
||||
|
||||
let rom_title = emu::rom_title(&cpu).to_string();
|
||||
|
||||
tracing::info!("Initialize Gamepad");
|
||||
|
@ -116,20 +88,20 @@ fn main() {
|
|||
}
|
||||
|
||||
// Set up state for the Immediate-mode GUI
|
||||
let mut app = GuiState::new(rom_title);
|
||||
let mut last_key = gui::unused_key();
|
||||
let event_loop: EventLoop<Event<()>> = EventLoopBuilder::with_user_event().build();
|
||||
|
||||
let mut app = Gui::new(rom_title, &event_loop, &cpu);
|
||||
let mut last_key = gui::unused_key(); // TODO: Fix this awful impl
|
||||
|
||||
// used for egui animations
|
||||
let start_time = Instant::now();
|
||||
|
||||
event_loop.run(move |event, _, control_flow| {
|
||||
platform.handle_event(&event);
|
||||
app.handle_event(&event);
|
||||
|
||||
match event {
|
||||
Event::MainEventsCleared => {
|
||||
if app.quit {
|
||||
emu::save_and_exit(&cpu, control_flow);
|
||||
}
|
||||
app.maybe_quit(&cpu, control_flow);
|
||||
|
||||
match app.mode {
|
||||
EmuMode::Running => emu::run_frame(&mut cpu, &mut gamepad, last_key),
|
||||
|
@ -145,64 +117,14 @@ fn main() {
|
|||
// Input has been consumed, reset it
|
||||
last_key = gui::unused_key();
|
||||
|
||||
window.request_redraw();
|
||||
app.request_redraw();
|
||||
}
|
||||
Event::RedrawRequested(..) => {
|
||||
platform.update_time(start_time.elapsed().as_secs_f64());
|
||||
|
||||
let data = emu::pixel_buf(&cpu);
|
||||
gui::write_to_texture(&queue, &texture, data, texture_size);
|
||||
|
||||
let output_frame = match surface.get_current_texture() {
|
||||
Ok(frame) => frame,
|
||||
Err(e) => {
|
||||
eprintln!("Dropped frame with error: {}", e);
|
||||
return;
|
||||
}
|
||||
};
|
||||
let output_view = gui::create_view(&output_frame);
|
||||
|
||||
// Begin to draw Egui components
|
||||
platform.begin_frame();
|
||||
gui::draw_egui(&cpu, &mut app, &platform.context(), texture_id);
|
||||
// End the UI frame. We could now handle the output and draw the UI with the backend.
|
||||
let full_output = platform.end_frame(Some(&window));
|
||||
let paint_jobs = platform.context().tessellate(full_output.shapes);
|
||||
|
||||
let mut encoder = gui::create_command_encoder(&device);
|
||||
let screen_descriptor = gui::create_screen_descriptor(&window, &config);
|
||||
let tdelta = full_output.textures_delta;
|
||||
// Upload all resources for the GPU.
|
||||
render_pass
|
||||
.add_textures(&device, &queue, &tdelta)
|
||||
.expect("add texture ok");
|
||||
render_pass.update_buffers(&device, &queue, &paint_jobs, &screen_descriptor);
|
||||
|
||||
// Record all render passes.
|
||||
gui::execute_render_pass(
|
||||
&mut render_pass,
|
||||
&mut encoder,
|
||||
&output_view,
|
||||
paint_jobs,
|
||||
&screen_descriptor,
|
||||
)
|
||||
.expect("record render passes");
|
||||
|
||||
// Submit the commands.
|
||||
queue.submit(std::iter::once(encoder.finish()));
|
||||
|
||||
// Redraw egui
|
||||
output_frame.present();
|
||||
app.update_time(start_time.elapsed().as_secs_f64());
|
||||
}
|
||||
Event::WindowEvent { event, .. } => match event {
|
||||
WindowEvent::Resized(size) => {
|
||||
config.width = size.width;
|
||||
config.height = size.height;
|
||||
surface.configure(&device, &config);
|
||||
}
|
||||
WindowEvent::CloseRequested => {
|
||||
emu::save_and_exit(&cpu, control_flow);
|
||||
}
|
||||
WindowEvent::Resized(size) => app.resize(size),
|
||||
WindowEvent::CloseRequested => emu::save_and_exit(&cpu, control_flow),
|
||||
WindowEvent::KeyboardInput { input, .. } => last_key = input,
|
||||
_ => {}
|
||||
},
|
||||
|
|
Loading…
Reference in New Issue