-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathSDLRenderingWindow.cpp
537 lines (439 loc) · 14.7 KB
/
SDLRenderingWindow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
#include "SDLRenderingWindow.h"
#include "ProjectMSDLApplication.h"
#include "ProjectMWrapper.h"
#include <Poco/Delegate.h>
#include <Poco/NotificationCenter.h>
#include <Poco/Util/Application.h>
#ifdef USE_GLEW
#include <GL/glew.h>
#endif
#include <SDL2/SDL_opengl.h>
const char* SDLRenderingWindow::name() const
{
return "SDL2 Rendering Window";
}
void SDLRenderingWindow::initialize(Poco::Util::Application& app)
{
auto& projectMSDLApp = dynamic_cast<ProjectMSDLApplication&>(app);
_userConfig = projectMSDLApp.UserConfiguration();
_config = app.config().createView("window");
if (!_renderingWindow)
{
CreateSDLWindow();
}
Poco::NotificationCenter::defaultCenter().addObserver(_updateWindowTitleObserver);
// Observe user configuration changes (set via the settings window)
_userConfig->propertyChanged += Poco::delegate(this, &SDLRenderingWindow::OnConfigurationPropertyChanged);
_userConfig->propertyRemoved += Poco::delegate(this, &SDLRenderingWindow::OnConfigurationPropertyRemoved);
_controller = FindController();
}
void SDLRenderingWindow::uninitialize()
{
_userConfig->propertyRemoved -= Poco::delegate(this, &SDLRenderingWindow::OnConfigurationPropertyRemoved);
_userConfig->propertyChanged -= Poco::delegate(this, &SDLRenderingWindow::OnConfigurationPropertyChanged);
Poco::NotificationCenter::defaultCenter().removeObserver(_updateWindowTitleObserver);
if (_renderingWindow)
{
DestroySDLWindow();
_renderingWindow = nullptr;
}
if (_controller)
{
SDL_GameControllerClose(_controller);
_controller = nullptr;
}
}
void SDLRenderingWindow::GetDrawableSize(int& width, int& height) const
{
SDL_GL_GetDrawableSize(_renderingWindow, &width, &height);
}
void SDLRenderingWindow::Swap() const
{
SDL_GL_SwapWindow(_renderingWindow);
}
void SDLRenderingWindow::ToggleFullscreen()
{
if (_fullscreen)
{
Windowed();
}
else
{
Fullscreen();
}
}
void SDLRenderingWindow::Fullscreen()
{
SDL_GetWindowSize(_renderingWindow, &_lastWindowWidth, &_lastWindowHeight);
SDL_ShowCursor(false);
if (_config->getBool("fullscreen.exclusiveMode", false))
{
int fullscreenWidth = _config->getInt("fullscreen.width", 0);
int fullscreenHeight = _config->getInt("fullscreen.height", 0);
if (fullscreenWidth > 0 && fullscreenHeight > 0)
{
SDL_RestoreWindow(_renderingWindow);
SDL_SetWindowSize(_renderingWindow, fullscreenWidth, fullscreenHeight);
}
SDL_SetWindowFullscreen(_renderingWindow, SDL_WINDOW_FULLSCREEN);
if (_logger.debug())
{
int width{0};
int height{0};
SDL_GetWindowSize(_renderingWindow, &width, &height);
poco_debug_f2(_logger, "Entered exclusive fullscreen mode with actual resolution %dx%d",
width, height);
}
}
else
{
SDL_SetWindowFullscreen(_renderingWindow, SDL_WINDOW_FULLSCREEN_DESKTOP);
if (_logger.debug())
{
int width{0};
int height{0};
SDL_GetWindowSize(_renderingWindow, &width, &height);
poco_debug_f2(_logger, "Entered fake fullscreen mode with resolution %dx%d",
width, height);
}
}
_fullscreen = true;
}
void SDLRenderingWindow::Windowed()
{
SDL_SetWindowFullscreen(_renderingWindow, 0);
SDL_SetWindowBordered(_renderingWindow, _config->getBool("borderless", false) ? SDL_FALSE : SDL_TRUE);
if (_lastWindowWidth > 0 && _lastWindowHeight > 0)
{
SDL_SetWindowSize(_renderingWindow, _lastWindowWidth, _lastWindowHeight);
SDL_ShowCursor(true);
poco_debug_f2(_logger, "Entered windowed mode with size %dx%d",
_lastWindowWidth, _lastWindowHeight);
}
_fullscreen = false;
}
void SDLRenderingWindow::ShowCursor(bool visible)
{
SDL_ShowCursor(visible);
}
void SDLRenderingWindow::NextDisplay()
{
auto numDisplays = SDL_GetNumVideoDisplays();
if (numDisplays < 2)
{
poco_debug(_logger, "Cannot move the window anywhere.");
return;
}
bool wasFullscreen{_fullscreen};
if (_fullscreen)
{
poco_debug(_logger, "Leaving fullscreen before moving.");
Windowed();
}
// Find current display
auto currentDisplay = GetCurrentDisplay();
int left;
int top;
SDL_GetWindowPosition(_renderingWindow, &left, &top);
int newDisplay = (currentDisplay + 1) % numDisplays;
poco_debug_f1(_logger, "Moving window to display %?d.", newDisplay);
if (newDisplay != currentDisplay)
{
SDL_Rect oldBounds{};
SDL_Rect newBounds;
int leftOffset{0};
int topOffset{0};
SDL_GetDisplayBounds(newDisplay, &newBounds);
if (currentDisplay >= 0)
{
SDL_GetDisplayBounds(currentDisplay, &oldBounds);
leftOffset = left - oldBounds.x;
topOffset = top - oldBounds.y;
}
int newLeft = newBounds.x + leftOffset;
int newTop = newBounds.y + topOffset;
SDL_SetWindowPosition(_renderingWindow, newLeft, newTop);
poco_debug_f4(_logger, "Old position: X=%?d Y=%?d, new position: X=%?d Y=%?d.",
left, top, newLeft, newTop);
}
if (wasFullscreen)
{
poco_debug(_logger, "Was in fullscreen before moving.");
Fullscreen();
}
}
void SDLRenderingWindow::CreateSDLWindow()
{
SDL_InitSubSystem(SDL_INIT_VIDEO | SDL_INIT_GAMECONTROLLER);
int width{_config->getInt("width", 800)};
int height{_config->getInt("height", 600)};
int left{_config->getInt("left", 0)};
int top{_config->getInt("top", 0)};
bool positionOverridden = _config->getBool("overridePosition", false);
if (!positionOverridden)
{
left = SDL_WINDOWPOS_UNDEFINED;
top = SDL_WINDOWPOS_UNDEFINED;
}
auto display = _config->getInt("monitor", 0);
if (display > 0)
{
poco_debug_f1(_logger, "User requested to place window on monitor %?d.", display);
auto numDisplays = SDL_GetNumVideoDisplays();
if (display > numDisplays)
{
display = numDisplays;
}
SDL_Rect bounds;
SDL_GetDisplayBounds(display - 1, &bounds);
if (positionOverridden)
{
left += bounds.x;
top += bounds.y;
}
else
{
left = bounds.x;
top = bounds.y;
}
poco_debug_f3(_logger, "Creating window on monitor %?d at X=%?d Y=%?d.", display, left, top);
}
#if USE_GLES
// use GLES 2.0
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
#else
// projectM Requires at least Core Profile 3.30 for samplers etc.
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
#endif
_renderingWindow = SDL_CreateWindow("projectM", left, top, width, height,
SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);
if (!_renderingWindow)
{
auto errorMessage = "Could not create SDL rendering window. Error: " + std::string(SDL_GetError());
poco_fatal(_logger, errorMessage);
throw Poco::Exception(errorMessage);
}
_glContext = SDL_GL_CreateContext(_renderingWindow);
if (!_glContext)
{
auto errorMessage = "Could not create OpenGL rendering context. Error: " + std::string(SDL_GetError());
poco_fatal(_logger, errorMessage);
throw Poco::Exception(errorMessage);
}
SDL_SetWindowTitle(_renderingWindow, "projectM");
SDL_GL_MakeCurrent(_renderingWindow, _glContext);
UpdateSwapInterval();
#ifdef USE_GLEW
auto glewError = glewInit();
if (glewError != GLEW_OK)
{
auto errorMessage = "Could not initialize GLEW. Error: " + std::string(reinterpret_cast<const char*>(glewGetErrorString(glewError)));
poco_fatal(_logger, errorMessage);
throw Poco::Exception(errorMessage);
}
poco_debug_f1(_logger, "Initialized GLEW: %s", std::string(reinterpret_cast<const char*>(glewGetString(GLEW_VERSION))));
#endif
if (_config->getBool("fullscreen", false))
{
Fullscreen();
}
else
{
Windowed();
}
if (_logger.debug())
{
DumpOpenGLInfo();
}
}
void SDLRenderingWindow::DestroySDLWindow()
{
poco_debug(_logger, "Closing rendering window and destroying OpenGL context.");
SDL_GL_DeleteContext(_glContext);
_glContext = nullptr;
SDL_DestroyWindow(_renderingWindow);
_renderingWindow = nullptr;
SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_GAMECONTROLLER);
}
void SDLRenderingWindow::DumpOpenGLInfo()
{
poco_debug_f1(_logger, "- GL_VERSION: %s", std::string(reinterpret_cast<const char*>(glGetString(GL_VERSION))));
poco_debug_f1(_logger, "- GL_SHADING_LANGUAGE_VERSION: %s",
std::string(reinterpret_cast<const char*>(glGetString(GL_SHADING_LANGUAGE_VERSION))));
poco_debug_f1(_logger, "- GL_VENDOR: %s", std::string(reinterpret_cast<const char*>(glGetString(GL_VENDOR))));
}
int SDLRenderingWindow::GetCurrentDisplay()
{
int left;
int top;
SDL_GetWindowPosition(_renderingWindow, &left, &top);
auto numDisplays = SDL_GetNumVideoDisplays();
poco_debug_f1(_logger, "Displays available: %?d.", numDisplays);
poco_debug_f2(_logger, "Window position is X=%?d Y=%?d", left, top);
// Find current display
int currentDisplay{-1};
for (int display = 0; display < numDisplays; display++)
{
SDL_Rect bounds;
SDL_GetDisplayBounds(display, &bounds);
poco_debug_f1(_logger, "Bounds for display %?d:", display);
poco_debug_f4(_logger, " X=%?d Y=%?d W=%?d H=%?d", bounds.x, bounds.y, bounds.w, bounds.h);
// Need to add 1 to left and top, as some window managers will screw up here.
if (left + 1 >= bounds.x && left + 1 < bounds.x + bounds.w && top + 1 >= bounds.y && top + 1 < bounds.y + bounds.h)
{
poco_debug_f1(_logger, "Window is currently on display %?d.", display);
currentDisplay = display;
break;
}
}
return currentDisplay;
}
void SDLRenderingWindow::GetWindowSize(int& width, int& height)
{
SDL_GetWindowSize(_renderingWindow, &width, &height);
}
void SDLRenderingWindow::GetWindowPosition(int& left, int& top, bool relative)
{
SDL_GetWindowPosition(_renderingWindow, &left, &top);
if (!relative)
{
return;
}
SDL_Rect bounds;
SDL_GetDisplayBounds(GetCurrentDisplay(), &bounds);
left -= bounds.x;
top -= bounds.y;
}
SDL_Window* SDLRenderingWindow::GetRenderingWindow() const
{
return _renderingWindow;
}
SDL_GLContext SDLRenderingWindow::GetGlContext() const
{
return _glContext;
}
void SDLRenderingWindow::UpdateWindowTitleNotificationHandler(POCO_UNUSED const Poco::AutoPtr<UpdateWindowTitleNotification>& notification)
{
UpdateWindowTitle();
}
void SDLRenderingWindow::UpdateWindowTitle()
{
std::string newTitle = "projectM";
if (_config->getBool("displayPresetNameInTitle", true))
{
auto& app = Poco::Util::Application::instance();
auto& projectMWrapper = app.getSubsystem<ProjectMWrapper>();
auto presetName = projectm_playlist_item(projectMWrapper.Playlist(), projectm_playlist_get_position(projectMWrapper.Playlist()));
if (presetName)
{
Poco::Path presetFile(presetName);
projectm_playlist_free_string(presetName);
newTitle += " ➫ " + presetFile.getBaseName();
}
if (projectm_get_preset_locked(projectMWrapper.ProjectM()))
{
newTitle += " [locked]";
}
}
SDL_SetWindowTitle(_renderingWindow, newTitle.c_str());
}
void SDLRenderingWindow::UpdateSwapInterval()
{
if (!_config->getBool("waitForVerticalSync", true))
{
SDL_GL_SetSwapInterval(0);
return;
}
if (_config->getBool("adaptiveVerticalSync", true))
{
if (SDL_GL_SetSwapInterval(-1) == 0)
{
return;
}
}
SDL_GL_SetSwapInterval(1);
}
void SDLRenderingWindow::OnConfigurationPropertyChanged(const Poco::Util::AbstractConfiguration::KeyValue& property)
{
OnConfigurationPropertyRemoved(property.key());
}
void SDLRenderingWindow::OnConfigurationPropertyRemoved(const std::string& key)
{
if (!_renderingWindow)
{
return;
}
if (key == "window.waitForVerticalSync" || key == "window.adaptiveVerticalSync")
{
UpdateSwapInterval();
}
if (key == "window.borderless")
{
SDL_SetWindowBordered(_renderingWindow, _config->getBool("borderless", false) ? SDL_FALSE : SDL_TRUE);
}
if (key == "window.displayPresetNameInTitle")
{
UpdateWindowTitle();
}
}
SDL_GameController* SDLRenderingWindow::FindController() {
//Check for joysticks
if( SDL_NumJoysticks() < 1 )
{
poco_debug(_logger, "No joysticks connected");
return nullptr;
}
//For simplicity, we’ll only be setting up and tracking a single
//controller at a time
for (int i = 0; i < SDL_NumJoysticks(); i++) {
if (SDL_IsGameController(i)) {
poco_debug(_logger, "Adding first controller");
return SDL_GameControllerOpen(i);
}
else {
poco_debug(_logger, "Connected joystick is not a SDL game controller");
}
}
return nullptr;
}
void SDLRenderingWindow::ControllerAdd(const int id )
{
if (!_controller)
{
if (SDL_IsGameController(id)) {
_controller = SDL_GameControllerOpen(id);
poco_debug(_logger, "Controller added!");
}
else {
poco_debug(_logger, "Connected joystick is not a SDL game controller");
}
}
}
void SDLRenderingWindow::ControllerRemove(const int id )
{
if (_controller && id == SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(_controller)))
{
SDL_GameControllerClose(_controller);
poco_debug(_logger, "Controller removed!");
_controller = FindController();
}
}
bool SDLRenderingWindow::ControllerIsOurs(const int id )
{
int instance = SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(_controller));
if (!_controller )
{
poco_debug(_logger, "No controller initialized");
return false;
}
if (id != instance)
{
poco_debug_f2(_logger, "Use controller %?d instead of %?d. Currently only one controller is supported", instance, id);
return false;
}
return true;
}