merge
authoralfadur
Fri, 22 Mar 2019 23:46:48 +0300
changeset 14715 e519802076e9
parent 14714 4df1aac5f090 (current diff)
parent 14713 cc6ab1e3f7d5 (diff)
child 14716 8e74d4eb89f5
merge
rust/lib-hedgewars-engine/src/world.rs
--- a/qmlfrontend/engine_instance.cpp	Fri Mar 22 23:45:59 2019 +0300
+++ b/qmlfrontend/engine_instance.cpp	Fri Mar 22 23:46:48 2019 +0300
@@ -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 23:45:59 2019 +0300
+++ b/qmlfrontend/engine_instance.h	Fri Mar 22 23:46:48 2019 +0300
@@ -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 23:45:59 2019 +0300
+++ b/qmlfrontend/engine_interface.h	Fri Mar 22 23:46:48 2019 +0300
@@ -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 23:45:59 2019 +0300
+++ b/qmlfrontend/game_view.cpp	Fri Mar 22 23:46:48 2019 +0300
@@ -58,17 +58,21 @@
             &GameViewRenderer::paint, Qt::DirectConnection);
   }
 
-  if (m_windowChanged) {
-    QSize windowSize = window()->size();
-    m_renderer->setViewportSize(windowSize * window()->devicePixelRatio());
-    m_centerX = windowSize.width() / 2;
-    m_centerY = windowSize.height() / 2;
+  if (m_windowChanged || (m_viewportSize != window()->size())) {
+    m_windowChanged = false;
+
+    if (m_engineInstance)
+      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);
 }
@@ -80,10 +84,6 @@
 
 void GameViewRenderer::tick(quint32 delta) { m_delta = delta; }
 
-void GameViewRenderer::setViewportSize(const QSize& size) {
-  // flibResizeWindow(size.width(), size.height());
-}
-
 void GameViewRenderer::setEngineInstance(EngineInstance* engineInstance) {
   m_engineInstance = engineInstance;
 }
@@ -98,3 +98,8 @@
 
   // m_window->resetOpenGLState();
 }
+
+void GameViewRenderer::onViewportSizeChanged(QQuickWindow* window) {
+  if (m_engineInstance)
+    m_engineInstance->setOpenGLContext(window->openglContext());
+}
--- a/qmlfrontend/game_view.h	Fri Mar 22 23:45:59 2019 +0300
+++ b/qmlfrontend/game_view.h	Fri Mar 22 23:46:48 2019 +0300
@@ -13,15 +13,15 @@
 class GameViewRenderer : public QObject, protected QOpenGLFunctions {
   Q_OBJECT
  public:
-  GameViewRenderer();
-  ~GameViewRenderer();
+  explicit GameViewRenderer();
+  ~GameViewRenderer() override;
 
   void tick(quint32 delta);
-  void setViewportSize(const QSize& size);
   void setEngineInstance(EngineInstance* engineInstance);
 
  public slots:
   void paint();
+  void onViewportSizeChanged(QQuickWindow* window);
 
  private:
   quint32 m_delta;
@@ -35,7 +35,7 @@
                  setEngineInstance NOTIFY engineInstanceChanged)
 
  public:
-  GameView();
+  explicit GameView();
 
   Q_INVOKABLE void tick(quint32 delta);
 
@@ -56,9 +56,9 @@
   quint32 m_delta;
   QScopedPointer<GameViewRenderer> m_renderer;
   bool m_windowChanged;
-  qint32 m_centerX;
-  qint32 m_centerY;
   QPointer<EngineInstance> m_engineInstance;
+  QSize m_viewportSize;
+  QPoint m_centerPoint;
 };
 
 #endif  // GAMEVIEW_H
--- a/rust/lib-hedgewars-engine/src/lib.rs	Fri Mar 22 23:45:59 2019 +0300
+++ b/rust/lib-hedgewars-engine/src/lib.rs	Fri Mar 22 23:46:48 2019 +0300
@@ -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 {