summaryrefslogtreecommitdiffstats
path: root/src/graphics.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/graphics.rs')
-rw-r--r--src/graphics.rs177
1 files changed, 94 insertions, 83 deletions
diff --git a/src/graphics.rs b/src/graphics.rs
index 7e8a0e1..8c5fd73 100644
--- a/src/graphics.rs
+++ b/src/graphics.rs
@@ -36,22 +36,16 @@ use sfml::graphics::{
#[folder = "res/maps"]
pub struct MapAssets;
-#[derive(Debug)]
+
pub struct Graphics {
- // sfml elements
+ // rendering elements
window: RenderWindow,
- view: View,
+ scenes: Vec<Box<Scene>>,
+ current_scene: Box<Scene>,
// status
running: bool,
- panning: bool,
- // pan
- pan_start: Vector2f,
- // loaded resources
- tilesets: HashMap<u32, Image>,
- textures: HashMap<u32, Texture>,
}
-
impl Graphics {
pub fn new() -> Graphics {
let default_window_size = (1280, 720);
@@ -73,38 +67,35 @@ impl Graphics {
window.set_framerate_limit(default_framerate);
window.set_vertical_sync_enabled(true);
- let mut graphics = Graphics {
- // sfml elements
+ let map_scene = Box::new(MapScene::new(&window));
+
+ return Graphics {
window: window,
- view: View::new(
- Vector2f::new(
- default_window_size.0 as f32 / 2.0,
- default_window_size.1 as f32 / 2.0
- ),
- Vector2f::new(
- default_window_size.0 as f32,
- default_window_size.1 as f32,
- ),
- ),
- // status
+ scenes: Vec::new(),
+ current_scene: map_scene,
running: true,
- panning: false,
- // zoom and pan
- pan_start: Vector2f::new(0.0, 0.0),
- // resources
- tilesets: HashMap::new(),
- textures: HashMap::new(),
};
-
- graphics.window.set_view(&graphics.view);
-
- return graphics;
}
pub fn is_running(self: &mut Self) -> bool {
return self.running;
}
+ pub fn update(self: &mut Self) {
+ while let Some(event) = self.window.poll_event() {
+ match event {
+ Event::Closed => {
+ self.window.close();
+ self.running = false;
+ }
+ _ => {}
+ }
+
+ self.current_scene.event(&mut self.window, event)
+ }
+
+ }
+
pub fn render(self: &mut Self, state: &crate::game::State) {
// background
@@ -114,10 +105,41 @@ impl Graphics {
};
self.window.clear(&bgcolor);
+ self.current_scene.render(&mut self.window, state);
+ self.window.display();
+ }
+
+}
- // render map
+trait Scene {
+ fn render(self: &mut Self, window: &mut RenderWindow, state: &crate::game::State);
+ fn event(self: &mut Self, window: &mut RenderWindow, event: Event);
+}
+
+struct MapScene {
+ view: View,
+ // pan
+ panning: bool,
+ pan_start: Vector2f,
+ // loaded resources
+ tilesets: HashMap<u32, Image>,
+ textures: HashMap<u32, Texture>,
+}
+
+impl MapScene {
+ fn new(window: &RenderWindow) -> MapScene {
+ return MapScene {
+ view: window.default_view().to_owned(),
+ panning: false,
+ pan_start: Vector2f::new(0.0, 0.0),
+ tilesets: HashMap::new(),
+ textures: HashMap::new()
+ };
+ }
+}
- // for each tile
+impl Scene for MapScene {
+ fn render(self: &mut Self, window: &mut RenderWindow, state: &crate::game::State) {
for y in 0 .. state.map.height {
for x in 0 .. state.map.width {
// for each layer
@@ -204,62 +226,51 @@ impl Graphics {
});
- self.window.draw(&tile_rect);
+ window.draw(&tile_rect);
}
}
}
-
- // draw panels
-
-
- self.window.display();
}
- pub fn update(self: &mut Self) {
- while let Some(ev) = self.window.poll_event() {
- match ev {
- Event::Closed => {
- self.window.close();
- self.running = false;
- },
- Event::MouseWheelScrolled { wheel: _, delta, x: _, y: _ } => {
- // delta is probalby a value between -1 and 1
- let zoom = 1.0 + delta * 0.1;
- self.view.zoom(zoom);
- self.window.set_view(&self.view);
- },
- Event::MouseButtonPressed { button, x, y} => {
- match button {
- Button::Right => {},
- Button::Left => {
- // set pan start position
- self.panning = true;
- self.pan_start = self.window.map_pixel_to_coords_current_view(&Vector2i::new(x, y));
- }
- _ => {},
- }
- },
- Event::MouseButtonReleased { button, x: _, y: _ } => {
- match button {
- Button::Right => {}
- Button::Left => {
- self.panning = false;
- },
- _ => {},
- }
- },
- Event::MouseMoved { x, y } => {
- if self.panning {
- // get delta of vectors
- let delta = self.pan_start - self.window.map_pixel_to_coords_current_view(&Vector2i::new(x, y));
-
- // move view by moving the center by delta
- self.view.move_(delta);
- self.window.set_view(&self.view);
+ fn event(self: &mut Self, window: &mut RenderWindow, event: Event) {
+ match event {
+ Event::MouseWheelScrolled { wheel: _, delta, x: _, y: _ } => {
+ // delta is probalby a value between -1 and 1
+ let zoom = 1.0 + delta * 0.1;
+ self.view.zoom(zoom);
+ window.set_view(&self.view);
+ },
+ Event::MouseButtonPressed { button, x, y} => {
+ match button {
+ Button::Right => {},
+ Button::Left => {
+ // set pan start position
+ self.panning = true;
+ self.pan_start = window.map_pixel_to_coords_current_view(&Vector2i::new(x, y));
}
+ _ => {},
+ }
+ },
+ Event::MouseButtonReleased { button, x: _, y: _ } => {
+ match button {
+ Button::Right => {}
+ Button::Left => {
+ self.panning = false;
+ },
+ _ => {},
+ }
+ },
+ Event::MouseMoved { x, y } => {
+ if self.panning {
+ // get delta of vectors
+ let delta = self.pan_start - window.map_pixel_to_coords_current_view(&Vector2i::new(x, y));
+
+ // move view by moving the center by delta
+ self.view.move_(delta);
+ window.set_view(&self.view);
}
- _ => {},
}
+ _ => {},
}
}
} \ No newline at end of file