Skip to content

Commit

Permalink
Share GUI implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
dcvz committed Jul 4, 2023
1 parent 71c10ca commit 650e5ae
Show file tree
Hide file tree
Showing 5 changed files with 380 additions and 37 deletions.
4 changes: 2 additions & 2 deletions cpp/helix.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ void HLXDisplaySetup(const char* title, void (*draw_menu)(void*), void (*draw_wi
}

void HLXDisplayStartFrame() {
_frame = GUIStartFrame(_gui, _event_loop);
GUIStartFrame(_gui, _event_loop);
}

void HLXDisplayProcessDrawLists(u64* commands) {
GUIDrawLists(_gui, _frame, commands);
GUIDrawLists(_gui, commands);
}

void HLXDisplayEndFrame() {
Expand Down
4 changes: 2 additions & 2 deletions include/helix/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ void AudioPlayerQueueBuffer(void* player, const uint8_t* buf, size_t len);
// GUI
void* GUICreateEventLoop(void);
void* GUICreate(const char* title, void* event_loop, void (*draw_menu_callback)(void*), void (*draw_windows_callback)(void*), void* gamepad_manager);
void* GUIStartFrame(void* gui, void* event_loop);
void GUIDrawLists(void* gui, void* frame, uint64_t* commands);
void GUIStartFrame(void* gui, void* event_loop);
void GUIDrawLists(void* gui, uint64_t* commands);
void GUIEndFrame(void* gui);

f32 GUIGetAspectRatio(void* gui);
Expand Down
77 changes: 45 additions & 32 deletions src/gui.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::gamepad::manager::GamepadManager;
use fast3d::output::RCPOutput;
use fast3d::rcp::RCP;
use fast3d::rdp::OutputDimensions;
use winit::platform::run_return::EventLoopExtRunReturn;

pub mod windows;
Expand Down Expand Up @@ -75,13 +76,8 @@ impl<'a> Gui<'a> {
// Setup ImGui
let mut imgui = imgui::Context::create();

// Setup Renderer
let (width, height) = (800, 600);
let renderer = Renderer::new(width, height, title, event_loop_wrapper, &mut imgui)?;

// Create the imgui + winit platform
let mut platform = imgui_winit_support::WinitPlatform::init(&mut imgui);
renderer.attach_window(&mut platform, &mut imgui);

// Setup Dear ImGui style
imgui.set_ini_filename(None);
Expand All @@ -102,6 +98,11 @@ impl<'a> Gui<'a> {
}),
}]);

// Setup Renderer
let (width, height) = (800, 600);
let renderer = Renderer::new(width, height, title, event_loop_wrapper, &mut imgui)?;
renderer.attach_window(&mut platform, &mut imgui);

// Initial UI state
let last_frame_time = std::time::Instant::now();

Expand Down Expand Up @@ -139,14 +140,24 @@ impl<'a> Gui<'a> {
..
},
..
} => self.gfx_renderer.resize(size.width, size.height),
} => {
self.gfx_renderer.resize(size.width, size.height);

// TODO: Fix resizing on OpenGL
#[cfg(feature = "wgpu_renderer")]
self.gfx_renderer
.handle_event(&mut self.platform, &mut self.imgui, &event);
}
winit::event::Event::WindowEvent {
event: winit::event::WindowEvent::ModifiersChanged(modifiers),
..
} => {
if let Some(gamepad_manager) = self.gamepad_manager.as_mut() {
gamepad_manager.handle_modifiers_changed(modifiers);
}

self.gfx_renderer
.handle_event(&mut self.platform, &mut self.imgui, &event);
}
winit::event::Event::WindowEvent {
event: winit::event::WindowEvent::KeyboardInput { input, .. },
Expand All @@ -155,13 +166,15 @@ impl<'a> Gui<'a> {
if let Some(gamepad_manager) = self.gamepad_manager.as_mut() {
gamepad_manager.handle_keyboard_input(input);
}

self.gfx_renderer
.handle_event(&mut self.platform, &mut self.imgui, &event);
}
event => {
self.gfx_renderer
.handle_event(&mut self.platform, &mut self.imgui, &event)
}
_ => (),
}

