Skip to content

Commit

Permalink
used clippy to clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
BarthPaleologue committed Sep 28, 2023
1 parent 552dc90 commit c41ed94
Show file tree
Hide file tree
Showing 10 changed files with 190 additions and 187 deletions.
150 changes: 0 additions & 150 deletions src/app.rs

This file was deleted.

6 changes: 2 additions & 4 deletions src/camera.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
use std::f32::consts::PI;
use cgmath::{EuclideanSpace, Matrix4, perspective, Point3, Rad, Vector3};

#[path = "./transform.rs"]
mod transform;

use transform::{Transform};
use crate::app::OPENGL_TO_WGPU_MATRIX;
use crate::transform::Transform;
use crate::engine::OPENGL_TO_WGPU_MATRIX;

pub struct BasicCamera {
pub transform: Transform,
Expand Down
150 changes: 150 additions & 0 deletions src/engine.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
use winit::event_loop::{ControlFlow, EventLoop};
use winit::window::{Window, WindowBuilder};
use cgmath::*;
use wgpu::{Device, Queue, Surface, SurfaceConfiguration};
use winit::dpi::PhysicalSize;
use winit::event::{ElementState, Event, KeyboardInput, VirtualKeyCode, WindowEvent};
use winit::platform::unix::EventLoopExtUnix;
use crate::scene::Scene;

pub const OPENGL_TO_WGPU_MATRIX: Matrix4<f32> = Matrix4::new(
1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 0.5, 0.0,
0.0, 0.0, 0.5, 1.0,
);

pub struct Engine {
pub window: Window,
pub surface: Surface,
pub device: Device,
pub queue: Queue,
pub config: SurfaceConfiguration,
pub size: PhysicalSize<u32>,
}


impl Engine {
pub fn new(name: &str, any_thread: bool) -> (Self, EventLoop<()>) {
env_logger::init();
let event_loop = if any_thread { EventLoop::new_any_thread() } else { EventLoop::new() };
let window = WindowBuilder::new().build(&event_loop).unwrap();
window.set_title(name);

let (surface, device, queue, config, size) = pollster::block_on(init_wgpu(&window));

let app = Engine {
window,
surface,
device,
queue,
config,
size,
};

(app, event_loop)
}

pub fn resize(&mut self, new_size: PhysicalSize<u32>) {
if new_size.width > 0 && new_size.height > 0 {
self.size = new_size;
self.config.width = new_size.width;
self.config.height = new_size.height;
self.surface.configure(&self.device, &self.config);
}
}

pub fn manage_event(&mut self, event: &WindowEvent) {
match event {
WindowEvent::Resized(physical_size) => {
self.resize(*physical_size);
}
WindowEvent::ScaleFactorChanged { new_inner_size, .. } => {
self.resize(**new_inner_size);
}
_ => {}
}
}

pub fn start(mut self, mut scene: Scene, event_loop: EventLoop<()>, mut callback: impl FnMut() + 'static) {
event_loop.run(move |event, _, control_flow| match event {
Event::WindowEvent {
ref event,
window_id
} if window_id == self.window.id() => {
self.manage_event(event);
scene.manage_event(event);

match event {
WindowEvent::CloseRequested | WindowEvent::KeyboardInput {
input:
KeyboardInput {
state: ElementState::Pressed,
virtual_keycode: Some(VirtualKeyCode::Escape),
..
},
..
} => *control_flow = ControlFlow::Exit,
_ => {}
}
}
Event::RedrawRequested(_) => {
scene.update(&mut self);

callback();

match scene.render(&mut self) {
Ok(_) => {}
Err(wgpu::SurfaceError::Lost) => {
scene.resize(self.window.inner_size());
self.resize(self.window.inner_size());
}
Err(wgpu::SurfaceError::OutOfMemory) => *control_flow = ControlFlow::Exit,
Err(e) => eprintln!("{}", e)
}
}
Event::MainEventsCleared => {
self.window.request_redraw();
}
_ => {}
});
}
}

pub async fn init_wgpu(window: &Window) -> (Surface, Device, Queue, SurfaceConfiguration, PhysicalSize<u32>) {
let size = window.inner_size();
let instance = wgpu::Instance::new(wgpu::Backends::all());
//let instance = wgpu::Instance::new(wgpu::Backends::VULKAN);
let surface = unsafe { instance.create_surface(window) };
let adapter = instance
.request_adapter(&wgpu::RequestAdapterOptions {
power_preference: wgpu::PowerPreference::default(),
compatible_surface: Some(&surface),
force_fallback_adapter: false,
})
.await
.unwrap();

let (device, queue) = adapter
.request_device(
&wgpu::DeviceDescriptor {
label: None,
features: wgpu::Features::POLYGON_MODE_LINE,
limits: wgpu::Limits::default(),
},
None, // Trace path
)
.await
.unwrap();

let config = wgpu::SurfaceConfiguration {
usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
format: *surface.get_supported_formats(&adapter).first().unwrap(),
width: size.width,
height: size.height,
present_mode: wgpu::PresentMode::Fifo,
};
surface.configure(&device, &config);

(surface, device, queue, config, size)
}
5 changes: 1 addition & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use winit::platform::unix::EventLoopExtUnix;

pub mod engine;
pub mod scene;
pub mod camera;
pub mod mesh;
pub mod procedural;
pub mod transform;
pub mod material;
pub mod app;
pub mod engine;
12 changes: 7 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
extern crate gilgamesh;

use gilgamesh::app::App;
use gilgamesh::engine::Engine;
use gilgamesh::mesh::Mesh;
use gilgamesh::scene::Scene;

fn main() {
let (mut app, event_loop) = App::new();
let (mut engine, event_loop) = Engine::new("Gilgamesh", false);

let mut scene = Scene::new(&app);
let mut scene = Scene::new(&engine);

let sphere = Mesh::new_procedural_sphere(5.0, 32, &|x, y, z| {
f32::powi(f32::sin(60.0 * x * y * z), 2) * 0.5
}, 0.5, &mut app);
}, 0.5, &mut engine);

scene.add_mesh(sphere);

app.start(scene, event_loop);
engine.start(scene, event_loop, move || {
println!("Hello, world!");
});
}

0 comments on commit c41ed94

Please sign in to comment.