mirror of
				https://git.zaroz.cloud/nintendo-back-up/yuzu/yuzu-mainline.git
				synced 2025-03-21 01:53:15 +00:00 
			
		
		
		
	sdl_impl: Reduce allocations in GetButtonMappingForDevice()
These maps can be constexpr arrays of std::pair.
This commit is contained in:
		
							parent
							
								
									f2a680ca89
								
							
						
					
					
						commit
						69fa6b4906
					
				@ -3,6 +3,7 @@
 | 
				
			|||||||
// Refer to the license.txt file included.
 | 
					// Refer to the license.txt file included.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <algorithm>
 | 
					#include <algorithm>
 | 
				
			||||||
 | 
					#include <array>
 | 
				
			||||||
#include <atomic>
 | 
					#include <atomic>
 | 
				
			||||||
#include <cmath>
 | 
					#include <cmath>
 | 
				
			||||||
#include <functional>
 | 
					#include <functional>
 | 
				
			||||||
@ -670,11 +671,21 @@ Common::ParamPackage BuildParamPackageForAnalog(int port, const std::string& gui
 | 
				
			|||||||
} // Anonymous namespace
 | 
					} // Anonymous namespace
 | 
				
			||||||
 | 
					
 | 
				
			||||||
ButtonMapping SDLState::GetButtonMappingForDevice(const Common::ParamPackage& params) {
 | 
					ButtonMapping SDLState::GetButtonMappingForDevice(const Common::ParamPackage& params) {
 | 
				
			||||||
 | 
					    if (!params.Has("guid") || !params.Has("port")) {
 | 
				
			||||||
 | 
					        return {};
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    const auto joystick = GetSDLJoystickByGUID(params.Get("guid", ""), params.Get("port", 0));
 | 
				
			||||||
 | 
					    auto* controller = joystick->GetSDLGameController();
 | 
				
			||||||
 | 
					    if (controller == nullptr) {
 | 
				
			||||||
 | 
					        return {};
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // This list is missing ZL/ZR since those are not considered buttons in SDL GameController.
 | 
					    // This list is missing ZL/ZR since those are not considered buttons in SDL GameController.
 | 
				
			||||||
    // We will add those afterwards
 | 
					    // We will add those afterwards
 | 
				
			||||||
    // This list also excludes Screenshot since theres not really a mapping for that
 | 
					    // This list also excludes Screenshot since theres not really a mapping for that
 | 
				
			||||||
    std::unordered_map<Settings::NativeButton::Values, SDL_GameControllerButton>
 | 
					    using ButtonBindings =
 | 
				
			||||||
        switch_to_sdl_button = {
 | 
					        std::array<std::pair<Settings::NativeButton::Values, SDL_GameControllerButton>, 17>;
 | 
				
			||||||
 | 
					    static constexpr ButtonBindings switch_to_sdl_button{{
 | 
				
			||||||
        {Settings::NativeButton::A, SDL_CONTROLLER_BUTTON_B},
 | 
					        {Settings::NativeButton::A, SDL_CONTROLLER_BUTTON_B},
 | 
				
			||||||
        {Settings::NativeButton::B, SDL_CONTROLLER_BUTTON_A},
 | 
					        {Settings::NativeButton::B, SDL_CONTROLLER_BUTTON_A},
 | 
				
			||||||
        {Settings::NativeButton::X, SDL_CONTROLLER_BUTTON_Y},
 | 
					        {Settings::NativeButton::X, SDL_CONTROLLER_BUTTON_Y},
 | 
				
			||||||
@ -692,30 +703,25 @@ ButtonMapping SDLState::GetButtonMappingForDevice(const Common::ParamPackage& pa
 | 
				
			|||||||
        {Settings::NativeButton::SL, SDL_CONTROLLER_BUTTON_LEFTSHOULDER},
 | 
					        {Settings::NativeButton::SL, SDL_CONTROLLER_BUTTON_LEFTSHOULDER},
 | 
				
			||||||
        {Settings::NativeButton::SR, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER},
 | 
					        {Settings::NativeButton::SR, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER},
 | 
				
			||||||
        {Settings::NativeButton::Home, SDL_CONTROLLER_BUTTON_GUIDE},
 | 
					        {Settings::NativeButton::Home, SDL_CONTROLLER_BUTTON_GUIDE},
 | 
				
			||||||
        };
 | 
					    }};
 | 
				
			||||||
    if (!params.Has("guid") || !params.Has("port")) {
 | 
					
 | 
				
			||||||
        return {};
 | 
					    // Add the missing bindings for ZL/ZR
 | 
				
			||||||
    }
 | 
					    using ZBindings =
 | 
				
			||||||
    const auto joystick = GetSDLJoystickByGUID(params.Get("guid", ""), params.Get("port", 0));
 | 
					        std::array<std::pair<Settings::NativeButton::Values, SDL_GameControllerAxis>, 2>;
 | 
				
			||||||
    auto* controller = joystick->GetSDLGameController();
 | 
					    static constexpr ZBindings switch_to_sdl_axis{{
 | 
				
			||||||
    if (controller == nullptr) {
 | 
					        {Settings::NativeButton::ZL, SDL_CONTROLLER_AXIS_TRIGGERLEFT},
 | 
				
			||||||
        return {};
 | 
					        {Settings::NativeButton::ZR, SDL_CONTROLLER_AXIS_TRIGGERRIGHT},
 | 
				
			||||||
    }
 | 
					    }};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    ButtonMapping mapping;
 | 
				
			||||||
 | 
					    mapping.reserve(switch_to_sdl_button.size() + switch_to_sdl_axis.size());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    ButtonMapping mapping{};
 | 
					 | 
				
			||||||
    for (const auto& [switch_button, sdl_button] : switch_to_sdl_button) {
 | 
					    for (const auto& [switch_button, sdl_button] : switch_to_sdl_button) {
 | 
				
			||||||
        const auto& binding = SDL_GameControllerGetBindForButton(controller, sdl_button);
 | 
					        const auto& binding = SDL_GameControllerGetBindForButton(controller, sdl_button);
 | 
				
			||||||
        mapping.insert_or_assign(
 | 
					        mapping.insert_or_assign(
 | 
				
			||||||
            switch_button,
 | 
					            switch_button,
 | 
				
			||||||
            BuildParamPackageForBinding(joystick->GetPort(), joystick->GetGUID(), binding));
 | 
					            BuildParamPackageForBinding(joystick->GetPort(), joystick->GetGUID(), binding));
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					 | 
				
			||||||
    // Add the missing bindings for ZL/ZR
 | 
					 | 
				
			||||||
    std::unordered_map<Settings::NativeButton::Values, SDL_GameControllerAxis> switch_to_sdl_axis =
 | 
					 | 
				
			||||||
        {
 | 
					 | 
				
			||||||
            {Settings::NativeButton::ZL, SDL_CONTROLLER_AXIS_TRIGGERLEFT},
 | 
					 | 
				
			||||||
            {Settings::NativeButton::ZR, SDL_CONTROLLER_AXIS_TRIGGERRIGHT},
 | 
					 | 
				
			||||||
        };
 | 
					 | 
				
			||||||
    for (const auto& [switch_button, sdl_axis] : switch_to_sdl_axis) {
 | 
					    for (const auto& [switch_button, sdl_axis] : switch_to_sdl_axis) {
 | 
				
			||||||
        const auto& binding = SDL_GameControllerGetBindForAxis(controller, sdl_axis);
 | 
					        const auto& binding = SDL_GameControllerGetBindForAxis(controller, sdl_axis);
 | 
				
			||||||
        mapping.insert_or_assign(
 | 
					        mapping.insert_or_assign(
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user