ohsp

Prototype for a game with dual thruster controls.
git clone git://git.amin.space/ohsp.git
Log | Files | Refs | LICENSE

commit 84f5223a8955eeb16ad3cdefd78119b352e367b4
parent e22b5a4ca66ff8f32a7553916f3448a8bc13821a
Author: amin <dev@aminmesbah.com>
Date:   Mon, 20 Nov 2017 04:09:30 +0000

Record 10 seconds of input and play it back

- r key toggles input recording
- l key toggles looped input playback

This was so easy to do!

FossilOrigin-Name: c531afd0e94d91352aec6daa0731140544769059d44d0dc6bec810f5baddce5c
Diffstat:
Msrc/platform_sdl.c | 77+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/platform_sdl.h | 16++++++++++++++++
2 files changed, 93 insertions(+), 0 deletions(-)

diff --git a/src/platform_sdl.c b/src/platform_sdl.c @@ -16,6 +16,7 @@ extern bool PAUSED; static struct SDLOffscreenBuffer global_back_buffer = {0}; +static struct SDLInputRecord input_record = {0}; struct SDLWindowDimension sdl_get_window_dimension(SDL_Window *window) @@ -116,6 +117,42 @@ bool sdl_handle_event(SDL_Event *event, struct GameControllerInput *controller_i { PAUSED = !PAUSED; } + else if (key_code == SDLK_r) + { + if (input_record.state == INACTIVE) + { + printf("input_record: RECORDING\n"); + input_record.record_head = 0; + input_record.state = RECORDING; + } + else if (input_record.state == RECORDING) + { + printf("input_record: INACTIVE\n"); + input_record.state = INACTIVE; + } + else + { + printf("ERROR: input_record invalid state\n"); + } + } + else if (key_code == SDLK_l) + { + if (input_record.state == INACTIVE) + { + printf("input_record: PLAYING_BACK\n"); + input_record.playback_head = 0; + input_record.state = PLAYING_BACK; + } + else if (input_record.state == PLAYING_BACK) + { + printf("input_record: INACTIVE\n"); + input_record.state = INACTIVE; + } + else + { + printf("ERROR: input_record invalid state\n"); + } + } } } break; @@ -275,6 +312,46 @@ int main(int argc, char *argv[]) } SDL_PumpEvents(); + switch (input_record.state) + { + case INACTIVE: {} break; + + case RECORDING: + { + // TODO: sdl_input_record(&controller_input); + if (input_record.record_head < INPUT_RECORD_SIZE) + { + input_record.input_buffer[input_record.record_head] = controller_input; + ++input_record.record_head; + } + else + { + printf("Input record buffer full\n"); + input_record.state = INACTIVE; + } + } break; + + case PLAYING_BACK: + { + if (input_record.playback_head < input_record.record_head) + { + // TODO: sdl_input_playback(&controller_input); + controller_input = input_record.input_buffer[input_record.playback_head]; + ++input_record.playback_head; + } + else + { + printf("Playback restarting\n"); + input_record.playback_head = 0; + } + } break; + + default: + { + printf("Unhandled SDLInputRecordState\n"); + } break; + } + dimension = sdl_get_window_dimension(window); struct OffscreenBuffer buffer = diff --git a/src/platform_sdl.h b/src/platform_sdl.h @@ -8,6 +8,7 @@ #define GAME_LIB_PATH "./release/game.so" #define MAX_CONTROLLERS 4 #define DEADZONE_THRESHOLD 9000 +#define INPUT_RECORD_SIZE (UPDATES_PER_SECOND * 10) // 10 seconds of input frames struct SDLOffscreenBuffer { @@ -34,5 +35,20 @@ struct SDLGameCode game_render_t *game_render; }; +enum SDLInputRecordState +{ + INACTIVE, + RECORDING, + PLAYING_BACK, +}; + +struct SDLInputRecord +{ + struct GameControllerInput input_buffer[INPUT_RECORD_SIZE]; + enum SDLInputRecordState state; + uint64_t record_head; + uint64_t playback_head; +}; + #define PLATFORM_SDL_H #endif