Skip to content

Commit

Permalink
Finally fixed the UI
Browse files Browse the repository at this point in the history
  • Loading branch information
AJMC2002 committed Sep 29, 2023
1 parent 885da18 commit 4c053f1
Show file tree
Hide file tree
Showing 13 changed files with 551 additions and 413 deletions.
11 changes: 9 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
egui_glfw_gl = "0.13.1"
egui = "0.22.0"
glfw = "0.41.0"
gl = "0.14"
#nalgebra-glm = "0.18.0"
copypasta-ext = "0.4.4"
#egui_demo_lib = "0.22.0"
egui_glfw = { branch = "v0.4.0-release", git = "https://github.com/ishbosamiya/egui_glfw.git" }
cgmath = "0.18.0"
image = "0.24.6"
impl_ops = "0.1.1"
cgmath = "0.18.0"
#glu-sys = "0.1.4"
11 changes: 0 additions & 11 deletions resources/shaders/basic/basic.frag

This file was deleted.

14 changes: 0 additions & 14 deletions resources/shaders/basic/basic.vert

This file was deleted.

3 changes: 3 additions & 0 deletions src/graphics.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
pub mod context;
pub mod geometry;
pub mod window;
pub mod wrapper;

pub use context::*;
pub use window::*;
269 changes: 170 additions & 99 deletions src/graphics/window.rs
Original file line number Diff line number Diff line change
@@ -1,99 +1,170 @@
use egui_glfw_gl::gl::types::GLenum;
use egui_glfw_gl::glfw;
use egui_glfw_gl::glfw::{Action, Context, Key, WindowEvent};
use egui_glfw_gl::{gl, EguiInputState};
use std::sync::mpsc::Receiver;

pub struct Window {
glfw: glfw::Glfw,
window: glfw::Window,
events: Receiver<(f64, WindowEvent)>,
}

impl Window {
pub fn new(width: u32, height: u32, title: &str) -> Window {
let mut glfw = glfw::init(glfw::FAIL_ON_ERRORS).unwrap();
glfw.window_hint(glfw::WindowHint::ContextVersion(4, 6));
glfw.window_hint(glfw::WindowHint::OpenGlProfile(
glfw::OpenGlProfileHint::Core,
));
glfw.window_hint(glfw::WindowHint::DoubleBuffer(true));
glfw.window_hint(glfw::WindowHint::Resizable(false));

let (mut window, events) = glfw
.create_window(width, height, title, glfw::WindowMode::Windowed)
.expect("Failed to create GLFW window!");

window.set_char_polling(true);
window.set_key_polling(true);
window.set_cursor_pos_polling(true);
window.set_mouse_button_polling(true);
window.set_framebuffer_size_polling(true);

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

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

Window {
glfw,
window,
events,
}
}

pub fn window_handle(&self) -> &glfw::Window {
&self.window
}

pub fn window_handle_mut(&mut self) -> &mut glfw::Window {
&mut self.window
}

pub fn glfw_handle(&self) -> &glfw::Glfw {
&self.glfw
}

pub fn glfw_handle_mut(&mut self) -> &mut glfw::Glfw {
&mut self.glfw
}

pub fn update(&mut self, egui_input_state: &mut EguiInputState) {
self.window.swap_buffers();
self.glfw.poll_events();
self.process_events(egui_input_state);
self.process_errors();
}

pub fn events(&self) -> glfw::FlushedMessages<'_, (f64, WindowEvent)> {
glfw::flush_messages(&self.events)
}

fn process_events(&mut self, egui_input_state: &mut EguiInputState) {
for (_, event) in glfw::flush_messages(&self.events) {
println!("EVENT - {:?}", event);
match event {
glfw::WindowEvent::FramebufferSize(width, height) => unsafe {
gl::Viewport(0, 0, width, height)
},
glfw::WindowEvent::Key(Key::Escape, _, Action::Press, _) => {
self.window.set_should_close(true)
}
_ => egui_glfw_gl::handle_event(event, egui_input_state),
}
}
}

fn process_errors(&self) {
unsafe {
let mut e: GLenum;
while {
e = gl::GetError();
e != gl::NO_ERROR
} {
println!("ERROR - {:?}", e as GLenum)
}
}
}
}
use std::ffi::CStr;
use egui_glfw::EguiBackend;
use gl::types::*;
use glfw::{self, Action, Context, Key, WindowEvent};
use std::os::raw::c_void;
use std::ptr;
use std::sync::mpsc::Receiver;

pub struct Window {
glfw: glfw::Glfw,
window: glfw::Window,
ui: EguiBackend,
events: Receiver<(f64, WindowEvent)>,
}

