From 0fa42e5bd7173e734a365a58a774a78981cc396c Mon Sep 17 00:00:00 2001 From: Arne Schwettmann Date: Sun, 21 Nov 2021 23:36:04 -0600 Subject: [PATCH] fix gamepad controls and screen scaling (fixes Switch) --- src/Draw.lua | 4 +- src/Input.lua | 111 ++++++++++++++++++++++++++++++++------------------ src/Load.lua | 17 +++++++- 3 files changed, 89 insertions(+), 43 deletions(-) diff --git a/src/Draw.lua b/src/Draw.lua index 1ed98ce..81e6640 100755 --- a/src/Draw.lua +++ b/src/Draw.lua @@ -203,7 +203,7 @@ function drawTitleScreen() love.graphics.printf("c - Configure controls", 0, centerY-7-16+75,width,"center") love.graphics.printf("f - Toggle fullscreen", 0, centerY-7+75,width,"center") love.graphics.printf("t - Toggle scaling (fullscreen only)", 0, centerY-7+16+75,width,"center") - love.graphics.printf("q - Quit", 0, centerY-7+32+75,width,"center") + love.graphics.printf("q or Select - Quit", 0, centerY-7+32+75,width,"center") end function drawControlScreen() @@ -293,7 +293,7 @@ function drawGameOverScreen() love.graphics.setColor(1,1,1,1) love.graphics.rectangle("fill", floor(centerX-100), floor(centerY+75-40), 200, 80 ) love.graphics.setColor(0,0,0,1) - love.graphics.printf("Press q to restart", 0, centerY-5+75,width,"center") + love.graphics.printf("Press Q to restart", 0, centerY-5+75,width,"center") end function drawProgressBar(x,y,width,height,percent) diff --git a/src/Input.lua b/src/Input.lua index 72b5df7..0fa0bdb 100755 --- a/src/Input.lua +++ b/src/Input.lua @@ -87,39 +87,59 @@ function getInputPlayer(playerNumber) local joyXInput,joyYInput = 0,0 if ( joystickNum <= numJoysticks ) then local joystick = joysticks[joystickNum] - local numButtons = joystick:getButtonCount() - if joystick:getAxisCount()>=1 then - joyXInput,joyYInput = joystick:getAxes() - io.write(joyXInput) - end - if joystick:getHatCount()>=1 then - local hatDirection = joystick:getHat(1) - if hatDirection == 'u' then - joyYInput = -1 - joyXInput = 0 - elseif hatDirection == 'd' then - joyYInput = 1 - elseif hatDirection == 'l' then - joyXInput = -1 - elseif hatDirection == 'r' then - joyXInput = 1 - elseif hatDirection == 'ld' then - joyXInput = -1 - joyYInput = 1 - elseif hatDirection == 'lu' then - joyXInput = -1 - joyYInput = -1 - elseif hatDirection == 'rd' then - joyXInput = 1 - joyYInput = 1 - elseif hatDirection == 'ru' then - joyXInput = 1 - joyYInput = -1 - end - end - if numButtons>0 then - if anyButtonPressed(joystick, numButtons) then - buttonInput=true + if (joystick:isGamepad()) then + joyXInput = joystick:getGamepadAxis("leftx") + joyYInput = joystick:getGamepadAxis("lefty") + if joystick:isGamepadDown("dpup") then + joyYInput = -1 + elseif joystick:isGamepadDown("dpdown") then + joyYInput = 1 + end + if joystick:isGamepadDown("dpleft") then + joyXInput = -1 + elseif joystick:isGamepadDown("dpright") then + joyXInput = 1 + end + if joystick:isGamepadDown("a", "b", "x", "y") then + buttonInput = true + end + if joystick:isGamepadDown("back") then + love.keypressed("q") + end + else + local numButtons = joystick:getButtonCount() + if joystick:getAxisCount()>=1 then + joyXInput,joyYInput = joystick:getAxes() + end + if joystick:getHatCount()>=1 then + local hatDirection = joystick:getHat(1) + if hatDirection == 'u' then + joyYInput = -1 + joyXInput = 0 + elseif hatDirection == 'd' then + joyYInput = 1 + elseif hatDirection == 'l' then + joyXInput = -1 + elseif hatDirection == 'r' then + joyXInput = 1 + elseif hatDirection == 'ld' then + joyXInput = -1 + joyYInput = 1 + elseif hatDirection == 'lu' then + joyXInput = -1 + joyYInput = -1 + elseif hatDirection == 'rd' then + joyXInput = 1 + joyYInput = 1 + elseif hatDirection == 'ru' then + joyXInput = 1 + joyYInput = -1 + end + end + if numButtons>0 then + if anyButtonPressed(joystick, numButtons) then + buttonInput=true + end end end if joyXInput > 0.25 then @@ -156,18 +176,31 @@ end function love.gamepadpressed(joystick, button) -- press fire on title screen to start single player game with joystick controls if (displayingTitleScreen - and button ~= "dpup" - and button ~= "dpdown" - and button ~= "dpleft" - and button ~= "dpright") then + and button ~= "dpup" + and button ~= "dpdown" + and button ~= "dpleft" + and button ~= "dpright") then + if (button == "back") then + love.keypressed('q') + end for i=1,#joysticks,1 do if joysticks[i]:getID()==joystick:getID() then - -- set first player to the joystick that was pressed and start game - controls[1]=i+2 numPlayers=1 + for j=1,5,1 do + if controls[j]==i+2 then + numPlayers=j + end + end startGame() end end + elseif (gameLost + and button ~= "dpup" + and button ~= "dpdown" + and button ~= "dpleft" + and button ~= "dpright" + and button ~= "back") then + titleScreen() end end diff --git a/src/Load.lua b/src/Load.lua index 465f1c3..2b9c728 100755 --- a/src/Load.lua +++ b/src/Load.lua @@ -182,9 +182,9 @@ function love.load() fsaa=flags.msaa initializeFullscreenMode() love.graphics.setDefaultFilter("nearest","nearest") - if fullscreen then + -- if fullscreen then toggleScaling() - end + -- end --canvas1X=love.graphics.newCanvas(width,height) --canvas1X:setFilter("nearest","nearest") @@ -249,6 +249,19 @@ function love.load() joysticks = love.joystick.getJoysticks() numJoysticks = love.joystick.getJoystickCount(); + if numJoysticks == 0 then + controls = {1,2,3,4,5} + elseif numJoysticks == 1 then + controls = {3,1,2,4,5} + elseif numJoysticks == 2 then + controls = {3,4,1,2,5} + elseif numJoysticks == 3 then + controls = {3,4,5,1,2} + elseif numJoysticks == 4 then + controls = {3,4,5,6,1} + elseif numJoysticks >= 5 then + controls = {3,4,5,6,7} + end --pauseGame() titleScreen() end