Skip to content

Instantly share code, notes, and snippets.

@hypernewbie
Created October 22, 2019 20:36
Show Gist options
  • Save hypernewbie/052637c52de5984b6e7b40d59ada5463 to your computer and use it in GitHub Desktop.
Save hypernewbie/052637c52de5984b6e7b40d59ada5463 to your computer and use it in GitHub Desktop.
Hacky replay record for XBox One layout controller input.
/*
The MIT License (MIT) Copyright (c) 2019 Xi Chen
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial
portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#pragma once
#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <vector>
#include <string>
#include <functional>
#ifndef REPLAY_CONTROLLER_TEST
#define REPLAY_CONTROLLER_TEST false
#endif // REPLAY_CONTROLLER_TEST
struct Replay_Event {
float time;
int type;
int idx;
float value;
};
struct Replay_State {
bool writing = false;
float time = 0.0f;
float lastWriteTime = 0.0f;
int currentReplayIdx = 0;
std::string fileName;
std::vector< Replay_Event > ev;
std::function< void( float ) > bindingsAxis[5];
std::function< void( void ) > bindingsButton[10][2];
std::function< void( void ) > bindingsHat[9];
};
const int VX_RLY_EVENT_AXIS = 0;
const int VX_RLY_EVENT_BUTTON = 0;
const int VX_RLY_EVENT_HAT = 0;
// This mirrors XB3 controller layout!
//
const int VX_RLY_AXIS_X = 0;
const int VX_RLY_AXIS_Y = 1;
const int VX_RLY_AXIS_Z = 2;
const int VX_RLY_ROT_X = 3;
const int VX_RLY_ROT_Y = 4;
const int VX_RLY_BUTTON_A = 0;
const int VX_RLY_BUTTON_B = 1;
const int VX_RLY_BUTTON_X = 2;
const int VX_RLY_BUTTON_Y = 3;
const int VX_RLY_BUTTON_L1 = 4;
const int VX_RLY_BUTTON_L2 = 5;
const int VX_RLY_BUTTON_SELECT = 6;
const int VX_RLY_BUTTON_START = 7;
const int VX_RLY_BUTTON_L3 = 8;
const int VX_RLY_BUTTON_R3 = 9;
const int VX_RLY_HAT_NONE = 0;
const int VX_RLY_HAT_UP = 1;
const int VX_RLY_HAT_UPRIGHT = 2;
const int VX_RLY_HAT_RIGHT = 3;
const int VX_RLY_HAT_DOWNRIGHT = 4;
const int VX_RLY_HAT_DOWN = 5;
const int VX_RLY_HAT_DOWNLEFT = 6;
const int VX_RLY_HAT_LEFT = 7;
const int VX_RLY_HAT_UPLEFT = 8;
inline void Replay_Record_Axis( Replay_State *state, int axis, float v )
{
Replay_Event e;
e.type = VX_RLY_EVENT_AXIS;
e.idx = axis;
e.value = v;
e.time = state->time;
state->ev.push_back( e );
}
inline void Replay_Record_Button( Replay_State *state, int button, bool press )
{
Replay_Event e;
e.type = VX_RLY_EVENT_BUTTON;
e.idx = button;
e.value = press ? 1.0f : 0.0f;
e.time = state->time;
state->ev.push_back( e );
}
inline void Replay_Record_Hat( Replay_State *state, int value )
{
Replay_Event e;
e.type = VX_RLY_EVENT_HAT;
e.idx = value;
e.value = float( value );
e.time = state->time;
state->ev.push_back( e );
}
inline void Replay_InitRecord( Replay_State *state, std::string fileName )
{
state->writing = true;
state->fileName = fileName;
state->time = 0.0f;
state->ev.clear();
FILE *fp = fopen( state->fileName.c_str(), "wb" );
fclose( fp );
}
inline void Replay_BindAxis( Replay_State *state, int axis, std::function< void( float ) > callback )
{
state->bindingsAxis[ axis ] = callback;
}
inline void Replay_BindButton( Replay_State *state, int button, bool press, std::function< void( void ) > callback )
{
state->bindingsButton[ button ][ press ? 1 : 0 ] = callback;
}
inline void Replay_BindAxis( Replay_State *state, int hatValue, std::function< void( void ) > callback )
{
state->bindingsHat[ hatValue ] = callback;
}
inline void Replay_InitReplay( Replay_State *state, std::string fileName )
{
state->writing = false;
state->fileName = fileName;
state->time = 0.0f;
state->currentReplayIdx = 0;
state->ev.clear();
FILE *fp = fopen( state->fileName.c_str(), "rb" );
if ( !fp ) return;
while ( !feof( fp ) ) {
Replay_Event e;
fread( &e, sizeof( Replay_Event ), 1, fp );
state->ev.push_back( e );
}
fclose( fp );
}
inline void Replay_Flush( Replay_State *state )
{
if ( state->writing ) {
if ( state->ev.size() > 0 ) {
FILE *fp = fopen( state->fileName.c_str(), "ab" );
assert( fp );
fwrite( state->ev.data(), sizeof( Replay_Event ), state->ev.size(), fp );
fclose( fp );
state->ev.clear();
}
state->lastWriteTime = state->time;
}
}
inline void Replay_Step( Replay_State *state, float deltaTime )
{
state->time += deltaTime;
if ( state->writing ) {
float timeSinceLastWrite = state->time - state->lastWriteTime;
if ( timeSinceLastWrite > 0.2f ) {
// Write at 5 FPS.
Replay_Flush( state );
}
return;
}
if ( state->currentReplayIdx >= state->ev.size() )
return;
auto e = state->ev[ state->currentReplayIdx ];
while ( state->time >= e.time ) {
if ( e.type == VX_RLY_EVENT_AXIS ) {
if ( state->bindingsAxis[ e.idx ] )
state->bindingsAxis[ e.idx ]( e.value );
} else if ( e.type == VX_RLY_EVENT_BUTTON ) {
if ( state->bindingsButton[ e.idx ][ ( e.value < 0.5f ) ? 0 : 1 ] )
state->bindingsButton[ e.idx ][ ( e.value < 0.5f ) ? 0 : 1 ]();
} else {
if ( state->bindingsHat[ e.idx ] )
state->bindingsHat[ e.idx ]();
}
state->currentReplayIdx++;
if ( state->currentReplayIdx >= state->ev.size() )
break;
e = state->ev[ state->currentReplayIdx ];
}
}
#if REPLAY_CONTROLLER_TEST
void Replay_Test() {
Replay_State state;
// Test record.
Replay_InitRecord( &state, "replay_test.txt" );
Replay_Record_Axis( &state, VX_RLY_AXIS_X, 0.45f );
Replay_Record_Axis( &state, VX_RLY_AXIS_Y, 0.54f );
for ( int i = 0; i < 60; i++ ) {
Replay_Step( &state, 1.0f );
Replay_Record_Axis( &state, VX_RLY_AXIS_Z, 0.05f + i * 0.01f );
}
Replay_Record_Axis( &state, VX_RLY_AXIS_Z, 0.65f );
Replay_Record_Button( &state, VX_RLY_BUTTON_X, true );
for ( int i = 0; i < 10; i++ ) {
Replay_Step( &state, 1.0f );
Replay_Record_Axis( &state, VX_RLY_AXIS_Z, 0.05f + i * 0.01f );
}
Replay_Record_Button( &state, VX_RLY_BUTTON_X, false );
Replay_Record_Button( &state, VX_RLY_BUTTON_Y, true );
Replay_Record_Hat( &state, VX_RLY_HAT_LEFT );
for ( int i = 0; i < 10; i++ ) {
Replay_Step( &state, 1.0f );
}
// Test replay.
static int eventCount = 0;
Replay_InitReplay( &state, "replay_test.txt" );
Replay_BindAxis( &state, VX_RLY_AXIS_X, [&]( float v ){ printf( "time %.4f: X axis value %f\n", state.time, v ); } );
Replay_BindAxis( &state, VX_RLY_AXIS_Y, [&]( float v ){ printf( "time %.4f: Y axis value %f\n", state.time, v ); } );
Replay_BindAxis( &state, VX_RLY_AXIS_Z, [&]( float v ){ printf( "time %.4f: Z axis value %f\n", state.time, v ); } );
for ( int i = 0; i < 90; i++ ) {
Replay_Step( &state, 1.0f );
}
}
#endif // REPLAY_CONTROLLER_TEST
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment