Skip to content

Commit

Permalink
Rename the cats directory to fallback_textures. Now selecting dif…
Browse files Browse the repository at this point in the history
…ferent fallback textures every time (this includes the cats).
  • Loading branch information
CaspianA1 committed Jul 29, 2024
1 parent ae27e71 commit 9346978
Show file tree
Hide file tree
Showing 44 changed files with 45 additions and 20 deletions.
4 changes: 2 additions & 2 deletions assets/app_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"icon_path": "assets/plane.bmp",
"maybe_pause_subduration_ms_when_window_unfocused": 250,

"o1": {"Windowed": [1200, 800, false, null]},
"screen_option": {"Windowed": [1200, 800, false, null]},
"o2": "FullscreenDesktop",
"screen_option": "Fullscreen",
"o3": "Fullscreen",

"hide_cursor": true,
"use_linear_filtering": true,
Expand Down
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
36 changes: 30 additions & 6 deletions src/dashboard_defs/dashboard.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::borrow::Cow;
use std::sync::atomic::{AtomicUsize, Ordering};

use chrono::Duration;
use sdl2::{render::BlendMode, ttf::{FontStyle, Hinting}};
Expand Down Expand Up @@ -34,6 +35,33 @@ use crate::{
}
};

//////////

static FALLBACK_TEXTURE_CREATION_INFO_PATH_INDEX: AtomicUsize = AtomicUsize::new(0);

lazy_static::lazy_static!(
static ref FALLBACK_TEXTURE_PATHS: Vec<String> =
std::fs::read_dir("assets/fallback_textures").unwrap()
.map(|maybe_dir_entry| maybe_dir_entry.map(|dir_entry| {
let path = dir_entry.path();
assert!(path.is_file());
path.to_str().unwrap().to_owned()
}))
.collect::<Result<Vec<_>, _>>().unwrap();
);

fn get_fallback_texture_creation_info() -> TextureCreationInfo<'static> {
let ordering = Ordering::SeqCst;
let mut index = FALLBACK_TEXTURE_CREATION_INFO_PATH_INDEX.fetch_add(1, ordering);

if index >= FALLBACK_TEXTURE_PATHS.len() {
index = 0;
FALLBACK_TEXTURE_CREATION_INFO_PATH_INDEX.store(0, ordering);
}

TextureCreationInfo::Path(Cow::Borrowed(&FALLBACK_TEXTURE_PATHS[index]))
}

////////// TODO: maybe split `make_dashboard` into some smaller sub-functions

