WIP - Day 46 : Got new input system working

This commit is contained in:
Edward R. Gonzalez 2023-12-29 19:21:44 -05:00
parent 470e85ea3a
commit 467c2ee34b
7 changed files with 818 additions and 553 deletions

File diff suppressed because it is too large Load Diff

View File

@ -7,6 +7,9 @@
NS_ENGINE_BEGIN NS_ENGINE_BEGIN
// Max controllers for the platform layer and thus for all other layers is 4. (Sanity and xinput limit)
constexpr u32 Max_Controllers = 4;
struct DigitalBtn struct DigitalBtn
{ {
s32 half_transitions; s32 half_transitions;
@ -105,6 +108,7 @@ struct XInputPadState
}; };
}; };
}; };
using XInputPadStates = XInputPadState*[ Max_Controllers ];
struct DualsensePadState struct DualsensePadState
{ {
@ -137,6 +141,26 @@ struct DualsensePadState
}; };
}; };
}; };
using DualsensePadStates = DualsensePadState*[ Max_Controllers ];
#define NEW_INPUT_DESIGN 1
#if NEW_INPUT_DESIGN
struct InputStateSnapshot
{
KeyboardState keyboard;
MousesState mouse;
XInputPadState xpads [ Max_Controllers ];
DualsensePadState ds_pads[ Max_Controllers ];
};
#else
struct ControllerStateSnapshot
{
KeyboardState keyboard;
MousesState mouse;
XInputPadState xpad;
DualsensePadState ds_pad;
};
struct ControllerState struct ControllerState
{ {
@ -146,23 +170,23 @@ struct ControllerState
DualsensePadState* ds_pad; DualsensePadState* ds_pad;
}; };
struct ControllerStateSnapshot
{
KeyboardState keyboard;
MousesState mouse;
XInputPadState xpad;
DualsensePadState ds_pad;
};
struct InputState
{
ControllerState controllers[4];
};
struct InputStateSnapshot struct InputStateSnapshot
{ {
ControllerStateSnapshot controllers[4]; ControllerStateSnapshot controllers[4];
}; };
#endif
struct InputState
{
#if NEW_INPUT_DESIGN
KeyboardState* keyboard;
MousesState* mouse;
XInputPadStates xpads;
DualsensePadStates ds_pads;
#else
ControllerState controllers[4];
#endif
};
using InputBindCallback = void( void* ); using InputBindCallback = void( void* );
using InputBindCallback_DigitalBtn = void( engine::DigitalBtn* button ); using InputBindCallback_DigitalBtn = void( engine::DigitalBtn* button );

View File

