Allow to move camera around
authorunc0rr
Fri, 22 Mar 2019 21:29:10 +0100
changeset 14718 cc6ab1e3f7d5
parent 14717 57293f34ce59
child 14720 e519802076e9
Allow to move camera around
qmlfrontend/engine_instance.cpp
qmlfrontend/engine_instance.h
qmlfrontend/engine_interface.h
qmlfrontend/game_view.cpp
qmlfrontend/game_view.h
rust/lib-hedgewars-engine/src/lib.rs
rust/lib-hedgewars-engine/src/world.rs
--- a/qmlfrontend/engine_instance.cpp	Fri Mar 22 21:06:12 2019 +0100
+++ b/qmlfrontend/engine_instance.cpp	Fri Mar 22 21:29:10 2019 +0100
@@ -41,11 +41,13 @@
       reinterpret_cast<Engine::render_frame_t*>(hwlib.resolve("render_frame"));
   advance_simulation = reinterpret_cast<Engine::advance_simulation_t*>(
       hwlib.resolve("advance_simulation"));
+  move_camera =
+      reinterpret_cast<Engine::move_camera_t*>(hwlib.resolve("move_camera"));
 
   m_isValid = hedgewars_engine_protocol_version && start_engine &&
               generate_preview && dispose_preview && cleanup && send_ipc &&
               read_ipc && setup_current_gl_context && render_frame &&
-              advance_simulation;
+              advance_simulation && move_camera;
   emit isValidChanged(m_isValid);
 
   if (isValid()) {
@@ -53,6 +55,8 @@
              << hedgewars_engine_protocol_version();
 
     m_instance = start_engine();
+  } else {
+    qDebug("Engine library load failed");
   }
 }
 
@@ -71,6 +75,10 @@
   advance_simulation(m_instance, ticks);
 }
 
+void EngineInstance::moveCamera(const QPoint& delta) {
+  move_camera(m_instance, delta.x(), delta.y());
+}
+
 void EngineInstance::renderFrame() { render_frame(m_instance); }
 
 void EngineInstance::setOpenGLContext(QOpenGLContext* context) {
--- a/qmlfrontend/engine_instance.h	Fri Mar 22 21:06:12 2019 +0100
+++ b/qmlfrontend/engine_instance.h	Fri Mar 22 21:29:10 2019 +0100
@@ -19,6 +19,7 @@
 
   void sendConfig(const GameConfig& config);
   void advance(quint32 ticks);
+  void moveCamera(const QPoint& delta);
   void renderFrame();
   void setOpenGLContext(QOpenGLContext* context);
   QImage generatePreview();
@@ -44,6 +45,7 @@
   Engine::setup_current_gl_context_t* setup_current_gl_context;
   Engine::render_frame_t* render_frame;
   Engine::advance_simulation_t* advance_simulation;
+  Engine::move_camera_t* move_camera;
   bool m_isValid;
 };
 
--- a/qmlfrontend/engine_interface.h	Fri Mar 22 21:06:12 2019 +0100
+++ b/qmlfrontend/engine_interface.h	Fri Mar 22 21:29:10 2019 +0100
@@ -37,6 +37,9 @@
 
 typedef bool advance_simulation_t(EngineInstance* engine_state, uint32_t ticks);
 
+typedef void move_camera_t(EngineInstance* engine_state, int32_t delta_x,
+                           int32_t delta_y);
+
 #ifdef __cplusplus
 }
 };
--- a/qmlfrontend/game_view.cpp	Fri Mar 22 21:06:12 2019 +0100
+++ b/qmlfrontend/game_view.cpp	Fri Mar 22 21:29:10 2019 +0100
@@ -65,12 +65,14 @@
       m_engineInstance->setOpenGLContext(window()->openglContext());
 
     m_viewportSize = window()->size();
+    m_centerPoint = QPoint(m_viewportSize.width(), m_viewportSize.height()) / 2;
   }
 
-  // QPoint mousePos = mapFromGlobal(QCursor::pos()).toPoint();
-  // if (flibUpdateMousePosition(m_centerX, m_centerY, mousePos.x(),
-  // mousePos.y()))
-  //  QCursor::setPos(mapToGlobal(QPointF(m_centerX, m_centerY)).toPoint());
+  if (m_engineInstance) {
+    QPoint mousePos = mapFromGlobal(QCursor::pos()).toPoint();
+    m_engineInstance->moveCamera(mousePos - m_centerPoint);
+    QCursor::setPos(mapToGlobal(m_centerPoint).toPoint());
+  }
 
   if (m_renderer) m_renderer->tick(m_delta);
 }
--- a/qmlfrontend/game_view.h	Fri Mar 22 21:06:12 2019 +0100
+++ b/qmlfrontend/game_view.h	Fri Mar 22 21:29:10 2019 +0100
@@ -58,6 +58,7 @@
   bool m_windowChanged;
   QPointer<EngineInstance> m_engineInstance;
   QSize m_viewportSize;
+  QPoint m_centerPoint;
 };
 
 #endif  // GAMEVIEW_H
--- a/rust/lib-hedgewars-engine/src/lib.rs	Fri Mar 22 21:06:12 2019 +0100
+++ b/rust/lib-hedgewars-engine/src/lib.rs	Fri Mar 22 21:29:10 2019 +0100
@@ -10,6 +10,8 @@
     os::raw::{c_char, c_void},
 };
 
+use integral_geometry::Point;
+
 use self::instance::EngineInstance;
 
 #[repr(C)]
@@ -101,6 +103,14 @@
     engine_state.world.step();
     true
 }
+
+#[no_mangle]
+pub extern "C" fn move_camera(engine_state: &mut EngineInstance, delta_x: i32, delta_y: i32) {
+    engine_state
+        .world
+        .move_camera(Point::new(delta_x, delta_y), 0.0);
+}
+
 #[no_mangle]
 pub extern "C" fn cleanup(engine_state: *mut EngineInstance) {
     unsafe {
--- a/rust/lib-hedgewars-engine/src/world.rs	Fri Mar 22 21:06:12 2019 +0100
+++ b/rust/lib-hedgewars-engine/src/world.rs	Fri Mar 22 21:29:10 2019 +0100
@@ -49,7 +49,7 @@
 
         if let Some(ref state) = self.game_state {
             self.camera.position = state.land.play_box().center();
-            
+
             let theme =
                 Theme::load(Path::new("../../share/hedgewars/Data/Themes/Cheese/")).unwrap();
             let texture = MapGenerator::new().make_texture(&state.land, &theme);