Mercurial > hg > orthanc-stone
annotate Samples/Sdl/BasicScene.cpp @ 1174:ab3f3bb1bdb4
Merge
author | Benjamin Golinvaux <bgo@osimis.io> |
---|---|
date | Wed, 20 Nov 2019 13:25:38 +0100 |
parents | 64e5f3ff6360 |
children | 2d8ab34c8c91 |
rev | line source |
---|---|
600 | 1 /** |
2 * Stone of Orthanc | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
5 * Copyright (C) 2017-2019 Osimis S.A., Belgium | |
6 * | |
7 * This program is free software: you can redistribute it and/or | |
8 * modify it under the terms of the GNU Affero General Public License | |
9 * as published by the Free Software Foundation, either version 3 of | |
10 * the License, or (at your option) any later version. | |
11 * | |
12 * This program is distributed in the hope that it will be useful, but | |
13 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 * Affero General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU Affero General Public License | |
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 **/ | |
20 | |
21 | |
22 // From Stone | |
891
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
23 #include "../../Framework/Viewport/SdlViewport.h" |
600 | 24 #include "../../Framework/Scene2D/CairoCompositor.h" |
610
0c4c0693b97c
OrthancStone::StoneInitialize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
607
diff
changeset
|
25 #include "../../Framework/Scene2D/ColorTextureSceneLayer.h" |
600 | 26 #include "../../Framework/Scene2D/OpenGLCompositor.h" |
27 #include "../../Framework/Scene2D/PanSceneTracker.h" | |
28 #include "../../Framework/Scene2D/RotateSceneTracker.h" | |
29 #include "../../Framework/Scene2D/ZoomSceneTracker.h" | |
700
059e1fd05fd6
Introduced the ViewportController that sits between the application and the
Benjamin Golinvaux <bgo@osimis.io>
parents:
654
diff
changeset
|
30 #include "../../Framework/Scene2DViewport/ViewportController.h" |
858
e3c56d4f863f
GuiAdapter : mouse event routing in SDL + split the undo stack from the
Benjamin Golinvaux <bgo@osimis.io>
parents:
818
diff
changeset
|
31 #include "../../Framework/Scene2DViewport/UndoStack.h" |
700
059e1fd05fd6
Introduced the ViewportController that sits between the application and the
Benjamin Golinvaux <bgo@osimis.io>
parents:
654
diff
changeset
|
32 |
610
0c4c0693b97c
OrthancStone::StoneInitialize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
607
diff
changeset
|
33 #include "../../Framework/StoneInitialization.h" |
654
462a5074f914
Turned the scene into an observable to be able to dynamically react to
Benjamin Golinvaux <bgo@osimis.io>
parents:
617
diff
changeset
|
34 #include "../../Framework/Messages/MessageBroker.h" |
600 | 35 |
36 // From Orthanc framework | |
37 #include <Core/Logging.h> | |
38 #include <Core/OrthancException.h> | |
39 #include <Core/Images/Image.h> | |
40 #include <Core/Images/ImageProcessing.h> | |
41 #include <Core/Images/PngWriter.h> | |
42 | |
720
f2ee003ba955
Switch to make_shared in various places
Benjamin Golinvaux <bgo@osimis.io>
parents:
706
diff
changeset
|
43 #include <boost/make_shared.hpp> |
f2ee003ba955
Switch to make_shared in various places
Benjamin Golinvaux <bgo@osimis.io>
parents:
706
diff
changeset
|
44 |
600 | 45 #include <SDL.h> |
46 #include <stdio.h> | |
47 | |
602
03c4b998fcd0
display scene positions in the basic sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
48 static const unsigned int FONT_SIZE = 32; |
03c4b998fcd0
display scene positions in the basic sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
49 static const int LAYER_POSITION = 150; |
600 | 50 |
905
88bf49aebc13
introduced ICompositor and allow SdlViewport to work with a CairoCompositor (to run on machines without OpenGL drivers)
Alain Mazy <alain@mazy.be>
parents:
891
diff
changeset
|
51 #define OPENGL_ENABLED 0 |
891
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
52 |
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
53 void PrepareScene(OrthancStone::Scene2D& scene) |
600 | 54 { |
818
e42b491f1fb2
Removed typedefs to shared_ptr by making them explicit. Removed using namespace
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
55 using namespace OrthancStone; |
891
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
56 |
600 | 57 // Texture of 2x2 size |
58 { | |
59 Orthanc::Image i(Orthanc::PixelFormat_RGB24, 2, 2, false); | |
60 | |
61 uint8_t *p = reinterpret_cast<uint8_t*>(i.GetRow(0)); | |
62 p[0] = 255; | |
63 p[1] = 0; | |
64 p[2] = 0; | |
65 | |
66 p[3] = 0; | |
67 p[4] = 255; | |
68 p[5] = 0; | |
69 | |
70 p = reinterpret_cast<uint8_t*>(i.GetRow(1)); | |
71 p[0] = 0; | |
72 p[1] = 0; | |
73 p[2] = 255; | |
74 | |
75 p[3] = 255; | |
76 p[4] = 0; | |
77 p[5] = 0; | |
78 | |
79 scene.SetLayer(12, new ColorTextureSceneLayer(i)); | |
80 | |
81 std::auto_ptr<ColorTextureSceneLayer> l(new ColorTextureSceneLayer(i)); | |
82 l->SetOrigin(-3, 2); | |
83 l->SetPixelSpacing(1.5, 1); | |
84 l->SetAngle(20.0 / 180.0 * M_PI); | |
85 scene.SetLayer(14, l.release()); | |
86 } | |
87 | |
88 // Texture of 1x1 size | |
89 { | |
90 Orthanc::Image i(Orthanc::PixelFormat_RGB24, 1, 1, false); | |
91 | |
92 uint8_t *p = reinterpret_cast<uint8_t*>(i.GetRow(0)); | |
93 p[0] = 255; | |
94 p[1] = 0; | |
95 p[2] = 0; | |
96 | |
97 std::auto_ptr<ColorTextureSceneLayer> l(new ColorTextureSceneLayer(i)); | |
98 l->SetOrigin(-2, 1); | |
99 l->SetAngle(20.0 / 180.0 * M_PI); | |
100 scene.SetLayer(13, l.release()); | |
101 } | |
102 | |
103 // Some lines | |
104 { | |
105 std::auto_ptr<PolylineSceneLayer> layer(new PolylineSceneLayer); | |
106 | |
804
61ba4b504e9a
PolylineSceneLayer now has one color per chain
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
721
diff
changeset
|
107 layer->SetThickness(10); |
600 | 108 |
109 PolylineSceneLayer::Chain chain; | |
110 chain.push_back(ScenePoint2D(0 - 0.5, 0 - 0.5)); | |
111 chain.push_back(ScenePoint2D(0 - 0.5, 2 - 0.5)); | |
112 chain.push_back(ScenePoint2D(2 - 0.5, 2 - 0.5)); | |
113 chain.push_back(ScenePoint2D(2 - 0.5, 0 - 0.5)); | |
804
61ba4b504e9a
PolylineSceneLayer now has one color per chain
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
721
diff
changeset
|
114 layer->AddChain(chain, true, 255, 0, 0); |
600 | 115 |
116 chain.clear(); | |
117 chain.push_back(ScenePoint2D(-5, -5)); | |
118 chain.push_back(ScenePoint2D(5, -5)); | |
119 chain.push_back(ScenePoint2D(5, 5)); | |
120 chain.push_back(ScenePoint2D(-5, 5)); | |
804
61ba4b504e9a
PolylineSceneLayer now has one color per chain
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
721
diff
changeset
|
121 layer->AddChain(chain, true, 0, 255, 0); |
600 | 122 |
123 double dy = 1.01; | |
124 chain.clear(); | |
125 chain.push_back(ScenePoint2D(-4, -4)); | |
126 chain.push_back(ScenePoint2D(4, -4 + dy)); | |
127 chain.push_back(ScenePoint2D(-4, -4 + 2.0 * dy)); | |
128 chain.push_back(ScenePoint2D(4, 2)); | |
804
61ba4b504e9a
PolylineSceneLayer now has one color per chain
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
721
diff
changeset
|
129 layer->AddChain(chain, false, 0, 0, 255); |
600 | 130 |
131 scene.SetLayer(50, layer.release()); | |
132 } | |
133 | |
134 // Some text | |
602
03c4b998fcd0
display scene positions in the basic sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
135 { |
03c4b998fcd0
display scene positions in the basic sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
136 std::auto_ptr<TextSceneLayer> layer(new TextSceneLayer); |
03c4b998fcd0
display scene positions in the basic sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
137 layer->SetText("Hello"); |
03c4b998fcd0
display scene positions in the basic sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
138 scene.SetLayer(100, layer.release()); |
03c4b998fcd0
display scene positions in the basic sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
139 } |
600 | 140 } |
141 | |
142 | |
143 void TakeScreenshot(const std::string& target, | |
818
e42b491f1fb2
Removed typedefs to shared_ptr by making them explicit. Removed using namespace
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
144 const OrthancStone::Scene2D& scene, |
600 | 145 unsigned int canvasWidth, |
146 unsigned int canvasHeight) | |
147 { | |
818
e42b491f1fb2
Removed typedefs to shared_ptr by making them explicit. Removed using namespace
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
148 using namespace OrthancStone; |
600 | 149 // Take a screenshot, then save it as PNG file |
700
059e1fd05fd6
Introduced the ViewportController that sits between the application and the
Benjamin Golinvaux <bgo@osimis.io>
parents:
654
diff
changeset
|
150 CairoCompositor compositor(scene, canvasWidth, canvasHeight); |
600 | 151 compositor.SetFont(0, Orthanc::EmbeddedResources::UBUNTU_FONT, FONT_SIZE, Orthanc::Encoding_Latin1); |
152 compositor.Refresh(); | |
153 | |
154 Orthanc::ImageAccessor canvas; | |
155 compositor.GetCanvas().GetReadOnlyAccessor(canvas); | |
156 | |
157 Orthanc::Image png(Orthanc::PixelFormat_RGB24, canvas.GetWidth(), canvas.GetHeight(), false); | |
158 Orthanc::ImageProcessing::Convert(png, canvas); | |
159 | |
160 Orthanc::PngWriter writer; | |
161 writer.WriteToFile(target, png); | |
162 } | |
163 | |
164 | |
891
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
165 void HandleApplicationEvent(const SDL_Event& event, |
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
166 boost::shared_ptr<OrthancStone::ViewportController>& controller, |
818
e42b491f1fb2
Removed typedefs to shared_ptr by making them explicit. Removed using namespace
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
167 boost::shared_ptr<OrthancStone::IFlexiblePointerTracker>& activeTracker) |
600 | 168 { |
818
e42b491f1fb2
Removed typedefs to shared_ptr by making them explicit. Removed using namespace
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
169 using namespace OrthancStone; |
891
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
170 |
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
171 Scene2D& scene = controller->GetScene(); |
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
172 IViewport& viewport = controller->GetViewport(); |
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
173 |
602
03c4b998fcd0
display scene positions in the basic sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
174 if (event.type == SDL_MOUSEMOTION) |
03c4b998fcd0
display scene positions in the basic sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
175 { |
03c4b998fcd0
display scene positions in the basic sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
176 int scancodeCount = 0; |
03c4b998fcd0
display scene positions in the basic sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
177 const uint8_t* keyboardState = SDL_GetKeyboardState(&scancodeCount); |
03c4b998fcd0
display scene positions in the basic sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
178 |
03c4b998fcd0
display scene positions in the basic sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
179 if (activeTracker.get() == NULL && |
03c4b998fcd0
display scene positions in the basic sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
180 SDL_SCANCODE_LCTRL < scancodeCount && |
03c4b998fcd0
display scene positions in the basic sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
181 keyboardState[SDL_SCANCODE_LCTRL]) |
03c4b998fcd0
display scene positions in the basic sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
182 { |
03c4b998fcd0
display scene positions in the basic sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
183 // The "left-ctrl" key is down, while no tracker is present |
03c4b998fcd0
display scene positions in the basic sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
184 |
700
059e1fd05fd6
Introduced the ViewportController that sits between the application and the
Benjamin Golinvaux <bgo@osimis.io>
parents:
654
diff
changeset
|
185 PointerEvent e; |
891
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
186 e.AddPosition(viewport.GetPixelCenterCoordinates(event.button.x, event.button.y)); |
617 | 187 |
700
059e1fd05fd6
Introduced the ViewportController that sits between the application and the
Benjamin Golinvaux <bgo@osimis.io>
parents:
654
diff
changeset
|
188 ScenePoint2D p = e.GetMainPosition().Apply(scene.GetCanvasToSceneTransform()); |
602
03c4b998fcd0
display scene positions in the basic sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
189 |
03c4b998fcd0
display scene positions in the basic sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
190 char buf[64]; |
03c4b998fcd0
display scene positions in the basic sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
191 sprintf(buf, "(%0.02f,%0.02f)", p.GetX(), p.GetY()); |
03c4b998fcd0
display scene positions in the basic sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
192 |
03c4b998fcd0
display scene positions in the basic sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
193 if (scene.HasLayer(LAYER_POSITION)) |
03c4b998fcd0
display scene positions in the basic sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
194 { |
700
059e1fd05fd6
Introduced the ViewportController that sits between the application and the
Benjamin Golinvaux <bgo@osimis.io>
parents:
654
diff
changeset
|
195 TextSceneLayer& layer = |
059e1fd05fd6
Introduced the ViewportController that sits between the application and the
Benjamin Golinvaux <bgo@osimis.io>
parents:
654
diff
changeset
|
196 dynamic_cast<TextSceneLayer&>(scene.GetLayer(LAYER_POSITION)); |
602
03c4b998fcd0
display scene positions in the basic sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
197 layer.SetText(buf); |
03c4b998fcd0
display scene positions in the basic sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
198 layer.SetPosition(p.GetX(), p.GetY()); |
03c4b998fcd0
display scene positions in the basic sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
199 } |
03c4b998fcd0
display scene positions in the basic sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
200 else |
03c4b998fcd0
display scene positions in the basic sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
201 { |
700
059e1fd05fd6
Introduced the ViewportController that sits between the application and the
Benjamin Golinvaux <bgo@osimis.io>
parents:
654
diff
changeset
|
202 std::auto_ptr<TextSceneLayer> |
059e1fd05fd6
Introduced the ViewportController that sits between the application and the
Benjamin Golinvaux <bgo@osimis.io>
parents:
654
diff
changeset
|
203 layer(new TextSceneLayer); |
602
03c4b998fcd0
display scene positions in the basic sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
204 layer->SetColor(0, 255, 0); |
03c4b998fcd0
display scene positions in the basic sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
205 layer->SetText(buf); |
03c4b998fcd0
display scene positions in the basic sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
206 layer->SetBorder(20); |
700
059e1fd05fd6
Introduced the ViewportController that sits between the application and the
Benjamin Golinvaux <bgo@osimis.io>
parents:
654
diff
changeset
|
207 layer->SetAnchor(BitmapAnchor_BottomCenter); |
602
03c4b998fcd0
display scene positions in the basic sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
208 layer->SetPosition(p.GetX(), p.GetY()); |
03c4b998fcd0
display scene positions in the basic sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
209 scene.SetLayer(LAYER_POSITION, layer.release()); |
03c4b998fcd0
display scene positions in the basic sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
210 } |
617 | 211 } |
212 else | |
213 { | |
214 scene.DeleteLayer(LAYER_POSITION); | |
602
03c4b998fcd0
display scene positions in the basic sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
215 } |
03c4b998fcd0
display scene positions in the basic sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
216 } |
03c4b998fcd0
display scene positions in the basic sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
217 else if (event.type == SDL_MOUSEBUTTONDOWN) |
600 | 218 { |
700
059e1fd05fd6
Introduced the ViewportController that sits between the application and the
Benjamin Golinvaux <bgo@osimis.io>
parents:
654
diff
changeset
|
219 PointerEvent e; |
891
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
220 e.AddPosition(viewport.GetPixelCenterCoordinates(event.button.x, event.button.y)); |
600 | 221 |
222 switch (event.button.button) | |
223 { | |
224 case SDL_BUTTON_MIDDLE: | |
720
f2ee003ba955
Switch to make_shared in various places
Benjamin Golinvaux <bgo@osimis.io>
parents:
706
diff
changeset
|
225 activeTracker = boost::make_shared<PanSceneTracker>(controller, e); |
600 | 226 break; |
227 | |
228 case SDL_BUTTON_RIGHT: | |
891
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
229 activeTracker = boost::make_shared<ZoomSceneTracker> |
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
230 (controller, e, viewport.GetCanvasHeight()); |
600 | 231 break; |
232 | |
233 case SDL_BUTTON_LEFT: | |
720
f2ee003ba955
Switch to make_shared in various places
Benjamin Golinvaux <bgo@osimis.io>
parents:
706
diff
changeset
|
234 activeTracker = boost::make_shared<RotateSceneTracker>(controller, e); |
600 | 235 break; |
236 | |
237 default: | |
238 break; | |
239 } | |
240 } | |
241 else if (event.type == SDL_KEYDOWN && | |
242 event.key.repeat == 0 /* Ignore key bounce */) | |
243 { | |
244 switch (event.key.keysym.sym) | |
245 { | |
246 case SDLK_s: | |
891
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
247 controller->FitContent(viewport.GetCanvasWidth(), |
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
248 viewport.GetCanvasHeight()); |
600 | 249 break; |
250 | |
251 case SDLK_c: | |
617 | 252 TakeScreenshot("screenshot.png", scene, |
891
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
253 viewport.GetCanvasWidth(), |
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
254 viewport.GetCanvasHeight()); |
600 | 255 break; |
256 | |
257 default: | |
258 break; | |
259 } | |
260 } | |
261 } | |
262 | |
905
88bf49aebc13
introduced ICompositor and allow SdlViewport to work with a CairoCompositor (to run on machines without OpenGL drivers)
Alain Mazy <alain@mazy.be>
parents:
891
diff
changeset
|
263 #if OPENGL_ENABLED==1 |
600 | 264 static void GLAPIENTRY |
265 OpenGLMessageCallback(GLenum source, | |
266 GLenum type, | |
267 GLuint id, | |
268 GLenum severity, | |
269 GLsizei length, | |
270 const GLchar* message, | |
271 const void* userParam ) | |
272 { | |
273 if (severity != GL_DEBUG_SEVERITY_NOTIFICATION) | |
274 { | |
275 fprintf(stderr, "GL CALLBACK: %s type = 0x%x, severity = 0x%x, message = %s\n", | |
276 ( type == GL_DEBUG_TYPE_ERROR ? "** GL ERROR **" : "" ), | |
277 type, severity, message ); | |
278 } | |
279 } | |
905
88bf49aebc13
introduced ICompositor and allow SdlViewport to work with a CairoCompositor (to run on machines without OpenGL drivers)
Alain Mazy <alain@mazy.be>
parents:
891
diff
changeset
|
280 #endif |
600 | 281 |
891
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
282 void Run(OrthancStone::MessageBroker& broker, |
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
283 OrthancStone::SdlViewport& viewport) |
600 | 284 { |
818
e42b491f1fb2
Removed typedefs to shared_ptr by making them explicit. Removed using namespace
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
285 using namespace OrthancStone; |
891
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
286 |
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
287 boost::shared_ptr<ViewportController> controller( |
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
288 new ViewportController(boost::make_shared<UndoStack>(), broker, viewport)); |
611
e3f21a265be5
Added version directive to GLSL shader code + glew init function in sample code
Benjamin Golinvaux <bgo@osimis.io>
parents:
610
diff
changeset
|
289 |
905
88bf49aebc13
introduced ICompositor and allow SdlViewport to work with a CairoCompositor (to run on machines without OpenGL drivers)
Alain Mazy <alain@mazy.be>
parents:
891
diff
changeset
|
290 #if OPENGL_ENABLED==1 |
600 | 291 glEnable(GL_DEBUG_OUTPUT); |
613
412a2d01a189
automatic initialization of glew
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
611
diff
changeset
|
292 glDebugMessageCallback(OpenGLMessageCallback, 0); |
905
88bf49aebc13
introduced ICompositor and allow SdlViewport to work with a CairoCompositor (to run on machines without OpenGL drivers)
Alain Mazy <alain@mazy.be>
parents:
891
diff
changeset
|
293 #endif |
600 | 294 |
818
e42b491f1fb2
Removed typedefs to shared_ptr by making them explicit. Removed using namespace
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
295 boost::shared_ptr<IFlexiblePointerTracker> tracker; |
600 | 296 |
891
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
297 bool firstShown = true; |
600 | 298 bool stop = false; |
299 while (!stop) | |
300 { | |
891
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
301 viewport.Refresh(); |
600 | 302 |
303 SDL_Event event; | |
304 while (!stop && | |
305 SDL_PollEvent(&event)) | |
306 { | |
307 if (event.type == SDL_QUIT) | |
308 { | |
309 stop = true; | |
310 break; | |
311 } | |
312 else if (event.type == SDL_MOUSEMOTION) | |
313 { | |
700
059e1fd05fd6
Introduced the ViewportController that sits between the application and the
Benjamin Golinvaux <bgo@osimis.io>
parents:
654
diff
changeset
|
314 if (tracker) |
600 | 315 { |
700
059e1fd05fd6
Introduced the ViewportController that sits between the application and the
Benjamin Golinvaux <bgo@osimis.io>
parents:
654
diff
changeset
|
316 PointerEvent e; |
891
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
317 e.AddPosition(viewport.GetPixelCenterCoordinates( |
700
059e1fd05fd6
Introduced the ViewportController that sits between the application and the
Benjamin Golinvaux <bgo@osimis.io>
parents:
654
diff
changeset
|
318 event.button.x, event.button.y)); |
059e1fd05fd6
Introduced the ViewportController that sits between the application and the
Benjamin Golinvaux <bgo@osimis.io>
parents:
654
diff
changeset
|
319 tracker->PointerMove(e); |
600 | 320 } |
321 } | |
322 else if (event.type == SDL_MOUSEBUTTONUP) | |
323 { | |
700
059e1fd05fd6
Introduced the ViewportController that sits between the application and the
Benjamin Golinvaux <bgo@osimis.io>
parents:
654
diff
changeset
|
324 if (tracker) |
600 | 325 { |
700
059e1fd05fd6
Introduced the ViewportController that sits between the application and the
Benjamin Golinvaux <bgo@osimis.io>
parents:
654
diff
changeset
|
326 PointerEvent e; |
891
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
327 e.AddPosition(viewport.GetPixelCenterCoordinates( |
700
059e1fd05fd6
Introduced the ViewportController that sits between the application and the
Benjamin Golinvaux <bgo@osimis.io>
parents:
654
diff
changeset
|
328 event.button.x, event.button.y)); |
059e1fd05fd6
Introduced the ViewportController that sits between the application and the
Benjamin Golinvaux <bgo@osimis.io>
parents:
654
diff
changeset
|
329 tracker->PointerUp(e); |
059e1fd05fd6
Introduced the ViewportController that sits between the application and the
Benjamin Golinvaux <bgo@osimis.io>
parents:
654
diff
changeset
|
330 if(!tracker->IsAlive()) |
706
ef07304d4423
Fixed C++ code to be C++03 compatible
Benjamin Golinvaux <bgo@osimis.io>
parents:
700
diff
changeset
|
331 tracker.reset(); |
600 | 332 } |
333 } | |
891
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
334 else if (event.type == SDL_WINDOWEVENT) |
600 | 335 { |
891
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
336 switch (event.window.event) |
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
337 { |
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
338 case SDL_WINDOWEVENT_SIZE_CHANGED: |
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
339 tracker.reset(); |
909 | 340 viewport.UpdateSize(event.window.data1, event.window.data2); |
891
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
341 break; |
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
342 |
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
343 case SDL_WINDOWEVENT_SHOWN: |
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
344 if (firstShown) |
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
345 { |
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
346 // Once the window is first shown, fit the content to its size |
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
347 controller->FitContent(viewport.GetCanvasWidth(), viewport.GetCanvasHeight()); |
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
348 firstShown = false; |
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
349 } |
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
350 |
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
351 break; |
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
352 |
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
353 default: |
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
354 break; |
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
355 } |
600 | 356 } |
357 else if (event.type == SDL_KEYDOWN && | |
358 event.key.repeat == 0 /* Ignore key bounce */) | |
359 { | |
360 switch (event.key.keysym.sym) | |
361 { | |
362 case SDLK_f: | |
905
88bf49aebc13
introduced ICompositor and allow SdlViewport to work with a CairoCompositor (to run on machines without OpenGL drivers)
Alain Mazy <alain@mazy.be>
parents:
891
diff
changeset
|
363 viewport.GetWindow().ToggleMaximize(); |
600 | 364 break; |
365 | |
366 case SDLK_q: | |
367 stop = true; | |
368 break; | |
369 | |
370 default: | |
371 break; | |
372 } | |
373 } | |
602
03c4b998fcd0
display scene positions in the basic sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
374 |
891
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
375 HandleApplicationEvent(event, controller, tracker); |
600 | 376 } |
377 | |
378 SDL_Delay(1); | |
379 } | |
380 } | |
381 | |
382 | |
607
f4b37a991dac
using glew to build on Windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
602
diff
changeset
|
383 |
f4b37a991dac
using glew to build on Windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
602
diff
changeset
|
384 |
f4b37a991dac
using glew to build on Windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
602
diff
changeset
|
385 /** |
f4b37a991dac
using glew to build on Windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
602
diff
changeset
|
386 * IMPORTANT: The full arguments to "main()" are needed for SDL on |
f4b37a991dac
using glew to build on Windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
602
diff
changeset
|
387 * Windows. Otherwise, one gets the linking error "undefined reference |
f4b37a991dac
using glew to build on Windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
602
diff
changeset
|
388 * to `SDL_main'". https://wiki.libsdl.org/FAQWindows |
f4b37a991dac
using glew to build on Windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
602
diff
changeset
|
389 **/ |
f4b37a991dac
using glew to build on Windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
602
diff
changeset
|
390 int main(int argc, char* argv[]) |
600 | 391 { |
891
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
392 OrthancStone::StoneInitialize(); |
613
412a2d01a189
automatic initialization of glew
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
611
diff
changeset
|
393 Orthanc::Logging::EnableInfoLevel(true); |
600 | 394 |
395 try | |
396 { | |
905
88bf49aebc13
introduced ICompositor and allow SdlViewport to work with a CairoCompositor (to run on machines without OpenGL drivers)
Alain Mazy <alain@mazy.be>
parents:
891
diff
changeset
|
397 #if OPENGL_ENABLED==1 |
88bf49aebc13
introduced ICompositor and allow SdlViewport to work with a CairoCompositor (to run on machines without OpenGL drivers)
Alain Mazy <alain@mazy.be>
parents:
891
diff
changeset
|
398 OrthancStone::SdlOpenGLViewport viewport("Hello", 1024, 768); |
88bf49aebc13
introduced ICompositor and allow SdlViewport to work with a CairoCompositor (to run on machines without OpenGL drivers)
Alain Mazy <alain@mazy.be>
parents:
891
diff
changeset
|
399 #else |
88bf49aebc13
introduced ICompositor and allow SdlViewport to work with a CairoCompositor (to run on machines without OpenGL drivers)
Alain Mazy <alain@mazy.be>
parents:
891
diff
changeset
|
400 OrthancStone::SdlCairoViewport viewport("Hello", 1024, 768); |
88bf49aebc13
introduced ICompositor and allow SdlViewport to work with a CairoCompositor (to run on machines without OpenGL drivers)
Alain Mazy <alain@mazy.be>
parents:
891
diff
changeset
|
401 #endif |
891
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
402 PrepareScene(viewport.GetScene()); |
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
403 |
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
404 viewport.GetCompositor().SetFont(0, Orthanc::EmbeddedResources::UBUNTU_FONT, |
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
405 FONT_SIZE, Orthanc::Encoding_Latin1); |
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
406 |
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
407 OrthancStone::MessageBroker broker; |
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
408 Run(broker, viewport); |
600 | 409 } |
410 catch (Orthanc::OrthancException& e) | |
411 { | |
412 LOG(ERROR) << "EXCEPTION: " << e.What(); | |
413 } | |
414 | |
891
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
415 OrthancStone::StoneFinalize(); |
600 | 416 |
417 return 0; | |
418 } |