move camera into engine
authoralfadur
Fri, 22 Mar 2019 20:01:47 +0300
changeset 14709 12db7e435ea6
parent 14708 81030dcbd2d8
child 14710 19122a329774
move camera into engine
rust/hwrunner/Cargo.toml
rust/hwrunner/src/main.rs
rust/lib-hedgewars-engine/src/instance.rs
rust/lib-hedgewars-engine/src/lib.rs
rust/lib-hedgewars-engine/src/render.rs
rust/lib-hedgewars-engine/src/render/camera.rs
rust/lib-hedgewars-engine/src/world.rs
--- a/rust/hwrunner/Cargo.toml	Fri Mar 22 19:09:28 2019 +0300
+++ b/rust/hwrunner/Cargo.toml	Fri Mar 22 20:01:47 2019 +0300
@@ -5,9 +5,8 @@
 edition = "2018"
 
 [dependencies]
-#gfx = "0.17"
 glutin = "0.20"
 gl = "0.11"
-#gfx_window_glutin = "0.26"
+integral-geometry = { path = "../integral-geometry" }
 
 lib-hedgewars-engine = { path = "../lib-hedgewars-engine" }
--- a/rust/hwrunner/src/main.rs	Fri Mar 22 19:09:28 2019 +0300
+++ b/rust/hwrunner/src/main.rs	Fri Mar 22 20:01:47 2019 +0300
@@ -13,7 +13,11 @@
     ContextTrait,
 };
 
-use hedgewars_engine::instance::EngineInstance;
+use hedgewars_engine::{
+    instance::EngineInstance,
+};
+
+use integral_geometry::Point;
 
 fn init(event_loop: &EventsLoop, size: dpi::LogicalSize) -> WindowedContext {
     use glutin::{
@@ -51,11 +55,7 @@
 
     let mut engine = EngineInstance::new();
 
-    // dirty dirty code follows; DO NOT USE
-    let mut zoom = 1f32;
     let mut dragging = false;
-    let mut x = 0f32;
-    let mut y = 0f32;
 
     use std::time::Instant;
 
@@ -85,23 +85,25 @@
                         }
                     }
                     WindowEvent::MouseWheel { delta, .. } => {
-                        match delta {
+                        let zoom_change = match delta {
                             MouseScrollDelta::LineDelta(x, y) => {
-                                zoom += y as f32 * 0.1f32;
+                                y as f32 * 0.1f32
                             }
                             MouseScrollDelta::PixelDelta(delta) => {
                                 let physical = delta.to_physical(window.get_hidpi_factor());
-                                zoom += physical.y as f32 * 0.1f32;
+                                physical.y as f32 * 0.1f32
                             }
-                        }
+                        };
+                        engine.world.move_camera(Point::ZERO, zoom_change);
                     }
                     _ => ()
                 },
                 Event::DeviceEvent { event, .. } => match event {
                     DeviceEvent::MouseMotion { delta } => {
                         if dragging {
-                            x -= delta.0 as f32;
-                            y -= delta.1 as f32;
+                            engine.world.move_camera(
+                                Point::new(delta.0 as i32, delta.1 as i32), 0.0
+                            )
                         }
                     }
                     _ => {}
@@ -112,8 +114,7 @@
 
         unsafe { window.make_current().unwrap() };
 
-        // temporary params.. dont actually handle input here
-        engine.render(x, y, w as f32 * zoom, h as f32 * zoom);
+        engine.render();
 
         window.swap_buffers().unwrap();
     }
--- a/rust/lib-hedgewars-engine/src/instance.rs	Fri Mar 22 19:09:28 2019 +0300
+++ b/rust/lib-hedgewars-engine/src/instance.rs	Fri Mar 22 20:01:47 2019 +0300
@@ -38,8 +38,8 @@
         }
     }
 
-    pub fn render(&mut self, x: f32, y: f32, w: f32, h: f32) {
-        self.world.render(x, y, w, h);
+    pub fn render(&mut self) {
+        self.world.render();
     }
 
     fn process_unordered_message(&mut self, message: &UnorderedEngineMessage) {
--- a/rust/lib-hedgewars-engine/src/lib.rs	Fri Mar 22 19:09:28 2019 +0300
+++ b/rust/lib-hedgewars-engine/src/lib.rs	Fri Mar 22 20:01:47 2019 +0300
@@ -1,5 +1,5 @@
 pub mod instance;
-mod ipc;
+pub mod ipc;
 mod render;
 mod world;
 
--- a/rust/lib-hedgewars-engine/src/render.rs	Fri Mar 22 19:09:28 2019 +0300
+++ b/rust/lib-hedgewars-engine/src/render.rs	Fri Mar 22 20:01:47 2019 +0300
@@ -1,5 +1,6 @@
 mod map;
 mod gl;
+pub mod camera;
 
 pub use self::map::*;
 use self::gl::*;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rust/lib-hedgewars-engine/src/render/camera.rs	Fri Mar 22 20:01:47 2019 +0300
@@ -0,0 +1,18 @@
+use integral_geometry::{Point, Rect, Size};
+
+#[derive(Debug)]
+pub struct Camera {
+    pub position: Point,
+    pub zoom: f32,
+    size: Size
+}
+
+impl Camera {
+    pub fn new() -> Self {
+        Self {position: Point::ZERO, zoom: 0.0, size: Size::new(1024, 768) }
+    }
+
+    pub fn viewport(&self) -> Rect {
+        Rect::from_size(self.position, self.size)
+    }
+}
\ No newline at end of file
--- a/rust/lib-hedgewars-engine/src/world.rs	Fri Mar 22 19:09:28 2019 +0300
+++ b/rust/lib-hedgewars-engine/src/world.rs	Fri Mar 22 20:01:47 2019 +0300
@@ -10,7 +10,10 @@
 use lfprng::LaggedFibonacciPRNG;
 use hwphysics as hwp;
 
-use crate::render::MapRenderer;
+use crate::render::{
+    MapRenderer,
+    camera::Camera
+};
 
 struct GameState {
     land: Land2D<u32>,
@@ -31,6 +34,7 @@
     preview: Option<Land2D<u8>>,
     game_state: Option<GameState>,
     renderer: MapRenderer,
+    camera: Camera
 }
 
 impl World {
@@ -40,6 +44,7 @@
             preview: None,
             game_state: None,
             renderer: MapRenderer::new(512, 512),
+            camera: Camera::new()
         }
     }
 
@@ -95,16 +100,19 @@
         self.game_state = Some(GameState::new(land, physics));
     }
 
-    pub fn render(&mut self, x: f32, y: f32, w: f32, h: f32) {
+    pub fn move_camera(&mut self, position_shift: Point, zoom_shift: f32) {
+        self.camera.position += position_shift;
+        self.camera.zoom += zoom_shift;
+    }
+
+    pub fn render(&mut self) {
+
         unsafe {
             gl::ClearColor(0.4f32, 0f32, 0.2f32, 1f32);
             gl::Clear(gl::COLOR_BUFFER_BIT);
         }
 
-        self.renderer.render(Rect::new(
-            Point::new(x as _, y as _),
-            Point::new((x + w) as _, (y + h) as _),
-        ));
+        self.renderer.render(self.camera.viewport());
     }
 
     pub fn step(&mut self) {