Skip to content

Commit 0234033

Browse files
committed
Implementing review: Move _controller pointer to SDLRenderingWindow class
1 parent ebb0f1c commit 0234033

File tree

4 files changed

+80
-43
lines changed

4 files changed

+80
-43
lines changed

src/RenderLoop.cpp

+5-29
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ void RenderLoop::Run()
3030

3131
_projectMWrapper.DisplayInitialPreset();
3232

33-
_controller = _sdlRenderingWindow.FindController();
34-
3533
while (!_wantsToQuit)
3634
{
3735
limiter.TargetFPS(_projectMWrapper.TargetFPS());
@@ -51,10 +49,6 @@ void RenderLoop::Run()
5149
_projectMWrapper.UpdateRealFPS(limiter.FPS());
5250
}
5351

54-
//Close game controller
55-
SDL_GameControllerClose(_controller);
56-
_controller = nullptr;
57-
5852
notificationCenter.removeObserver(_quitNotificationObserver);
5953

6054
projectm_playlist_set_preset_switched_event_callback(_playlistHandle, nullptr, nullptr);
@@ -110,12 +104,14 @@ void RenderLoop::PollEvents()
110104
break;
111105

112106
case SDL_CONTROLLERDEVICEADDED:
113-
ControllerAdd(event.cdevice.which);
107+
poco_debug(_logger, "Controller added event received");
108+
_sdlRenderingWindow.ControllerAdd(event.cdevice.which);
114109

115110
break;
116111

117112
case SDL_CONTROLLERDEVICEREMOVED:
118-
ControllerRemove(event.cdevice.which);
113+
poco_debug(_logger, "Controller remove event received");
114+
_sdlRenderingWindow.ControllerRemove(event.cdevice.which);
119115

120116
break;
121117

@@ -350,30 +346,10 @@ void RenderLoop::MouseUpEvent(const SDL_MouseButtonEvent& event)
350346
}
351347
}
352348

353-
void RenderLoop::ControllerAdd(const int id )
354-
{
355-
if (!_controller)
356-
{
357-
_controller = SDL_GameControllerOpen(id);
358-
}
359-
poco_debug(_logger, "Controller added!");
360-
}
361-
362-
void RenderLoop::ControllerRemove(const int id )
363-
{
364-
if (_controller && id == SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(_controller)))
365-
{
366-
SDL_GameControllerClose(_controller);
367-
_controller = _sdlRenderingWindow.FindController();
368-
}
369-
poco_debug(_logger, "Controller removed!");
370-
}
371-
372349
void RenderLoop::ControllerDownEvent(const SDL_Event& event)
373350
{
374-
if (!_controller || event.cdevice.which != SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(_controller)))
351+
if (!_sdlRenderingWindow.ControllerIsOurs(event.cdevice.which) )
375352
{
376-
poco_debug(_logger, "No controller initialized");
377353
return;
378354
}
379355

src/RenderLoop.h

-14
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,6 @@ class RenderLoop
6161
*/
6262
void MouseUpEvent(const SDL_MouseButtonEvent& event);
6363

