From 452d1cb1ea22b3572ea716fbd7e20f0cff3de5ec Mon Sep 17 00:00:00 2001 From: Melik Houij Date: Thu, 16 Mar 2023 09:07:12 +0100 Subject: [PATCH] messing around with imgui --- src/main.rs | 149 ++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 105 insertions(+), 44 deletions(-) diff --git a/src/main.rs b/src/main.rs index d13544f..aa00574 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,7 +8,10 @@ const SCR_WIDTH: u32 = 1600; const SCR_HEIGHT: u32 = 900; const TITLE: &str = "GLFWtest"; - +enum gameState { + Paused, + Playing +} use imgui_glow_renderer::AutoRenderer; @@ -59,6 +62,7 @@ fn main() { Ok(ret) => ret, Err(s) => panic!("Error with Initializing SDL Context: {}", s) }; + let gl = std::sync::Arc::new(unsafe{glow::Context::from_loader_function(|s| video.gl_get_proc_address(s) as *const _)}); let event_loop = match sdl.event_pump() @@ -113,70 +117,127 @@ fn main() { }; + + let mut State = gameState::Playing; + + + let time = std::time::Instant::now(); let mut spinSpeed = 1.0; let mut current_rad = 1.0; let projection: Matrix4 = perspective(Deg(45.0), SCR_WIDTH as f32/ SCR_HEIGHT as f32, 0.1, 100.0); + + + println!("entering main loop"); + + + + + // NOTE main loop here - 'running: loop { - let currentFrame = time.elapsed().as_secs_f32(); - deltaTime = currentFrame - lastFrame; - lastFrame = currentFrame; - current_rad += spinSpeed/15.0; - unsafe{ - gl.enable(glow::DEPTH_TEST); - gl.clear(glow::COLOR_BUFFER_BIT | glow::DEPTH_BUFFER_BIT); + 'main: loop { + match &State { + gameState::Playing => { + let currentFrame = time.elapsed().as_secs_f32(); + deltaTime = currentFrame - lastFrame; + lastFrame = currentFrame; + current_rad += spinSpeed/15.0; - ourshader.Use(); - let Projection: Matrix4 = perspective(Deg(camera.Zoom), SCR_WIDTH as f32 / SCR_HEIGHT as f32, 0.1, 100.0); + unsafe{ + gl.enable(glow::DEPTH_TEST); + gl.clear(glow::COLOR_BUFFER_BIT | glow::DEPTH_BUFFER_BIT); - let view = camera.GetViewMatrix(); - ourshader.setMat4("projection", &projection); - ourshader.setMat4("view", &view); + ourshader.Use(); + let Projection: Matrix4 = perspective(Deg(camera.Zoom), SCR_WIDTH as f32 / SCR_HEIGHT as f32, 0.1, 100.0); + + let view = camera.GetViewMatrix(); + ourshader.setMat4("projection", &projection); + ourshader.setMat4("view", &view); - let mut model: Matrix4 = Matrix4::from_axis_angle(vec3(0.0, -1.0, 0.0).normalize(), - cgmath::Rad(current_rad)); - model = model * Matrix4::from_scale(0.2); - ourshader.setMat4("model", &model); + let mut model: Matrix4 = Matrix4::from_axis_angle(vec3(0.0, -1.0, 0.0).normalize(), + cgmath::Rad(current_rad)); + model = model * Matrix4::from_scale(0.2); + ourshader.setMat4("model", &model); + } + ourModel.Draw(&ourshader); + + platform.prepare_frame(&mut imgui, &window, &events_loop); + let ui = imgui.new_frame(); + + ui.window("POLY I WILL FUCKING") + .size([500.0, 100.0], imgui::Condition::FirstUseEver) + .build(|| { + ui.text("you serve no purpose in life"); + ui.text("Your Purpose in life is to suck my dick"); + ui.slider("The Spin Speed", 0.1, 10.0, &mut spinSpeed); + }); + + drawDebugUI(ui, &ourModel); + let draw_data = imgui.render(); + + + renderer.render(draw_data).unwrap(); + + + + window.gl_swap_window(); + for event in events_loop.poll_iter() { + platform.handle_event(&mut imgui, &event); + + match event { + Event::Quit {..} | + Event::KeyDown { keycode: Some(Keycode::Escape), .. } => { + break 'main + }, + Event::KeyDown {keycode: Some(Keycode::P), ..} => { + State = gameState::Paused; + } + _ => {} + } + } } - ourModel.Draw(&ourshader); - platform.prepare_frame(&mut imgui, &window, &events_loop); - let ui = imgui.new_frame(); + gameState::Paused => { - ui.window("POLY I WILL FUCKING") - .size([500.0, 100.0], imgui::Condition::FirstUseEver) - .build(|| { - ui.text("you serve no purpose in life"); - ui.text("Your Purpose in life is to suck my dick"); - ui.slider("The Spin Speed", 0.1, 10.0, &mut spinSpeed); - }); + for event in events_loop.poll_iter() { + platform.handle_event(&mut imgui, &event); - let draw_data = imgui.render(); - - renderer.render(draw_data).unwrap(); - - - - window.gl_swap_window(); - for event in events_loop.poll_iter() { - platform.handle_event(&mut imgui, &event); - - match event { - Event::Quit {..} | - Event::KeyDown { keycode: Some(Keycode::Escape), .. } => { - break 'running - }, - _ => {} + match event { + Event::KeyDown {keycode: Some(Keycode::P), ..} => { + State = gameState::Playing; + } + _ => {} + } + } } } } } + + +fn drawDebugUI(ui: &imgui::Ui, model: &model::Model) { + ui.window("Stats") + .size([300.0,500.0], imgui::Condition::Always) + .build(|| { + ui.text("Model Information"); + ui.separator(); + ui.text(format!("count meshes: {}", model.meshes.len())); + for i in 0..model.meshes.len() + { + ui.text(format!("Mesh Number: {}",i )); + ui.text(format!("count vertices: {}", model.meshes[i].vertices.len())); + ui.separator(); + } + + }); + + +}