rewrite it in rust #1
|
@ -1,4 +1,5 @@
|
|||
/shrooms-vb
|
||||
/shrooms-vb.exe
|
||||
.vscode
|
||||
output
|
||||
/shrooms-vb
|
||||
/shrooms-vb.exe
|
||||
.vscode
|
||||
output
|
||||
/target
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,16 @@
|
|||
[package]
|
||||
name = "shrooms-vb-native"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
anyhow = "1"
|
||||
imgui = "0.12"
|
||||
imgui-wgpu = { git = "https://github.com/Yatekii/imgui-wgpu-rs", rev = "2edd348" }
|
||||
imgui-winit-support = "0.13"
|
||||
pollster = "0.4"
|
||||
wgpu = "22.1"
|
||||
winit = "0.30"
|
||||
|
||||
[build-dependencies]
|
||||
cc = "1"
|
|
@ -0,0 +1,11 @@
|
|||
use std::path::Path;
|
||||
|
||||
fn main() {
|
||||
cc::Build::new()
|
||||
.include(Path::new("shrooms-vb-core/core"))
|
||||
.opt_level(3)
|
||||
.flag_if_supported("-flto")
|
||||
.flag_if_supported("-fno-strict-aliasing")
|
||||
.file(Path::new("shrooms-vb-core/core/vb.c"))
|
||||
.compile("vb");
|
||||
}
|
|
@ -0,0 +1,325 @@
|
|||
use imgui::*;
|
||||
use imgui_wgpu::{Renderer, RendererConfig};
|
||||
use imgui_winit_support::WinitPlatform;
|
||||
use pollster::block_on;
|
||||
use std::{sync::Arc, time::Instant};
|
||||
use winit::{
|
||||
application::ApplicationHandler,
|
||||
dpi::LogicalSize,
|
||||
event::{Event, WindowEvent},
|
||||
event_loop::{ActiveEventLoop, ControlFlow, EventLoop},
|
||||
keyboard::{Key, NamedKey},
|
||||
window::Window,
|
||||
};
|
||||
|
||||
struct ImguiState {
|
||||
context: imgui::Context,
|
||||
platform: WinitPlatform,
|
||||
renderer: Renderer,
|
||||
clear_color: wgpu::Color,
|
||||
last_frame: Instant,
|
||||
last_cursor: Option<MouseCursor>,
|
||||
}
|
||||
|
||||
struct AppWindow {
|
||||
device: wgpu::Device,
|
||||
queue: wgpu::Queue,
|
||||
window: Arc<Window>,
|
||||
surface_desc: wgpu::SurfaceConfiguration,
|
||||
surface: wgpu::Surface<'static>,
|
||||
hidpi_factor: f64,
|
||||
imgui: Option<ImguiState>,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct App {
|
||||
window: Option<AppWindow>,
|
||||
}
|
||||
|
||||
impl AppWindow {
|
||||
fn setup_gpu(event_loop: &ActiveEventLoop) -> Self {
|
||||
let instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
|
||||
backends: wgpu::Backends::PRIMARY,
|
||||
..Default::default()
|
||||
});
|
||||
|
||||
let window = {
|
||||
let size = LogicalSize::new(384, 244);
|
||||
|
||||
let attributes = Window::default_attributes()
|
||||
.with_inner_size(size)
|
||||
.with_title("Shrooms VB");
|
||||
Arc::new(event_loop.create_window(attributes).unwrap())
|
||||
};
|
||||
|
||||
let size = window.inner_size();
|
||||
let hidpi_factor = window.scale_factor();
|
||||
let surface = instance.create_surface(window.clone()).unwrap();
|
||||
|
||||
let adapter = block_on(instance.request_adapter(&wgpu::RequestAdapterOptions {
|
||||
power_preference: wgpu::PowerPreference::HighPerformance,
|
||||
compatible_surface: Some(&surface),
|
||||
force_fallback_adapter: false,
|
||||
}))
|
||||
.unwrap();
|
||||
|
||||
let (device, queue) =
|
||||
block_on(adapter.request_device(&wgpu::DeviceDescriptor::default(), None)).unwrap();
|
||||
|
||||
// Set up swap chain
|
||||
let surface_desc = wgpu::SurfaceConfiguration {
|
||||
usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
|
||||
format: wgpu::TextureFormat::Bgra8UnormSrgb,
|
||||
width: size.width,
|
||||
height: size.height,
|
||||
present_mode: wgpu::PresentMode::Fifo,
|
||||
desired_maximum_frame_latency: 2,
|
||||
alpha_mode: wgpu::CompositeAlphaMode::Auto,
|
||||
view_formats: vec![wgpu::TextureFormat::Bgra8Unorm],
|
||||
};
|
||||
|
||||
surface.configure(&device, &surface_desc);
|
||||
|
||||
let imgui = None;
|
||||
Self {
|
||||
device,
|
||||
queue,
|
||||
window,
|
||||
surface_desc,
|
||||
surface,
|
||||
hidpi_factor,
|
||||
imgui,
|
||||
}
|
||||
}
|
||||
|
||||
fn setup_imgui(&mut self) {
|
||||
let mut context = imgui::Context::create();
|
||||
let mut platform = imgui_winit_support::WinitPlatform::new(&mut context);
|
||||
platform.attach_window(
|
||||
context.io_mut(),
|
||||
&self.window,
|
||||
imgui_winit_support::HiDpiMode::Default,
|
||||
);
|
||||
context.set_ini_filename(None);
|
||||
|
||||
let font_size = (13.0 * self.hidpi_factor) as f32;
|
||||
context.io_mut().font_global_scale = (1.0 / self.hidpi_factor) as f32;
|
||||
|
||||
context.fonts().add_font(&[FontSource::DefaultFontData {
|
||||
config: Some(imgui::FontConfig {
|
||||
oversample_h: 1,
|
||||
pixel_snap_h: true,
|
||||
size_pixels: font_size,
|
||||
..Default::default()
|
||||
}),
|
||||
}]);
|
||||
|
||||
//
|
||||
// Set up dear imgui wgpu renderer
|
||||
//
|
||||
let clear_color = wgpu::Color::BLACK;
|
||||
|
||||
let renderer_config = RendererConfig {
|
||||
texture_format: self.surface_desc.format,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let renderer = Renderer::new(&mut context, &self.device, &self.queue, renderer_config);
|
||||
let last_frame = Instant::now();
|
||||
let last_cursor = None;
|
||||
|
||||
self.imgui = Some(ImguiState {
|
||||
context,
|
||||
platform,
|
||||
renderer,
|
||||
clear_color,
|
||||
last_frame,
|
||||
last_cursor,
|
||||
})
|
||||
}
|
||||
|
||||
fn new(event_loop: &ActiveEventLoop) -> Self {
|
||||
let mut window = Self::setup_gpu(event_loop);
|
||||
window.setup_imgui();
|
||||
window
|
||||
}
|
||||
}
|
||||
|
||||
impl ApplicationHandler for App {
|
||||
fn resumed(&mut self, event_loop: &ActiveEventLoop) {
|
||||
self.window = Some(AppWindow::new(event_loop));
|
||||
}
|
||||
|
||||
fn window_event(
|
||||
&mut self,
|
||||
event_loop: &ActiveEventLoop,
|
||||
window_id: winit::window::WindowId,
|
||||
event: WindowEvent,
|
||||
) {
|
||||
let window = self.window.as_mut().unwrap();
|
||||
let imgui = window.imgui.as_mut().unwrap();
|
||||
let mut quit = false;
|
||||
|
||||
match &event {
|
||||
WindowEvent::Resized(size) => {
|
||||
window.surface_desc = wgpu::SurfaceConfiguration {
|
||||
usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
|
||||
format: wgpu::TextureFormat::Bgra8UnormSrgb,
|
||||
width: size.width,
|
||||
height: size.height,
|
||||
present_mode: wgpu::PresentMode::Fifo,
|
||||
desired_maximum_frame_latency: 2,
|
||||
alpha_mode: wgpu::CompositeAlphaMode::Auto,
|
||||
view_formats: vec![wgpu::TextureFormat::Bgra8Unorm],
|
||||
};
|
||||
|
||||
window
|
||||
.surface
|
||||
.configure(&window.device, &window.surface_desc);
|
||||
}
|
||||
WindowEvent::CloseRequested => event_loop.exit(),
|
||||
WindowEvent::KeyboardInput { event, .. } => {
|
||||
if let Key::Named(NamedKey::Escape) = event.logical_key {
|
||||
if event.state.is_pressed() {
|
||||
quit = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
WindowEvent::RedrawRequested => {
|
||||
let now = Instant::now();
|
||||
imgui
|
||||
.context
|
||||
.io_mut()
|
||||
.update_delta_time(now - imgui.last_frame);
|
||||
imgui.last_frame = now;
|
||||
|
||||
let frame = match window.surface.get_current_texture() {
|
||||
Ok(frame) => frame,
|
||||
Err(e) => {
|
||||
eprintln!("dropped frame: {e:?}");
|
||||
return;
|
||||
}
|
||||
};
|
||||
imgui
|
||||
.platform
|
||||
.prepare_frame(imgui.context.io_mut(), &window.window)
|
||||
.expect("Failed to prepare frame");
|
||||
let ui = imgui.context.frame();
|
||||
ui.main_menu_bar(|| {
|
||||
ui.menu("ROM", || {
|
||||
if ui.menu_item("Open ROM") {
|
||||
println!("clicked");
|
||||
}
|
||||
if ui.menu_item("Quit") {
|
||||
event_loop.exit();
|
||||
}
|
||||
});
|
||||
ui.menu("Emulation", || {
|
||||
if ui.menu_item("Pause") {
|
||||
println!("clicked");
|
||||
}
|
||||
if ui.menu_item("Reset") {
|
||||
println!("clicked");
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
let mut encoder: wgpu::CommandEncoder = window
|
||||
.device
|
||||
.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None });
|
||||
|
||||
if imgui.last_cursor != ui.mouse_cursor() {
|
||||
imgui.last_cursor = ui.mouse_cursor();
|
||||
imgui.platform.prepare_render(ui, &window.window);
|
||||
}
|
||||
|
||||
let view = frame
|
||||
.texture
|
||||
.create_view(&wgpu::TextureViewDescriptor::default());
|
||||
let mut rpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
|
||||
label: None,
|
||||
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
|
||||
view: &view,
|
||||
resolve_target: None,
|
||||
ops: wgpu::Operations {
|
||||
load: wgpu::LoadOp::Clear(imgui.clear_color),
|
||||
store: wgpu::StoreOp::Store,
|
||||
},
|
||||
})],
|
||||
depth_stencil_attachment: None,
|
||||
timestamp_writes: None,
|
||||
occlusion_query_set: None,
|
||||
});
|
||||
|
||||
imgui
|
||||
.renderer
|
||||
.render(
|
||||
imgui.context.render(),
|
||||
&window.queue,
|
||||
&window.device,
|
||||
&mut rpass,
|
||||
)
|
||||
.expect("Rendering failed");
|
||||
|
||||
drop(rpass);
|
||||
|
||||
window.queue.submit(Some(encoder.finish()));
|
||||
|
||||
frame.present();
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
|
||||
imgui.platform.handle_event::<()>(
|
||||
imgui.context.io_mut(),
|
||||
&window.window,
|
||||
&Event::WindowEvent { window_id, event },
|
||||
);
|
||||
|
||||
if quit {
|
||||
self.window.take();
|
||||
}
|
||||
}
|
||||
|
||||
fn user_event(&mut self, _event_loop: &ActiveEventLoop, event: ()) {
|
||||
let window = self.window.as_mut().unwrap();
|
||||
let imgui = window.imgui.as_mut().unwrap();
|
||||
imgui.platform.handle_event::<()>(
|
||||
imgui.context.io_mut(),
|
||||
&window.window,
|
||||
&Event::UserEvent(event),
|
||||
);
|
||||
}
|
||||
|
||||
fn device_event(
|
||||
&mut self,
|
||||
_event_loop: &ActiveEventLoop,
|
||||
device_id: winit::event::DeviceId,
|
||||
event: winit::event::DeviceEvent,
|
||||
) {
|
||||
let window = self.window.as_mut().unwrap();
|
||||
let imgui = window.imgui.as_mut().unwrap();
|
||||
imgui.platform.handle_event::<()>(
|
||||
imgui.context.io_mut(),
|
||||
&window.window,
|
||||
&Event::DeviceEvent { device_id, event },
|
||||
);
|
||||
}
|
||||
|
||||
fn about_to_wait(&mut self, _event_loop: &ActiveEventLoop) {
|
||||
let window = self.window.as_mut().unwrap();
|
||||
let imgui = window.imgui.as_mut().unwrap();
|
||||
window.window.request_redraw();
|
||||
imgui.platform.handle_event::<()>(
|
||||
imgui.context.io_mut(),
|
||||
&window.window,
|
||||
&Event::AboutToWait,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let event_loop = EventLoop::new().unwrap();
|
||||
event_loop.set_control_flow(ControlFlow::Poll);
|
||||
event_loop.run_app(&mut App::default()).unwrap();
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
|
||||
#[repr(C)]
|
||||
struct VB { _data: [u8; 0] }
|
||||
|
||||
#[link(name = "vb")]
|
||||
extern "C" {
|
||||
#[link_name = "vbInit"]
|
||||
fn vb_init(sim: *mut VB) -> *mut VB;
|
||||
|
||||
#[link_name = "vbSizeOf"]
|
||||
fn vb_size_of() -> usize;
|
||||
|
||||
#[link_name = "vbGetKeys"]
|
||||
fn vb_get_keys(sim: *mut VB) -> u16;
|
||||
}
|
||||
|
||||
pub struct CoreVB {
|
||||
sim: *mut VB,
|
||||
_data: Box<[u8]>,
|
||||
}
|
||||
|
||||
impl CoreVB {
|
||||
pub fn new() -> Self {
|
||||
let size = unsafe { vb_size_of() };
|
||||
let mut data = vec![0; size].into_boxed_slice();
|
||||
let sim = data.as_mut_ptr() as *mut VB;
|
||||
unsafe { vb_init(sim) };
|
||||
CoreVB {
|
||||
sim,
|
||||
_data: data
|
||||
}
|
||||
}
|
||||
|
||||
pub fn keys(&self) -> u16 {
|
||||
unsafe { vb_get_keys(self.sim) }
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue