Setup game controller status on game state.

This commit is contained in:
2026-10-21 07:28:49 +02:00
parent 7fbdb9f5cf
commit 5038bc5673
3 changed files with 27 additions and 17 deletions
+9 -4
View File
@@ -49,7 +49,11 @@ Game :: struct {
// resolution state
window_w: i32,
window_h: i32,
res_index: int
res_index: int,
// Controller state.
controller_gid: i32,
controller_connected: bool,
}
// AO: lets me load this at compile time!
@@ -60,6 +64,7 @@ gamepad_compat := #load("../gamecontrollerdb.txt", string)
// ------------------------------------------------------------------------------
game_init :: proc(game: ^Game) {
rl.SetConfigFlags({.WINDOW_ALWAYS_RUN, .MSAA_4X_HINT, .WINDOW_RESIZABLE})
game.controller_gid = -1
game.res_index = 0
game.window_w = RESOLUTIONS[game.res_index].width
game.window_h = RESOLUTIONS[game.res_index].height
@@ -67,9 +72,10 @@ game_init :: proc(game: ^Game) {
rl.InitWindow(game.window_w, game.window_h, WINDOW_TITLE)
rl.SetTargetFPS(60)
rl.SetExitKey(.KEY_NULL)
rl.ToggleBorderlessWindowed()
rl.HideCursor()
game_set_resolution(game, game.res_index)
gamepad_cstr := strings.clone_to_cstring(gamepad_compat)
defer delete(gamepad_cstr)
rl.SetGamepadMappings(gamepad_cstr)
@@ -115,7 +121,7 @@ game_init :: proc(game: ^Game) {
game_update :: proc(game: ^Game) {
dt := rl.GetFrameTime()
input := poll_input()
input := poll_input(game)
// -------------------------------------------------------------------------
// Update Camera Shake
@@ -183,7 +189,6 @@ game_render :: proc(game: ^Game) {
render_game_to_window(game)
// when DEBUG do rl.DrawFPS(10, rl.GetScreenHeight() - 20)
rl.EndDrawing()
}
+18 -12
View File
@@ -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)
-1
View File
@@ -1,6 +1,5 @@
package game
import "core:strings"
import "core:fmt"
import rl "vendor:raylib"