add: assets, some changes

This commit is contained in:
Charles
2025-03-23 19:04:35 -07:00
parent d6853a1f37
commit 613e44e085
4 changed files with 42 additions and 6 deletions
+42 -6
View File
@@ -17,6 +17,7 @@ pub fn main() -> Result<(), String> {
.window("rust-sdl2 demo: Video", 1024, 600)
.position_centered()
.opengl()
.fullscreen()
.build()
.map_err(|e| e.to_string())?;
@@ -195,6 +196,12 @@ pub fn main() -> Result<(), String> {
.load_texture_bytes(include_bytes!("../assets/dog.png"))
.unwrap(),
);
textures.insert(
"heart",
texture_creator
.load_texture_bytes(include_bytes!("../assets/heart.png"))
.unwrap(),
);
let opts: Vec<&str> = textures.keys().map(|s| *s).collect();
@@ -251,26 +258,41 @@ pub fn main() -> Result<(), String> {
}
}
// Find the most-exact match to show
let mut matches = vec![];
let mut skip_chars = 0;
let mut chars = line.chars();
for i in 0..line.len() {
let part_line = find_matches(chars.as_str(), &opts);
for part in part_line {
matches.push((i, part));
}
if matches.len() == 0 {
// Advance the line so we don't leak chars
skip_chars = i;
}
chars.next();
}
let matches: Vec<&str> = matches.into_iter().map(|(_, s)| s).collect();
let show = if matches.len() > 0 { matches[0] } else { "" };
// Discard extra characters from line
// Discard extra characters from line; find any lines that could match
// and discard up to there
let mut skip_chars = 0;
let mut chars = line.chars();
for i in 0..line.len() {
let part_line = find_could_match(chars.as_str(), &opts);
if part_line.len() == 0 {
// Advance the line so we don't leak chars
skip_chars = i;
} else {
break;
}
chars.next();
}
let (_, tline) = line.split_at(skip_chars);
line = String::from(tline);
//println!("Show {show}");
//println!("Matches {:?}", matches);
//println!("Line {line}");
canvas.clear();
if let Some(tex) = textures.get(show) {
canvas.copy(tex, None, None).unwrap();
@@ -295,3 +317,17 @@ fn find_matches<'a, 'b>(word: &'b str, opts: &'a [&'a str]) -> Vec<&'a str> {
}
ret
}
fn find_could_match<'a, 'b>(word: &'b str, opts: &'a [&'a str]) -> Vec<&'a str> {
if word.len() == 0 {
return vec![];
}
let mut ret = vec![];
for opt in opts {
if opt.starts_with(word) {
ret.push(*opt)
}
}
ret
}