Setup game controller status on game state.
This commit is contained in:
+18
-12
@@ -1,5 +1,6 @@
|
||||
package game
|
||||
|
||||
import "core:strings"
|
||||
import rl "vendor:raylib"
|
||||
|
||||
// ------------------------------------------------------------------------------
|
||||
@@ -21,35 +22,40 @@ Input_Event :: struct {
|
||||
}
|
||||
|
||||
// Currently active controller
|
||||
active_gamepad_id: i32 = -1
|
||||
is_real_pad :: proc(i: i32) -> bool {
|
||||
return rl.IsGamepadAvailable(i) &&
|
||||
!strings.contains(string(rl.GetGamepadName(i)), "Mouse") &&
|
||||
!strings.contains(string(rl.GetGamepadName(i)), "Keyboard")
|
||||
}
|
||||
|
||||
update_active_gamepad :: proc() {
|
||||
update_active_gamepad :: proc(game: ^Game) {
|
||||
// is there currently a controller, if so check if its still connected.
|
||||
if active_gamepad_id != -1 {
|
||||
if rl.IsGamepadAvailable(active_gamepad_id) {
|
||||
if game.controller_gid != -1 {
|
||||
if rl.IsGamepadAvailable(game.controller_gid) {
|
||||
// do nothing still connected
|
||||
return
|
||||
}
|
||||
// else we lost connection
|
||||
active_gamepad_id = -1
|
||||
rl.TraceLog(.WARNING, "Controller disconnected!!")
|
||||
game.controller_gid = -1
|
||||
game.controller_connected = false
|
||||
}
|
||||
|
||||
for i: i32 = 0; i < 4; i += 1 {
|
||||
if rl.IsGamepadAvailable(i) {
|
||||
active_gamepad_id = i
|
||||
if rl.IsGamepadAvailable(i) && is_real_pad(i) {
|
||||
game.controller_connected = true
|
||||
game.controller_gid = i
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
poll_input :: proc() -> Input_Event {
|
||||
poll_input :: proc(game: ^Game) -> Input_Event {
|
||||
input_event := Input_Event{}
|
||||
input_dir := rl.Vector2{0, 0}
|
||||
|
||||
update_active_gamepad()
|
||||
gamepad_active := active_gamepad_id != -1
|
||||
gid := active_gamepad_id
|
||||
update_active_gamepad(game)
|
||||
gamepad_active := game.controller_connected
|
||||
gid := game.controller_gid
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
// Continuous Input (keyboard + gamepad d-pad)
|
||||
|
||||
Reference in New Issue
Block a user