wrap texture hashmap in a struct for convenience
This commit is contained in:
parent
bf82d1455f
commit
cf920d7a63
3 changed files with 49 additions and 56 deletions
45
src/main.rs
45
src/main.rs
|
@ -1,8 +1,4 @@
|
||||||
use std::{
|
use std::{fs::read_to_string, ops::Rem};
|
||||||
collections::HashMap,
|
|
||||||
fs::{read_dir, read_to_string},
|
|
||||||
ops::Rem,
|
|
||||||
};
|
|
||||||
|
|
||||||
use marble_engine::{
|
use marble_engine::{
|
||||||
board::Board,
|
board::Board,
|
||||||
|
@ -54,24 +50,6 @@ enum SimState {
|
||||||
Stepping,
|
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() {
|
fn main() {
|
||||||
let (mut rl, thread) = raylib::init()
|
let (mut rl, thread) = raylib::init()
|
||||||
.resizable()
|
.resizable()
|
||||||
|
@ -82,9 +60,9 @@ fn main() {
|
||||||
rl.set_mouse_cursor(MouseCursor::MOUSE_CURSOR_CROSSHAIR);
|
rl.set_mouse_cursor(MouseCursor::MOUSE_CURSOR_CROSSHAIR);
|
||||||
rl.set_exit_key(None);
|
rl.set_exit_key(None);
|
||||||
|
|
||||||
let mut textures: HashMap<String, Texture2D> = HashMap::new();
|
let mut textures = Textures::default();
|
||||||
load_textures_from("assets", &mut rl, &thread, &mut textures);
|
textures.load_dir("assets", &mut rl, &thread);
|
||||||
load_textures_from("assets/tiles", &mut rl, &thread, &mut textures);
|
textures.load_dir("assets/tiles", &mut rl, &thread);
|
||||||
|
|
||||||
let mut game = Game::new_sandbox();
|
let mut game = Game::new_sandbox();
|
||||||
let board = parse(&read_to_string("boards/adder.mbl").unwrap());
|
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 {
|
if self.sim_state == SimState::Editing {
|
||||||
self.source_board
|
self.source_board
|
||||||
.draw(d, textures, self.view_offset, self.zoom);
|
.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);
|
self.draw_board(d, textures);
|
||||||
|
|
||||||
let height = d.get_screen_height();
|
let height = d.get_screen_height();
|
||||||
|
@ -295,7 +273,7 @@ impl Game {
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
tool_button((0, -1), "eraser", Tool::SetTile(tile_from_char(' ')));
|
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, 0), "marble", Tool::SetTile(tile_from_char('o')));
|
||||||
tool_button((0, 1), "block", Tool::SetTile(tile_from_char('#')));
|
tool_button((0, 1), "block", Tool::SetTile(tile_from_char('#')));
|
||||||
|
@ -359,18 +337,15 @@ impl Game {
|
||||||
t.texture()
|
t.texture()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Tool::Math => format!("{}_off",self.tool_menu_math.texture_name()),
|
Tool::Math => format!("{}_off", self.tool_menu_math.texture_name()),
|
||||||
Tool::Gate => format!("{}_off",self.tool_menu_gate.texture_name()),
|
Tool::Gate => format!("{}_off", self.tool_menu_gate.texture_name()),
|
||||||
Tool::Arrow => self.tool_menu_arrow.arrow_texture_name().into(),
|
Tool::Arrow => self.tool_menu_arrow.arrow_texture_name().into(),
|
||||||
Tool::Mirror => self.tool_menu_mirror.texture_name().into(),
|
Tool::Mirror => self.tool_menu_mirror.texture_name().into(),
|
||||||
Tool::Number => todo!(),
|
Tool::Number => todo!(),
|
||||||
};
|
};
|
||||||
|
|
||||||
let tex = textures
|
|
||||||
.get(&tex)
|
|
||||||
.unwrap_or_else(|| textures.get("missing").unwrap());
|
|
||||||
d.draw_texture_ex(
|
d.draw_texture_ex(
|
||||||
tex,
|
textures.get(&tex),
|
||||||
tile_screen_pos,
|
tile_screen_pos,
|
||||||
0.,
|
0.,
|
||||||
(1 << self.zoom) as f32,
|
(1 << self.zoom) as f32,
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use std::collections::HashMap;
|
use crate::Textures;
|
||||||
|
|
||||||
use super::tile::*;
|
use super::tile::*;
|
||||||
use raylib::prelude::*;
|
use raylib::prelude::*;
|
||||||
|
@ -97,22 +97,14 @@ impl Board {
|
||||||
self.height
|
self.height
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn draw(
|
pub fn draw(&self, d: &mut RaylibDrawHandle, textures: &Textures, offset: Vector2, zoom: i32) {
|
||||||
&self,
|
|
||||||
d: &mut RaylibDrawHandle,
|
|
||||||
textures: &HashMap<String, Texture2D>,
|
|
||||||
offset: Vector2,
|
|
||||||
zoom: i32,
|
|
||||||
) {
|
|
||||||
let tile_size = 16 << zoom;
|
let tile_size = 16 << zoom;
|
||||||
for x in 0..self.width {
|
for x in 0..self.width {
|
||||||
for y in 0..self.height {
|
for y in 0..self.height {
|
||||||
if let Some(tile) = self.get((x, y).into()) {
|
if let Some(tile) = self.get((x, y).into()) {
|
||||||
let px = x as i32 * tile_size + offset.x as i32 + tile_size / 2;
|
let px = x as i32 * tile_size + offset.x as i32 + tile_size / 2;
|
||||||
let py = y as i32 * tile_size + offset.y as i32 + tile_size / 2;
|
let py = y as i32 * tile_size + offset.y as i32 + tile_size / 2;
|
||||||
let texture = textures
|
let texture = textures.get(&tile.texture());
|
||||||
.get(&tile.texture())
|
|
||||||
.unwrap_or_else(|| textures.get("missing").unwrap());
|
|
||||||
d.draw_texture_ex(
|
d.draw_texture_ex(
|
||||||
texture,
|
texture,
|
||||||
Vector2::new((px - tile_size / 2) as f32, (py - tile_size / 2) as f32),
|
Vector2::new((px - tile_size / 2) as f32, (py - tile_size / 2) as f32),
|
||||||
|
|
32
src/util.rs
32
src/util.rs
|
@ -1,5 +1,33 @@
|
||||||
|
use std::{collections::HashMap, fs::read_dir};
|
||||||
|
|
||||||
use raylib::prelude::*;
|
use raylib::prelude::*;
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
|
pub struct Textures {
|
||||||
|
map: HashMap<String, Texture2D>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Textures {
|
||||||
|
pub fn load_dir(&mut self, folder: &str, rl: &mut RaylibHandle, thread: &RaylibThread) {
|
||||||
|
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();
|
||||||
|
self.map.insert(name.to_string(), texture);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get(&self, name: &str) -> &Texture2D {
|
||||||
|
self.map
|
||||||
|
.get(name)
|
||||||
|
.unwrap_or_else(|| self.map.get("missing").unwrap())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn text_input(
|
pub fn text_input(
|
||||||
d: &mut RaylibDrawHandle,
|
d: &mut RaylibDrawHandle,
|
||||||
bounds: Rectangle,
|
bounds: Rectangle,
|
||||||
|
@ -53,7 +81,7 @@ pub fn text_input(
|
||||||
pub fn texture_button<T>(
|
pub fn texture_button<T>(
|
||||||
d: &mut RaylibDrawHandle,
|
d: &mut RaylibDrawHandle,
|
||||||
pos: Vector2,
|
pos: Vector2,
|
||||||
texture: Option<&Texture2D>,
|
texture: &Texture2D,
|
||||||
option: T,
|
option: T,
|
||||||
current: &mut T,
|
current: &mut T,
|
||||||
tex_size: f32,
|
tex_size: f32,
|
||||||
|
@ -74,7 +102,6 @@ pub fn texture_button<T>(
|
||||||
height: tex_size + border * 2.,
|
height: tex_size + border * 2.,
|
||||||
};
|
};
|
||||||
d.draw_rectangle_rec(bounds, color);
|
d.draw_rectangle_rec(bounds, color);
|
||||||
if let Some(texture) = texture {
|
|
||||||
d.draw_texture_ex(
|
d.draw_texture_ex(
|
||||||
texture,
|
texture,
|
||||||
pos + Vector2::new(border, border),
|
pos + Vector2::new(border, border),
|
||||||
|
@ -82,7 +109,6 @@ pub fn texture_button<T>(
|
||||||
tex_size / texture.width as f32,
|
tex_size / texture.width as f32,
|
||||||
Color::WHITE,
|
Color::WHITE,
|
||||||
);
|
);
|
||||||
}
|
|
||||||
|
|
||||||
let mouse_pos = d.get_mouse_position();
|
let mouse_pos = d.get_mouse_position();
|
||||||
if d.is_mouse_button_pressed(MouseButton::MOUSE_BUTTON_LEFT)
|
if d.is_mouse_button_pressed(MouseButton::MOUSE_BUTTON_LEFT)
|
||||||
|
|
Loading…
Reference in a new issue