Jump to content

Basic Gamepad Control


Marleys Ghost
 Share

Recommended Posts

Basic Gamepad Control

 

If you have used the Simple Scene Loader with Player Controller in Blitzmax, this is a small addition to it that will allow the use of a gamepad by implementing FreeJoy.

 

This example is based on the code I used to enable the use of my Xbox 360 controllers using the Xbox 360 Wireless Receiver for Windows, but should be simple to alter for use with other gamepads.

 

The additions to the Simple Scene Loader code are enclosed in dashed lines.

 

The additions are annotated with help for settings.

 

I hope this will be of some use to someone.

 

SuperStrict

Framework leadwerks.engine

Import "PATH TO YOUR 2.3 SDK FOLDER\BMX\Framework\framework.bmx"

'------------------------------------------------------------------------
Import Pub.FreeJoy
'------------------------------------------------------------------------

Include "lua-gluefunctions.bmx"

AppTitle:String = "Basic Example : BMAX + Leadwerks Engine 2.3"
GCSetMode(2)

'Register the directory under which the abstract file system will search.
RegisterAbstractPath("PATH TO YOUR 2.3 SDK FOLDER")

'Create a Graphics window
Graphics(800, 600)


AFilter(1) 'Set the anisotropic filter. Range 1-16

'Using AFilter(MaxAFilter()) will use the maximum supported anisotropic filter on your PC
'Adding the line "DrawText ("MaxAFilter = " + MaxAFilter(), 0, 80)"
'after "fw.Render()" will display on screen the maximum supported anisotropic filter.

TFilter(1) 'Set trilinear filtering : 0 = off : 1 = on.


'Create a new Framework object.
Global fw:TFramework = TFramework.Create()
If Not fw RuntimeError "Failed to initialize engine."

       SetScriptObject("fw", fw)'Setup the global script objects.
       SetPostFX()'Setup the post processing FX
       SetVegetationShadowMode(0)'Setup vegetation layer shadows : 0 = off : 1 = on.

'Load the scene. 
       Global scene:TEntity = LoadScene("abstract::basic.sbx")

'Setup the player controller.
       Global player:TController = CreateController(2.0)
       PositionEntity(player, StringToVec3(scene.getkey("cameraposition")))
       EntityType(player, 3) 
       SetBodyMass(player, 50.0)
       SetBodyBuoyancyMode(player,0)
       Collisions(1, 3, 3)

'Setup the player movement variables.
       Global move:Float = 0.0
       Global strafe:Float = 0.0
       Global camrotation:TVec3 = Vec3(0)
       Global mx:Float = 0.0
       Global my:Float = 0.0
       Global jump:Float = 0.0
       Global playerpos:TVec3 = Vec3(0)

       HideMouse()

'Set stats level : 0 = No statistics : 1 = Show frame rate : 2 = Full statistics. 
       fw.SetStats(2)

'Main program loop.
Repeat
       If KeyHit(KEY_ESCAPE) Exit
       If AppTerminate() Exit

       UpdateAppTime()
       UpdateWorld(AppSpeed())

       PlayerController()

       fw.Update()
       fw.Render()

'------------------------------------------------------------------------
Gamepadstats()
'------------------------------------------------------------------------	

       Flip(0)
Forever
fw.renderer.gbuffer = Null
GCCollect()
End

'Functions.

Function SetPostFX()'0 = off : 1 = on.
       fw.renderer.SetAntialias(1)
       fw.renderer.SetBloom(1) 
       fw.renderer.SetGodRays(1)
       fw.renderer.SetHDR(1)
       fw.renderer.SetSSAO(0)
       fw.renderer.SetWireFrame(0)     
End Function

Function PlayerController()
       mx = Curve(MouseX() - GraphicsWidth() / 2, mx, 6)
       my = Curve(MouseY() - GraphicsHeight() / 2, my, 6)
       MoveMouse(GraphicsWidth() / 2, GraphicsHeight() / 2)

'------------------------------------------------------------------------
If JoyCount()>0
	Gamepadinput_rightthumbstick()
EndIf
'------------------------------------------------------------------------

       camrotation.X = camrotation.X + my / 10.0
       camrotation.Y = camrotation.Y - mx / 10.0
       RotateEntity(fw.Main.camera, camrotation)

       move = KeyDown(KEY_W) - KeyDown(KEY_S)
       strafe = KeyDown(KEY_D) - KeyDown(KEY_A)
       jump:Float = 0.0
       If KeyHit(KEY_SPACE) & Not ControllerAirborne(player)
               jump = 4.0
       End If
       If KeyDown(KEY_LSHIFT) | KeyDown(KEY_RSHIFT)
               move = move * 3.0
               strafe = strafe * 3.0
       End If
       playerpos = EntityPosition(player)
       playerpos.Y = playerpos.Y + 0.80
       PositionEntity(fw.Main.camera, playerpos)

'------------------------------------------------------------------------
If JoyCount()>0
   	Gamepadinput_leftthumbstick()
	Gamepadinput_buttons()
EndIf
'------------------------------------------------------------------------

       UpdateController(player, camrotation.Y, move * 3, strafe * 3, jump, 500, 10)
End Function

Function StringToVec2:TVec2(text:String, scale:Float = 1.0)
       Local t:TVec2 = Vec2(1)
       Local sarr:String[]
       sarr = text.split(",")
       If sarr
               If sarr.length > 0 t.x = Float(sarr[0]) * scale
               If sarr.length > 1 t.y = Float(sarr[1]) * scale
       EndIf
       Return t
