Add advance_simulation() function to engine lib, some WIP on frontend
authorunc0rr
Sun, 25 Nov 2018 22:32:49 +0100
changeset 14299 21be7838a127
parent 14298 b8871dd33ec4
child 14300 fd8e0e52d5bd
Add advance_simulation() function to engine lib, some WIP on frontend
qmlfrontend/Page1.qml
qmlfrontend/Page1Form.ui.qml
qmlfrontend/engine_instance.cpp
qmlfrontend/engine_interface.h
qmlfrontend/hwengine.cpp
qmlfrontend/main.cpp
rust/lib-hedgewars-engine/src/instance.rs
rust/lib-hedgewars-engine/src/ipc.rs
rust/lib-hedgewars-engine/src/lib.rs
--- a/qmlfrontend/Page1.qml	Sun Nov 25 05:52:06 2018 +0300
+++ b/qmlfrontend/Page1.qml	Sun Nov 25 22:32:49 2018 +0100
@@ -3,8 +3,7 @@
 
 Page1Form {
   tickButton.onClicked: {
-
-    //    item1.tick(100)
+    gameView.tick(100)
   }
   gameButton.onClicked: {
     HWEngine.runQuickGame()
--- a/qmlfrontend/Page1Form.ui.qml	Sun Nov 25 05:52:06 2018 +0300
+++ b/qmlfrontend/Page1Form.ui.qml	Sun Nov 25 22:32:49 2018 +0100
@@ -11,8 +11,8 @@
   width: 1024
   height: 800
   property alias tickButton: tickButton
+  property alias gameView: gameView
 
-  //property alias item1: item1
   ColumnLayout {
     anchors.fill: parent
 
@@ -73,13 +73,11 @@
       }
     }
   }
-  /*
-    GameView {
-        id: item1
-        x: 8
-        y: 154
-        width: 1008
-        height: 638
-    }
-    */
+  GameView {
+    id: gameView
+    x: 8
+    y: 154
+    width: 1008
+    height: 638
+  }
 }
--- a/qmlfrontend/engine_instance.cpp	Sun Nov 25 05:52:06 2018 +0300
+++ b/qmlfrontend/engine_instance.cpp	Sun Nov 25 22:32:49 2018 +0100
@@ -1,5 +1,7 @@
 #include "engine_instance.h"
 
+extern "C" void (*getProcAddress())(const char* fn) { return nullptr; }
+
 EngineInstance::EngineInstance(QObject* parent)
     : QObject(parent), m_instance(Engine::start_engine()) {}
 
@@ -16,7 +18,9 @@
 
 void EngineInstance::renderFrame() {}
 
-void EngineInstance::setOpenGLContext(QOpenGLContext* context) {}
+void EngineInstance::setOpenGLContext(QOpenGLContext* context) {
+  Engine::setup_current_gl_context(m_instance, 0, 0, &getProcAddress);
+}
 
 Engine::PreviewInfo EngineInstance::generatePreview() {
   Engine::PreviewInfo pinfo;
--- a/qmlfrontend/engine_interface.h	Sun Nov 25 05:52:06 2018 +0300
+++ b/qmlfrontend/engine_interface.h	Sun Nov 25 22:32:49 2018 +0100
@@ -29,6 +29,13 @@
 typedef size_t read_ipc_t(EngineInstance* engine_state, uint8_t* buf,
                           size_t size);
 
+typedef void setup_current_gl_context_t(EngineInstance* engine_state,
+                                        uint16_t width, uint16_t height,
+                                        void (*())(const char*));
+typedef void render_frame_t(EngineInstance* engine_state);
+
+typedef bool advance_simulation_t(EngineInstance* engine_state, uint32_t ticks);
+
 extern protocol_version_t* protocol_version;
 extern start_engine_t* start_engine;
 extern generate_preview_t* generate_preview;
@@ -37,6 +44,10 @@
 extern send_ipc_t* send_ipc;
 extern read_ipc_t* read_ipc;
 
+extern setup_current_gl_context_t* setup_current_gl_context;
+extern render_frame_t* render_frame;
+extern advance_simulation_t* advance_simulation;
+
 #ifdef __cplusplus
 }
 };
--- a/qmlfrontend/hwengine.cpp	Sun Nov 25 05:52:06 2018 +0300
+++ b/qmlfrontend/hwengine.cpp	Sun Nov 25 22:32:49 2018 +0100
@@ -5,6 +5,7 @@
 
 #include "engine_instance.h"
 #include "engine_interface.h"
