X-Input Gamepad Controller

Requires X360 controller + M$ driver (search). Recognizes all buttons and directions. Features light-up buttons when pressed, vibrations and realistic moveable analog/thumb sticks. Tested in 1366x768 resolution (standard). See INCLUDE\INPUT.INC. Preview:

structure GAMEPAD
 integer n
 ushort buttons
 ubyte trigger.l, trigger.r
 short thumb.lx, thumb.ly
 short thumb.rx, thumb.ry
ends ?gamepad

powers PAD.*,\
 UP, DOWN, LEFT, RIGHT, START, BACK,\
 LEFT.T, RIGHT.T, LEFT.S, RIGHT.S,\
 SKIP, SKIP2, A, B, X, Y

numeric GAMEPAD.*, 1, 2, 3, 4 ; 0-3

; example: if.gamepad UP ; after update.gamepad

macro if.gamepad x {
 movzx r0, word [gamepad.buttons]
 . r0&PAD.#x
 if true
}   

structure XVIBRATION
 ushort speed.l, speed.r
ends

XVIBRATION vibration

; example: vibrate.gamepad GAMEPAD.3, 7FFFh, 7FFFh

macro vibrate.gamepad n, l, r {
 ?if ~l eq
   . [vibration.speed.l]=l, [vibration.speed.r]=r
 ?else
   . [vibration.speed.l]=0FFFFh, [vibration.speed.r]=0FFFFh
 ?end ?if
 XInputSetState n, vibration
}

macro stop.vibration n { vibrate.gamepad n, 0, 0 } 

 set.folder image.folder
 try load.image.t gamepad.i, gamepad.file
 center.image gamepad.i
 copy.image gamepad.reflect.i, gamepad.i ; create
 mirror.image.v gamepad.reflect.i        ; reflection
 . r0=[gamepad.i.x], r1=[gamepad.i.y]
 . r1+[gamepad.i.h], r1+2
 move.image gamepad.reflect.i, r0, r1
 . r0=[gamepad.reflect.i.w]
 . r1=[gamepad.reflect.i.h]
 . r2=r1, r2/4, r1-r2
 size.image gamepad.reflect.i, r0, r1
 set.image.key gamepad.reflect.i
 set.image.alpha gamepad.reflect.i, 128 

 draw.image gamepad.i
 draw.image gamepad.reflect.i

 update.gamepad

 if.gamepad A
   draw.image a.button.i
   vibrate.gamepad GAMEPAD.1
 end

 if.gamepad B
   draw.image b.button.i
   vibrate.gamepad GAMEPAD.1
 end         

; center thumbs: left and right

 . r0=[gamepad.i.x], r0+50
 . r1=[gamepad.i.y], r1+47
 move.image thumb1.button.i, r0, r1
 . r0=[gamepad.i.x], r0+208
 . r1=[gamepad.i.y], r1+108
 move.image thumb2.button.i, r0, r1
 . n=10

 movsx r2, word [gamepad.thumb.lx]
 movsx r3, word [gamepad.thumb.ly]
 . x=r2, y=r3

 ; reduce x/y to 1-16 for thumb sticks

 if x<-2048
   . r0=x, -r0, r0>>>11
   move.image.left thumb1.button.i, r0
 end
 if x>2048
   . r0=x, r0>>>11
   move.image.right thumb1.button.i, r0
 end
 if y<-2048
   . r0=y, -r0, r0>>>11
   move.image.down thumb1.button.i, r0
 end
 if y>2048
   . r0=y, r0>>>11
   move.image.up thumb1.button.i, r0
 end            
  
 if lx>0
   if lx<2048
     . lx=0
   end
 else.if lx<0
   if lx>-2048
     . lx=0
   end
 end
 if ly>0
   if ly<2048
     . ly=0
   end
 else.if ly<0
   if ly>-2048
     . ly=0
   end
 end
 if rx>0
   if rx<2048
     . rx=0
   end
 else.if rx<0
   if rx>-2048
     . rx=0
   end
 end
 if ry>0
   if ry<2048
     . ry=0
   end
 else.if ry<0
   if ry>-2048
     . ry=0
   end
 end