changeset 938:eaaa9b574e05 toa2019073101

Fixed the Stone samples wrt the recent API changes (SdlOpenGLViewport, ICompositor...)
author Benjamin Golinvaux <bgo@osimis.io>
date Wed, 31 Jul 2019 10:58:16 +0200
parents 86ac61a040c9
children ab90628e70d9
files Samples/MultiPlatform/BasicScene/mainSdl.cpp Samples/Sdl/FusionMprSdl.cpp Samples/Sdl/FusionMprSdl.h Samples/Sdl/TrackerSampleApp.cpp Samples/Sdl/TrackerSampleApp.h
diffstat 5 files changed, 48 insertions(+), 54 deletions(-) [+]
line wrap: on
line diff
--- a/Samples/MultiPlatform/BasicScene/mainSdl.cpp	Wed Jul 31 10:24:09 2019 +0200
+++ b/Samples/MultiPlatform/BasicScene/mainSdl.cpp	Wed Jul 31 10:58:16 2019 +0200
@@ -183,7 +183,7 @@
     SdlOpenGLViewport viewport("Hello", 1024, 768);
     MessageBroker broker;
     boost::shared_ptr<UndoStack> undoStack(new UndoStack);
-    boost::shared_ptr<ViewportController> controller = boost::make_shared<ViewportController>(undoStack, boost::ref(broker), viewport);
+    boost::shared_ptr<ViewportController> controller = boost::make_shared<ViewportController>(undoStack, boost::ref(broker), boost::ref(viewport));
     interactor.reset(new BasicScene2DInteractor(controller));
     PrepareScene(controller->GetScene());
     Run(controller);
--- a/Samples/Sdl/FusionMprSdl.cpp	Wed Jul 31 10:24:09 2019 +0200
+++ b/Samples/Sdl/FusionMprSdl.cpp	Wed Jul 31 10:58:16 2019 +0200
@@ -103,7 +103,8 @@
   void FusionMprSdlApp::DisplayInfoText()
   {
     // do not try to use stuff too early!
-    if (compositor_.get() == NULL)
+    ICompositor* pCompositor = &(viewport_.GetCompositor());
+    if (pCompositor == NULL)
       return;
 
     std::stringstream msg;
@@ -116,10 +117,10 @@
 	std::string msgS = msg.str();
 
     TextSceneLayer* layerP = NULL;
-    if (GetScene()->HasLayer(FIXED_INFOTEXT_LAYER_ZINDEX))
+    if (GetScene().HasLayer(FIXED_INFOTEXT_LAYER_ZINDEX))
     {
       TextSceneLayer& layer = dynamic_cast<TextSceneLayer&>(
-        GetScene()->GetLayer(FIXED_INFOTEXT_LAYER_ZINDEX));
+        GetScene().GetLayer(FIXED_INFOTEXT_LAYER_ZINDEX));
       layerP = &layer;
     }
     else
@@ -131,29 +132,29 @@
       layer->SetBorder(20);
       layer->SetAnchor(BitmapAnchor_TopLeft);
       //layer->SetPosition(0,0);
-      GetScene()->SetLayer(FIXED_INFOTEXT_LAYER_ZINDEX, layer.release());
+      GetScene().SetLayer(FIXED_INFOTEXT_LAYER_ZINDEX, layer.release());
     }
     // position the fixed info text in the upper right corner
     layerP->SetText(msgS.c_str());