@ -6,6 +6,7 @@ Doing my best to follow his advice to leave cleaning to when things are more "ce
*/ */
#if INTELLISENSE_DIRECTIVES #if INTELLISENSE_DIRECTIVES
#include "engine.hpp" #include "engine.hpp"
#include "input.hpp"
#endif #endif
NS_ENGINE_BEGIN NS_ENGINE_BEGIN
@ -105,6 +106,26 @@ void end_playback_input( Memory* memory, InputState* input, platform::ModuleAPI*
InputStateSnapshot input_state_snapshot( InputState* input ) InputStateSnapshot input_state_snapshot( InputState* input )
{ {
#if NEW_INPUT_DESIGN
InputStateSnapshot snapshot = {};
if ( input->keyboard )
snapshot.keyboard = * input->keyboard;
if ( input->mouse )
snapshot.mouse = * input->mouse;
for ( s32 idx = 0; idx < Max_Controllers; ++ idx )
{
XInputPadState* xpad = input->xpads[ idx ];
if ( xpad )
snapshot.xpads[ idx ] = * xpad;
DualsensePadState* ds_pad = input->ds_pads[ idx ];
if ( ds_pad )
snapshot.ds_pads[ idx ] = * ds_pad;
}
return snapshot;
#else
InputStateSnapshot snapshot = {}; InputStateSnapshot snapshot = {};
for ( s32 idx = 0; idx < array_count( snapshot.controllers ); ++ idx ) for ( s32 idx = 0; idx < array_count( snapshot.controllers ); ++ idx )
{ {
@ -113,20 +134,19 @@ InputStateSnapshot input_state_snapshot( InputState* input )
continue; continue;
if ( controller->ds_pad ) if ( controller->ds_pad )
snapshot.controllers[idx].ds_pad = *controller->ds_pad; snapshot.controllers[idx].ds_pad = *controller->ds_pad;
if ( controller->xpad ) if ( controller->xpad )
snapshot.controllers[idx].xpad = *controller->xpad; snapshot.controllers[idx].xpad = *controller->xpad;
if ( controller->keyboard ) if ( controller->keyboard )
{
snapshot.controllers[idx].keyboard = *controller->keyboard; snapshot.controllers[idx].keyboard = *controller->keyboard;
}
if ( controller->mouse ) if ( controller->mouse )
snapshot.controllers[idx].mouse = *controller->mouse; snapshot.controllers[idx].mouse = *controller->mouse;
} }
return snapshot; return snapshot;
#endif
} }
internal internal
@ -150,17 +170,35 @@ void play_input( SnapshotFn* load_snapshot, Memory* memory, InputState* input, p
return; return;
} }
#if NEW_INPUT_DESIGN
if ( input->keyboard )
* input->keyboard = new_input.keyboard;
if ( input->mouse )
* input->mouse = new_input.mouse;
for ( s32 idx = 0; idx < Max_Controllers; ++ idx )
{
XInputPadState* xpad = input->xpads[ idx ];
if ( xpad )
* xpad = new_input.xpads[ idx ];
DualsensePadState* ds_pad = input->ds_pads[ idx ];
if ( ds_pad )
* ds_pad = new_input.ds_pads[ idx ];
}
#else
for ( s32 idx = 0; idx < array_count( new_input.controllers ); ++ idx ) for ( s32 idx = 0; idx < array_count( new_input.controllers ); ++ idx )
{ {
ControllerState* controller = & input->controllers[idx]; ControllerState* controller = & input->controllers[idx];
if ( controller == nullptr ) if ( controller == nullptr )
continue; continue;
if ( controller->ds_pad ) if ( controller->ds_pad )
*controller->ds_pad = new_input.controllers[idx].ds_pad; *controller->ds_pad = new_input.controllers[idx].ds_pad;
if ( controller->xpad ) if ( controller->xpad )
*controller->xpad = new_input.controllers[idx].xpad; *controller->xpad = new_input.controllers[idx].xpad;
if ( controller->keyboard ) if ( controller->keyboard )
{ {
@ -168,8 +206,9 @@ void play_input( SnapshotFn* load_snapshot, Memory* memory, InputState* input, p
} }
if ( controller->mouse ) if ( controller->mouse )
*controller->mouse = new_input.controllers[idx].mouse; *controller->mouse = new_input.controllers[idx].mouse;
} }
#endif
} }
void process_loop_mode( SnapshotFn* take_snapshot, SnapshotFn* load_snapshot void process_loop_mode( SnapshotFn* take_snapshot, SnapshotFn* load_snapshot

View File

@ -172,7 +172,6 @@ void TileMap_set_tile_value( MemoryArena* arena, TileMap* tile_map, s32 abs_tile
TileChunk_set_tile_value( chunk, tile_map, chunk_pos.tile_x, chunk_pos.tile_y, value ); TileChunk_set_tile_value( chunk, tile_map, chunk_pos.tile_x, chunk_pos.tile_y, value );
} }
internal internal
b32 TileMap_are_on_same_tile( TileMapPos* pos_a, TileMapPos* pos_b ) b32 TileMap_are_on_same_tile( TileMapPos* pos_a, TileMapPos* pos_b )
{ {

View File

@ -61,36 +61,15 @@ struct ActionableMode
Player : Controller, Actionables, ActionSets Player : Controller, Actionables, ActionSets
*/ */
struct Player #if NEW_INPUT_DESIGN
struct ControllerState
{ {
// So far just has an assigned controller. engine::KeyboardState* keyboard;
engine::ControllerState* controller; engine::MousesState* mouse;
engine::XInputPadState* xpad;
// Possilby some other stuff in the future. engine::DualsensePadState* ds_pad;
};
struct PlayerState
{
f32 width;
f32 height;
engine::TileMapPos position;
Vel2 move_velocity;
b32 mid_jump;
f32 jump_time;
};
struct PlayerActions
{
s32 player_x_move_digital;
s32 player_y_move_digital;
f32 player_x_move_analog;
f32 player_y_move_analog;
b32 sprint;
b32 jump;
}; };
#endif
enum EHeroBitmapsDirection : u32 enum EHeroBitmapsDirection : u32
{ {
@ -112,9 +91,52 @@ struct HeroBitmaps
Bitmap torso; Bitmap torso;
}; };
struct PlayerState
{
f32 width;
f32 height;
engine::TileMapPos position;
Vel2 move_velocity;
b32 mid_jump;
f32 jump_time;
EHeroBitmapsDirection hero_direction;
};
struct PlayerActions
{
s32 player_x_move_digital;
s32 player_y_move_digital;
f32 player_x_move_analog;
f32 player_y_move_analog;
b32 sprint;
b32 jump;
b32 join;
};
struct Player
{
#if NEW_INPUT_DESIGN
// So far just has an assigned controller.
ControllerState controller;
#else
engine::ControllerState* controller;
#endif
PlayerState state;
};
struct GameState struct GameState
{ {
Player player_1;
Player player_2;
PlayerState player_state; PlayerState player_state;
PlayerState player_state_2;
using Bitmap = engine::Bitmap; using Bitmap = engine::Bitmap;
@ -127,7 +149,6 @@ struct GameState
engine::TileMapPos camera_pos; engine::TileMapPos camera_pos;
EHeroBitmapsDirection hero_direction;
HeroBitmaps hero_bitmaps[4]; HeroBitmaps hero_bitmaps[4];
}; };

View File

@ -9,12 +9,9 @@
NS_PLATFORM_BEGIN NS_PLATFORM_BEGIN
using namespace win32; using namespace win32;
// Max controllers for the platform layer and thus for all other layers is 4. (Sanity and xinput limit)
constexpr u32 Max_Controllers = 4;
using JSL_DeviceHandle = int; using JSL_DeviceHandle = int;
using EngineXInputPadStates = engine::XInputPadState[ Max_Controllers ]; using EngineXInputPadStates = engine::XInputPadState [ engine::Max_Controllers ];
using EngineDSPadStates = engine::DualsensePadState[Max_Controllers]; using EngineDSPadStates = engine::DualsensePadState[ engine::Max_Controllers ];
internal void internal void
input_process_digital_btn( engine::DigitalBtn* old_state, engine::DigitalBtn* new_state, u32 raw_btns, u32 btn_flag ) input_process_digital_btn( engine::DigitalBtn* old_state, engine::DigitalBtn* new_state, u32 raw_btns, u32 btn_flag )
@ -100,7 +97,11 @@ poll_input( HWND window_handle, engine::InputState* input, u32 jsl_num_devices,
input_process_digital_btn( & old_keyboard->left_shift, & new_keyboard->left_shift, GetAsyncKeyState( VK_LSHIFT ), is_down ); input_process_digital_btn( & old_keyboard->left_shift, & new_keyboard->left_shift, GetAsyncKeyState( VK_LSHIFT ), is_down );
input_process_digital_btn( & old_keyboard->right_shift, & new_keyboard->right_shift, GetAsyncKeyState( VK_RSHIFT ), is_down ); input_process_digital_btn( & old_keyboard->right_shift, & new_keyboard->right_shift, GetAsyncKeyState( VK_RSHIFT ), is_down );
#if NEW_INPUT_DESIGN
input->keyboard = new_keyboard;
#else
input->controllers[0].keyboard = new_keyboard; input->controllers[0].keyboard = new_keyboard;
#endif
} }
// Mouse polling // Mouse polling
@ -122,12 +123,16 @@ poll_input( HWND window_handle, engine::InputState* input, u32 jsl_num_devices,
new_mouse->X.end = (f32)mouse_pos.x; new_mouse->X.end = (f32)mouse_pos.x;
new_mouse->Y.end = (f32)mouse_pos.y; new_mouse->Y.end = (f32)mouse_pos.y;
#if NEW_INPUT_DESIGN
input->mouse = new_mouse;
#else
input->controllers[0].mouse = new_mouse; input->controllers[0].mouse = new_mouse;
#endif
} }
// XInput Polling // XInput Polling
// TODO(Ed) : Should we poll this more frequently? // TODO(Ed) : Should we poll this more frequently?
for ( DWORD controller_index = 0; controller_index < Max_Controllers; ++ controller_index ) for ( DWORD controller_index = 0; controller_index < engine::Max_Controllers; ++ controller_index )
{ {
XINPUT_STATE controller_state; XINPUT_STATE controller_state;
b32 xinput_detected = xinput_get_state( controller_index, & controller_state ) == XI_PluggedIn; b32 xinput_detected = xinput_get_state( controller_index, & controller_state ) == XI_PluggedIn;
@ -136,9 +141,9 @@ poll_input( HWND window_handle, engine::InputState* input, u32 jsl_num_devices,
XINPUT_GAMEPAD* xpad = & controller_state.Gamepad; XINPUT_GAMEPAD* xpad = & controller_state.Gamepad;
engine::XInputPadState* old_xpad = old_xpads[ controller_index ]; engine::XInputPadState* old_xpad = old_xpads[ controller_index ];
engine::XInputPadState* new_xpad = new_xpads[ controller_index ]; engine::XInputPadState* new_xpad = new_xpads[ controller_index ];
input_process_digital_btn( & old_xpad->dpad.up, & new_xpad->dpad.up, xpad->wButtons, XINPUT_GAMEPAD_DPAD_UP ); input_process_digital_btn( & old_xpad->dpad.up, & new_xpad->dpad.up, xpad->wButtons, XINPUT_GAMEPAD_DPAD_UP );
input_process_digital_btn( & old_xpad->dpad.down, & new_xpad->dpad.down, xpad->wButtons, XINPUT_GAMEPAD_DPAD_DOWN ); input_process_digital_btn( & old_xpad->dpad.down, & new_xpad->dpad.down, xpad->wButtons, XINPUT_GAMEPAD_DPAD_DOWN );
input_process_digital_btn( & old_xpad->dpad.left, & new_xpad->dpad.left, xpad->wButtons, XINPUT_GAMEPAD_DPAD_LEFT ); input_process_digital_btn( & old_xpad->dpad.left, & new_xpad->dpad.left, xpad->wButtons, XINPUT_GAMEPAD_DPAD_LEFT );
input_process_digital_btn( & old_xpad->dpad.right, & new_xpad->dpad.right, xpad->wButtons, XINPUT_GAMEPAD_DPAD_RIGHT ); input_process_digital_btn( & old_xpad->dpad.right, & new_xpad->dpad.right, xpad->wButtons, XINPUT_GAMEPAD_DPAD_RIGHT );
input_process_digital_btn( & old_xpad->Y, & new_xpad->Y, xpad->wButtons, XINPUT_GAMEPAD_Y ); input_process_digital_btn( & old_xpad->Y, & new_xpad->Y, xpad->wButtons, XINPUT_GAMEPAD_Y );
@ -166,11 +171,19 @@ poll_input( HWND window_handle, engine::InputState* input, u32 jsl_num_devices,
new_xpad->stick.left.X.average = left_x; new_xpad->stick.left.X.average = left_x;
new_xpad->stick.left.Y.average = left_y; new_xpad->stick.left.Y.average = left_y;
#if NEW_INPUT_DESIGN
input->xpads[ controller_index ] = new_xpad;
#else
input->controllers[ controller_index ].xpad = new_xpad; input->controllers[ controller_index ].xpad = new_xpad;
#endif
} }
else else
{ {
#if NEW_INPUT_DESIGN
input->xpads[ controller_index ] = nullptr;
#else
input->controllers[ controller_index ].xpad = nullptr; input->controllers[ controller_index ].xpad = nullptr;
#endif
} }
} }
@ -180,6 +193,12 @@ poll_input( HWND window_handle, engine::InputState* input, u32 jsl_num_devices,
if ( ! JslStillConnected( jsl_device_handles[ jsl_device_index ] ) ) if ( ! JslStillConnected( jsl_device_handles[ jsl_device_index ] ) )
{ {
OutputDebugStringA( "Error: JSLStillConnected returned false\n" ); OutputDebugStringA( "Error: JSLStillConnected returned false\n" );
#if NEW_INPUT_DESIGN
input->ds_pads[ jsl_device_index ] = nullptr;
#else
input->controllers[ jsl_device_index ].ds_pad = nullptr;
#endif
continue; continue;
} }
@ -225,7 +244,11 @@ poll_input( HWND window_handle, engine::InputState* input, u32 jsl_num_devices,
new_ds_pad->stick.left.X.average = left_x; new_ds_pad->stick.left.X.average = left_x;
new_ds_pad->stick.left.Y.average = left_y; new_ds_pad->stick.left.Y.average = left_y;
#if NEW_INPUT_DESIGN
input->ds_pads[ jsl_device_index ] = new_ds_pad;
#else
input->controllers[ jsl_device_index ].ds_pad = new_ds_pad; input->controllers[ jsl_device_index ].ds_pad = new_ds_pad;
#endif
} }
} }

View File

@ -962,7 +962,7 @@ WinMain( HINSTANCE instance, HINSTANCE prev_instance, LPSTR commandline, int sho
mouse_states[0] = {}; mouse_states[0] = {};
mouse_states[0] = {}; mouse_states[0] = {};
for ( s32 id = 0; id < Max_Controllers; ++ id ) for ( s32 id = 0; id < engine::Max_Controllers; ++ id )
{ {
xpad_states[0][ id ] = {}; xpad_states[0][ id ] = {};
xpad_states[1][ id ] = {}; xpad_states[1][ id ] = {};