use crate::write_log; use self::{ rendering::Rendering, rfactor_data::{DataConfig, RFactorData}, }; mod pipeline; mod rendering; mod rfactor_data; use anyhow::Result; use cgmath::vec3; use std::sync::{Arc, Mutex}; use vulkan_rs::prelude::*; pub struct Overlay { instance: Option>, device: Option>, queue: Option>>, rendering: Option, rfactor_data: Option, } impl Overlay { pub const fn new() -> Self { Self { instance: None, device: None, queue: None, rendering: None, rfactor_data: None, } } pub fn set_instance(&mut self, instance: Arc) { self.instance = Some(instance); } pub fn instance(&self) -> Arc { self.instance.as_ref().unwrap().clone() } pub fn set_device(&mut self, device: Arc) { self.device = Some(device); } pub fn device(&self) -> Arc { self.device.as_ref().unwrap().clone() } pub fn set_queue(&mut self, queue: Arc>) { self.queue = Some(queue); } pub fn queue(&self) -> Arc> { self.queue.as_ref().unwrap().clone() } pub fn swapchain(&self, swapchain: VkSwapchainKHR) -> Option<&Arc> { let sc = self.rendering.as_ref().unwrap().swapchain(); if sc.vk_handle() == swapchain { Some(sc) } else { None } } pub fn create_rendering(&mut self, swapchain: Arc) -> Result<()> { write_log!("-> create rendering: start"); self.rendering = None; write_log!("-> create rendering: old cleared"); self.rendering = Some(Rendering::new(self.device(), self.queue(), swapchain)?); write_log!("-> create rendering: new created"); write_log!("-> create rendering: end"); Ok(()) } pub fn render(&mut self) -> Result<()> { let swapchain = self.rendering.as_ref().unwrap().swapchain().clone(); if self.rfactor_data.is_none() { self.rfactor_data = RFactorData::new( DataConfig { radar_scale: 1.0, radar_car_distance: 20.0, safe_color: vec3(0.0, 0.75, 0.0), danger_color: vec3(0.75, 0.0, 0.0), }, self.device(), self.rendering .as_mut() .unwrap() .single_color_pipeline() .descriptor_layout(), swapchain.width(), swapchain.height(), ) .ok(); write_log!("created RFactorData"); } // check twice for rfactor data, because of borrowing rules if let Some(rfactor) = &mut self.rfactor_data { rfactor.update()?; } let objects = match &self.rfactor_data { Some(rfactor) => rfactor.objects(), None => Vec::new(), }; self.rendering.as_mut().unwrap().render(swapchain, &objects) } }