64-
/**
65-
* @brief Handles SDL game controller add events (plugin in a new controller) events.
66-
* @param id The added controller id
67-
*/
68-
void ControllerAdd( int id );
69-
70-
/**
71-
* @brief Handles SDL game controller remove events.
72-
* @param id The removed controller id
73-
*/
74-
void ControllerRemove( int id );
75-
7664
/**
7765
* @brief Handles SDL game controller button down events.
7866
* @param event The controller button event
@@ -112,6 +100,4 @@ class RenderLoop
112100
ModifierKeyStates _keyStates; //!< Current "pressed" states of modifier keys
113101

114102
Poco::Logger& _logger{Poco::Logger::get("RenderLoop")}; //!< The class logger.
115-
116-
SDL_GameController *_controller;
117103
};

src/SDLRenderingWindow.cpp

+56
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ void SDLRenderingWindow::initialize(Poco::Util::Application& app)
3535
// Observe user configuration changes (set via the settings window)
3636
_userConfig->propertyChanged += Poco::delegate(this, &SDLRenderingWindow::OnConfigurationPropertyChanged);
3737
_userConfig->propertyRemoved += Poco::delegate(this, &SDLRenderingWindow::OnConfigurationPropertyRemoved);
38+
39+
_controller = FindController();
3840
}
3941

4042
void SDLRenderingWindow::uninitialize()
@@ -48,6 +50,12 @@ void SDLRenderingWindow::uninitialize()
4850
DestroySDLWindow();
4951
_renderingWindow = nullptr;
5052
}
53+
54+
if (_controller)
55+
{
56+
SDL_GameControllerClose(_controller);
57+
_controller = nullptr;
58+
}
5159
}
5260

5361
void SDLRenderingWindow::GetDrawableSize(int& width, int& height) const
@@ -467,15 +475,63 @@ SDL_GameController* SDLRenderingWindow::FindController() {
467475
if( SDL_NumJoysticks() < 1 )
468476
{
469477
poco_debug(_logger, "No joysticks connected");
478+
return nullptr;
470479
}
471480

472481
//For simplicity, we’ll only be setting up and tracking a single
473482
//controller at a time
474483
for (int i = 0; i < SDL_NumJoysticks(); i++) {
475484
if (SDL_IsGameController(i)) {
485+
poco_debug(_logger, "Adding first controller");
476486
return SDL_GameControllerOpen(i);
477487
}
488+
else {
489+
poco_debug(_logger, "Connected joystick is not a SDL game controller");
490+
}
478491
}
479492

480493
return nullptr;
481494
}
495+
496+
void SDLRenderingWindow::ControllerAdd(const int id )
497+
{
498+
if (!_controller)
499+
{
500+
if (SDL_IsGameController(id)) {
501+
_controller = SDL_GameControllerOpen(id);
502+
poco_debug(_logger, "Controller added!");
503+
}
504+
else {
505+
poco_debug(_logger, "Connected joystick is not a SDL game controller");
506+
}
507+
}
508+
}
509+
510+
void SDLRenderingWindow::ControllerRemove(const int id )
511+
{
512+
if (_controller && id == SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(_controller)))
513+
{
514+
SDL_GameControllerClose(_controller);
515+
poco_debug(_logger, "Controller removed!");
516+
_controller = FindController();
517+
}
518+
}
519+
520+
bool SDLRenderingWindow::ControllerIsOurs(const int id )
521+
{
522+
int instance = SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(_controller));
523+
524+
if (!_controller )
525+
{
526+
poco_debug(_logger, "No controller initialized");
527+
return false;
528+
}
529+
530+
if (id != instance)
531+
{
532+
poco_debug_f2(_logger, "Use controller %?d instead of %?d. Currently only one controller is supported", instance, id);
533+
return false;
534+
}
535+
536+
return true;
537+
}

src/SDLRenderingWindow.h

+19
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,24 @@ class SDLRenderingWindow : public Poco::Util::Subsystem
102102
*/
103103
SDL_GameController* FindController();
104104

105+
/**
106+
* @brief Handles SDL game controller add events (plugin in a new controller) events.
107+
* @param id The added controller id
108+
*/
109+
void ControllerAdd(const int id );
110+
111+
/**
112+
* @brief Handles SDL game controller remove events.
113+
* @param id The removed controller id
114+
*/
115+
void ControllerRemove(const int id );
116+
117+
/**
118+
* @brief Returns true if the given controller is initialized and the one we currently use.
119+
* @param id The removed controller id
120+
*/
121+
bool ControllerIsOurs(const int id );
122+
105123
protected:
106124

107125
/**
@@ -162,6 +180,7 @@ class SDLRenderingWindow : public Poco::Util::Subsystem
162180

163181
bool _fullscreen{ false };
164182

183+
SDL_GameController *_controller;
165184
};
166185

167186

0 commit comments

Comments
 (0)