Setup game controller status on game state.
This commit is contained in:
+9
-4
@@ -49,7 +49,11 @@ Game :: struct {
|
|||||||
// resolution state
|
// resolution state
|
||||||
window_w: i32,
|
window_w: i32,
|
||||||
window_h: 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!
|
// AO: lets me load this at compile time!
|
||||||
@@ -60,6 +64,7 @@ gamepad_compat := #load("../gamecontrollerdb.txt", string)
|
|||||||
// ------------------------------------------------------------------------------
|
// ------------------------------------------------------------------------------
|
||||||
game_init :: proc(game: ^Game) {
|
game_init :: proc(game: ^Game) {
|
||||||
rl.SetConfigFlags({.WINDOW_ALWAYS_RUN, .MSAA_4X_HINT, .WINDOW_RESIZABLE})
|
rl.SetConfigFlags({.WINDOW_ALWAYS_RUN, .MSAA_4X_HINT, .WINDOW_RESIZABLE})
|
||||||
|
game.controller_gid = -1
|
||||||
game.res_index = 0
|
game.res_index = 0
|
||||||
game.window_w = RESOLUTIONS[game.res_index].width
|
game.window_w = RESOLUTIONS[game.res_index].width
|
||||||
game.window_h = RESOLUTIONS[game.res_index].height
|
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.InitWindow(game.window_w, game.window_h, WINDOW_TITLE)
|
||||||
rl.SetTargetFPS(60)
|
rl.SetTargetFPS(60)
|
||||||
rl.SetExitKey(.KEY_NULL)
|
rl.SetExitKey(.KEY_NULL)
|
||||||
rl.ToggleBorderlessWindowed()
|
|
||||||
rl.HideCursor()
|
rl.HideCursor()
|
||||||
|
|
||||||
|
game_set_resolution(game, game.res_index)
|
||||||
|
|
||||||
gamepad_cstr := strings.clone_to_cstring(gamepad_compat)
|
gamepad_cstr := strings.clone_to_cstring(gamepad_compat)
|
||||||
defer delete(gamepad_cstr)
|
defer delete(gamepad_cstr)
|
||||||
rl.SetGamepadMappings(gamepad_cstr)
|
rl.SetGamepadMappings(gamepad_cstr)
|
||||||
@@ -115,7 +121,7 @@ game_init :: proc(game: ^Game) {
|
|||||||
|
|
||||||
game_update :: proc(game: ^Game) {
|
game_update :: proc(game: ^Game) {
|
||||||
dt := rl.GetFrameTime()
|
dt := rl.GetFrameTime()
|
||||||
input := poll_input()
|
input := poll_input(game)
|
||||||
|
|
||||||
// -------------------------------------------------------------------------
|
// -------------------------------------------------------------------------
|
||||||
// Update Camera Shake
|
// Update Camera Shake
|
||||||
@@ -183,7 +189,6 @@ game_render :: proc(game: ^Game) {
|
|||||||
|
|
||||||
render_game_to_window(game)
|
render_game_to_window(game)
|
||||||
|
|
||||||
|
|
||||||
// when DEBUG do rl.DrawFPS(10, rl.GetScreenHeight() - 20)
|
// when DEBUG do rl.DrawFPS(10, rl.GetScreenHeight() - 20)
|
||||||
rl.EndDrawing()
|
rl.EndDrawing()
|
||||||
}
|
}
|
||||||
|
|||||||
+18
-12
@@ -1,5 +1,6 @@
|
|||||||
package game
|
package game
|
||||||
|
|
||||||
|
import "core:strings"
|
||||||
import rl "vendor:raylib"
|
import rl "vendor:raylib"
|
||||||
|
|
||||||
// ------------------------------------------------------------------------------
|
// ------------------------------------------------------------------------------
|
||||||
@@ -21,35 +22,40 @@ Input_Event :: struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Currently active controller
|
// 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.
|
// is there currently a controller, if so check if its still connected.
|
||||||
if active_gamepad_id != -1 {
|
if game.controller_gid != -1 {
|
||||||
if rl.IsGamepadAvailable(active_gamepad_id) {
|
if rl.IsGamepadAvailable(game.controller_gid) {
|
||||||
// do nothing still connected
|
// do nothing still connected
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// else we lost connection
|
// else we lost connection
|
||||||
active_gamepad_id = -1
|
game.controller_gid = -1
|
||||||
rl.TraceLog(.WARNING, "Controller disconnected!!")
|
game.controller_connected = false
|
||||||
}
|
}
|
||||||
|
|
||||||
for i: i32 = 0; i < 4; i += 1 {
|
for i: i32 = 0; i < 4; i += 1 {
|
||||||
if rl.IsGamepadAvailable(i) {
|
if rl.IsGamepadAvailable(i) && is_real_pad(i) {
|
||||||
active_gamepad_id = i
|
game.controller_connected = true
|
||||||
|
game.controller_gid = i
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
poll_input :: proc() -> Input_Event {
|
poll_input :: proc(game: ^Game) -> Input_Event {
|
||||||
input_event := Input_Event{}
|
input_event := Input_Event{}
|
||||||
input_dir := rl.Vector2{0, 0}
|
input_dir := rl.Vector2{0, 0}
|
||||||
|
|
||||||
update_active_gamepad()
|
update_active_gamepad(game)
|
||||||
gamepad_active := active_gamepad_id != -1
|
gamepad_active := game.controller_connected
|
||||||
gid := active_gamepad_id
|
gid := game.controller_gid
|
||||||
|
|
||||||
// --------------------------------------------------------------------------
|
// --------------------------------------------------------------------------
|
||||||
// Continuous Input (keyboard + gamepad d-pad)
|
// Continuous Input (keyboard + gamepad d-pad)
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
package game
|
package game
|
||||||
|
|
||||||
import "core:strings"
|
|
||||||
import "core:fmt"
|
import "core:fmt"
|
||||||
import rl "vendor:raylib"
|
import rl "vendor:raylib"
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user