From e539fe402efbbff579e64003d78d2abca73dfa44 Mon Sep 17 00:00:00 2001 From: Thomas Forgione Date: Sat, 30 Mar 2019 10:13:29 +0100 Subject: [PATCH] Working on editor --- src/app/editor.rs | 80 +++++++++++++++++++++++++++++++++++++- src/engine/map/mod.rs | 7 +--- src/engine/renderer/mod.rs | 17 +++++++- 3 files changed, 96 insertions(+), 8 deletions(-) diff --git a/src/app/editor.rs b/src/app/editor.rs index f328e4d..5d2d93f 100644 --- a/src/app/editor.rs +++ b/src/app/editor.rs @@ -1 +1,79 @@ -fn main() {} +use clap::{App, crate_version}; + +use sfml::system::Vector2; +use sfml::window::{Event, Key}; +use sfml::graphics::{RectangleShape, RenderTarget, Transformable, Color, Shape}; + +use rusty::engine::renderer::Renderer; +use rusty::engine::map::{GraphicTile, Map}; + +fn main() { + let _ = App::new("Rusty Editor") + .version(crate_version!()) + .get_matches(); + + let mut renderer = Renderer::new(800, 600, false); + + let mut running = true; + + let map = Map::from_str(include_str!("../../assets/levels/level2.txt")).unwrap(); + + loop { + // Manage the events + while let Some(event) = renderer.poll_event() { + match event { + // Quit the game if window is closed + Event::Closed => running = false, + + // Quit the game if escape is pressed + Event::KeyPressed { + code: Key::Escape, .. + } => running = false, + + _ => (), + } + } + + // Do the rendering + renderer.clear(); + + // Show the top panel + let top_panel_size = Vector2::new(renderer.window().size().x as f32, 50.0); + let mut top_panel = RectangleShape::with_size(top_panel_size); + top_panel.set_fill_color(&Color::rgb(255, 0, 0)); + renderer.window_mut().draw(&top_panel); + + // Show the left panel + let left_panel_size = Vector2::new(100.0, renderer.window().size().y as f32 - top_panel_size.y); + let mut left_panel = RectangleShape::with_size(left_panel_size); + left_panel.set_position((0.0, top_panel_size.y)); + left_panel.set_fill_color(&Color::rgb(0, 255, 0)); + renderer.window_mut().draw(&left_panel); + + // Show the map + for i in 0 .. map.rows() { + for j in 0 .. map.cols() { + let tile = map.at(i, j); + if tile.graphic != GraphicTile::Hidden { + renderer.translate_and_draw(&tile, (left_panel_size.x, top_panel_size.y)); + } + } + } + + // Draw the border of the map + let mut border = RectangleShape::new(); + border.set_size(((map.cols() * 16) as f32, (map.rows() * 16) as f32)); + border.set_position((left_panel_size.x, top_panel_size.y)); + border.set_fill_color(&Color::rgba(0, 0, 0, 0)); + border.set_outline_color(&Color::rgb(0, 0, 0)); + border.set_outline_thickness(2.0); + renderer.window_mut().draw(&border); + + // Display and manage the frame rate + renderer.display(); + + if !running { + break; + } + } +} diff --git a/src/engine/map/mod.rs b/src/engine/map/mod.rs index 9f97bf3..5de1766 100644 --- a/src/engine/map/mod.rs +++ b/src/engine/map/mod.rs @@ -209,7 +209,7 @@ impl GraphicTile { } } - panic!("Did not find a tile, implementation error"); + GraphicTile::Center } /// Returns the offset to the corresponding graphic tile in the texture. @@ -279,11 +279,6 @@ impl Map { tiles[(rows - 1, i)] = CollisionTile::full(); } - tiles[(25, 12)] = CollisionTile::full(); - tiles[(25, 13)] = CollisionTile::full(); - tiles[(25, 14)] = CollisionTile::full(); - tiles[(25, 15)] = CollisionTile::full(); - Map::from_collision_tiles(tiles) } diff --git a/src/engine/renderer/mod.rs b/src/engine/renderer/mod.rs index 48e28b8..b144751 100644 --- a/src/engine/renderer/mod.rs +++ b/src/engine/renderer/mod.rs @@ -72,12 +72,17 @@ impl Renderer { /// Draws a drawable. pub fn draw(&mut self, drawable: &D) { + self.translate_and_draw(drawable, (0.0, 0.0)); + } + + /// Draws a drawable with a certain translation. + pub fn translate_and_draw>>(&mut self, drawable: &D, translation: V) { let texture = self.texture_manager.get(drawable.texture()); let mut sprite = Sprite::with_texture(&texture); sprite.set_texture_rect(&drawable.texture_rect()); use sfml::graphics::Transformable; - sprite.set_position(drawable.position()); + sprite.set_position(drawable.position() + translation.into()); self.window.draw(&sprite); } @@ -116,4 +121,14 @@ impl Renderer { pub fn poll_event(&mut self) -> Option { self.window.poll_event() } + + /// Returns a reference to the window. + pub fn window(&self) -> &RenderWindow { + &self.window + } + + /// Returns a mutable reference to the window. + pub fn window_mut(&mut self) -> &mut RenderWindow { + &mut self.window + } }