Using Scene instead of main loop, there is a bug

Cannot Exit the Program
pull/3/head
Milk.H 2023-03-20 16:31:42 +01:00
parent 2229e3e742
commit 60eba9cd4e
No known key found for this signature in database
GPG Key ID: 3D9DAE46AAC37BD8
2 changed files with 80 additions and 67 deletions

View File

@ -36,6 +36,7 @@ mod model;
mod camera;
mod scene;
use scene::Scene;
fn main() {
@ -79,19 +80,6 @@ fn main() {
let mut imgui = imgui::Context::create();
imgui
.fonts()
.add_font(&[imgui::FontSource::DefaultFontData {config: None}]);
let mut platform = SdlPlatform::init(&mut imgui);
let mut renderer = match AutoRenderer::initialize(gl.clone(), &mut imgui)
{
Ok(ret) => ret,
Err(s) => panic!("Failed to initialize Imgui Platform Renderer: {}", s),
};
// NOTE intiialize Camera
let mut camera = camera::Camera {
@ -118,15 +106,14 @@ fn main() {
let mut State = gameState::Playing;
let mut scene = Scene::new(gl.clone());
let time = std::time::Instant::now();
let mut spinSpeed = 1.0;
let mut current_rad = 1.0;
let projection: Matrix4<f32> = perspective(Deg(45.0), SCR_WIDTH as f32/ SCR_HEIGHT as f32, 0.1, 100.0);
println!("entering main loop");
@ -138,57 +125,11 @@ fn main() {
'main: loop {
match &State {
gameState::Playing => {
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);
ourshader.Use();
let Projection: Matrix4<f32> = 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<f32> = 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();
scene.update(&mut events_loop, &window);
window.gl_swap_window();
for event in events_loop.poll_iter() {
platform.handle_event(&mut imgui, &event);
match event {
Event::Quit {..} |
@ -206,7 +147,6 @@ fn main() {
gameState::Paused => {
for event in events_loop.poll_iter() {
platform.handle_event(&mut imgui, &event);
match event {
Event::KeyDown {keycode: Some(Keycode::P), ..} => {

View File

@ -4,7 +4,7 @@ use crate::camera::Camera;
use crate::shader::shader;
use std::sync::Arc;
use std::rc::Rc;
use glow::*;
use imgui_glow_renderer::AutoRenderer;
use imgui_sdl2_support::SdlPlatform;
@ -12,9 +12,17 @@ use imgui_sdl2_support::SdlPlatform;
use cgmath::{Matrix4, vec3, Point3, Deg, perspective};
use cgmath::prelude::*;
use sdl2::keyboard::Keycode;
use sdl2::event::Event;
const SCR_WIDTH: u32 = 1600;
const SCR_HEIGHT: u32 = 900;
const MODELS: [&str; 3]= ["resources/models/TestCarModel/CarW4.obj", "resources/models/TestCarModel/CarW1.obj",
"resources/models/TestCarModel/CarW0.obj"];
/// XXX this is temporary, thus the name
pub struct tempData {
pub projection: Matrix4<f32>,
@ -27,7 +35,7 @@ pub struct tempData {
struct Scene {
pub struct Scene {
gl: Arc<glow::Context>,
pub Car: Model,
camera: Camera,
@ -40,7 +48,7 @@ struct Scene {
}
impl Scene{
fn new(gl: Arc<glow::Context>) -> Scene
pub fn new(gl: Arc<glow::Context>) -> Scene
{
let Car = Model::new(MODELS[0], Arc::clone(&gl));
@ -88,10 +96,75 @@ impl Scene{
time,
tempData,
}
}
pub fn update(&mut self ,events_loop: &mut sdl2::EventPump, window: &sdl2::video::Window)
{
self.tempData.currentFrame = self.time.elapsed().as_secs_f32();
self.tempData.deltaTime = self.tempData.currentFrame - self.tempData.lastFrame;
self.tempData.lastFrame = self.tempData.currentFrame;
self.tempData.current_rad += self.tempData.spinSpeed/15.0;
unsafe {
self.gl.enable(glow::DEPTH_TEST);
self.gl.clear(glow::COLOR_BUFFER_BIT | glow::DEPTH_BUFFER_BIT);
self.shaders[0].Use();
self.tempData.projection = perspective(Deg(self.camera.Zoom), SCR_WIDTH as f32 /SCR_HEIGHT as f32, 0.1, 100.0);
let view = self.camera.GetViewMatrix();
self.shaders[0].setMat4("projection", &self.tempData.projection);
self.shaders[0].setMat4("view", &view);
let mut model: Matrix4<f32> = Matrix4::from_axis_angle(vec3(0.0, -1.0, 0.0).normalize(),
cgmath::Rad(self.tempData.current_rad));
model = model * Matrix4::from_scale(0.2);
self.shaders[0].setMat4("model", &model);
}
self.Car.Draw(&self.shaders[0]);
self.drawImgui(&events_loop, &window);
let quit = false;
for event in events_loop.poll_iter() {
self.imgui_plat.handle_event(&mut self.imgui, &event);
}
}
fn drawImgui(&mut self, events_loop: &sdl2::EventPump, window: &sdl2::video::Window)
{
self.imgui_plat.prepare_frame(&mut self.imgui, &window, &events_loop );
let ui = self.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 self.tempData.spinSpeed);
});
ui.window("Stats")
.size([300.0,500.0], imgui::Condition::Always)
.build(|| {
ui.text("Model Information");
ui.separator();
ui.text(format!("count meshes: {}", self.Car.meshes.len()));
for i in 0..self.Car.meshes.len()
{
ui.text(format!("Mesh Number: {}",i ));
ui.text(format!("count vertices: {}", self.Car.meshes[i].vertices.len()));
ui.separator();
}
});
let draw_data = self.imgui.render();
self.imgui_rend.render(draw_data).unwrap();
}
}