Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
authorunC0Rr
Fri, 07 Dec 2018 14:30:35 +0100
changeset 14373 4409344db447
parent 14372 b6824a53d4b1
child 14374 e5db279308d7
Rework EngineInstance::generatePreview, add preview cleanup function in enginelib
qmlfrontend/engine_instance.cpp
qmlfrontend/engine_instance.h
qmlfrontend/engine_interface.h
qmlfrontend/hwengine.cpp
rust/lib-hedgewars-engine/src/lib.rs
rust/lib-hedgewars-engine/src/world.rs
--- a/qmlfrontend/engine_instance.cpp	Fri Dec 07 13:56:45 2018 +0100
+++ b/qmlfrontend/engine_instance.cpp	Fri Dec 07 14:30:35 2018 +0100
@@ -27,6 +27,8 @@
       reinterpret_cast<Engine::start_engine_t*>(hwlib.resolve("start_engine"));
   generate_preview = reinterpret_cast<Engine::generate_preview_t*>(
       hwlib.resolve("generate_preview"));
+  dispose_preview = reinterpret_cast<Engine::dispose_preview_t*>(
+      hwlib.resolve("dispose_preview"));
   cleanup = reinterpret_cast<Engine::cleanup_t*>(hwlib.resolve("cleanup"));
 
   send_ipc = reinterpret_cast<Engine::send_ipc_t*>(hwlib.resolve("send_ipc"));
@@ -41,8 +43,9 @@
       hwlib.resolve("advance_simulation"));
 
   m_isValid = hedgewars_engine_protocol_version && start_engine &&
-              generate_preview && cleanup && send_ipc && read_ipc &&
-              setup_current_gl_context && render_frame && advance_simulation;
+              generate_preview && dispose_preview && cleanup && send_ipc &&
+              read_ipc && setup_current_gl_context && render_frame &&
+              advance_simulation;
   emit isValidChanged(m_isValid);
 
   if (isValid()) {
@@ -79,12 +82,23 @@
                            &getProcAddress);
 }
 