impl Window {
pub fn new(width: u32, height: u32, title: &str) -> Window {
let mut glfw = glfw::init(glfw::FAIL_ON_ERRORS).unwrap();
glfw.window_hint(glfw::WindowHint::ContextVersion(4, 6));
glfw.window_hint(glfw::WindowHint::OpenGlProfile(
glfw::OpenGlProfileHint::Core,
));
glfw.window_hint(glfw::WindowHint::DoubleBuffer(true));
glfw.window_hint(glfw::WindowHint::Resizable(true));

let (mut window, events) = glfw
.create_window(width, height, title, glfw::WindowMode::Windowed)
.expect("Failed to create GLFW window!");

window.set_key_polling(true);
window.set_cursor_pos_polling(true);
window.set_mouse_button_polling(true);
window.set_framebuffer_size_polling(true);
window.set_scroll_polling(true);
window.set_char_polling(true);

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

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

unsafe {
gl::Disable(gl::CULL_FACE);
gl::Enable(gl::DEPTH_TEST);
gl::Enable(gl::MULTISAMPLE);
gl::Enable(gl::DEBUG_OUTPUT);
// gl::Enable(gl::FRAMEBUFFER_SRGB);
gl::DebugMessageCallback(Some(debug_callback), ptr::null());
}

let ui = EguiBackend::new(&mut window, &mut glfw);

Window {
glfw,
window,
ui,
events,
}
}
pub fn glfw_handle(&self) -> &glfw::Glfw {
&self.glfw
}

pub fn glfw_handle_mut(&mut self) -> &mut glfw::Glfw {
&mut self.glfw
}

pub fn window_handle(&self) -> &glfw::Window {
&self.window
}

pub fn window_handle_mut(&mut self) -> &mut glfw::Window {
&mut self.window
}

pub fn ui_handle(&self) -> &EguiBackend {
&self.ui
}

pub fn update(&mut self) {
self.glfw.poll_events();
self.process_events();
self.process_errors();
self.update_ui();
self.window.swap_buffers();
}

pub fn events(&self) -> glfw::FlushedMessages<'_, (f64, WindowEvent)> {
glfw::flush_messages(&self.events)
}

fn process_events(&mut self) {
for (_, event) in glfw::flush_messages(&self.events) {
self.ui.handle_event(&event, &self.window);
// println!("EVENT - {:?}", event);
match event {
WindowEvent::FramebufferSize(width, height) => unsafe {
gl::Viewport(0, 0, width, height)
},
WindowEvent::Key(Key::Escape, _, Action::Press, _) => {
self.window.set_should_close(true)
}
_ => (),
}
}
}

fn process_errors(&self) {
unsafe {
let mut e: GLenum;
while {
e = gl::GetError();
e != gl::NO_ERROR
} {
println!("ERROR - {:?}", e as GLenum)
}
}
}

fn update_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 ui_begin_frame(&mut self) {
self.ui.begin_frame(&mut self.window, &mut self.glfw)
}
}

extern "system" fn debug_callback(
source: GLenum,
gltype: GLenum,
id: GLuint,
severity: GLenum,
_length: GLsizei,
message: *const GLchar,
_user_param: *mut c_void,
) {
unsafe {
println!(
"GL CALLBACK: {} type = {}, severity = {}, message = {}",
match gltype {
gl::DEBUG_TYPE_ERROR => "** GL ERROR **",
_ => ""
},
CStr::from_ptr(gl::GetString(gltype) as _).to_str().expect("Failed to convert debug GL type to String"),
CStr::from_ptr(gl::GetString(severity) as _).to_str().expect("Failed to convert debug severity to String"),
CStr::from_ptr(message as _).to_str().expect("Failed to convert debug message to String")
)
}
// println!(
// "OpenGL Debug Message: source={}, type={}, id={}, severity={}, message={}",
// source,
// gltype,
// id,
// severity,
// unsafe { std::ffi::CStr::from_ptr(message).to_str().unwrap() }
// );
}
22 changes: 11 additions & 11 deletions src/graphics/wrapper.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
pub mod bo;
pub mod shader_program;
pub mod texture;
pub mod vao;
pub mod vertex_attrib;

pub use bo::*;
pub use shader_program::*;
pub use texture::*;
pub use vao::*;
pub use vertex_attrib::*;
pub mod bo;
pub mod shader_program;
pub mod texture;
pub mod vao;
pub mod vertex_attrib;

pub use bo::*;
pub use shader_program::*;
pub use texture::*;
pub use vao::*;
pub use vertex_attrib::*;
9 changes: 4 additions & 5 deletions src/graphics/wrapper/bo.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use std::mem;

use egui_glfw_gl::gl;
use egui_glfw_gl::gl::types::*;
use gl::types::*;

// Buffer Object
pub trait BO<Ty> {
fn new(usage: GLenum, data: Box<[Ty]>) -> Self;
fn new(usage: GLenum, data: Vec<Ty>) -> Self;
fn bind(&self);
fn unbind(&self);
//fn store(&self);
Expand All @@ -19,7 +18,7 @@ pub struct VBO {
}

impl BO<f32> for VBO {
fn new(usage: GLenum, data: Box<[f32]>) -> VBO {
fn new(usage: GLenum, data: Vec<f32>) -> VBO {
let mut id = 0;
unsafe {
gl::GenBuffers(1, &mut id);
Expand Down Expand Up @@ -60,7 +59,7 @@ pub struct EBO {
}

impl BO<i32> for EBO {
fn new(usage: GLenum, data: Box<[i32]>) -> EBO {
fn new(usage: GLenum, data: Vec<i32>) -> EBO {
let mut id = 0;
unsafe {
gl::GenBuffers(1, &mut id);
Expand Down
Loading

0 comments on commit 4c053f1

Please sign in to comment.