comparison Deprecated/Samples/MultiPlatform/BasicScene/mainSdl.cpp @ 1402:65e1e4b08302

moved deprecated samples into Deprecated
author Alain Mazy <alain@mazy.be>
date Wed, 29 Apr 2020 20:50:53 +0200
parents Samples/Deprecated/MultiPlatform/BasicScene/mainSdl.cpp@eac254fb6791
children 182bf3106ee2
comparison
equal deleted inserted replaced
1401:f6a2d46d2b76 1402:65e1e4b08302
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-2020 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 "Framework/Viewport/SdlViewport.h"
24 #include "Framework/Scene2D/OpenGLCompositor.h"
25 #include "Framework/Scene2DViewport/UndoStack.h"
26 #include "Framework/StoneInitialization.h"
27 #include "Framework/Messages/MessageBroker.h"
28
29 // From Orthanc framework
30 #include <Core/Logging.h>
31 #include <Core/OrthancException.h>
32
33 #include <boost/make_shared.hpp>
34 #include <boost/ref.hpp>
35
36 #include <SDL.h>
37 #include <stdio.h>
38
39
40 #include "BasicScene.h"
41
42 using namespace OrthancStone;
43
44 boost::shared_ptr<BasicScene2DInteractor> interactor;
45
46 void HandleApplicationEvent(boost::shared_ptr<OrthancStone::ViewportController> controller,
47 const SDL_Event& event)
48 {
49 using namespace OrthancStone;
50 Scene2D& scene(controller->GetScene());
51 if (event.type == SDL_MOUSEBUTTONDOWN || event.type == SDL_MOUSEBUTTONUP || event.type == SDL_MOUSEMOTION)
52 {
53 // TODO: this code is copy/pasted from GuiAdapter::Run() -> find the right place
54 int scancodeCount = 0;
55 const uint8_t* keyboardState = SDL_GetKeyboardState(&scancodeCount);
56 bool ctrlPressed(false);
57 bool shiftPressed(false);
58 bool altPressed(false);
59
60 if (SDL_SCANCODE_LCTRL < scancodeCount && keyboardState[SDL_SCANCODE_LCTRL])
61 ctrlPressed = true;
62 if (SDL_SCANCODE_RCTRL < scancodeCount && keyboardState[SDL_SCANCODE_RCTRL])
63 ctrlPressed = true;
64 if (SDL_SCANCODE_LSHIFT < scancodeCount && keyboardState[SDL_SCANCODE_LSHIFT])
65 shiftPressed = true;
66 if (SDL_SCANCODE_RSHIFT < scancodeCount && keyboardState[SDL_SCANCODE_RSHIFT])
67 shiftPressed = true;
68 if (SDL_SCANCODE_LALT < scancodeCount && keyboardState[SDL_SCANCODE_LALT])
69 altPressed = true;
70
71 GuiAdapterMouseEvent guiEvent;
72 ConvertFromPlatform(guiEvent, ctrlPressed, shiftPressed, altPressed, event);
73 PointerEvent pointerEvent;
74 pointerEvent.AddPosition(controller->GetViewport().GetPixelCenterCoordinates(event.button.x, event.button.y));
75
76 interactor->OnMouseEvent(guiEvent, pointerEvent);
77 return;
78 }
79 else if ((event.type == SDL_KEYDOWN || event.type == SDL_KEYUP) && event.key.repeat == 0 /* Ignore key bounce */)
80 {
81 GuiAdapterKeyboardEvent guiEvent;
82 ConvertFromPlatform(guiEvent, event);
83
84 interactor->OnKeyboardEvent(guiEvent);
85 }
86
87 }
88
89
90 static void GLAPIENTRY
91 OpenGLMessageCallback(GLenum source,
92 GLenum type,
93 GLuint id,
94 GLenum severity,
95 GLsizei length,
96 const GLchar* message,
97 const void* userParam )
98 {
99 if (severity != GL_DEBUG_SEVERITY_NOTIFICATION)
100 {
101 fprintf(stderr, "GL CALLBACK: %s type = 0x%x, severity = 0x%x, message = %s\n",
102 ( type == GL_DEBUG_TYPE_ERROR ? "** GL ERROR **" : "" ),
103 type, severity, message );
104 }
105 }
106
107
108 void Run(boost::shared_ptr<OrthancStone::ViewportController> controller)
109 {
110 SdlViewport& sdlViewport = dynamic_cast<SdlViewport&>(controller->GetViewport());
111
112 glEnable(GL_DEBUG_OUTPUT);
113 glDebugMessageCallback(OpenGLMessageCallback, 0);
114
115 controller->GetViewport().GetCompositor().SetFont(0, Orthanc::EmbeddedResources::UBUNTU_FONT,
116 BASIC_SCENE_FONT_SIZE, Orthanc::Encoding_Latin1);
117
118 controller->GetViewport().Refresh();
119 controller->FitContent();
120
121
122 bool stop = false;
123 while (!stop)
124 {
125 controller->GetViewport().Refresh();
126
127 SDL_Event event;
128 while (!stop &&
129 SDL_PollEvent(&event))
130 {
131 if (event.type == SDL_QUIT)
132 {
133 stop = true;
134 break;
135 }
136 else if (event.type == SDL_WINDOWEVENT &&
137 event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED)
138 {
139 sdlViewport.UpdateSize(event.window.data1, event.window.data2);
140 }
141 else if (event.type == SDL_KEYDOWN &&
142 event.key.repeat == 0 /* Ignore key bounce */)
143 {
144 switch (event.key.keysym.sym)
145 {
146 case SDLK_f:
147 sdlViewport.GetWindow().ToggleMaximize();
148 break;
149
150 case SDLK_q:
151 stop = true;
152 break;
153
154 default:
155 break;
156 }
157 }
158
159 HandleApplicationEvent(controller, event);
160 }
161
162 SDL_Delay(1);
163 }
164 interactor.reset();
165 }
166
167
168
169
170 /**
171 * IMPORTANT: The full arguments to "main()" are needed for SDL on
172 * Windows. Otherwise, one gets the linking error "undefined reference
173 * to `SDL_main'". https://wiki.libsdl.org/FAQWindows
174 **/
175 int main(int argc, char* argv[])
176 {
177 using namespace OrthancStone;
178 StoneInitialize();
179 Orthanc::Logging::EnableInfoLevel(true);
180
181 try
182 {
183 SdlOpenGLViewport viewport("Hello", 1024, 768);
184 MessageBroker broker;
185 boost::shared_ptr<UndoStack> undoStack(new UndoStack);
186 boost::shared_ptr<ViewportController> controller = boost::make_shared<ViewportController>(undoStack, boost::ref(broker), boost::ref(viewport));
187 interactor.reset(new BasicScene2DInteractor(controller));
188 PrepareScene(controller->GetScene());
189 Run(controller);
190 }
191 catch (Orthanc::OrthancException& e)
192 {
193 LOG(ERROR) << "EXCEPTION: " << e.What();
194 }
195
196 StoneFinalize();
197
198 return 0;
199 }