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,20 +108,24 @@ 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 {
device.clone(), None
descriptor_layout, } else {
PositionOnlyVertex::from_2d_corners( Some(RadarObject::new(
ortho * Matrix4::from_translation(radar_center.extend(0.0)), device.clone(),
[ descriptor_layout,
vec2(-radar_extent, -radar_extent), PositionOnlyVertex::from_2d_corners(
vec2(-radar_extent, radar_extent), ortho * Matrix4::from_translation(radar_center.extend(0.0)),
vec2(radar_extent, radar_extent), [
vec2(radar_extent, -radar_extent), vec2(-radar_extent, -radar_extent),
], vec2(-radar_extent, radar_extent),
), vec2(radar_extent, radar_extent),
[0.5, 0.5, 0.5, config.radar_transparency], vec2(radar_extent, -radar_extent),
)?, ],
),
[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,63 +239,67 @@ 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");
// make sure there are enough cars in buffer
if self.car_handles.len() < telemetries.len() {
let size_diff = telemetries.len() - self.car_handles.len();
for _ in 0..size_diff {
self.car_handles
.push(self.create_car_object(vec2(0.0, 0.0), [0.0, 0.0, 0.0, 0.0])?);
}
}
let mut player_position = CarPosition::default();
let mut other_positions = Vec::new();
for telemetry in telemetries {
let car = CarPosition::new(
convert_vec(telemetry.position),
[
convert_vec(telemetry.orientation[0]),
convert_vec(telemetry.orientation[1]),
convert_vec(telemetry.orientation[2]),
],
);
if telemetry.id == *player_id {
player_position = car
} else {
other_positions.push(car);
}
}
// update radar objects
self.cars.clear(); self.cars.clear();
let mut buffer_car_index = 0;
for other_position in other_positions { if should_render {
let diff = player_position.position - other_position.position; // make sure there are enough cars in buffer
let distance = diff.magnitude(); if self.car_handles.len() < telemetries.len() {
let size_diff = telemetries.len() - self.car_handles.len();
// check if car is close enough to the players car for _ in 0..size_diff {
if distance < self.config.radar_car_distance { self.car_handles.push(
let offset = self.create_car_object(vec2(0.0, 0.0), [0.0, 0.0, 0.0, 0.0])?,
diff.xz() * (self.radar_extent / self.config.radar_car_distance); );
}
}
let buffered_car = self.car_handles[buffer_car_index].clone(); let mut player_position = CarPosition::default();
buffer_car_index += 1; let mut other_positions = Vec::new();
buffered_car.update(
self.ortho,
offset,
player_position.rotation,
other_position.rotation,
self.radar_center,
self.car_width,
self.car_height,
[0.9, 0.9, 0.0, 0.9],
)?;
self.cars.push(buffered_car); for telemetry in telemetries {
let car = CarPosition::new(
convert_vec(telemetry.position),
[
convert_vec(telemetry.orientation[0]),
convert_vec(telemetry.orientation[1]),
convert_vec(telemetry.orientation[2]),
],
);
if telemetry.id == *player_id {
player_position = car
} else {
other_positions.push(car);
}
}
// update radar objects
let mut buffer_car_index = 0;
for other_position in other_positions {
let diff = player_position.position - other_position.position;
let distance = diff.magnitude();
// check if car is close enough to the players car
if distance < self.config.radar_car_distance {
let offset =
diff.xz() * (self.radar_extent / self.config.radar_car_distance);
let buffered_car = self.car_handles[buffer_car_index].clone();
buffer_car_index += 1;
buffered_car.update(
self.ortho,
offset,
player_position.rotation,
other_position.rotation,
self.radar_center,
self.car_width,
self.car_height,
[0.9, 0.9, 0.0, 0.9],
)?;
self.cars.push(buffered_car);
}
} }
} }
} }
@ -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);