diff --git a/src/bin/player.rs b/src/bin/player.rs index 22fddd1..2fc23a9 100644 --- a/src/bin/player.rs +++ b/src/bin/player.rs @@ -1,5 +1,8 @@ #[windows_subsystem = "windows"] +use std::collections::HashMap; +use std::path::PathBuf; + use log::error; use pixels::{Error, SurfaceTexture, PixelsBuilder}; use pixels::wgpu::BackendBit; @@ -7,14 +10,16 @@ use winit::dpi::{LogicalPosition, LogicalSize, PhysicalSize}; use winit::event::{Event, VirtualKeyCode}; use winit::event_loop::{ControlFlow, EventLoop}; use winit_input_helper::WinitInputHelper; -use std::collections::HashMap; + +use peachy::Game; #[derive(Clone, Debug)] struct Image { pixels: [u8; 64] } -struct Game { +struct State { + game: Game, width: usize, height: usize, player_position: (u8, u8), @@ -24,7 +29,7 @@ struct Game { music: HashMap, } -impl Game { +impl State { fn draw(&self, screen: &mut [u8]) { // clear screen for pixel in screen.chunks_exact_mut(4) { @@ -68,7 +73,11 @@ impl Game { fn main() -> Result<(), Error> { env_logger::init(); - let mut game = Game { + let path = PathBuf::from("src/test-resources/basic"); + let game = peachy::Game::from_dir(path).unwrap(); + + let mut state = State { + game, width: 16, height: 9, player_position: (8, 4), @@ -97,48 +106,50 @@ fn main() -> Result<(), Error> { let (window, p_width, p_height, mut _hidpi_factor) = create_window( "pixels test", - (game.width * 8) as f64, - (game.height * 8) as f64, + (state.game.config.width * 8) as f64, + (state.game.config.height * 8) as f64, &event_loop ); let surface_texture = SurfaceTexture::new(p_width, p_height, &window); let mut pixels = PixelsBuilder::new( - (game.width * 8) as u32, (game.height * 8) as u32, surface_texture + (state.game.config.width * 8) as u32, + (state.game.config.height * 8) as u32, + surface_texture ) .wgpu_backend(BackendBit::GL | BackendBit::PRIMARY) .enable_vsync(false) .build()?; - let device = rodio::default_output_device().unwrap(); + // let device = rodio::default_output_device().unwrap(); + // + // let source = rodio_xm::XMSource::from_bytes( + // include_bytes!("../ninety degrees.xm") + // ); + // + // let sink = rodio::Sink::new(&device); + // sink.append(source); + // sink.pause(); + // + // game.music.insert(":ninety degrees".into(), sink); + // + // let source = rodio_xm::XMSource::from_bytes( + // include_bytes!("../orn_keygentheme2001.xm") + // ); + // + // let sink = rodio::Sink::new(&device); + // sink.append(source); + // sink.pause(); + // + // game.music.insert("orn_keygentheme2001".into(), sink); - let source = rodio_xm::XMSource::from_bytes( - include_bytes!("../ninety degrees.xm") - ); - - let sink = rodio::Sink::new(&device); - sink.append(source); - sink.pause(); - - game.music.insert(":ninety degrees".into(), sink); - - let source = rodio_xm::XMSource::from_bytes( - include_bytes!("../orn_keygentheme2001.xm") - ); - - let sink = rodio::Sink::new(&device); - sink.append(source); - sink.pause(); - - game.music.insert("orn_keygentheme2001".into(), sink); - - game.current_music = None; + state.current_music = None; event_loop.run(move |event, _, control_flow| { // The one and only event that winit_input_helper doesn't have for us... if let Event::RedrawRequested(_) = event { - game.draw(pixels.get_frame()); + state.draw(pixels.get_frame()); if pixels .render() @@ -161,49 +172,49 @@ fn main() -> Result<(), Error> { if input.key_pressed(VirtualKeyCode::M) { // pause the current tune - if game.current_music.is_some() { - game.music.get(game.current_music.as_ref().unwrap()).unwrap().pause(); + if state.current_music.is_some() { + state.music.get(state.current_music.as_ref().unwrap()).unwrap().pause(); } - if game.current_music.is_none() || game.current_music.as_ref().unwrap() == "orn_keygentheme2001" { + if state.current_music.is_none() || state.current_music.as_ref().unwrap() == "orn_keygentheme2001" { // play the first tune - game.current_music = Some(":ninety degrees".into()); - game.music.get(game.current_music.as_ref().unwrap()).unwrap().play(); + state.current_music = Some(":ninety degrees".into()); + state.music.get(state.current_music.as_ref().unwrap()).unwrap().play(); } else { // play the second tune - game.current_music = Some("orn_keygentheme2001".into()); - game.music.get(game.current_music.as_ref().unwrap()).unwrap().play(); + state.current_music = Some("orn_keygentheme2001".into()); + state.music.get(state.current_music.as_ref().unwrap()).unwrap().play(); } } if input.key_pressed(VirtualKeyCode::Left) { - let (x, y) = game.player_position; + let (x, y) = state.player_position; if x > 0 { - game.player_position = (x - 1, y); + state.player_position = (x - 1, y); window.request_redraw(); } } if input.key_pressed(VirtualKeyCode::Right) { - let (x, y) = game.player_position; - if x < game.width as u8 - 1 { - game.player_position = (x + 1, y); + let (x, y) = state.player_position; + if x < state.game.config.width as u8 - 1 { + state.player_position = (x + 1, y); window.request_redraw(); } } if input.key_pressed(VirtualKeyCode::Up) { - let (x, y) = game.player_position; + let (x, y) = state.player_position; if y > 0 { - game.player_position = (x, y - 1); + state.player_position = (x, y - 1); window.request_redraw(); } } if input.key_pressed(VirtualKeyCode::Down) { - let (x, y) = game.player_position; - if y < game.height as u8 - 1 { - game.player_position = (x, y + 1); + let (x, y) = state.player_position; + if y < state.game.config.height as u8 - 1 { + state.player_position = (x, y + 1); window.request_redraw(); } } diff --git a/src/config.rs b/src/config.rs index c034769..54678ce 100644 --- a/src/config.rs +++ b/src/config.rs @@ -3,15 +3,15 @@ use serde_derive::{Serialize, Deserialize}; #[derive(Debug, Eq, PartialEq, Serialize, Deserialize)] pub struct Config { /// used in the window title bar - name: Option, - width: u8, - height: u8, + pub name: Option, + pub width: u8, + pub height: u8, /// animation rate in milliseconds - tick: u64, + pub tick: u64, /// if this is not specified, the game will pick the first room it finds - starting_room: Option, + pub starting_room: Option, /// major / minor - version: (u8, u8), + pub version: (u8, u8), } #[cfg(test)] diff --git a/src/lib.rs b/src/lib.rs index bada357..e3226ad 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -99,14 +99,14 @@ pub struct Position { // } pub struct Game { - config: Config, - palettes: Vec, - images: Vec, - tiles: Vec, - entities: Vec, - // variables: Vec, - // triggers: HashMap, - music: Vec, + pub config: Config, + pub palettes: Vec, + pub images: Vec, + pub tiles: Vec, + pub entities: Vec, + // pub variables: Vec, + // pub triggers: HashMap, + pub music: Vec, } #[derive(Debug)] @@ -129,7 +129,6 @@ impl Game { for file in music_files.unwrap() { let file = file.unwrap(); music.push(Music::from_file(file.path())); - println!("music found: {:?}", file.file_name()); } } @@ -142,11 +141,10 @@ impl Game { for file in palette_files { let file = file.unwrap(); palettes.push(Palette::from_file(file.path())); - println!("palette found: {:?}", file.file_name()); } let mut images_dir = path.clone(); - images_dir.push("palettes"); + images_dir.push("images"); let image_files = images_dir.read_dir() .expect("couldn't read image dir"); @@ -154,11 +152,10 @@ impl Game { for file in image_files { let file = file.unwrap(); images.push(Image::from_file(file.path())); - println!("image found: {:?}", file.file_name()); } let mut tiles_dir = path.clone(); - tiles_dir.push("palettes"); + tiles_dir.push("tiles"); let tiles_files = tiles_dir.read_dir() .expect("couldn't read tile dir"); @@ -166,11 +163,10 @@ impl Game { for file in tiles_files { let file = file.unwrap(); tiles.push(Tile::from_file(file.path())); - println!("tile found: {:?}", file.file_name()); } let mut entities_dir = path.clone(); - entities_dir.push("palettes"); + entities_dir.push("entities"); let entities_files = entities_dir.read_dir() .expect("couldn't read tile dir"); @@ -178,7 +174,6 @@ impl Game { for file in entities_files { let file = file.unwrap(); entities.push(Entity::from_file(file.path())); - println!("entity found: {:?}", file.file_name()); } let mut game_config = path.clone();