forked from projectM-visualizer/frontend-sdl-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRenderLoop.cpp
408 lines (326 loc) · 13.5 KB
/
RenderLoop.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
#include "RenderLoop.h"
#include "FPSLimiter.h"
#include "gui/ProjectMGUI.h"
#include <Poco/NotificationCenter.h>
#include <Poco/Util/Application.h>
#include <SDL2/SDL.h>
#include "ProjectMSDLApplication.h"
RenderLoop::RenderLoop()
: _audioCapture(Poco::Util::Application::instance().getSubsystem<AudioCapture>())
, _projectMWrapper(Poco::Util::Application::instance().getSubsystem<ProjectMWrapper>())
, _sdlRenderingWindow(Poco::Util::Application::instance().getSubsystem<SDLRenderingWindow>())
, _projectMHandle(_projectMWrapper.ProjectM())
, _playlistHandle(_projectMWrapper.Playlist())
, _projectMGui(Poco::Util::Application::instance().getSubsystem<ProjectMGUI>())
, _userConfig(ProjectMSDLApplication::instance().UserConfiguration())
{
}
void RenderLoop::Run()
{
FPSLimiter limiter;
auto& notificationCenter{Poco::NotificationCenter::defaultCenter()};
notificationCenter.addObserver(_quitNotificationObserver);
_projectMWrapper.DisplayInitialPreset();
while (!_wantsToQuit)
{
limiter.TargetFPS(_projectMWrapper.TargetFPS());
limiter.StartFrame();
PollEvents();
CheckViewportSize();
_audioCapture.FillBuffer();
_projectMWrapper.RenderFrame();
_projectMGui.Draw();
_sdlRenderingWindow.Swap();
limiter.EndFrame();
// Pass projectM the actual FPS value of the last frame.
_projectMWrapper.UpdateRealFPS(limiter.FPS());
}
notificationCenter.removeObserver(_quitNotificationObserver);
projectm_playlist_set_preset_switched_event_callback(_playlistHandle, nullptr, nullptr);
}
void RenderLoop::PollEvents()
{
SDL_Event event;
while (SDL_PollEvent(&event))
{
_projectMGui.ProcessInput(event);
switch (event.type)
{
case SDL_MOUSEWHEEL:
if (!_projectMGui.WantsMouseInput())
{
ScrollEvent(event.wheel);
}
break;
case SDL_KEYDOWN:
if (!_projectMGui.WantsKeyboardInput())
{
KeyEvent(event.key, true);
}
break;
case SDL_KEYUP:
if (!_projectMGui.WantsKeyboardInput())
{
KeyEvent(event.key, false);
}
break;
case SDL_MOUSEBUTTONDOWN:
if (!_projectMGui.WantsMouseInput())
{
MouseDownEvent(event.button);
}
break;
case SDL_MOUSEBUTTONUP:
if (!_projectMGui.WantsMouseInput())
{
MouseUpEvent(event.button);
}
break;
case SDL_DROPFILE: {
char* droppedFilePath = event.drop.file;
// first we want to get the config settings that are relevant ehre
// namely skipToDropped and droppedFolderOverride
// we can get them from the projectMWrapper, in the _projectMConfigView available on it
bool skipToDropped = _userConfig->getBool("projectM.skipToDropped", true);
bool droppedFolderOverride = _userConfig->getBool("projectM.droppedFolderOverride", false);
bool shuffle = projectm_playlist_get_shuffle(_playlistHandle);
if (shuffle && skipToDropped) {
// if shuffle is enabled, we disable it temporarily, so the dropped preset is played next
// if skipToDropped is false, we also keep shuffle enabled, as it doesn't matter since the current preset is unaffected
projectm_playlist_set_shuffle(_playlistHandle, false);
}
int index = projectm_playlist_get_position(_playlistHandle) + 1;
do {
Poco::File droppedFile(droppedFilePath);
if (!droppedFile.isDirectory()) {
// handle dropped preset file
Poco::Path droppedFileP(droppedFilePath);
if (!droppedFile.exists() || (droppedFileP.getExtension() != "milk" && droppedFileP.getExtension() != "prjm")) {
std::string toastMessage = std::string("Invalid preset file: ") + droppedFilePath;
Poco::NotificationCenter::defaultCenter().postNotification(new DisplayToastNotification(toastMessage));
poco_information_f1(_logger, "%s", toastMessage);
break; // exit the block and go to the shuffle check
}
if (projectm_playlist_insert_preset(_playlistHandle, droppedFilePath, index, true)) {
if(skipToDropped){
projectm_playlist_play_next(_playlistHandle, true);
}
poco_information_f1(_logger, "Added preset: %s", std::string(droppedFilePath));
// no need to toast single presets, as its obvious if a preset was loaded.
}
} else {
// handle dropped directory
// if droppedFolderOverride is enabled, we clear the playlist first
// current edge case: if the dropped directory is invalid or contains no presets, then it still clears the playlist
if (droppedFolderOverride) {
projectm_playlist_clear(_playlistHandle);
index = 0;
}
uint32_t addedFilesCount = projectm_playlist_insert_path(_playlistHandle, droppedFilePath, index, true, true);
if (addedFilesCount > 0) {
std::string toastMessage = "Added " + std::to_string(addedFilesCount) + " presets from " + droppedFilePath;
poco_information_f1(_logger, "%s", toastMessage);
if(skipToDropped || droppedFolderOverride){
// if skip to dropped is true, or if a folder was dropped and it overrode the playlist, we skip to the next preset
projectm_playlist_play_next(_playlistHandle, true);
}
Poco::NotificationCenter::defaultCenter().postNotification(new DisplayToastNotification(toastMessage));
}else{
std::string toastMessage = std::string("No presets found in: ") + droppedFilePath;
Poco::NotificationCenter::defaultCenter().postNotification(new DisplayToastNotification(toastMessage));
poco_information_f1(_logger, "%s", toastMessage);
}
}
} while (false);
if (shuffle && skipToDropped) {
projectm_playlist_set_shuffle(_playlistHandle, true);
}
SDL_free(droppedFilePath);
break;
}
case SDL_QUIT:
_wantsToQuit = true;
break;
}
}
}
void RenderLoop::CheckViewportSize()
{
int renderWidth;
int renderHeight;
_sdlRenderingWindow.GetDrawableSize(renderWidth, renderHeight);
if (renderWidth != _renderWidth || renderHeight != _renderHeight)
{
projectm_set_window_size(_projectMHandle, renderWidth, renderHeight);
_renderWidth = renderWidth;
_renderHeight = renderHeight;
_projectMGui.UpdateFontSize();
poco_debug_f2(_logger, "Resized rendering canvas to %?dx%?d.", renderWidth, renderHeight);
}
}
void RenderLoop::KeyEvent(const SDL_KeyboardEvent& event, bool down)
{
auto keyModifier{static_cast<SDL_Keymod>(event.keysym.mod)};
auto keyCode{event.keysym.sym};
bool modifierPressed{false};
if (keyModifier & KMOD_LGUI || keyModifier & KMOD_RGUI || keyModifier & KMOD_LCTRL)
{
modifierPressed = true;
}
// Handle modifier keys and save state for use in other methods, e.g. mouse events
switch (keyCode)
{
case SDLK_LCTRL:
case SDLK_RCTRL:
_keyStates._ctrlPressed = down;
break;
case SDLK_LSHIFT:
case SDLK_RSHIFT:
_keyStates._shiftPressed = down;
break;
case SDLK_LALT:
case SDLK_RALT:
_keyStates._altPressed = down;
break;
case SDLK_LGUI:
case SDLK_RGUI:
_keyStates._metaPressed = down;
break;
default:
break;
}
if (!down)
{
return;
}
switch (keyCode)
{
case SDLK_ESCAPE:
_projectMGui.Toggle();
_sdlRenderingWindow.ShowCursor(_projectMGui.Visible());
break;
case SDLK_a: {
bool aspectCorrectionEnabled = !projectm_get_aspect_correction(_projectMHandle);
projectm_set_aspect_correction(_projectMHandle, aspectCorrectionEnabled);
}
break;
#ifdef _DEBUG
case SDLK_d:
// Write next rendered frame to file
projectm_write_debug_image_on_next_frame(_projectMHandle, nullptr);
break;
#endif
case SDLK_f:
if (modifierPressed)
{
_sdlRenderingWindow.ToggleFullscreen();
}
break;
case SDLK_i:
if (modifierPressed)
{
_audioCapture.NextAudioDevice();
}
break;
case SDLK_m:
if (modifierPressed)
{
_sdlRenderingWindow.NextDisplay();
break;
}
break;
case SDLK_n:
Poco::NotificationCenter::defaultCenter().postNotification(new PlaybackControlNotification(PlaybackControlNotification::Action::NextPreset, _keyStates._shiftPressed));
break;
case SDLK_p:
Poco::NotificationCenter::defaultCenter().postNotification(new PlaybackControlNotification(PlaybackControlNotification::Action::PreviousPreset, _keyStates._shiftPressed));
break;
case SDLK_r: {
Poco::NotificationCenter::defaultCenter().postNotification(new PlaybackControlNotification(PlaybackControlNotification::Action::RandomPreset, _keyStates._shiftPressed));
break;
}
case SDLK_q:
if (modifierPressed)
{
_wantsToQuit = true;
}
break;
case SDLK_y:
Poco::NotificationCenter::defaultCenter().postNotification(new PlaybackControlNotification(PlaybackControlNotification::Action::ToggleShuffle));
break;
case SDLK_BACKSPACE:
Poco::NotificationCenter::defaultCenter().postNotification(new PlaybackControlNotification(PlaybackControlNotification::Action::LastPreset, _keyStates._shiftPressed));
break;
case SDLK_SPACE:
Poco::NotificationCenter::defaultCenter().postNotification(new PlaybackControlNotification(PlaybackControlNotification::Action::TogglePresetLocked));
break;
case SDLK_UP:
// Increase beat sensitivity
_projectMWrapper.ChangeBeatSensitivity(0.01f);
break;
case SDLK_DOWN:
// Decrease beat sensitivity
_projectMWrapper.ChangeBeatSensitivity(-0.01f);
break;
}
}
void RenderLoop::ScrollEvent(const SDL_MouseWheelEvent& event)
{
// Wheel up is positive
if (event.y > 0)
{
projectm_playlist_play_next(_playlistHandle, true);
}
// Wheel down is negative
else if (event.y < 0)
{
projectm_playlist_play_previous(_playlistHandle, true);
}
}
void RenderLoop::MouseDownEvent(const SDL_MouseButtonEvent& event)
{
if (_projectMGui.WantsMouseInput())
{
return;
}
switch (event.button)
{
case SDL_BUTTON_LEFT:
if (!_mouseDown && _keyStates._shiftPressed)
{
// ToDo: Improve this to differentiate between single click (add waveform) and drag (move waveform).
int x;
int y;
int width;
int height;
_sdlRenderingWindow.GetDrawableSize(width, height);
SDL_GetMouseState(&x, &y);
// Scale those coordinates. libProjectM uses a scale of 0..1 instead of absolute pixel coordinates.
float scaledX = (static_cast<float>(x) / static_cast<float>(width));
float scaledY = (static_cast<float>(height - y) / static_cast<float>(height));
// Add a new waveform.
projectm_touch(_projectMHandle, scaledX, scaledY, 0, PROJECTM_TOUCH_TYPE_RANDOM);
poco_debug_f2(_logger, "Added new random waveform at %?d,%?d", x, y);
_mouseDown = true;
}
break;
case SDL_BUTTON_RIGHT:
_sdlRenderingWindow.ToggleFullscreen();
break;
case SDL_BUTTON_MIDDLE:
projectm_touch_destroy_all(_projectMHandle);
poco_debug(_logger, "Cleared all custom waveforms.");
break;
}
}
void RenderLoop::MouseUpEvent(const SDL_MouseButtonEvent& event)
{
if (event.button == SDL_BUTTON_LEFT)
{
_mouseDown = false;
}
}
void RenderLoop::QuitNotificationHandler(const Poco::AutoPtr<QuitNotification>& notification)
{
_wantsToQuit = true;
}