You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Trac3r-rust/src/main.rs

223 lines
5.7 KiB

6 years ago
#![allow(dead_code)]
#![allow(unused_variables)]
#![allow(unused_mut)]
6 years ago
6 years ago
extern crate cgmath;
extern crate image;
6 years ago
extern crate nalgebra as na;
extern crate rand;
6 years ago
extern crate sfml;
extern crate time;
extern crate hprof;
use sfml::system::*;
use vulkano::sync;
6 years ago
use crate::timer::Timer;
use vulkano::instance::Instance;
use vulkano::sync::GpuFuture;
use winit::{EventsLoop, WindowBuilder, WindowEvent, Event, DeviceEvent, VirtualKeyCode, ElementState};
use winit::dpi::LogicalSize;
use vulkano_win::VkSurfaceBuild;
use sprite::Sprite;
use crate::compu_state::CompuState;
use crate::compu_frame::CompuFrame;
use crate::compu_sprite::CompuSprite;
use crate::compu_kernel::CompuKernel;
use crate::compu_buffer::CompuBuffers;
use crate::util::load_raw;
use crate::canvas_frame::CanvasFrame;
5 years ago
pub mod util;
pub mod timer;
pub mod input;
pub mod vkprocessor;
pub mod vertex_2d;
pub mod vertex_3d;
pub mod sprite;
pub mod canvas;
pub mod canvas_frame;
pub mod canvas_shader;
pub mod canvas_buffer;
pub mod compu_state;
pub mod compu_frame;
pub mod compu_sprite;
pub mod compu_kernel;
pub mod compu_buffer;
5 years ago
/// Main Entry
pub fn main() {
hprof::start_frame();
let q1 = hprof::enter("setup");
let instance = {
let extensions = vulkano_win::required_extensions();
Instance::new(None, &extensions, None).unwrap()
};
let mut events_loop = EventsLoop::new();
let mut surface = WindowBuilder::new()
.with_dimensions(LogicalSize::from((800, 800)))
.build_vk_surface(&events_loop, instance.clone()).unwrap();
let mut window = surface.window();
let mut processor = vkprocessor::VkProcessor::new(&instance, &surface);
{
let g = hprof::enter("vulkan preload");
processor.create_swapchain(&surface);
processor.preload_kernels();
processor.preload_shaders();
processor.preload_textures();
}
let q2 = hprof::enter("Game Objects");
6 years ago
let mut timer = Timer::new();
let mut frame_future = Box::new(sync::now(processor.device.clone())) as Box<dyn GpuFuture>;
let step_size: f32 = 0.005;
let mut elapsed_time: f32;
let mut delta_time: f32;
6 years ago
let mut accumulator_time: f32 = 0.0;
let mut current_time: f32 = timer.elap_time();
6 years ago
let mut mouse_xy = Vector2i::new(0, 0);
6 years ago
let image_data = load_raw(String::from("funky-bird.jpg"));
let image_dimensions_f = ((image_data.1).0 as f32, (image_data.1).1 as f32);
let image_dimensions_u = image_data.1;
let compu_sprite1 = CompuSprite::new((0.0, -0.5), (0.4, 0.4), 3, image_dimensions_f,
// This swap image needs to match the size of the compute
processor.new_swap_image(image_dimensions_u));
let compute_buffer = processor.new_compute_buffer(image_data.0, image_data.1, 4);
let compute_kernel = processor.get_kernel_handle(String::from("simple-edge.compute"))
.expect("Can't find that kernel");
let funky_handle = processor.get_texture_handle(String::from("funky-bird.jpg")).unwrap();
let sfml_handle = processor.get_texture_handle(String::from("sfml.png")).unwrap();
let funky_sprite = Sprite::new_with_texture((0.0, -0.5), (0.5, 0.5), 2, funky_handle.clone());
let sfml_sprite = Sprite::new_with_texture((0.0, -0.5), (0.5, 0.5), 1, sfml_handle.clone());
drop(q2);
drop(q1);
let l = hprof::enter("Loop");
let mut exit = false;
let mut count = 0;
while let Some(p) = window.get_position() {
6 years ago
elapsed_time = timer.elap_time();
6 years ago
delta_time = elapsed_time - current_time;
current_time = elapsed_time;
if delta_time > 0.02 {
delta_time = 0.02;
6 years ago
}
6 years ago
accumulator_time += delta_time;
6 years ago
6 years ago
while (accumulator_time - step_size) >= step_size {
accumulator_time -= step_size;
}
6 years ago
// println!("{}", delta_time);
events_loop.poll_events(|event| {
match event {
Event::WindowEvent { event: WindowEvent::CloseRequested, .. } =>
{
exit = true;
}
Event::WindowEvent { event: WindowEvent::Resized(_), .. } => {
processor.recreate_swapchain(&surface);
}
Event::DeviceEvent { event: DeviceEvent::Key(keyboard_input), .. } => {
match keyboard_input.virtual_keycode.unwrap() {
VirtualKeyCode::A => {
if keyboard_input.state == ElementState::Pressed {
// processor.save_edges_image();
}
}
_ => ()
}
}
// Event::DeviceEvent { event: DeviceEvent::Button(mouse_input), .. } => {
// mouse_xy.x
// },
_ => ()
}
});
if exit {
break;
}
let mut compu_frame = CompuFrame::new();
compu_frame.add(compute_buffer.clone(), compute_kernel.clone());
compu_frame.add_with_image_swap(compute_buffer.clone(), compute_kernel.clone(), &compu_sprite1);
if elapsed_time > (count) as f32 {
count += 1;
compu_frame.add_chained(compute_buffer.clone(), compute_buffer.clone(), compute_kernel.clone());
}
let mut canvas = CanvasFrame::new();
canvas.draw(&funky_sprite);
canvas.draw(&sfml_sprite);
canvas.draw(&compu_sprite1);
{
let g = hprof::enter("Run");
processor.run(&surface,
canvas,
compu_frame);
}
}
drop(l);
hprof::end_frame();
hprof::profiler().print_timing();
}
7 years ago