Attempt at not rendering radar

This commit is contained in:
hodasemi 2023-01-16 22:29:23 +01:00
parent 0a89dd618b
commit 20ab9443e2
2 changed files with 142 additions and 109 deletions

View file

@ -40,6 +40,48 @@ macro_rules! write_log {
pub(crate) use write_log; pub(crate) use write_log;
fn get_config(home: &str) -> OverlayConfig {
let config_path = Path::new(&home).join(".config/rFactorHUD/config.json");
if config_path.exists() {
fs::read_to_string(&config_path)
.map(|s| {
serde_json::from_str(&s).unwrap_or({
write_log!("failed to deserialize config file");
OverlayConfig::new()
})
})
.unwrap_or({
write_log!(format!("failed to open config file: {:?}", config_path));
OverlayConfig::new()
})
} else {
if let Err(err) = std::fs::create_dir_all(config_path.parent().unwrap()) {
write_log!(format!("failed to create dirs for config file: {:?}", err));
}
let config = OverlayConfig::new();
match File::create(config_path) {
Ok(mut file) => match serde_json::to_string_pretty(&config) {
Ok(conf_str) => {
if let Err(err) = file.write_all(conf_str.as_bytes()) {
write_log!(format!("failed to write to config file: {:?}", err));
}
}
Err(err) => {
write_log!(format!("failed to serialize config: {:?}", err));
}
},
Err(err) => {
write_log!(format!("failed to create config file: {:?}", err));
}
}
config
}
}
#[no_mangle] #[no_mangle]
#[allow(non_snake_case)] #[allow(non_snake_case)]
pub(crate) extern "C" fn vkNegotiateLoaderLayerInterfaceVersion( pub(crate) extern "C" fn vkNegotiateLoaderLayerInterfaceVersion(
@ -70,48 +112,8 @@ pub(crate) extern "C" fn vkNegotiateLoaderLayerInterfaceVersion(
write_log!(" =================================================================="); write_log!(" ==================================================================");
} }
let config_path = Path::new(&home).join(".config/rFactorHUD/config.json");
let config = if config_path.exists() {
fs::read_to_string(&config_path)
.map(|s| {
serde_json::from_str(&s).unwrap_or({
write_log!("failed to deserialize config file");
OverlayConfig::new()
})
})
.unwrap_or({
write_log!(format!("failed to open config file: {:?}", config_path));
OverlayConfig::new()
})
} else {
if let Err(err) = std::fs::create_dir_all(config_path.parent().unwrap()) {
write_log!(format!("failed to create dirs for config file: {:?}", err));
}
let config = OverlayConfig::new();
match File::create(config_path) {
Ok(mut file) => match serde_json::to_string(&config) {
Ok(conf_str) => {
if let Err(err) = file.write_all(conf_str.as_bytes()) {
write_log!(format!("failed to write to config file: {:?}", err));
}
}
Err(err) => {
write_log!(format!("failed to serialize config: {:?}", err));
}
},
Err(err) => {
write_log!(format!("failed to create config file: {:?}", err));
}
}
config
};
unsafe { unsafe {
OVERLAY.set_config(config); OVERLAY.set_config(get_config(&home));
} }
unsafe { unsafe {
@ -548,3 +550,14 @@ pub fn log(msg: impl ToString) {
if let Err(_) = file.write_all(format!("{}\n", msg.to_string()).as_bytes()) {} if let Err(_) = file.write_all(format!("{}\n", msg.to_string()).as_bytes()) {}
} }
} }
#[cfg(test)]
mod test {
use crate::get_config;
#[test]
fn config() {
let home = std::env::var("HOME").unwrap();
get_config(&home);
}
}

View file

@ -55,7 +55,7 @@ pub struct RFactorData {
scoring_reader: ScoringReader, scoring_reader: ScoringReader,
// radar objects // radar objects
background: RadarObject, background: Option<RadarObject>,
player_car: RadarObject, player_car: RadarObject,
cars: Vec<RadarObject>, cars: Vec<RadarObject>,
@ -108,7 +108,10 @@ impl RFactorData {
telemetry_reader: TelemetryReader::new(start_time.elapsed().as_secs_f32())?, telemetry_reader: TelemetryReader::new(start_time.elapsed().as_secs_f32())?,
scoring_reader: ScoringReader::new(start_time.elapsed().as_secs_f32())?, scoring_reader: ScoringReader::new(start_time.elapsed().as_secs_f32())?,
background: RadarObject::new( background: if config.radar_transparency == 0.0 {
None
} else {
Some(RadarObject::new(
device.clone(), device.clone(),
descriptor_layout, descriptor_layout,
PositionOnlyVertex::from_2d_corners( PositionOnlyVertex::from_2d_corners(
@ -121,7 +124,8 @@ impl RFactorData {
], ],
), ),
[0.5, 0.5, 0.5, config.radar_transparency], [0.5, 0.5, 0.5, config.radar_transparency],
)?, )?)
},
player_car: RadarObject::new( player_car: RadarObject::new(
device.clone(), device.clone(),
descriptor_layout, descriptor_layout,
@ -196,6 +200,8 @@ impl RFactorData {
pub fn update(&mut self) -> Result<()> { pub fn update(&mut self) -> Result<()> {
write_log!(" =================== update RFactorData ==================="); write_log!(" =================== update RFactorData ===================");
let mut should_render = false;
// get scoring info // get scoring info
if let Some((scoring_info, vehicle_scorings)) = if let Some((scoring_info, vehicle_scorings)) =
self.scoring_reader.vehicle_scoring(self.now()) self.scoring_reader.vehicle_scoring(self.now())
@ -217,6 +223,14 @@ impl RFactorData {
} }
} }
} }
if let Some(id) = &self.player_id {
if let Some(vehicle_scoring) =
vehicle_scorings.iter().find(|scoring| scoring.mID == *id)
{
should_render = vehicle_scoring.mInPits != 0;
}
}
} }
// if player id is set (a map is loaded), check telemetry data // if player id is set (a map is loaded), check telemetry data
@ -225,13 +239,17 @@ impl RFactorData {
if let Some(telemetries) = self.telemetry_reader.query_telemetry(self.now()) { if let Some(telemetries) = self.telemetry_reader.query_telemetry(self.now()) {
write_log!("new telemetry update"); write_log!("new telemetry update");
self.cars.clear();
if should_render {
// make sure there are enough cars in buffer // make sure there are enough cars in buffer
if self.car_handles.len() < telemetries.len() { if self.car_handles.len() < telemetries.len() {
let size_diff = telemetries.len() - self.car_handles.len(); let size_diff = telemetries.len() - self.car_handles.len();
for _ in 0..size_diff { for _ in 0..size_diff {
self.car_handles self.car_handles.push(
.push(self.create_car_object(vec2(0.0, 0.0), [0.0, 0.0, 0.0, 0.0])?); self.create_car_object(vec2(0.0, 0.0), [0.0, 0.0, 0.0, 0.0])?,
);
} }
} }
@ -256,7 +274,6 @@ impl RFactorData {
} }
// update radar objects // update radar objects
self.cars.clear();
let mut buffer_car_index = 0; let mut buffer_car_index = 0;
for other_position in other_positions { for other_position in other_positions {
@ -286,6 +303,7 @@ impl RFactorData {
} }
} }
} }
}
Ok(()) Ok(())
} }
@ -299,7 +317,9 @@ impl RFactorData {
if let Some(_player_id) = &self.player_id { if let Some(_player_id) = &self.player_id {
// only draw radar when any car is near enough // only draw radar when any car is near enough
if !self.cars.is_empty() { if !self.cars.is_empty() {
objects.push(&self.background); if let Some(background) = &self.background {
objects.push(background);
}
for other_player_cars in &self.cars { for other_player_cars in &self.cars {
objects.push(other_player_cars); objects.push(other_player_cars);