Implement joystick, recognize background inputs

This commit is contained in:
MMaker 2025-08-23 23:17:44 -04:00
commit 0d8bf75228
Signed by: mmaker
GPG key ID: CCE79B8FEDA40FB2
2 changed files with 54 additions and 40 deletions

View file

@ -10,10 +10,19 @@ function Button:new(x, y, r, b)
end end
function Button:update(dt) function Button:update(dt)
if love.keyboard.isDown(self.b) then if self.b == "triggerleft" or self.b == "triggerright" then
local axis = joystick:getGamepadAxis(self.b)
if axis > 0.5 then
self.press = true self.press = true
else else
self.press = false self.press = false
end
else
if joystick:isGamepadDown(self.b) then
self.press = true
else
self.press = false
end
end end
end end

View file

@ -1,41 +1,52 @@
function love.load() function love.load()
-- Recognize background input
local ffi = require("ffi")
ffi.cdef[[
typedef enum
{
SDL_FALSE = 0,
SDL_TRUE = 1
} SDL_bool;
SDL_bool SDL_SetHint(const char *name, const char *value);
]]
local sdl = ffi.os == "Windows" and ffi.load("SDL2") or ffi.C
sdl.SDL_SetHint("SDL_JOYSTICK_ALLOW_BACKGROUND_EVENTS", "1")
local Button = require "button" local Button = require "button"
local joysticks = love.joystick.getJoysticks()
joystick = joysticks[1]
--movement keys --movement keys
movement = {} mL = Button(8, 27, 24, "dpleft")
mL = Button(8, 27, 24, "a") mD = Button(62, 27, 24, "dpdown")
mD = Button(62, 27, 24, "s") mR = Button(109, 53, 24, "dpright")
mR = Button(109, 53, 24, "d") mU = Button(127, 144, 30, "dpup")
mU = Button(127, 144, 30, "space") movement = {mL, mD, mR, mU}
--attack keys --attack keys
attack = {} bA = Button(159, 29, 24, "x")
bA = Button(159, 29, 24, "u") bB = Button(208, 8, 24, "y")
bB = Button(208, 8, 24, "i") bC = Button(262, 9, 24, "rightshoulder")
bC = Button(262, 9, 24, "o") bD = Button(317, 16, 24, "leftshoulder")
bD = Button(317, 16, 24, "p") bE = Button(155, 85, 24, "a")
bE = Button(155, 85, 24, "j") bF = Button(206, 64, 24, "b")
bF = Button(206, 64, 24, "k") bG = Button(262, 64, 24, "triggerright")
bG = Button(262, 64, 24, "l") bH = Button(315,72, 24, "triggerleft")
bH = Button(315,72, 24, ";") attack = {bA, bB, bC, bD, bE, bF, bG, bH}
end end
function love.update(dt) function love.update(dt)
--movement keys --movement keys
mL:update(dt) for _, button in ipairs(movement) do
mD:update(dt) button:update(dt)
mR:update(dt) end
mU:update(dt)
--attack keys --attack keys
bA:update(dt) for _, button in ipairs(attack) do
bB:update(dt) button:update(dt)
bC:update(dt) end
bD:update(dt)
bE:update(dt)
bF:update(dt)
bG:update(dt)
bH:update(dt)
end end
function love.draw() function love.draw()
@ -43,18 +54,12 @@ function love.draw()
love.graphics.setBackgroundColor(1, 179/255, 102/255) love.graphics.setBackgroundColor(1, 179/255, 102/255)
--movement keys --movement keys
mL:draw() for _, button in ipairs(movement) do
mD:draw() button:draw()
mR:draw() end
mU:draw()
--attack keys --attack keys
bA:draw() for _, button in ipairs(attack) do
bB:draw() button:draw()
bC:draw() end
bD:draw()
bE:draw()
bF:draw()
bG:draw()
bH:draw()
end end