EndFunction

Function StringToVec3:TVec3(text:String, scale:Float = 1.0)
       Local t:TVec3 = Vec3(1)
       Local sarr:String[]
       sarr = text.split(",")
       If sarr
               If sarr.length > 0 t.x = Float(sarr[0]) * scale
               If sarr.length > 1 t.y = Float(sarr[1]) * scale
               If sarr.length > 2 t.z = Float(sarr[2]) * scale
       EndIf
       Return t
EndFunction

Function StringToVec4:TVec4(text:String, scale:Float = 1.0)
       Local t:TVec4 = Vec4(1)
       Local sarr:String[]
       sarr = text.split(",")
       If sarr
               If sarr.length > 0 t.x = Float(sarr[0]) * scale
               If sarr.length > 1 t.y = Float(sarr[1]) * scale
               If sarr.length > 2 t.z = Float(sarr[2]) * scale
               If sarr.length > 3 t.w = Float(sarr[3]) * scale
       EndIf
       Return t
EndFunction

Function SetScriptObject(name:String, o:Object)
       Local size:Int=GetStackSize()
       lua_pushbmaxobject(luastate.L,o)
       lua_setglobal(luastate.L,name)
       SetStackSize(size)
EndFunction

Function GetStackSize:Int()
       Return lua_gettop(luastate.L)
EndFunction

Function SetStackSize(size:Int)
       Local currentsize:Int=GetStackSize()
       If size<currentsize
       lua_pop(luastate.L,currentsize-size)
       EndIf
EndFunction

'------------------------------------------------------------------------
Function Gamepadstats()
If JoyCount()>0

SetBlend(BLEND_ALPHA)

Local n:Int = JoyCount()
DrawText ("Number of Gamepads = "+n,0,80)
DrawText ("Gamepad Driver = "+JoyName(0),0,100)

DrawText ("Button A = "+JoyDown(0,0),0,120) 'Button 0
DrawText ("Button B = "+JoyDown(1,0),0,140) 'Button 1
DrawText ("Button X = "+JoyDown(2,0),0,160) 'Button 2
DrawText ("Button Y = "+JoyDown(3,0),0,180) 'Button 3
DrawText ("Button Left Shoulder = "+JoyDown(4,0),0,200) 'Button 4
DrawText ("Button Right Shoulder = "+JoyDown(5,0),0,220) 'Button 5
DrawText ("Back = "+JoyDown(6,0),0,240) 'Button 6
DrawText ("Start = "+JoyDown(7,0),0,260) 'Button 7
DrawText ("Left thumb click = "+JoyDown(8,0),0,280) 'Button 8
DrawText ("Right thumb click = "+JoyDown(9,0),0,300) 'Button 9

'D Pad Values : 0 = up : 0.25 = Right : 0.5 = Down : 0.75 = Left : -1 = Centred
DrawText ("D Pad = "+JoyHat#(0),0,320)

If JoyZ#(0)>0.01 
	DrawText ("Left Trigger "+JoyZ#(0),0,340)
EndIf
If JoyZ#(0)<-0.01
	DrawText ("Right Trigger "+JoyZ#(0),0,340)
EndIf

SetBlend(0)
EndIf
EndFunction

'Joypad/Gamepad Functions.

Function Gamepadinput_leftthumbstick() 'Movement
strafe=JoyX#(0)*(1) 'Change to (-1) to invert
If strafe<(0.2) And strafe>(-0.2) Then strafe=0.0 'Sets the dead zone range, from -1 to 1
move=JoyY#(0)*(-1) 'Change to (1) to invert
If move<(0.2) And move>(-0.2) Then move=0.0 'Sets the dead zone range, from -1 to 1
EndFunction

Function Gamepadinput_rightthumbstick() 'Mouselook
my = Curve(JoyR#(0)*25*(1), my, 6) 'Change to (-1) to invert : Change 25 to alter sensitivity
If JoyR#(0)<(0.2) And JoyR#(0)>(-0.2) Then my=0.0 'Sets the dead zone range, from -1 to 1
mx = Curve(JoyU#(0)*25*(1), mx, 6) 'Change to (-1) to invert : Change 25 to alter sensitivity
If JoyU#(0)<(0.2) And JoyU#(0)>(-0.2) Then mx=0.0 'Sets the dead zone range, from -1 to 1
EndFunction

'Example button use.
Function Gamepadinput_buttons()
If JoyHit(0,0) & Not ControllerAirborne(player) 'Jump using the Xbox360 contollers A button
	jump = 6.0
End If
If JoyDown(2,0) 'Run using the Xbox360 contollers X button
	move = move * 3.0
	strafe = strafe * 3.0
EndIf		
EndFunction
'------------------------------------------------------------------------

AMD Bulldozer FX-4 Quad Core 4100 Black Edition

2 x 4GB DDR3 1333Mhz Memory

Gigabyte GeForce GTX 550 Ti OC 1024MB GDDR5

Windows 7 Home 64 bit

 

BlitzMax 1.50 • Lua 5.1 MaxGUI 1.41 • UU3D Pro • MessiahStudio Pro • Silo Pro

3D Coat • ShaderMap Pro • Hexagon 2 • Photoshop, Gimp & Paint.NET

 

LE 2.5/3.4 • Skyline UE4 • CE3 SDK • Unity 5 • Esenthel Engine 2.0

 

Marleys Ghost's YouTube Channel Marleys Ghost's Blog

 

"I used to be alive like you .... then I took an arrow to the head"

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...