From 5f4cfacf8a090e706b6a1593a46d0803e7daa465 Mon Sep 17 00:00:00 2001 From: snippet <874740+musshorn@users.noreply.github.com> Date: Tue, 14 Oct 2025 20:55:49 +1100 Subject: [PATCH] Properly initialize variables + fix changes.txt --- changes.txt | 4 ++-- src/modules/joystick/sdl/Joystick.cpp | 10 ++++++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/changes.txt b/changes.txt index 83de36da2..782c165e3 100644 --- a/changes.txt +++ b/changes.txt @@ -29,8 +29,8 @@ Released: N/A * Added Joystick:hasSensor. * Added Joystick:isSensorEnabled and Joystick:setSensorEnabled. * Added Joystick:getSensorData. -* Added Joystick:getDeviceBatteryPercent and Joystick:getDevicePowerState, these expose how charged batteries in a controller are and if they're charging -* Added Joystick:getConnectionState which indicates if a joystick is connected wired or wireless +* Added Joystick:getPowerInfo which exposes infomation about batteries in a controller are and if they're charging. +* Added Joystick:getConnectionState which indicates if a joystick is connected wired or wireless. * Added new Gamepad API buttons: "misc1", "paddle1", "paddle2", "paddle3", "paddle4". and "touchpad". * Added World:getFixturesInArea(). * Added support for saving .exr image files via ImageData:encode. diff --git a/src/modules/joystick/sdl/Joystick.cpp b/src/modules/joystick/sdl/Joystick.cpp index e0d8a0bd2..efceccca8 100644 --- a/src/modules/joystick/sdl/Joystick.cpp +++ b/src/modules/joystick/sdl/Joystick.cpp @@ -465,9 +465,12 @@ void Joystick::getDeviceInfo(int &vendorID, int &productID, int &productVersion) Joystick::PowerType Joystick::getPowerInfo(int& batteryPercent) const { // Gets the battery state of a joystick/gamepad - Joystick::PowerType powerState; + Joystick::PowerType powerState = Joystick::PowerType::POWER_UNKNOWN; SDL_PowerState batteryState; + if (!isConnected()) + return powerState; + batteryState = SDL_GetJoystickPowerInfo(joyhandle, &batteryPercent); getConstant(batteryState, powerState); @@ -477,9 +480,12 @@ Joystick::PowerType Joystick::getPowerInfo(int& batteryPercent) const Joystick::ConnectionType Joystick::getConnectionState() const { // Gets the connection state of a joystick/gamepad (wired / wireless etc) - Joystick::ConnectionType connectionState; + Joystick::ConnectionType connectionState = Joystick::ConnectionType::CONNECTION_UNKNOWN; SDL_JoystickConnectionState sdlConnectionState; + if (!isConnected()) + return connectionState; + sdlConnectionState = SDL_GetJoystickConnectionState(joyhandle); getConstant(sdlConnectionState, connectionState);