-
Notifications
You must be signed in to change notification settings - Fork 35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding game controlers as input method #86
base: master
Are you sure you want to change the base?
Changes from 2 commits
1dd542a
ebb0f1c
0234033
6a77d4e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -196,7 +196,7 @@ void SDLRenderingWindow::NextDisplay() | |
|
||
void SDLRenderingWindow::CreateSDLWindow() | ||
{ | ||
SDL_InitSubSystem(SDL_INIT_VIDEO); | ||
SDL_InitSubSystem(SDL_INIT_VIDEO | SDL_INIT_GAMECONTROLLER); | ||
|
||
int width{_config->getInt("width", 800)}; | ||
int height{_config->getInt("height", 600)}; | ||
|
@@ -307,7 +307,7 @@ void SDLRenderingWindow::DestroySDLWindow() | |
SDL_DestroyWindow(_renderingWindow); | ||
_renderingWindow = nullptr; | ||
|
||
SDL_QuitSubSystem(SDL_INIT_VIDEO); | ||
SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_GAMECONTROLLER); | ||
} | ||
|
||
void SDLRenderingWindow::DumpOpenGLInfo() | ||
|
@@ -461,3 +461,21 @@ void SDLRenderingWindow::OnConfigurationPropertyRemoved(const std::string& key) | |
UpdateWindowTitle(); | ||
} | ||
} | ||
|
||
SDL_GameController* SDLRenderingWindow::FindController() { | ||
//Check for joysticks | ||
if( SDL_NumJoysticks() < 1 ) | ||
{ | ||
poco_debug(_logger, "No joysticks connected"); | ||
} | ||
|
||
//For simplicity, we’ll only be setting up and tracking a single | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you want, you could also add a setting and an option in the settings dialog to let the user select a specific controller. Then select the stored one automatically on startup, e.g. via its name or device path (which should map to the port it is connected to). If you're not into Dear ImGui, I can also add this stuff at a later time. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Didn't knew about Dear ImGui before I had a look at this project. Adding features to the GUI is a bit over my head at the moment (my available time is scrace). Actually I'm not even into C++ but this doesn't hinder me to tinker around :-) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, no problem! I've got the same time issue, but I'll try my best to spend a bit of it on projectM every week 😀 Thanks again for the contribution! |
||
//controller at a time | ||
for (int i = 0; i < SDL_NumJoysticks(); i++) { | ||
if (SDL_IsGameController(i)) { | ||
return SDL_GameControllerOpen(i); | ||
} | ||
} | ||
|
||
return nullptr; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally, store the controller pointer in the SDLRenderingWindow class, call the
FindController()
method when initializing the subsystem and destroy it if non-zero when uninitializing the subsystem, and move the controller add/remove methods there to keep all the SDL-related resource management in one class, so it better follows a RAII pattern. The actual event handling can still take place in the RenderLoop class. Since the event handler retrieves the controller handle, there's not even a need to add a getter for the pointer, and it can be kept private in the SDLRenderingWindow class just to keep track of the used resource.