-    double cX = compositor_->GetCanvasWidth() * (-0.5);
-    double cY = compositor_->GetCanvasHeight() * (-0.5);
-    GetScene()->GetCanvasToSceneTransform().Apply(cX,cY);
+    double cX = viewport_.GetCompositor().GetCanvasWidth() * (-0.5);
+    double cY = viewport_.GetCompositor().GetCanvasHeight() * (-0.5);
+    GetScene().GetCanvasToSceneTransform().Apply(cX,cY);
     layerP->SetPosition(cX, cY);
   }
 
   void FusionMprSdlApp::DisplayFloatingCtrlInfoText(const PointerEvent& e)
   {
-    ScenePoint2D p = e.GetMainPosition().Apply(GetScene()->GetCanvasToSceneTransform());
+    ScenePoint2D p = e.GetMainPosition().Apply(GetScene().GetCanvasToSceneTransform());
 
     char buf[128];
     sprintf(buf, "S:(%0.02f,%0.02f) C:(%0.02f,%0.02f)", 
       p.GetX(), p.GetY(), 
       e.GetMainPosition().GetX(), e.GetMainPosition().GetY());
 
-    if (GetScene()->HasLayer(FLOATING_INFOTEXT_LAYER_ZINDEX))
+    if (GetScene().HasLayer(FLOATING_INFOTEXT_LAYER_ZINDEX))
     {
       TextSceneLayer& layer =
-        dynamic_cast<TextSceneLayer&>(GetScene()->GetLayer(FLOATING_INFOTEXT_LAYER_ZINDEX));
+        dynamic_cast<TextSceneLayer&>(GetScene().GetLayer(FLOATING_INFOTEXT_LAYER_ZINDEX));
       layer.SetText(buf);
       layer.SetPosition(p.GetX(), p.GetY());
     }
@@ -165,13 +166,13 @@
       layer->SetBorder(20);
       layer->SetAnchor(BitmapAnchor_BottomCenter);
       layer->SetPosition(p.GetX(), p.GetY());
-      GetScene()->SetLayer(FLOATING_INFOTEXT_LAYER_ZINDEX, layer.release());
+      GetScene().SetLayer(FLOATING_INFOTEXT_LAYER_ZINDEX, layer.release());
     }
   }
 
   void FusionMprSdlApp::HideInfoText()
   {
-    GetScene()->DeleteLayer(FLOATING_INFOTEXT_LAYER_ZINDEX);
+    GetScene().DeleteLayer(FLOATING_INFOTEXT_LAYER_ZINDEX);
   }
 
   void FusionMprSdlApp::HandleApplicationEvent(
@@ -191,7 +192,7 @@
         // The "left-ctrl" key is down, while no tracker is present
         // Let's display the info text
         PointerEvent e;
-        e.AddPosition(compositor_->GetPixelCenterCoordinates(
+        e.AddPosition(controller_->GetViewport().GetPixelCenterCoordinates(
           event.button.x, event.button.y));
 
         DisplayFloatingCtrlInfoText(e);
@@ -204,7 +205,7 @@
         {
           //LOG(TRACE) << "(activeTracker_.get() != NULL)";
           PointerEvent e;
-          e.AddPosition(compositor_->GetPixelCenterCoordinates(
+          e.AddPosition(controller_->GetViewport().GetPixelCenterCoordinates(
             event.button.x, event.button.y));
           
           //LOG(TRACE) << "event.button.x = " << event.button.x << "     " <<
@@ -223,7 +224,7 @@
       if (activeTracker_)
       {
         PointerEvent e;
-        e.AddPosition(compositor_->GetPixelCenterCoordinates(event.button.x, event.button.y));
+        e.AddPosition(controller_->GetViewport().GetPixelCenterCoordinates(event.button.x, event.button.y));
         activeTracker_->PointerUp(e);
         if (!activeTracker_->IsAlive())
           activeTracker_.reset();
@@ -232,7 +233,7 @@
     else if (event.type == SDL_MOUSEBUTTONDOWN)
     {
       PointerEvent e;
-      e.AddPosition(compositor_->GetPixelCenterCoordinates(
+      e.AddPosition(controller_->GetViewport().GetPixelCenterCoordinates(
         event.button.x, event.button.y));
       if (activeTracker_)
       {
@@ -270,8 +271,8 @@
         }
         break;
       case SDLK_s:
-        controller_->FitContent(compositor_->GetCanvasWidth(),
-          compositor_->GetCanvasHeight());
+        controller_->FitContent(viewport_.GetCompositor().GetCanvasWidth(),
+          viewport_.GetCompositor().GetCanvasHeight());
         break;
 
       case SDLK_z:
@@ -309,8 +310,8 @@
       case SDLK_c:
         TakeScreenshot(
           "screenshot.png",
-          compositor_->GetCanvasWidth(),
-          compositor_->GetCanvasHeight());
+          viewport_.GetCompositor().GetCanvasWidth(),
+          viewport_.GetCompositor().GetCanvasHeight());
         break;
 
       default:
@@ -340,7 +341,7 @@
 
     case SDL_BUTTON_RIGHT:
       return boost::shared_ptr<IFlexiblePointerTracker>(new ZoomSceneTracker
-        (controller_, e, compositor_->GetCanvasHeight()));
+        (controller_, e, viewport_.GetCompositor().GetCanvasHeight()));
 
     case SDL_BUTTON_LEFT:
     {
@@ -372,7 +373,7 @@
             controller_, e));
         case FusionMprGuiTool_Zoom:
           return boost::shared_ptr<IFlexiblePointerTracker>(new ZoomSceneTracker(
-            controller_, e, compositor_->GetCanvasHeight()));
+            controller_, e, viewport_.GetCompositor().GetCanvasHeight()));
         //case GuiTool_AngleMeasure:
         //  return new AngleMeasureTracker(GetScene(), e);
         //case GuiTool_CircleMeasure:
@@ -409,6 +410,7 @@
     , oracle_(*this)
     , currentTool_(FusionMprGuiTool_Rotate)
     , undoStack_(new UndoStack)
+    , viewport_("Hello", 1024, 1024, false) // False means we do NOT let Windows treat this as a legacy application that needs to be scaled
   {
     //oracleObservable.RegisterObserverCallback
     //(new Callable
@@ -427,7 +429,7 @@
       <FusionMprSdlApp, OracleCommandExceptionMessage>(*this, &FusionMprSdlApp::Handle));
     
     controller_ = boost::shared_ptr<ViewportController>(
-      new ViewportController(undoStack_, broker_));
+      new ViewportController(undoStack_, broker_, viewport_));
 
     controller_->RegisterObserverCallback(
       new Callable<FusionMprSdlApp, ViewportController::SceneTransformChanged>
@@ -466,7 +468,7 @@
       p[4] = 0;
       p[5] = 0;
 
-      GetScene()->SetLayer(TEXTURE_2x2_1_ZINDEX, new ColorTextureSceneLayer(i));
+      GetScene().SetLayer(TEXTURE_2x2_1_ZINDEX, new ColorTextureSceneLayer(i));
     }
   }
 
@@ -483,7 +485,7 @@
     unsigned int canvasWidth,
     unsigned int canvasHeight)
   {
-    CairoCompositor compositor(*GetScene(), canvasWidth, canvasHeight);
+    CairoCompositor compositor(GetScene(), canvasWidth, canvasHeight);
     compositor.SetFont(0, Orthanc::EmbeddedResources::UBUNTU_FONT, FONT_SIZE_0, Orthanc::Encoding_Latin1);
     compositor.Refresh();
 
@@ -557,7 +559,7 @@
     const boost::shared_ptr<OrthancStone::IVolumeSlicer>& volume,
     OrthancStone::ILayerStyleConfigurator* style)
   {
-    source1_.reset(new OrthancStone::VolumeSceneLayerSource(*controller_->GetScene(), depth, volume));
+    source1_.reset(new OrthancStone::VolumeSceneLayerSource(controller_->GetScene(), depth, volume));
 
     if (style != NULL)
     {
@@ -569,7 +571,7 @@
     const boost::shared_ptr<OrthancStone::IVolumeSlicer>& volume,
     OrthancStone::ILayerStyleConfigurator* style)
   {
-    source2_.reset(new OrthancStone::VolumeSceneLayerSource(*controller_->GetScene(), depth, volume));
+    source2_.reset(new OrthancStone::VolumeSceneLayerSource(controller_->GetScene(), depth, volume));
 
     if (style != NULL)
     {
@@ -580,25 +582,21 @@
   void FusionMprSdlApp::SetStructureSet(int depth,
     const boost::shared_ptr<OrthancStone::DicomStructureSetLoader>& volume)
   {
-    source3_.reset(new OrthancStone::VolumeSceneLayerSource(*controller_->GetScene(), depth, volume));
+    source3_.reset(new OrthancStone::VolumeSceneLayerSource(controller_->GetScene(), depth, volume));
   }
   
   void FusionMprSdlApp::Run()
   {
     // False means we do NOT let Windows treat this as a legacy application
     // that needs to be scaled
-    SdlOpenGLContext window("Hello", 1024, 1024, false);
-
-    controller_->FitContent(window.GetCanvasWidth(), window.GetCanvasHeight());
+    controller_->FitContent(viewport_.GetCanvasWidth(), viewport_.GetCanvasHeight());
 
     glEnable(GL_DEBUG_OUTPUT);
     glDebugMessageCallback(OpenGLMessageCallback, 0);
 
-    compositor_.reset(new OpenGLCompositor(window, *GetScene()));
-
-    compositor_->SetFont(0, Orthanc::EmbeddedResources::UBUNTU_FONT,
+    viewport_.GetCompositor().SetFont(0, Orthanc::EmbeddedResources::UBUNTU_FONT,
       FONT_SIZE_0, Orthanc::Encoding_Latin1);
-    compositor_->SetFont(1, Orthanc::EmbeddedResources::UBUNTU_FONT,
+    viewport_.GetCompositor().SetFont(1, Orthanc::EmbeddedResources::UBUNTU_FONT,
       FONT_SIZE_1, Orthanc::Encoding_Latin1);
 
 
@@ -675,7 +673,7 @@
 
     while (!g_stopApplication)
     {
-      compositor_->Refresh();
+      viewport_.GetCompositor().Refresh();
 
 //////// from loader
       if (source1_.get() != NULL)
@@ -713,12 +711,11 @@
           switch (event.key.keysym.sym)
           {
           case SDLK_f:
-            window.GetWindow().ToggleMaximize();
+            viewport_.GetWindow().ToggleMaximize();
             break;
 
           case SDLK_s:
-            controller_->FitContent(
-              window.GetCanvasWidth(), window.GetCanvasHeight());
+            controller_->FitContent(viewport_.GetCanvasWidth(), viewport_.GetCanvasHeight());
             break;
 
           case SDLK_q:
@@ -733,10 +730,6 @@
       SDL_Delay(1);
     }
 
-    // the following is paramount because the compositor holds a reference
-    // to the scene and we do not want this reference to become dangling
-    compositor_.reset(NULL);
-
     //// from loader
 
     //Orthanc::SystemToolbox::ServerBarrier();
--- a/Samples/Sdl/FusionMprSdl.h	Wed Jul 31 10:24:09 2019 +0200
+++ b/Samples/Sdl/FusionMprSdl.h	Wed Jul 31 10:58:16 2019 +0200
@@ -18,6 +18,8 @@
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  **/
 
+#include "../../Framework/Viewport/SdlViewport.h"
+
 #include "../../Framework/Messages/IObserver.h"
 #include "../../Framework/Messages/IMessageEmitter.h"
 #include "../../Framework/Oracle/OracleCommandExceptionMessage.h"
@@ -41,7 +43,7 @@
   class ThreadedOracle;
   class VolumeSceneLayerSource;
   class NativeFusionMprApplicationContext;
-
+  class SdlOpenGLViewport;
    
   enum FusionMprGuiTool
   {
@@ -80,8 +82,8 @@
     void SetInfoDisplayMessage(std::string key, std::string value);
     void DisableTracker();
 
-    boost::shared_ptr<Scene2D> GetScene();
-    boost::shared_ptr<const Scene2D> GetScene() const;
+    Scene2D&       GetScene();
+    const Scene2D& GetScene() const;
 
     void HandleApplicationEvent(const SDL_Event& event);
 
@@ -174,7 +176,6 @@
 
     boost::shared_ptr<VolumeSceneLayerSource>  source1_, source2_, source3_;
 
-    std::auto_ptr<OpenGLCompositor> compositor_;
     /**
     WARNING: the measuring tools do store a reference to the scene, and it
     paramount that the scene gets destroyed AFTER the measurement tools.
@@ -196,7 +197,7 @@
 
     FusionMprGuiTool currentTool_;
     boost::shared_ptr<UndoStack> undoStack_;
-
+    SdlOpenGLViewport viewport_;
   };
 
 }
--- a/Samples/Sdl/TrackerSampleApp.cpp	Wed Jul 31 10:24:09 2019 +0200
+++ b/Samples/Sdl/TrackerSampleApp.cpp	Wed Jul 31 10:58:16 2019 +0200
@@ -641,7 +641,7 @@
 
   static bool g_stopApplication = false;
   
-  OpenGLCompositor& TrackerSampleApp::GetCompositor()
+  ICompositor& TrackerSampleApp::GetCompositor()
   {
     using namespace Orthanc;
     try
@@ -655,7 +655,7 @@
      }
   }
 
-  const OpenGLCompositor& TrackerSampleApp::GetCompositor() const
+  const ICompositor& TrackerSampleApp::GetCompositor() const
   {
     using namespace Orthanc;
     try
@@ -705,7 +705,7 @@
           switch (event.key.keysym.sym)
           {
           case SDLK_f:
-            viewport_.GetContext().GetWindow().ToggleMaximize();
+            viewport_.GetWindow().ToggleMaximize();
             break;
 
           case SDLK_q:
--- a/Samples/Sdl/TrackerSampleApp.h	Wed Jul 31 10:24:09 2019 +0200
+++ b/Samples/Sdl/TrackerSampleApp.h	Wed Jul 31 10:58:16 2019 +0200
@@ -84,12 +84,12 @@
     In the case of this app, the viewport is an SDL viewport and it has 
     a OpenGLCompositor& GetCompositor() method
     */
-    OpenGLCompositor& GetCompositor();
+    ICompositor& GetCompositor();
 
     /**
     See the other overload
     */
-    const OpenGLCompositor& GetCompositor() const;
+    const ICompositor& GetCompositor() const;
 
     /**
     This returns a random point in the canvas part of the scene, but in