+#include "game_view.h"
 #include "preview_image_provider.h"
 
 #include "hwengine.h"
@@ -30,7 +31,7 @@
   qDebug("HWEngine::exposeToQML");
   qmlRegisterSingletonType<HWEngine>("Hedgewars.Engine", 1, 0, "HWEngine",
                                      hwengine_singletontype_provider);
-  // qmlRegisterType<GameView>("Hedgewars.Engine", 1, 0, "GameView");
+  qmlRegisterType<GameView>("Hedgewars.Engine", 1, 0, "GameView");
 }
 
 void HWEngine::getPreview() {
--- a/qmlfrontend/main.cpp	Sun Nov 25 05:52:06 2018 +0300
+++ b/qmlfrontend/main.cpp	Sun Nov 25 22:32:49 2018 +0100
@@ -13,6 +13,9 @@
 cleanup_t* cleanup;
 send_ipc_t* send_ipc;
 read_ipc_t* read_ipc;
+setup_current_gl_context_t* setup_current_gl_context;
+render_frame_t* render_frame;
+advance_simulation_t* advance_simulation;
 };  // namespace Engine
 
 void loadEngineLibrary() {
@@ -39,6 +42,14 @@
   Engine::read_ipc =
       reinterpret_cast<Engine::read_ipc_t*>(hwlib.resolve("read_ipc"));
 
+  Engine::setup_current_gl_context =
+      reinterpret_cast<Engine::setup_current_gl_context_t*>(
+          hwlib.resolve("setup_current_gl_context"));
+  Engine::render_frame =
+      reinterpret_cast<Engine::render_frame_t*>(hwlib.resolve("render_frame"));
+  Engine::advance_simulation = reinterpret_cast<Engine::advance_simulation_t*>(
+      hwlib.resolve("advance_simulation"));
+
   if (Engine::protocol_version)
     qDebug() << "Loaded engine library with protocol version"
              << Engine::protocol_version();
--- a/rust/lib-hedgewars-engine/src/instance.rs	Sun Nov 25 05:52:06 2018 +0300
+++ b/rust/lib-hedgewars-engine/src/instance.rs	Sun Nov 25 22:32:49 2018 +0100
@@ -1,16 +1,11 @@
 use hedgewars_engine_messages::messages::{
     ConfigEngineMessage::*, EngineMessage::*, KeystrokeAction::*, SyncedEngineMessage::*,
-    UnorderedEngineMessage::*, UnsyncedEngineMessage::*, *
+    UnorderedEngineMessage::*, UnsyncedEngineMessage::*, *,
 };
 
-use gfx::{
-    format::{R8_G8_B8_A8, D24, Unorm}
-};
+use self::gfx_gl::{CommandBuffer, Resources};
+use gfx::format::{Unorm, D24, R8_G8_B8_A8};
 use gfx_device_gl as gfx_gl;
-use self::gfx_gl::{
-    Resources,
-    CommandBuffer
-};
 
 use super::{ipc::IPC, world::World};
 
@@ -19,13 +14,13 @@
     pub factory: gfx_gl::Factory,
     pub render_target: gfx::handle::RenderTargetView<Resources, (R8_G8_B8_A8, Unorm)>,
     pub depth_buffer: gfx::handle::DepthStencilView<Resources, (D24, Unorm)>,
-    pub command_buffer: gfx::Encoder<Resources, CommandBuffer>
+    pub command_buffer: gfx::Encoder<Resources, CommandBuffer>,
 }
 
 pub struct EngineInstance {
     pub world: World,
     pub ipc: IPC,
-    pub gl_context: Option<EngineGlContext>
+    pub gl_context: Option<EngineGlContext>,
 }
 
 impl EngineInstance {
@@ -34,7 +29,7 @@
         Self {
             world,
             ipc: IPC::new(),
-            gl_context: None
+            gl_context: None,
         }
     }
 
@@ -70,11 +65,11 @@
             println!("Processing message: {:?}", message);
             match message {
                 Unknown => println!("Unknown message"),
-                Empty => println!("Empty message") ,
+                Empty => println!("Empty message"),
                 Synced(_, _) => unimplemented!(),
                 Unsynced(_) => unimplemented!(),
                 Unordered(unordered_message) => self.process_unordered_message(&unordered_message),
-                Config(config_message) => self.process_config_message(&config_message)
+                Config(config_message) => self.process_config_message(&config_message),
             }
         }
     }
