comparison Samples/Sdl/BasicScene.cpp @ 600:6129b1e5ba42

BasicScene SDL sample
author Sebastien Jodogne <s.jodogne@gmail.com>
date Sat, 27 Apr 2019 12:38:25 +0200
parents
children 03c4b998fcd0
comparison
equal deleted inserted replaced
599:6da17230c7a3 600:6129b1e5ba42
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
23 #include "../../Applications/Sdl/SdlOpenGLWindow.h"
24 #include "../../Framework/Scene2D/CairoCompositor.h"
25 #include "../../Framework/Scene2D/OpenGLCompositor.h"
26 #include "../../Framework/Scene2D/Scene2D.h"
27 #include "../../Framework/Scene2D/PanSceneTracker.h"
28 #include "../../Framework/Scene2D/RotateSceneTracker.h"
29 #include "../../Framework/Scene2D/ZoomSceneTracker.h"
30 #include "../../Framework/Scene2D/ColorTextureSceneLayer.h"
31
32 // From Orthanc framework
33 #include <Core/Logging.h>
34 #include <Core/OrthancException.h>
35 #include <Core/Images/Image.h>
36 #include <Core/Images/ImageProcessing.h>
37 #include <Core/Images/PngWriter.h>
38
39 #include <SDL.h>
40 #include <stdio.h>
41
42 static const unsigned int FONT_SIZE = 64;
43
44
45 void PrepareScene(OrthancStone::Scene2D& scene)
46 {
47 using namespace OrthancStone;
48
49 // Texture of 2x2 size
50 {
51 Orthanc::Image i(Orthanc::PixelFormat_RGB24, 2, 2, false);
52
53 uint8_t *p = reinterpret_cast<uint8_t*>(i.GetRow(0));
54 p[0] = 255;
55 p[1] = 0;
56 p[2] = 0;
57
58 p[3] = 0;
59 p[4] = 255;
60 p[5] = 0;
61
62 p = reinterpret_cast<uint8_t*>(i.GetRow(1));
63 p[0] = 0;
64 p[1] = 0;
65 p[2] = 255;
66
67 p[3] = 255;
68 p[4] = 0;
69 p[5] = 0;
70
71 scene.SetLayer(12, new ColorTextureSceneLayer(i));
72
73 std::auto_ptr<ColorTextureSceneLayer> l(new ColorTextureSceneLayer(i));
74 l->SetOrigin(-3, 2);
75 l->SetPixelSpacing(1.5, 1);
76 l->SetAngle(20.0 / 180.0 * M_PI);
77 scene.SetLayer(14, l.release());
78 }
79
80 // Texture of 1x1 size
81 {
82 Orthanc::Image i(Orthanc::PixelFormat_RGB24, 1, 1, false);
83
84 uint8_t *p = reinterpret_cast<uint8_t*>(i.GetRow(0));
85 p[0] = 255;
86 p[1] = 0;
87 p[2] = 0;
88
89 std::auto_ptr<ColorTextureSceneLayer> l(new ColorTextureSceneLayer(i));
90 l->SetOrigin(-2, 1);
91 l->SetAngle(20.0 / 180.0 * M_PI);
92 scene.SetLayer(13, l.release());
93 }
94
95 // Some lines
96 {
97 std::auto_ptr<PolylineSceneLayer> layer(new PolylineSceneLayer);
98
99 layer->SetThickness(1);
100
101 PolylineSceneLayer::Chain chain;
102 chain.push_back(ScenePoint2D(0 - 0.5, 0 - 0.5));
103 chain.push_back(ScenePoint2D(0 - 0.5, 2 - 0.5));
104 chain.push_back(ScenePoint2D(2 - 0.5, 2 - 0.5));
105 chain.push_back(ScenePoint2D(2 - 0.5, 0 - 0.5));
106 layer->AddChain(chain, true);
107
108 chain.clear();
109 chain.push_back(ScenePoint2D(-5, -5));
110 chain.push_back(ScenePoint2D(5, -5));
111 chain.push_back(ScenePoint2D(5, 5));
112 chain.push_back(ScenePoint2D(-5, 5));
113 layer->AddChain(chain, true);
114
115 double dy = 1.01;
116 chain.clear();
117 chain.push_back(ScenePoint2D(-4, -4));
118 chain.push_back(ScenePoint2D(4, -4 + dy));
119 chain.push_back(ScenePoint2D(-4, -4 + 2.0 * dy));
120 chain.push_back(ScenePoint2D(4, 2));
121 layer->AddChain(chain, false);
122
123 layer->SetColor(0,255, 255);
124 scene.SetLayer(50, layer.release());
125 }
126
127 // Some text
128 scene.SetLayer(170, new TextSceneLayer(0, 0, "Hello", 0, BitmapAnchor_Center, 20));
129 }
130
131
132 void TakeScreenshot(const std::string& target,
133 const OrthancStone::Scene2D& scene,
134 unsigned int canvasWidth,
135 unsigned int canvasHeight)
136 {
137 // Take a screenshot, then save it as PNG file
138 OrthancStone::CairoCompositor compositor(scene, canvasWidth, canvasHeight);
139 compositor.SetFont(0, Orthanc::EmbeddedResources::UBUNTU_FONT, FONT_SIZE, Orthanc::Encoding_Latin1);
140 compositor.Refresh();
141
142 Orthanc::ImageAccessor canvas;
143 compositor.GetCanvas().GetReadOnlyAccessor(canvas);
144
145 Orthanc::Image png(Orthanc::PixelFormat_RGB24, canvas.GetWidth(), canvas.GetHeight(), false);
146 Orthanc::ImageProcessing::Convert(png, canvas);
147
148 Orthanc::PngWriter writer;
149 writer.WriteToFile(target, png);
150 }
151
152
153 void HandleApplicationEvent(OrthancStone::Scene2D& scene,
154 const SDL_Event& event,
155 std::auto_ptr<OrthancStone::IPointerTracker>& activeTracker,
156 unsigned int windowWidth,
157 unsigned int windowHeight)
158 {
159 if (event.type == SDL_MOUSEBUTTONDOWN)
160 {
161 OrthancStone::PointerEvent e;
162 e.AddIntegerPosition(event.button.x, event.button.y);
163
164 switch (event.button.button)
165 {
166 case SDL_BUTTON_MIDDLE:
167 activeTracker.reset(new OrthancStone::PanSceneTracker(scene, e));
168 break;
169
170 case SDL_BUTTON_RIGHT:
171 activeTracker.reset(new OrthancStone::ZoomSceneTracker
172 (scene, e, windowWidth, windowHeight));
173 break;
174
175 case SDL_BUTTON_LEFT:
176 activeTracker.reset(new OrthancStone::RotateSceneTracker
177 (scene, e, windowWidth, windowHeight));
178 break;
179
180 default:
181 break;
182 }
183 }
184 else if (event.type == SDL_KEYDOWN &&
185 event.key.repeat == 0 /* Ignore key bounce */)
186 {
187 switch (event.key.keysym.sym)
188 {
189 case SDLK_s:
190 scene.FitContent(windowWidth, windowHeight);
191 break;
192
193 case SDLK_c:
194 TakeScreenshot("screenshot.png", scene, windowWidth, windowHeight);
195 break;
196
197 default:
198 break;
199 }
200 }
201 }
202
203
204 static void GLAPIENTRY
205 OpenGLMessageCallback(GLenum source,
206 GLenum type,
207 GLuint id,
208 GLenum severity,
209 GLsizei length,
210 const GLchar* message,
211 const void* userParam )
212 {
213 if (severity != GL_DEBUG_SEVERITY_NOTIFICATION)
214 {
215 fprintf(stderr, "GL CALLBACK: %s type = 0x%x, severity = 0x%x, message = %s\n",
216 ( type == GL_DEBUG_TYPE_ERROR ? "** GL ERROR **" : "" ),
217 type, severity, message );
218 }
219 }
220
221
222 void Run(OrthancStone::Scene2D& scene)
223 {
224 OrthancStone::SdlOpenGLWindow window("Hello", 1024, 768);
225 scene.FitContent(window.GetCanvasWidth(), window.GetCanvasHeight());
226
227 glEnable(GL_DEBUG_OUTPUT);
228 glDebugMessageCallback(OpenGLMessageCallback, 0 );
229
230 OrthancStone::OpenGLCompositor compositor(window, scene);
231 compositor.SetFont(0, Orthanc::EmbeddedResources::UBUNTU_FONT,
232 FONT_SIZE, Orthanc::Encoding_Latin1);
233
234 std::auto_ptr<OrthancStone::IPointerTracker> tracker;
235
236 bool stop = false;
237 while (!stop)
238 {
239 compositor.Refresh();
240
241 SDL_Event event;
242 while (!stop &&
243 SDL_PollEvent(&event))
244 {
245 if (event.type == SDL_QUIT)
246 {
247 stop = true;
248 break;
249 }
250 else if (event.type == SDL_MOUSEMOTION)
251 {
252 if (tracker.get() != NULL)
253 {
254 OrthancStone::PointerEvent e;
255 e.AddIntegerPosition(event.button.x, event.button.y);
256 tracker->Update(e);
257 }
258 }
259 else if (event.type == SDL_MOUSEBUTTONUP)
260 {
261 if (tracker.get() != NULL)
262 {
263 tracker->Release();
264 tracker.reset(NULL);
265 }
266 }
267 else if (event.type == SDL_WINDOWEVENT &&
268 event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED)
269 {
270 tracker.reset(NULL);
271 compositor.UpdateSize();
272 }
273 else if (event.type == SDL_KEYDOWN &&
274 event.key.repeat == 0 /* Ignore key bounce */)
275 {
276 switch (event.key.keysym.sym)
277 {
278 case SDLK_f:
279 window.GetWindow().ToggleMaximize();
280 break;
281
282 case SDLK_q:
283 stop = true;
284 break;
285
286 default:
287 HandleApplicationEvent(scene, event, tracker, window.GetCanvasWidth(), window.GetCanvasHeight());
288 break;
289 }
290 }
291 else
292 {
293 HandleApplicationEvent(scene, event, tracker, window.GetCanvasWidth(), window.GetCanvasHeight());
294 }
295 }
296
297 SDL_Delay(1);
298 }
299 }
300
301
302 int main()
303 {
304 Orthanc::Logging::Initialize();
305 OrthancStone::SdlWindow::GlobalInitialize();
306
307 try
308 {
309 OrthancStone::Scene2D scene;
310 PrepareScene(scene);
311 Run(scene);
312 }
313 catch (Orthanc::OrthancException& e)
314 {
315 LOG(ERROR) << "EXCEPTION: " << e.What();
316 }
317
318 OrthancStone::SdlWindow::GlobalFinalize();
319 Orthanc::Logging::Finalize();
320
321 return 0;
322 }