// Forward events to ImGui
self.gfx_renderer
.handle_event(&mut self.platform, &mut self.imgui, &event);
});
}

Expand All @@ -184,25 +197,33 @@ impl<'a> Gui<'a> {
self.ui_state.last_frame_time = now;
}

pub fn start_frame(
&mut self,
event_loop_wrapper: &mut EventLoopWrapper,
) -> anyhow::Result<Option<Frame>> {
pub fn start_frame(&mut self, event_loop_wrapper: &mut EventLoopWrapper) -> anyhow::Result<()> {
// Handle events
self.handle_events(event_loop_wrapper);

// Prepare for drawing
self.gfx_renderer
.prepare_frame(&mut self.platform, &mut self.imgui)?;

// Grab the frame
Ok(self.gfx_renderer.get_current_texture())
Ok(())
}

pub fn process_draw_lists(&mut self, mut frame: Frame, commands: usize) -> anyhow::Result<()> {
pub fn process_draw_lists(&mut self, commands: usize) -> anyhow::Result<()> {
// Set RDP output dimensions
let size = self.gfx_renderer.window_size();
let dimensions = OutputDimensions {
width: size.width,
height: size.height,
aspect_ratio: size.width as f32 / size.height as f32,
};
self.rcp.rdp.output_dimensions = dimensions;

// Run the RCP
self.rcp.run(&mut self.rcp_output, commands);

// Grab the frame
let mut frame = self.gfx_renderer.get_current_texture().unwrap();

// Render RCP output
self.gfx_renderer
.process_rcp_output(&mut frame, &mut self.rcp_output)?;
Expand Down Expand Up @@ -230,9 +251,8 @@ impl<'a> Gui<'a> {
Ok(())
}

pub fn end_frame(&mut self) -> anyhow::Result<()> {
pub fn end_frame(&mut self) {
self.sync_frame_rate();
Ok(())
}
}

Expand Down Expand Up @@ -279,30 +299,23 @@ pub unsafe extern "C" fn GUICreate<'a>(
}

#[no_mangle]
pub extern "C" fn GUIStartFrame(
gui: Option<&mut Gui>,
event_loop: Option<&mut EventLoopWrapper>,
) -> Box<Option<Frame>> {
pub extern "C" fn GUIStartFrame(gui: Option<&mut Gui>, event_loop: Option<&mut EventLoopWrapper>) {
let gui = gui.unwrap();
let event_loop = event_loop.unwrap();
match gui.start_frame(event_loop) {
Ok(frame) => Box::new(frame),
Err(_) => Box::new(None),
}
gui.start_frame(event_loop).unwrap();
}

#[no_mangle]
pub extern "C" fn GUIDrawLists(gui: Option<&mut Gui>, frame: Option<Box<Frame>>, commands: u64) {
pub extern "C" fn GUIDrawLists(gui: Option<&mut Gui>, commands: u64) {
let gui = gui.unwrap();
let frame = frame.unwrap();
gui.process_draw_lists(*frame, commands.try_into().unwrap())
gui.process_draw_lists(commands.try_into().unwrap())
.unwrap();
}

#[no_mangle]
pub extern "C" fn GUIEndFrame(gui: Option<&mut Gui>) {
let gui = gui.unwrap();
gui.end_frame().unwrap();
gui.end_frame();
}

#[no_mangle]
Expand Down
5 changes: 5 additions & 0 deletions src/gui/glium_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,12 @@ impl<'a> Renderer<'a> {

// Rendering Functions

pub fn window_size(&self) -> winit::dpi::PhysicalSize<u32> {
self.display.gl_window().window().inner_size()
}

pub fn resize(&mut self, width: u32, height: u32) {
log::trace!("Resizing to {:?}x{:?}", width, height);
self.display
.gl_window()
.resize(glutin::dpi::PhysicalSize::new(width, height));
Expand Down
Loading

0 comments on commit 650e5ae

Please sign in to comment.