flat assembler
Message board for the users of flat assembler.

Index > Examples and Tutorials > X-Input Gamepad Controller

Author
Thread Post new topic Reply to topic
m3ntal



Joined: 08 Dec 2013
Posts: 296
m3ntal
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:
Code:
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    


Description:
Filesize: 97.9 KB
Viewed: 2230 Time(s)

xinput.jpg


Description:
Download
Filename: gamepad.zip
Filesize: 251.19 KB
Downloaded: 183 Time(s)

Post 24 Dec 2013, 22:58
View user's profile Send private message Reply with quote
HaHaAnonymous



Joined: 02 Dec 2012
Posts: 1180
Location: Unknown
HaHaAnonymous
[ Post removed by author. ]


Last edited by HaHaAnonymous on 28 Feb 2015, 18:54; edited 1 time in total
Post 25 Dec 2013, 01:15
View user's profile Send private message Reply with quote
m3ntal



Joined: 08 Dec 2013
Posts: 296
m3ntal
It should be compatible with any X360 controller. You can get them used on E-Bay for $5-$10 (paid $6 used for an "afterglow controller").
Post 25 Dec 2013, 03:58
View user's profile Send private message Reply with quote
tthsqe



Joined: 20 May 2009
Posts: 721
tthsqe
Code:
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
}    

hey - I didn't know it was so easy to use the vibrator functionality of controller. Will try soon...
Post 29 Dec 2013, 21:21
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2019, Tomasz Grysztar.

Powered by rwasa.