Implement triggering advancement of simulation in qmlfrontend
authorunC0Rr
Tue, 21 May 2024 14:38:30 +0200
changeset 16017 e8afb1bf2779
parent 16016 4933920eba89
child 16018 692903667ece
Implement triggering advancement of simulation in qmlfrontend
qmlfrontend/Page1.qml
qmlfrontend/game_view.cpp
qmlfrontend/game_view.h
rust/lib-hedgewars-engine/Cargo.toml
rust/lib-hedgewars-engine/src/instance.rs
rust/lib-hedgewars-engine/src/lib.rs
rust/lib-hedgewars-engine/src/world.rs
--- a/qmlfrontend/Page1.qml	Mon May 20 14:58:30 2024 +0200
+++ b/qmlfrontend/Page1.qml	Tue May 21 14:38:30 2024 +0200
@@ -83,7 +83,19 @@
   tickButton {
     onClicked: {
       tickButton.visible = false;
+    }
+  }
+
+  Timer {
+    id: advancingTimer
+
+    interval: 100
+    repeat: true
+    running: !tickButton.visible
+
+    onTriggered: {
       gameView.tick(100);
+      gameView.update();
     }
   }
 
--- a/qmlfrontend/game_view.cpp	Mon May 20 14:58:30 2024 +0200
+++ b/qmlfrontend/game_view.cpp	Tue May 21 14:38:30 2024 +0200
@@ -64,21 +64,16 @@
     m_gameViewSize = currentSize;
     m_dirty = true;
   }
+
+  m_gameView->executeActions();
 }
 
-GameView::GameView(QQuickItem* parent)
-    : QQuickFramebufferObject(parent), m_delta(0) {
+GameView::GameView(QQuickItem* parent) : QQuickFramebufferObject(parent) {
   setMirrorVertically(true);
 }
 
 void GameView::tick(quint32 delta) {
-  m_delta = delta;
-
-  if (window()) {
-    QTimer* timer = new QTimer(this);
-    connect(timer, &QTimer::timeout, this, &GameView::update);
-    timer->start(100);
-  }
+  addAction([delta](auto engine) { engine->advance(delta); });
 }
 
 EngineInstance* GameView::engineInstance() const { return m_engineInstance; }
@@ -87,6 +82,18 @@
   return new GameViewRenderer{};
 }
 
+void GameView::executeActions() {
+  if (!m_engineInstance) {
+    return;
+  }
+
+  for (const auto& action : m_actions) {
+    action(m_engineInstance);
+  }
+
+  m_actions.clear();
+}
+
 void GameView::setEngineInstance(EngineInstance* engineInstance) {
   if (m_engineInstance == engineInstance) {
     return;
@@ -96,3 +103,7 @@
 
   Q_EMIT engineInstanceChanged(m_engineInstance);
 }
+
+void GameView::addAction(std::function<void(EngineInstance*)>&& action) {
+  m_actions.append(std::move(action));
+}
--- a/qmlfrontend/game_view.h	Mon May 20 14:58:30 2024 +0200
+++ b/qmlfrontend/game_view.h	Tue May 21 14:38:30 2024 +0200
@@ -19,8 +19,8 @@
   Q_INVOKABLE void tick(quint32 delta);
 
   EngineInstance* engineInstance() const;
-
   Renderer* createRenderer() const override;
+  void executeActions();
 
  Q_SIGNALS:
   void engineInstanceChanged(EngineInstance* engineInstance);
@@ -29,10 +29,12 @@
   void setEngineInstance(EngineInstance* engineInstance);
 
  private:
-  quint32 m_delta;
   QPointer<EngineInstance> m_engineInstance;
   QSize m_viewportSize;
   QPoint m_centerPoint;
+  QList<std::function<void(EngineInstance*)>> m_actions;
+
+  void addAction(std::function<void(EngineInstance*)>&& action);
 };
 
 #endif  // GAMEVIEW_H
--- a/rust/lib-hedgewars-engine/Cargo.toml	Mon May 20 14:58:30 2024 +0200
+++ b/rust/lib-hedgewars-engine/Cargo.toml	Tue May 21 14:38:30 2024 +0200
@@ -20,6 +20,7 @@
 hwphysics = { path = "../hwphysics" }
 mapgen = { path = "../mapgen" }
 vec2d = { path = "../vec2d" }
+log = "0.4.21"
 
 [dev-dependencies]
 proptest = "0.9.2"
--- a/rust/lib-hedgewars-engine/src/instance.rs	Mon May 20 14:58:30 2024 +0200
+++ b/rust/lib-hedgewars-engine/src/instance.rs	Tue May 21 14:38:30 2024 +0200
@@ -35,6 +35,7 @@
         }
 
         world.init(template());
+        world.init_renderer();
 
         Self {
             world,
--- a/rust/lib-hedgewars-engine/src/lib.rs	Mon May 20 14:58:30 2024 +0200
+++ b/rust/lib-hedgewars-engine/src/lib.rs	Tue May 21 14:38:30 2024 +0200
@@ -154,6 +154,7 @@
         gl::Viewport(0, 0, width as i32, height as i32);
     }
     engine_state.world.create_renderer(width, height);
+    engine_state.world.init_renderer();
 }
 
 #[no_mangle]
--- a/rust/lib-hedgewars-engine/src/world.rs	Mon May 20 14:58:30 2024 +0200
+++ b/rust/lib-hedgewars-engine/src/world.rs	Tue May 21 14:38:30 2024 +0200
@@ -12,6 +12,7 @@
 };
 use lfprng::LaggedFibonacciPRNG;
 use std::path::{Path, PathBuf};
+use log::trace;
 
 use crate::render::{camera::Camera, GearEntry, GearRenderer, MapRenderer};
 
@@ -59,11 +60,15 @@
         self.gear_renderer = Some(GearRenderer::new(&self.data_path.as_path()));
         self.camera = Camera::with_size(Size::new(width as usize, height as usize));
 
+        if let Some(ref state) = self.game_state {
+            self.camera.position = state.land.play_box().center();
+        }
+    }
+
+    pub fn init_renderer(&mut self) {
         use mapgen::{theme::Theme, MapGenerator};
 
         if let Some(ref state) = self.game_state {
-            self.camera.position = state.land.play_box().center();
-
             let theme =
                 Theme::load(self.data_path.join(Path::new("Themes/Cheese/")).as_path()).unwrap();
             let texture = MapGenerator::new().make_texture(&state.land, &theme);