Skip to content

Commit dd2e1b1

Browse files
committed
Fix DirectInput controllers on game startup
Now SDL and DirectInput don't complain when a game starts with a DirectInput controller already connected. Fixes "JoypadSDL::process_events: Error opening gamepad at index 1: IDirectInputDevice8::SetCooperativeLevel() DirectX error 0x80070006"
1 parent 7cc3f37 commit dd2e1b1

File tree

3 files changed

+18
-18
lines changed

3 files changed

+18
-18
lines changed

drivers/sdl/joypad_sdl.cpp

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,19 @@ JoypadSDL::JoypadSDL() {
5858
singleton = this;
5959
}
6060

61+
#ifdef WINDOWS_ENABLED
62+
extern "C" {
63+
HWND SDL_HelperWindow;
64+
}
65+
66+
// Required for DInput joypads to work
67+
// TODO: remove this workaround when we update to newer version of SDL
68+
JoypadSDL::JoypadSDL(HWND p_helper_window) :
69+
JoypadSDL() {
70+
SDL_HelperWindow = p_helper_window;
71+
}
72+
#endif
73+
6174
JoypadSDL::~JoypadSDL() {
6275
// Process any remaining input events
6376
process_events();
@@ -247,17 +260,6 @@ void JoypadSDL::process_events() {
247260
}
248261
}
249262

250-
#ifdef WINDOWS_ENABLED
251-
extern "C" {
252-
HWND SDL_HelperWindow;
253-
}
254-
255-
// Required for DInput joypads to work
256-
void JoypadSDL::setup_sdl_helper_window(HWND p_hwnd) {
257-
SDL_HelperWindow = p_hwnd;
258-
}
259-
#endif
260-
261263
void JoypadSDL::close_joypad(int p_pad_idx) {
262264
int sdl_instance_idx = joypads[p_pad_idx].sdl_instance_idx;
263265

drivers/sdl/joypad_sdl.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@ typedef struct HWND__ *HWND;
3939
class JoypadSDL {
4040
public:
4141
JoypadSDL();
42+
#ifdef WINDOWS_ENABLED
43+
JoypadSDL(HWND p_helper_window);
44+
#endif
4245
~JoypadSDL();
4346

4447
static JoypadSDL *get_singleton();
4548

4649
Error initialize();
4750
void process_events();
48-
#ifdef WINDOWS_ENABLED
49-
void setup_sdl_helper_window(HWND p_hwnd);
50-
#endif
5151

5252
private:
5353
struct Joypad {

platform/windows/display_server_windows.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7188,10 +7188,8 @@ DisplayServerWindows::DisplayServerWindows(const String &p_rendering_driver, Win
71887188
}
71897189

71907190
#ifdef SDL_ENABLED
7191-
joypad_sdl = memnew(JoypadSDL());
7192-
if (joypad_sdl->initialize() == OK) {
7193-
joypad_sdl->setup_sdl_helper_window(windows[MAIN_WINDOW_ID].hWnd);
7194-
} else {
7191+
joypad_sdl = memnew(JoypadSDL(windows[MAIN_WINDOW_ID].hWnd));
7192+
if (joypad_sdl->initialize() != OK) {
71957193
ERR_PRINT("Couldn't initialize SDL joypad input driver.");
71967194
memdelete(joypad_sdl);
71977195
joypad_sdl = nullptr;

0 commit comments

Comments
 (0)