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;
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]
#[allow(non_snake_case)]
pub(crate) extern "C" fn vkNegotiateLoaderLayerInterfaceVersion(
@ -70,48 +112,8 @@ pub(crate) extern "C" fn vkNegotiateLoaderLayerInterfaceVersion(
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 {
OVERLAY.set_config(config);
OVERLAY.set_config(get_config(&home));
}
unsafe {
@ -548,3 +550,14 @@ pub fn log(msg: impl ToString) {
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,
// radar objects
background: RadarObject,
background: Option<RadarObject>,
player_car: RadarObject,
cars: Vec<RadarObject>,
@ -108,20 +108,24 @@ impl RFactorData {
telemetry_reader: TelemetryReader::new(start_time.elapsed().as_secs_f32())?,
scoring_reader: ScoringReader::new(start_time.elapsed().as_secs_f32())?,
background: RadarObject::new(
device.clone(),
descriptor_layout,
PositionOnlyVertex::from_2d_corners(
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),
],
),
[0.5, 0.5, 0.5, config.radar_transparency],
)?,
background: if config.radar_transparency == 0.0 {
None
} else {
Some(RadarObject::new(
device.clone(),
descriptor_layout,
PositionOnlyVertex::from_2d_corners(
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),
],
),
[0.5, 0.5, 0.5, config.radar_transparency],
)?)
},
player_car: RadarObject::new(
device.clone(),
descriptor_layout,
@ -196,6 +200,8 @@ impl RFactorData {
pub fn update(&mut self) -> Result<()> {
write_log!(" =================== update RFactorData ===================");
let mut should_render = false;
// get scoring info
if let Some((scoring_info, vehicle_scorings)) =
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
@ -225,63 +239,67 @@ impl RFactorData {
if let Some(telemetries) = self.telemetry_reader.query_telemetry(self.now()) {
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();
let mut buffer_car_index = 0;
for other_position in other_positions {
let diff = player_position.position - other_position.position;
let distance = diff.magnitude();
if should_render {
// make sure there are enough cars in buffer
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
if distance < self.config.radar_car_distance {
let offset =
diff.xz() * (self.radar_extent / self.config.radar_car_distance);
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 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],
)?;
let mut player_position = CarPosition::default();
let mut other_positions = Vec::new();
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 {
// only draw radar when any car is near enough
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 {
objects.push(other_player_cars);