# HG changeset patch # User Sebastien Jodogne # Date 1556361505 -7200 # Node ID 6129b1e5ba42aefca99cb5d162eb9ae30f6356b4 # Parent 6da17230c7a3e1d2b67dfa7b78763fb6b04734c7 BasicScene SDL sample diff -r 6da17230c7a3 -r 6129b1e5ba42 Framework/Scene2D/CairoCompositor.cpp --- a/Framework/Scene2D/CairoCompositor.cpp Sat Apr 27 10:56:25 2019 +0200 +++ b/Framework/Scene2D/CairoCompositor.cpp Sat Apr 27 12:38:25 2019 +0200 @@ -82,7 +82,7 @@ } - CairoCompositor::CairoCompositor(Scene2D& scene, + CairoCompositor::CairoCompositor(const Scene2D& scene, unsigned int canvasWidth, unsigned int canvasHeight) : helper_(scene, *this) diff -r 6da17230c7a3 -r 6129b1e5ba42 Framework/Scene2D/CairoCompositor.h --- a/Framework/Scene2D/CairoCompositor.h Sat Apr 27 10:56:25 2019 +0200 +++ b/Framework/Scene2D/CairoCompositor.h Sat Apr 27 12:38:25 2019 +0200 @@ -57,7 +57,7 @@ virtual Internals::CompositorHelper::ILayerRenderer* Create(const ISceneLayer& layer); public: - CairoCompositor(Scene2D& scene, + CairoCompositor(const Scene2D& scene, unsigned int canvasWidth, unsigned int canvasHeight); diff -r 6da17230c7a3 -r 6129b1e5ba42 Framework/Scene2D/Internals/CompositorHelper.h --- a/Framework/Scene2D/Internals/CompositorHelper.h Sat Apr 27 10:56:25 2019 +0200 +++ b/Framework/Scene2D/Internals/CompositorHelper.h Sat Apr 27 12:38:25 2019 +0200 @@ -58,7 +58,7 @@ typedef std::map Content; - Scene2D& scene_; + const Scene2D& scene_; IRendererFactory& factory_; Content content_; AffineTransform2D sceneTransform_; @@ -68,7 +68,7 @@ int depth); public: - CompositorHelper(Scene2D& scene, + CompositorHelper(const Scene2D& scene, IRendererFactory& factory) : scene_(scene), factory_(factory) diff -r 6da17230c7a3 -r 6129b1e5ba42 Framework/Scene2D/OpenGLCompositor.cpp --- a/Framework/Scene2D/OpenGLCompositor.cpp Sat Apr 27 10:56:25 2019 +0200 +++ b/Framework/Scene2D/OpenGLCompositor.cpp Sat Apr 27 12:38:25 2019 +0200 @@ -119,7 +119,7 @@ OpenGLCompositor::OpenGLCompositor(OpenGL::IOpenGLContext& context, - Scene2D& scene) : + const Scene2D& scene) : context_(context), helper_(scene, *this), colorTextureProgram_(context), diff -r 6da17230c7a3 -r 6129b1e5ba42 Framework/Scene2D/OpenGLCompositor.h --- a/Framework/Scene2D/OpenGLCompositor.h Sat Apr 27 10:56:25 2019 +0200 +++ b/Framework/Scene2D/OpenGLCompositor.h Sat Apr 27 12:38:25 2019 +0200 @@ -52,7 +52,7 @@ public: OpenGLCompositor(OpenGL::IOpenGLContext& context, - Scene2D& scene); + const Scene2D& scene); ~OpenGLCompositor(); diff -r 6da17230c7a3 -r 6129b1e5ba42 Framework/Scene2D/Scene2D.cpp --- a/Framework/Scene2D/Scene2D.cpp Sat Apr 27 10:56:25 2019 +0200 +++ b/Framework/Scene2D/Scene2D.cpp Sat Apr 27 12:38:25 2019 +0200 @@ -87,7 +87,7 @@ } - void Scene2D::Apply(IVisitor& visitor) + void Scene2D::Apply(IVisitor& visitor) const { for (Content::const_iterator it = content_.begin(); it != content_.end(); ++it) diff -r 6da17230c7a3 -r 6129b1e5ba42 Framework/Scene2D/Scene2D.h --- a/Framework/Scene2D/Scene2D.h Sat Apr 27 10:56:25 2019 +0200 +++ b/Framework/Scene2D/Scene2D.h Sat Apr 27 12:38:25 2019 +0200 @@ -69,7 +69,7 @@ void DeleteLayer(int depth); - void Apply(IVisitor& visitor); + void Apply(IVisitor& visitor) const; const AffineTransform2D& GetSceneToCanvasTransform() const { diff -r 6da17230c7a3 -r 6129b1e5ba42 Resources/CMake/OrthancStoneConfiguration.cmake --- a/Resources/CMake/OrthancStoneConfiguration.cmake Sat Apr 27 10:56:25 2019 +0200 +++ b/Resources/CMake/OrthancStoneConfiguration.cmake Sat Apr 27 12:38:25 2019 +0200 @@ -107,6 +107,13 @@ if (ENABLE_OPENGL) + include(FindOpenGL) + if (NOT OPENGL_FOUND) + message(FATAL_ERROR "Cannot find OpenGL on your system") + endif() + + link_libraries(${OPENGL_LIBRARIES}) + add_definitions( -DGL_GLEXT_PROTOTYPES=1 -DORTHANC_ENABLE_OPENGL=1 diff -r 6da17230c7a3 -r 6129b1e5ba42 Samples/Sdl/BasicScene.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Samples/Sdl/BasicScene.cpp Sat Apr 27 12:38:25 2019 +0200 @@ -0,0 +1,322 @@ +/** + * Stone of Orthanc + * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics + * Department, University Hospital of Liege, Belgium + * Copyright (C) 2017-2019 Osimis S.A., Belgium + * + * This program is free software: you can redistribute it and/or + * modify it under the terms of the GNU Affero General Public License + * as published by the Free Software Foundation, either version 3 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + **/ + + +// From Stone +#include "../../Applications/Sdl/SdlOpenGLWindow.h" +#include "../../Framework/Scene2D/CairoCompositor.h" +#include "../../Framework/Scene2D/OpenGLCompositor.h" +#include "../../Framework/Scene2D/Scene2D.h" +#include "../../Framework/Scene2D/PanSceneTracker.h" +#include "../../Framework/Scene2D/RotateSceneTracker.h" +#include "../../Framework/Scene2D/ZoomSceneTracker.h" +#include "../../Framework/Scene2D/ColorTextureSceneLayer.h" + +// From Orthanc framework +#include +#include +#include +#include +#include + +#include +#include + +static const unsigned int FONT_SIZE = 64; + + +void PrepareScene(OrthancStone::Scene2D& scene) +{ + using namespace OrthancStone; + + // Texture of 2x2 size + { + Orthanc::Image i(Orthanc::PixelFormat_RGB24, 2, 2, false); + + uint8_t *p = reinterpret_cast(i.GetRow(0)); + p[0] = 255; + p[1] = 0; + p[2] = 0; + + p[3] = 0; + p[4] = 255; + p[5] = 0; + + p = reinterpret_cast(i.GetRow(1)); + p[0] = 0; + p[1] = 0; + p[2] = 255; + + p[3] = 255; + p[4] = 0; + p[5] = 0; + + scene.SetLayer(12, new ColorTextureSceneLayer(i)); + + std::auto_ptr l(new ColorTextureSceneLayer(i)); + l->SetOrigin(-3, 2); + l->SetPixelSpacing(1.5, 1); + l->SetAngle(20.0 / 180.0 * M_PI); + scene.SetLayer(14, l.release()); + } + + // Texture of 1x1 size + { + Orthanc::Image i(Orthanc::PixelFormat_RGB24, 1, 1, false); + + uint8_t *p = reinterpret_cast(i.GetRow(0)); + p[0] = 255; + p[1] = 0; + p[2] = 0; + + std::auto_ptr l(new ColorTextureSceneLayer(i)); + l->SetOrigin(-2, 1); + l->SetAngle(20.0 / 180.0 * M_PI); + scene.SetLayer(13, l.release()); + } + + // Some lines + { + std::auto_ptr layer(new PolylineSceneLayer); + + layer->SetThickness(1); + + PolylineSceneLayer::Chain chain; + chain.push_back(ScenePoint2D(0 - 0.5, 0 - 0.5)); + chain.push_back(ScenePoint2D(0 - 0.5, 2 - 0.5)); + chain.push_back(ScenePoint2D(2 - 0.5, 2 - 0.5)); + chain.push_back(ScenePoint2D(2 - 0.5, 0 - 0.5)); + layer->AddChain(chain, true); + + chain.clear(); + chain.push_back(ScenePoint2D(-5, -5)); + chain.push_back(ScenePoint2D(5, -5)); + chain.push_back(ScenePoint2D(5, 5)); + chain.push_back(ScenePoint2D(-5, 5)); + layer->AddChain(chain, true); + + double dy = 1.01; + chain.clear(); + chain.push_back(ScenePoint2D(-4, -4)); + chain.push_back(ScenePoint2D(4, -4 + dy)); + chain.push_back(ScenePoint2D(-4, -4 + 2.0 * dy)); + chain.push_back(ScenePoint2D(4, 2)); + layer->AddChain(chain, false); + + layer->SetColor(0,255, 255); + scene.SetLayer(50, layer.release()); + } + + // Some text + scene.SetLayer(170, new TextSceneLayer(0, 0, "Hello", 0, BitmapAnchor_Center, 20)); +} + + +void TakeScreenshot(const std::string& target, + const OrthancStone::Scene2D& scene, + unsigned int canvasWidth, + unsigned int canvasHeight) +{ + // Take a screenshot, then save it as PNG file + OrthancStone::CairoCompositor compositor(scene, canvasWidth, canvasHeight); + compositor.SetFont(0, Orthanc::EmbeddedResources::UBUNTU_FONT, FONT_SIZE, Orthanc::Encoding_Latin1); + compositor.Refresh(); + + Orthanc::ImageAccessor canvas; + compositor.GetCanvas().GetReadOnlyAccessor(canvas); + + Orthanc::Image png(Orthanc::PixelFormat_RGB24, canvas.GetWidth(), canvas.GetHeight(), false); + Orthanc::ImageProcessing::Convert(png, canvas); + + Orthanc::PngWriter writer; + writer.WriteToFile(target, png); +} + + +void HandleApplicationEvent(OrthancStone::Scene2D& scene, + const SDL_Event& event, + std::auto_ptr& activeTracker, + unsigned int windowWidth, + unsigned int windowHeight) +{ + if (event.type == SDL_MOUSEBUTTONDOWN) + { + OrthancStone::PointerEvent e; + e.AddIntegerPosition(event.button.x, event.button.y); + + switch (event.button.button) + { + case SDL_BUTTON_MIDDLE: + activeTracker.reset(new OrthancStone::PanSceneTracker(scene, e)); + break; + + case SDL_BUTTON_RIGHT: + activeTracker.reset(new OrthancStone::ZoomSceneTracker + (scene, e, windowWidth, windowHeight)); + break; + + case SDL_BUTTON_LEFT: + activeTracker.reset(new OrthancStone::RotateSceneTracker + (scene, e, windowWidth, windowHeight)); + break; + + default: + break; + } + } + else if (event.type == SDL_KEYDOWN && + event.key.repeat == 0 /* Ignore key bounce */) + { + switch (event.key.keysym.sym) + { + case SDLK_s: + scene.FitContent(windowWidth, windowHeight); + break; + + case SDLK_c: + TakeScreenshot("screenshot.png", scene, windowWidth, windowHeight); + break; + + default: + break; + } + } +} + + +static void GLAPIENTRY +OpenGLMessageCallback(GLenum source, + GLenum type, + GLuint id, + GLenum severity, + GLsizei length, + const GLchar* message, + const void* userParam ) +{ + if (severity != GL_DEBUG_SEVERITY_NOTIFICATION) + { + fprintf(stderr, "GL CALLBACK: %s type = 0x%x, severity = 0x%x, message = %s\n", + ( type == GL_DEBUG_TYPE_ERROR ? "** GL ERROR **" : "" ), + type, severity, message ); + } +} + + +void Run(OrthancStone::Scene2D& scene) +{ + OrthancStone::SdlOpenGLWindow window("Hello", 1024, 768); + scene.FitContent(window.GetCanvasWidth(), window.GetCanvasHeight()); + + glEnable(GL_DEBUG_OUTPUT); + glDebugMessageCallback(OpenGLMessageCallback, 0 ); + + OrthancStone::OpenGLCompositor compositor(window, scene); + compositor.SetFont(0, Orthanc::EmbeddedResources::UBUNTU_FONT, + FONT_SIZE, Orthanc::Encoding_Latin1); + + std::auto_ptr tracker; + + bool stop = false; + while (!stop) + { + compositor.Refresh(); + + SDL_Event event; + while (!stop && + SDL_PollEvent(&event)) + { + if (event.type == SDL_QUIT) + { + stop = true; + break; + } + else if (event.type == SDL_MOUSEMOTION) + { + if (tracker.get() != NULL) + { + OrthancStone::PointerEvent e; + e.AddIntegerPosition(event.button.x, event.button.y); + tracker->Update(e); + } + } + else if (event.type == SDL_MOUSEBUTTONUP) + { + if (tracker.get() != NULL) + { + tracker->Release(); + tracker.reset(NULL); + } + } + else if (event.type == SDL_WINDOWEVENT && + event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) + { + tracker.reset(NULL); + compositor.UpdateSize(); + } + else if (event.type == SDL_KEYDOWN && + event.key.repeat == 0 /* Ignore key bounce */) + { + switch (event.key.keysym.sym) + { + case SDLK_f: + window.GetWindow().ToggleMaximize(); + break; + + case SDLK_q: + stop = true; + break; + + default: + HandleApplicationEvent(scene, event, tracker, window.GetCanvasWidth(), window.GetCanvasHeight()); + break; + } + } + else + { + HandleApplicationEvent(scene, event, tracker, window.GetCanvasWidth(), window.GetCanvasHeight()); + } + } + + SDL_Delay(1); + } +} + + +int main() +{ + Orthanc::Logging::Initialize(); + OrthancStone::SdlWindow::GlobalInitialize(); + + try + { + OrthancStone::Scene2D scene; + PrepareScene(scene); + Run(scene); + } + catch (Orthanc::OrthancException& e) + { + LOG(ERROR) << "EXCEPTION: " << e.What(); + } + + OrthancStone::SdlWindow::GlobalFinalize(); + Orthanc::Logging::Finalize(); + + return 0; +} diff -r 6da17230c7a3 -r 6129b1e5ba42 Samples/Sdl/CMakeLists.txt --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Samples/Sdl/CMakeLists.txt Sat Apr 27 12:38:25 2019 +0200 @@ -0,0 +1,65 @@ +cmake_minimum_required(VERSION 2.8.3) + + +##################################################################### +## Configuration of the Orthanc framework +##################################################################### + +# This CMake file defines the "ORTHANC_STONE_VERSION" macro, so it +# must be the first inclusion +include(${CMAKE_SOURCE_DIR}/../../Resources/CMake/Version.cmake) + +if (ORTHANC_STONE_VERSION STREQUAL "mainline") + set(ORTHANC_FRAMEWORK_VERSION "mainline") + set(ORTHANC_FRAMEWORK_DEFAULT_SOURCE "hg") +else() + set(ORTHANC_FRAMEWORK_VERSION "1.5.7") + set(ORTHANC_FRAMEWORK_DEFAULT_SOURCE "web") +endif() + +set(ORTHANC_FRAMEWORK_SOURCE "${ORTHANC_FRAMEWORK_DEFAULT_SOURCE}" CACHE STRING "Source of the Orthanc source code (can be \"hg\", \"archive\", \"web\" or \"path\")") +set(ORTHANC_FRAMEWORK_ARCHIVE "" CACHE STRING "Path to the Orthanc archive, if ORTHANC_FRAMEWORK_SOURCE is \"archive\"") +set(ORTHANC_FRAMEWORK_ROOT "" CACHE STRING "Path to the Orthanc source directory, if ORTHANC_FRAMEWORK_SOURCE is \"path\"") + + +##################################################################### +## Configuration of the Stone framework +##################################################################### + +include(${CMAKE_SOURCE_DIR}/../../Resources/CMake/OrthancStoneParameters.cmake) +include(${ORTHANC_ROOT}/Resources/CMake/DownloadPackage.cmake) + +DownloadPackage( + "a24b8136b8f3bb93f166baf97d9328de" + "http://orthanc.osimis.io/ThirdPartyDownloads/ubuntu-font-family-0.83.zip" + "${CMAKE_BINARY_DIR}/ubuntu-font-family-0.83") + +set(ORTHANC_STONE_APPLICATION_RESOURCES + UBUNTU_FONT ${CMAKE_BINARY_DIR}/ubuntu-font-family-0.83/Ubuntu-R.ttf + ) + +SET(ENABLE_GOOGLE_TEST OFF) +SET(ENABLE_LOCALE ON) +SET(ENABLE_SDL ON) +SET(ENABLE_WEB_CLIENT ON) +SET(ORTHANC_SANDBOXED OFF) +LIST(APPEND ORTHANC_BOOST_COMPONENTS program_options) + +include(${CMAKE_SOURCE_DIR}/../../Resources/CMake/OrthancStoneConfiguration.cmake) + +include_directories(${ORTHANC_STONE_ROOT}) + + +##################################################################### +## Build the samples +##################################################################### + +add_library(OrthancStone STATIC + ${ORTHANC_STONE_SOURCES} + ) + +add_executable(BasicScene + BasicScene.cpp + ) + +target_link_libraries(BasicScene OrthancStone)