wrap texture hashmap in a struct for convenience

This commit is contained in:
Crispy 2024-10-05 17:53:23 +02:00
parent bf82d1455f
commit cf920d7a63
3 changed files with 49 additions and 56 deletions

View file

@ -1,8 +1,4 @@
use std::{
collections::HashMap,
fs::{read_dir, read_to_string},
ops::Rem,
};
use std::{fs::read_to_string, ops::Rem};
use marble_engine::{
board::Board,
@ -54,24 +50,6 @@ enum SimState {
Stepping,
}
fn load_textures_from(
folder: &str,
rl: &mut RaylibHandle,
thread: &RaylibThread,
textures: &mut HashMap<String, Texture2D>,
) {
for d in read_dir(folder).unwrap().flatten() {
let path = d.path();
if path.is_file() {
let name = path.file_stem().unwrap().to_string_lossy();
let texture = rl
.load_texture(thread, &format!("{folder}/{name}.png"))
.unwrap();
textures.insert(name.to_string(), texture);
}
}
}
fn main() {
let (mut rl, thread) = raylib::init()
.resizable()
@ -82,9 +60,9 @@ fn main() {
rl.set_mouse_cursor(MouseCursor::MOUSE_CURSOR_CROSSHAIR);
rl.set_exit_key(None);
let mut textures: HashMap<String, Texture2D> = HashMap::new();
load_textures_from("assets", &mut rl, &thread, &mut textures);
load_textures_from("assets/tiles", &mut rl, &thread, &mut textures);
let mut textures = Textures::default();
textures.load_dir("assets", &mut rl, &thread);
textures.load_dir("assets/tiles", &mut rl, &thread);
let mut game = Game::new_sandbox();
let board = parse(&read_to_string("boards/adder.mbl").unwrap());
@ -213,7 +191,7 @@ impl Game {
}
}
fn draw_board(&self, d: &mut RaylibDrawHandle, textures: &HashMap<String, Texture2D>) {
fn draw_board(&self, d: &mut RaylibDrawHandle, textures: &Textures) {
if self.sim_state == SimState::Editing {
self.source_board
.draw(d, textures, self.view_offset, self.zoom);
@ -226,7 +204,7 @@ impl Game {
}
}
fn gui(&mut self, d: &mut RaylibDrawHandle, textures: &HashMap<String, Texture2D>) {
fn gui(&mut self, d: &mut RaylibDrawHandle, textures: &Textures) {
self.draw_board(d, textures);
let height = d.get_screen_height();
@ -295,7 +273,7 @@ impl Game {
);
};
tool_button((0, -1), "eraser", Tool::SetTile(tile_from_char(' ')));
tool_button((1, -1), "", Tool::None);
tool_button((1, -1), "transparent", Tool::None);
tool_button((0, 0), "marble", Tool::SetTile(tile_from_char('o')));
tool_button((0, 1), "block", Tool::SetTile(tile_from_char('#')));
@ -359,18 +337,15 @@ impl Game {
t.texture()
}
}
Tool::Math => format!("{}_off",self.tool_menu_math.texture_name()),
Tool::Gate => format!("{}_off",self.tool_menu_gate.texture_name()),
Tool::Math => format!("{}_off", self.tool_menu_math.texture_name()),
Tool::Gate => format!("{}_off", self.tool_menu_gate.texture_name()),
Tool::Arrow => self.tool_menu_arrow.arrow_texture_name().into(),
Tool::Mirror => self.tool_menu_mirror.texture_name().into(),
Tool::Number => todo!(),
};
let tex = textures
.get(&tex)
.unwrap_or_else(|| textures.get("missing").unwrap());
d.draw_texture_ex(
tex,
textures.get(&tex),
tile_screen_pos,
0.,
(1 << self.zoom) as f32,