Working on editor

This commit is contained in:
Thomas Forgione 2019-03-30 10:13:29 +01:00
parent 1ef818f58a
commit e539fe402e
No known key found for this signature in database
GPG Key ID: BFD17A2D71B3B5E7
3 changed files with 96 additions and 8 deletions

View File

@ -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;
}
}
}

View File

@ -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. /// Returns the offset to the corresponding graphic tile in the texture.
@ -279,11 +279,6 @@ impl Map {
tiles[(rows - 1, i)] = CollisionTile::full(); 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) Map::from_collision_tiles(tiles)
} }

View File

@ -72,12 +72,17 @@ impl Renderer {
/// Draws a drawable. /// Draws a drawable.
pub fn draw<D: Drawable>(&mut self, drawable: &D) { pub fn draw<D: Drawable>(&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<D: Drawable, V: Into<Vector2<f32>>>(&mut self, drawable: &D, translation: V) {
let texture = self.texture_manager.get(drawable.texture()); let texture = self.texture_manager.get(drawable.texture());
let mut sprite = Sprite::with_texture(&texture); let mut sprite = Sprite::with_texture(&texture);
sprite.set_texture_rect(&drawable.texture_rect()); sprite.set_texture_rect(&drawable.texture_rect());
use sfml::graphics::Transformable; use sfml::graphics::Transformable;
sprite.set_position(drawable.position()); sprite.set_position(drawable.position() + translation.into());
self.window.draw(&sprite); self.window.draw(&sprite);
} }
@ -116,4 +121,14 @@ impl Renderer {
pub fn poll_event(&mut self) -> Option<Event> { pub fn poll_event(&mut self) -> Option<Event> {
self.window.poll_event() 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
}
} }