-Engine::PreviewInfo EngineInstance::generatePreview() {
+QImage EngineInstance::generatePreview() {
   Engine::PreviewInfo pinfo;
 
   generate_preview(m_instance, &pinfo);
 
-  return pinfo;
+  QVector<QRgb> colorTable;
+  colorTable.resize(256);
+  for (int i = 0; i < 256; ++i) colorTable[i] = qRgba(255, 255, 0, i);
+
+  QImage previewImage(pinfo.land, static_cast<int>(pinfo.width),
+                      static_cast<int>(pinfo.height), QImage::Format_Indexed8);
+  previewImage.setColorTable(colorTable);
+
+  // Cannot use it here, since QImage refers to original bytes
+  // dispose_preview(m_instance);
+
+  return previewImage;
 }
 
 bool EngineInstance::isValid() const { return m_isValid; }
--- a/qmlfrontend/engine_instance.h	Fri Dec 07 13:56:45 2018 +0100
+++ b/qmlfrontend/engine_instance.h	Fri Dec 07 14:30:35 2018 +0100
@@ -1,6 +1,7 @@
 #ifndef ENGINEINSTANCE_H
 #define ENGINEINSTANCE_H
 
+#include <QImage>
 #include <QObject>
 #include <QOpenGLContext>
 
@@ -20,7 +21,7 @@
   void advance(quint32 ticks);
   void renderFrame();
   void setOpenGLContext(QOpenGLContext* context);
-  Engine::PreviewInfo generatePreview();
+  QImage generatePreview();
 
   bool isValid() const;
 
@@ -36,6 +37,7 @@
       hedgewars_engine_protocol_version;
   Engine::start_engine_t* start_engine;
   Engine::generate_preview_t* generate_preview;
+  Engine::dispose_preview_t* dispose_preview;
   Engine::cleanup_t* cleanup;
   Engine::send_ipc_t* send_ipc;
   Engine::read_ipc_t* read_ipc;
--- a/qmlfrontend/engine_interface.h	Fri Dec 07 13:56:45 2018 +0100
+++ b/qmlfrontend/engine_interface.h	Fri Dec 07 14:30:35 2018 +0100
@@ -22,6 +22,7 @@
 typedef EngineInstance* start_engine_t();
 typedef void generate_preview_t(EngineInstance* engine_state,
                                 PreviewInfo* preview);
+typedef void dispose_preview_t(EngineInstance* engine_state);
 typedef void cleanup_t(EngineInstance* engine_state);
 
 typedef void send_ipc_t(EngineInstance* engine_state, uint8_t* buf,
--- a/qmlfrontend/hwengine.cpp	Fri Dec 07 13:56:45 2018 +0100
+++ b/qmlfrontend/hwengine.cpp	Fri Dec 07 14:30:35 2018 +0100
@@ -2,8 +2,6 @@
 
 #include <QDebug>
 #include <QImage>
-#include <QLibrary>
-#include <QQmlEngine>
 #include <QUuid>
 
 #include "engine_instance.h"
@@ -27,17 +25,7 @@
 
   engine.sendConfig(m_gameConfig);
 
-  Engine::PreviewInfo preview = engine.generatePreview();
-
-  QVector<QRgb> colorTable;
-  colorTable.resize(256);
-  for (int i = 0; i < 256; ++i) colorTable[i] = qRgba(255, 255, 0, i);
-
-  QImage previewImage(preview.land, static_cast<int>(preview.width),
-                      static_cast<int>(preview.height),
-                      QImage::Format_Indexed8);
-  previewImage.setColorTable(colorTable);
-  previewImage.detach();
+  QImage previewImage = engine.generatePreview();
 
   if (m_previewAcceptor) m_previewAcceptor->setImage(previewImage);
 
--- a/rust/lib-hedgewars-engine/src/lib.rs	Fri Dec 07 13:56:45 2018 +0100
+++ b/rust/lib-hedgewars-engine/src/lib.rs	Fri Dec 07 14:30:35 2018 +0100
@@ -42,14 +42,19 @@
 
     (*engine_state).world.generate_preview();
 
-    let land_preview = (*engine_state).world.preview();
+    if let Some(land_preview) = (*engine_state).world.preview() {
+        *preview = PreviewInfo {
+            width: land_preview.width() as u32,
+            height: land_preview.height() as u32,
+            hedgehogs_number: 0,
+            land: land_preview.raw_pixels().as_ptr(),
+        };
+    }
+}
 
-    *preview = PreviewInfo {
-        width: land_preview.width() as u32,
-        height: land_preview.height() as u32,
-        hedgehogs_number: 0,
-        land: land_preview.raw_pixels().as_ptr(),
-    };
+#[no_mangle]
+pub extern "C" fn dispose_preview(engine_state: &mut EngineInstance, preview: &mut PreviewInfo) {
+    (*engine_state).world.dispose_preview();
 }
 
 #[no_mangle]
--- a/rust/lib-hedgewars-engine/src/world.rs	Fri Dec 07 13:56:45 2018 +0100
+++ b/rust/lib-hedgewars-engine/src/world.rs	Fri Dec 07 14:30:35 2018 +0100
@@ -26,7 +26,7 @@
 
 pub struct World {
     random_numbers_gen: LaggedFibonacciPRNG,
-    preview: Land2D<u8>,
+    preview: Option<Land2D<u8>>,
     game_state: Option<GameState>,
 }
 
@@ -34,7 +34,7 @@
     pub fn new() -> Self {
         Self {
             random_numbers_gen: LaggedFibonacciPRNG::new(&[]),
-            preview: Land2D::new(Size::new(0, 0), 0),
+            preview: None,
             game_state: None,
         }
     }
@@ -43,7 +43,7 @@
         self.random_numbers_gen = LaggedFibonacciPRNG::new(seed);
     }
 
-    pub fn preview(&self) -> &Land2D<u8> {
+    pub fn preview(&self) -> &Option<Land2D<u8>> {
         &self.preview
     }
 
@@ -63,7 +63,11 @@
 
         let params = LandGenerationParameters::new(0u8, u8::max_value(), 5, false, false);
         let landgen = TemplatedLandGenerator::new(template());
-        self.preview = landgen.generate_land(&params, &mut self.random_numbers_gen);
+        self.preview = Some(landgen.generate_land(&params, &mut self.random_numbers_gen));
+    }
+
+    pub fn dispose_preview(&mut self) {
+        self.preview = None
     }
 
     pub fn init(&mut self, template: OutlineTemplate) {