diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/game.rs | 3 | ||||
-rw-r--r-- | src/graphics.rs | 58 | ||||
-rw-r--r-- | src/main.rs | 28 |
3 files changed, 76 insertions, 13 deletions
diff --git a/src/game.rs b/src/game.rs index 335e9da..a913596 100644 --- a/src/game.rs +++ b/src/game.rs @@ -25,6 +25,9 @@ pub fn new() -> State { Err(e) => panic!("Failed to parse demo map: {}", e.description()), }; + // allow only orthogonal maps + assert_eq!(demo_map.orientation, tiled::Orientation::Orthogonal); + return State { running: true, map: demo_map diff --git a/src/graphics.rs b/src/graphics.rs index 003f182..deff94e 100644 --- a/src/graphics.rs +++ b/src/graphics.rs @@ -1,5 +1,6 @@ extern crate sfml; +use std::collections::HashMap; use sfml::window::{ ContextSettings, Event, @@ -9,9 +10,17 @@ use sfml::window::{ use sfml::graphics::{ RenderWindow, RenderTarget, + Texture, Color, + Rect, }; + +#[derive(RustEmbed)] +#[folder = "res"] +pub struct Assets; + + pub fn start() -> RenderWindow { let default_window_size = (1280, 720); let default_framerate = 80; @@ -34,8 +43,53 @@ pub fn start() -> RenderWindow { return window; } -pub fn render(window: &mut RenderWindow) { - window.clear(&Color::BLACK); +pub fn render(window: &mut RenderWindow, state: &crate::game::State) { + + // background + let bgcolor = match state.map.background_colour { + Some(color) => Color::rgb(color.red, color.green, color.blue), + None => Color::BLACK, + }; + + window.clear(&bgcolor); + + // load tilesets + let mut textures: HashMap<u32, Texture> = HashMap::new(); + for tileset in &state.map.tilesets { + + let image = &tileset.images[0]; + let asset = Assets::get(&image.source); + + if let Some(asset) = asset { + let texture = Texture::from_memory( + asset.as_ref(), + &Rect { + left: 0, + top: 0, + width: image.width, + height: image.height + } + ); + + if let Some(texture) = texture { + textures.insert(tileset.first_gid, texture); + } + } + } + + // render map + + // for each tile + for y in 0 .. state.map.height { + for x in 0 .. state.map.width { + // for each layer + for layer in &state.map.layers { + state.map.get_tileset_by_gid(layer.tiles[y as usize][x as usize]); + } + } + } + + window.display(); } diff --git a/src/main.rs b/src/main.rs index 4d32b95..9eb709f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,43 +8,49 @@ use std::thread; use std::sync::{Arc, Mutex}; fn main() { - let state_mutex = Arc::new(Mutex::new(game::new())); + let game_state_mutex = Arc::new(Mutex::new(game::new())); - let state = state_mutex.clone(); + let game_state = game_state_mutex.clone(); let game_thread = thread::spawn(move || { loop { // aquire state resource - let mut state = match state.lock() { - Ok(state) => state, + let mut game_state = match game_state.lock() { + Ok(game_state) => game_state, Err(poisoned) => poisoned.into_inner(), }; - if state.running == false { + if game_state.running == false { break; } - game::update(&mut state); + game::update(&mut game_state); } }); - let state = state_mutex.clone(); + let game_state = game_state_mutex.clone(); let graphics_thread = thread::spawn(move || { let mut window = graphics::start(); while window.is_open() { - graphics::render(&mut window); + // aquire state resource + let game_state = match game_state.lock() { + Ok(game_state) => game_state, + Err(poisoned) => poisoned.into_inner(), + }; + + graphics::render(&mut window, &game_state); graphics::update(&mut window); } // aquire state resource - let mut state = match state.lock() { - Ok(state) => state, + let mut game_state = match game_state.lock() { + Ok(game_state) => game_state, Err(poisoned) => poisoned.into_inner(), }; // stop game thread - state.running = false; + game_state.running = false; }); // wait for both thread to die |