/* TODO:
Expand Down Expand Up @@ -400,10 +428,6 @@ pub fn make_dashboard(

////////// Defining the shared state

// TODO: make it possible to get different variants of this texture (randomly chosen)
const FALLBACK_TEXTURE_CREATION_INFO: TextureCreationInfo<'static> =
TextureCreationInfo::Path(Cow::Borrowed("assets/no_texture_available.png"));

let initial_spin_window_size_guess = (1024, 1024);

let custom_model_expiry_durations = [
Expand All @@ -414,7 +438,7 @@ pub fn make_dashboard(
];

let spinitron_state = SpinitronState::new(
(&api_keys.spinitron, &FALLBACK_TEXTURE_CREATION_INFO,
(&api_keys.spinitron, get_fallback_texture_creation_info,
custom_model_expiry_durations, initial_spin_window_size_guess)
)?;

Expand All @@ -424,7 +448,7 @@ pub fn make_dashboard(
spinitron_state,
twilio_state,
font_info: &FONT_INFO,
fallback_texture_creation_info: &FALLBACK_TEXTURE_CREATION_INFO,
get_fallback_texture_creation_info,
curr_dashboard_error: None,
rand_generator: rand::thread_rng()
}
Expand Down
2 changes: 1 addition & 1 deletion src/dashboard_defs/shared_window_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub struct SharedWindowState<'a> {
pub font_info: &'a FontInfo,

// This is used whenever a texture can't be loaded
pub fallback_texture_creation_info: &'a TextureCreationInfo<'a>,
pub get_fallback_texture_creation_info: fn() -> TextureCreationInfo<'a>,

pub curr_dashboard_error: Option<String>,

Expand Down
2 changes: 1 addition & 1 deletion src/dashboard_defs/spinitron.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ pub fn make_spinitron_windows(
true,
params.texture_pool,
&texture_creation_info,
inner_shared_state.fallback_texture_creation_info
inner_shared_state.get_fallback_texture_creation_info
)
}

Expand Down
2 changes: 1 addition & 1 deletion src/dashboard_defs/updatable_text_pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ pub fn make_window<IndividualState: UpdatableTextWindowMethods + Clone + 'static
);

texture_contents.update_as_texture(true, params.texture_pool,
&texture_creation_info, &texture_creation_info)
&texture_creation_info, &inner_shared_state.get_fallback_texture_creation_info)
}

//////////
Expand Down
2 changes: 1 addition & 1 deletion src/dashboard_defs/weather.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ pub fn weather_updater_fn(params: WindowUpdaterParams) -> MaybeError {
weather_changed,
params.texture_pool,
&texture_creation_info,
inner_shared_state.fallback_texture_creation_info
inner_shared_state.get_fallback_texture_creation_info
)
}

Expand Down
13 changes: 7 additions & 6 deletions src/spinitron/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ impl ModelAgeData {
#[derive(Clone)]
struct SpinitronStateData {
api_key: String,
fallback_texture_creation_info: &'static TextureCreationInfo<'static>,
get_fallback_texture_creation_info: fn() -> TextureCreationInfo<'static>,

spin: Spin,
playlist: Playlist,
Expand All @@ -110,15 +110,15 @@ type WindowSize = (u32, u32);
// The third param is the fallback texture creation info, and the fourth one is the spin window size
type SpinitronStateDataParams<'a> = (
&'a str, // API key
&'static TextureCreationInfo<'static>, // Fallback texture creation info
fn() -> TextureCreationInfo<'static>, // Fallback texture creation info getter
[chrono::Duration; NUM_SPINITRON_MODEL_TYPES], // Custom model expiry durations
WindowSize
);

//////////

impl SpinitronStateData {
fn new((api_key, fallback_texture_creation_info,
fn new((api_key, get_fallback_texture_creation_info,
custom_model_expiry_durations, spin_window_size):
SpinitronStateDataParams) -> GenericResult<Self> {

Expand Down Expand Up @@ -148,7 +148,7 @@ impl SpinitronStateData {

let mut data = Self {
api_key: api_key.to_owned(),
fallback_texture_creation_info,
get_fallback_texture_creation_info,

spin, playlist, persona, show,

Expand Down Expand Up @@ -186,15 +186,16 @@ impl SpinitronStateData {

let age_state = self.age_data[model_name as usize].curr_age_state.clone();
let model = self.get_model_by_name(model_name);
let get_fallback = || Cow::Owned((self.get_fallback_texture_creation_info)());

let info = match model.get_texture_creation_info(age_state, size_pixels) {
Some(texture_creation_info) => Cow::Owned(texture_creation_info),
None => Cow::Borrowed(self.fallback_texture_creation_info)
None => get_fallback()
};

load_for_info(info).or_else(|error| {
log::warn!("Reverting to fallback texture for Spinitron model. Error: '{error}'");
load_for_info(Cow::Borrowed(self.fallback_texture_creation_info))
load_for_info(get_fallback())
})
}

Expand Down
4 changes: 2 additions & 2 deletions src/window_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ impl WindowContents {
should_remake: bool,
texture_pool: &mut TexturePool,
texture_creation_info: &TextureCreationInfo,
fallback_texture_creation_info: &TextureCreationInfo) -> MaybeError {
get_fallback_texture_creation_info: fn() -> TextureCreationInfo<'static>) -> MaybeError {

/* This is a macro for making or remaking a texture. If making or
remaking fails, a fallback texture is put into that texture's slot. */
Expand All @@ -105,7 +105,7 @@ impl WindowContents {
log::warn!("Unexpectedly failed while trying to {} texture, and reverting to a fallback \
texture. Reason: '{failure_reason}'.", $make_or_remake_description);

$make_or_remake(fallback_texture_creation_info, $($extra_args),*)
$make_or_remake(&get_fallback_texture_creation_info(), $($extra_args),*)
}
)
}};
Expand Down

0 comments on commit 9346978

Please sign in to comment.