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

@ -1,41 +1,52 @@
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 joysticks = love.joystick.getJoysticks()
joystick = joysticks[1]
--movement keys
movement = {}
mL = Button(8, 27, 24, "a")
mD = Button(62, 27, 24, "s")
mR = Button(109, 53, 24, "d")
mU = Button(127, 144, 30, "space")
mL = Button(8, 27, 24, "dpleft")
mD = Button(62, 27, 24, "dpdown")
mR = Button(109, 53, 24, "dpright")
mU = Button(127, 144, 30, "dpup")
movement = {mL, mD, mR, mU}
--attack keys
attack = {}
bA = Button(159, 29, 24, "u")
bB = Button(208, 8, 24, "i")
bC = Button(262, 9, 24, "o")
bD = Button(317, 16, 24, "p")
bE = Button(155, 85, 24, "j")
bF = Button(206, 64, 24, "k")
bG = Button(262, 64, 24, "l")
bH = Button(315,72, 24, ";")
bA = Button(159, 29, 24, "x")
bB = Button(208, 8, 24, "y")
bC = Button(262, 9, 24, "rightshoulder")
bD = Button(317, 16, 24, "leftshoulder")
bE = Button(155, 85, 24, "a")
bF = Button(206, 64, 24, "b")
bG = Button(262, 64, 24, "triggerright")
bH = Button(315,72, 24, "triggerleft")
attack = {bA, bB, bC, bD, bE, bF, bG, bH}
end
function love.update(dt)
--movement keys
mL:update(dt)
mD:update(dt)
mR:update(dt)
mU:update(dt)
for _, button in ipairs(movement) do
button:update(dt)
end
--attack keys
bA:update(dt)
bB:update(dt)
bC:update(dt)
bD:update(dt)
bE:update(dt)
bF:update(dt)
bG:update(dt)
bH:update(dt)
for _, button in ipairs(attack) do
button:update(dt)
end
end
function love.draw()
@ -43,18 +54,12 @@ function love.draw()
love.graphics.setBackgroundColor(1, 179/255, 102/255)
--movement keys
mL:draw()
mD:draw()
mR:draw()
mU:draw()
for _, button in ipairs(movement) do
button:draw()
end
--attack keys
bA:draw()
bB:draw()
bC:draw()
bD:draw()
bE:draw()
bF:draw()
bG:draw()
bH:draw()
for _, button in ipairs(attack) do
button:draw()
end
end