--- a/rust/lib-hedgewars-engine/src/ipc.rs	Sun Nov 25 05:52:06 2018 +0300
+++ b/rust/lib-hedgewars-engine/src/ipc.rs	Sun Nov 25 22:32:49 2018 +0100
@@ -19,7 +19,7 @@
         self.out_buffer.write(&message.to_bytes()).unwrap();
     }
 
-    pub fn iter(& mut self) -> IPCMessagesIterator {
+    pub fn iter(&mut self) -> IPCMessagesIterator {
         IPCMessagesIterator::new(self)
     }
 }
@@ -45,14 +45,12 @@
 }
 
 pub struct IPCMessagesIterator<'a> {
-    ipc: &'a mut IPC
+    ipc: &'a mut IPC,
 }
 
 impl<'a> IPCMessagesIterator<'a> {
     pub fn new(ipc: &'a mut IPC) -> Self {
-        Self {
-            ipc
-        }
+        Self { ipc }
     }
 }
 
--- a/rust/lib-hedgewars-engine/src/lib.rs	Sun Nov 25 05:52:06 2018 +0300
+++ b/rust/lib-hedgewars-engine/src/lib.rs	Sun Nov 25 22:32:49 2018 +0100
@@ -1,24 +1,18 @@
+pub mod instance;
 mod ipc;
 mod world;
-pub mod instance;
 
+use gfx::{format::Formatted, Encoder};
 use std::{
+    ffi::CString,
     io::{Read, Write},
-    ffi::{CString},
-    os::raw::{c_void, c_char},
-    mem::replace
-};
-use gfx::{
-    Encoder,
-    format::Formatted,
+    mem::replace,
+    os::raw::{c_char, c_void},
 };
 
 use gfx_device_gl as gfx_gl;
 
-use self::instance::{
-    EngineInstance,
-    EngineGlContext
-};
+use self::instance::{EngineGlContext, EngineInstance};
 
 #[repr(C)]
 #[derive(Copy, Clone)]
@@ -60,17 +54,21 @@
 #[no_mangle]
 pub extern "C" fn send_ipc(engine_state: &mut EngineInstance, buf: *const u8, size: usize) {
     unsafe {
-        (*engine_state).ipc.write(std::slice::from_raw_parts(buf, size)).unwrap();
+        (*engine_state)
+            .ipc
+            .write(std::slice::from_raw_parts(buf, size))
+            .unwrap();
     }
 }
 
 #[no_mangle]
-pub extern "C" fn read_ipc(
-    engine_state: &mut EngineInstance,
-    buf: *mut u8,
-    size: usize,
-) -> usize {
-    unsafe { (*engine_state).ipc.read(std::slice::from_raw_parts_mut(buf, size)).unwrap_or(0) }
+pub extern "C" fn read_ipc(engine_state: &mut EngineInstance, buf: *mut u8, size: usize) -> usize {
+    unsafe {
+        (*engine_state)
+            .ipc
+            .read(std::slice::from_raw_parts_mut(buf, size))
+            .unwrap_or(0)
+    }
 }
 
 #[no_mangle]
@@ -78,7 +76,7 @@
     engine_state: &mut EngineInstance,
     width: u16,
     height: u16,
-    gl_loader: extern "C" fn (*const c_char) -> *const c_void
+    gl_loader: extern "C" fn(*const c_char) -> *const c_void,
 ) {
     let (device, mut factory) = gfx_gl::create(|name| {
         let c_name = CString::new(name).unwrap();
@@ -89,7 +87,7 @@
     let (render_target, depth_buffer) = gfx_gl::create_main_targets_raw(
         dimensions,
         gfx::format::Rgba8::get_format().0,
-        gfx::format::Depth::get_format().0
+        gfx::format::Depth::get_format().0,
     );
 
     let mut command_buffer: Encoder<_, _> = factory.create_command_buffer().into();
@@ -99,7 +97,7 @@
         factory,
         render_target: gfx::memory::Typed::new(render_target),
         depth_buffer: gfx::memory::Typed::new(depth_buffer),
-        command_buffer
+        command_buffer,
     })
 }
 
@@ -113,6 +111,10 @@
 }
 
 #[no_mangle]
+pub extern "C" fn advance_simulation(engine_state: &mut EngineInstance, ticks: u32) -> bool {
+    unimplemented!()
+}
+#[no_mangle]
 pub extern "C" fn cleanup(engine_state: *mut EngineInstance) {
     unsafe {
         Box::from_raw(engine_state);