Skip to content

Commit

Permalink
Fixed segfault from ui
Browse files Browse the repository at this point in the history
  • Loading branch information
AJMC2002 committed Nov 14, 2023
1 parent 6015f88 commit 19f87ad
Show file tree
Hide file tree
Showing 4 changed files with 297 additions and 382 deletions.
File renamed without changes.
File renamed without changes.
111 changes: 57 additions & 54 deletions src/graphics/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ impl Window {
.create_window(width, height, title, glfw::WindowMode::Windowed)
.expect("Failed to create GLFW window!");

window.make_current();

window.set_key_polling(true);
window.set_cursor_pos_polling(true);
window.set_mouse_button_polling(true);
Expand All @@ -47,11 +45,14 @@ impl Window {
window.set_cursor_pos(last_pos.0, last_pos.1);
window.set_sticky_keys(true);

glfw.set_swap_interval(glfw::SwapInterval::Sync(1));
window.make_current();

gl::load_with(|symbol| window.get_proc_address(symbol) as *const _);

gl::Viewport::load_with(|symbol| window.get_proc_address(symbol) as *const _);

glfw.set_swap_interval(glfw::SwapInterval::Sync(1));

unsafe {
gl::Disable(gl::CULL_FACE);
gl::Enable(gl::DEPTH_TEST);
Expand Down Expand Up @@ -105,6 +106,25 @@ impl Window {
self.time_delta
}

pub fn begin_ui(&mut self) {
self.ui.begin_frame(&self.window, &mut self.glfw)
}

pub fn end_ui(&mut self) {
let (w, h) = self.window.get_framebuffer_size();
let output = self.ui.end_frame((w as _, h as _));
if !output.platform_output.copied_text.is_empty() {
match copypasta_ext::try_context() {
Some(mut context) => context
.set_contents(output.platform_output.copied_text)
.unwrap(),
None => {
eprintln!("enable to gather context for clipboard");
}
}
}
}

pub fn update(&mut self) {
self.glfw.poll_events();
self.process_events();
Expand Down Expand Up @@ -140,40 +160,40 @@ impl Window {
WindowEvent::Scroll(x_offset, y_offset) => {
self.camera.scroll_callback(x_offset, y_offset)
}
// //egui bindings
// glfw::WindowEvent::Key(
// glfw::Key::X,
// _,
// glfw::Action::Press,
// glfw::Modifiers::Control,
// ) => {
// self.ui.push_event(egui::Event::Cut);
// }
// glfw::WindowEvent::Key(
// glfw::Key::C,
// _,
// glfw::Action::Press,
// glfw::Modifiers::Control,
// ) => {
// self.ui.push_event(egui::Event::Copy);
// }
// glfw::WindowEvent::Key(
// glfw::Key::V,
// _,
// glfw::Action::Press,
// glfw::Modifiers::Control,
// ) => {
// let text = match copypasta_ext::try_context() {
// Some(mut context) => Some(context.get_contents().unwrap()),
// None => {
// eprintln!("enable to gather context for clipboard");
// None
// }
// };
// if let Some(text) = text {
// self.ui.push_event(egui::Event::Text(text));
// }
// }
//egui bindings
glfw::WindowEvent::Key(
glfw::Key::X,
_,
glfw::Action::Press,
glfw::Modifiers::Control,
) => {
self.ui.push_event(egui::Event::Cut);
}
glfw::WindowEvent::Key(
glfw::Key::C,
_,
glfw::Action::Press,
glfw::Modifiers::Control,
) => {
self.ui.push_event(egui::Event::Copy);
}
glfw::WindowEvent::Key(
glfw::Key::V,
_,
glfw::Action::Press,
glfw::Modifiers::Control,
) => {
let text = match copypasta_ext::try_context() {
Some(mut context) => Some(context.get_contents().unwrap()),
None => {
eprintln!("enable to gather context for clipboard");
None
}
};
if let Some(text) = text {
self.ui.push_event(egui::Event::Text(text));
}
}
_ => (),
}
}
Expand All @@ -190,23 +210,6 @@ impl Window {
}
}
}

pub fn render_ui(&mut self, render_ui: fn(egui::Context)) {
self.ui.begin_frame(&self.window, &mut self.glfw);
render_ui(self.ui.get_egui_ctx().to_owned());
let (w, h) = self.window.get_framebuffer_size();
let output = self.ui.end_frame((w as _, h as _));
if !output.platform_output.copied_text.is_empty() {
match copypasta_ext::try_context() {
Some(mut context) => context
.set_contents(output.platform_output.copied_text)
.unwrap(),
None => {
eprintln!("enable to gather context for clipboard");
}
}
}
}
}

extern "system" fn debug_callback(
Expand Down
Loading

0 comments on commit 19f87ad

Please sign in to comment.