From e2b532c9560fa2772e2fee102da0b4e0dc57a845 Mon Sep 17 00:00:00 2001 From: KikooDX Date: Thu, 23 Dec 2021 11:52:37 +0100 Subject: [PATCH] save replay --- CMakeLists.txt | 2 +- inc/conf.h | 1 + inc/input.h | 6 ++++++ src/input.c | 31 +++++++++++++++++++++++++++++++ src/main.c | 2 ++ 5 files changed, 41 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6d7ee72..4a626ff 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,7 +5,7 @@ project(JTMM2 C) include(GenerateG3A) include(Fxconv) -find_package(Gint 2.6.0 REQUIRED) +find_package(Gint 2.6.0 REQUIRED) # dev branch include_directories(inc) diff --git a/inc/conf.h b/inc/conf.h index 85f169c..895e0aa 100644 --- a/inc/conf.h +++ b/inc/conf.h @@ -26,3 +26,4 @@ #define MISSILE_ACCEL 0.06f #define MISSILE_FRICTION (MISSILE_ACCEL / MISSILE_MAX_SPEED) #define MISSILE_COOLDOWN (TARGET_FPS / 2) +#define REPLAY_SIZE 18000 /* 5 minutes */ diff --git a/inc/input.h b/inc/input.h index 2896117..4495338 100644 --- a/inc/input.h +++ b/inc/input.h @@ -1,4 +1,5 @@ #pragma once +#include "conf.h" enum Key { K_LEFT, @@ -17,13 +18,18 @@ enum Key { }; enum KeyState { KS_UP, KS_DOWN, KS_PRESS }; +typedef unsigned char PackedFrame; + struct Input { enum Key keys[K_COUNT]; enum KeyState states[K_COUNT]; + int replay_cursor; + PackedFrame replay[REPLAY_SIZE]; }; void input_init(void); void input_update(void); +void input_write_replay(void); int input_down(enum Key); int input_pressed(enum Key); diff --git a/src/input.c b/src/input.c index 303f67a..17ede90 100644 --- a/src/input.c +++ b/src/input.c @@ -1,11 +1,16 @@ #include "input.h" +#include #include +#include +#include static struct Input input; static const int default_map[K_COUNT] = { KEY_LEFT, KEY_RIGHT, KEY_UP, KEY_DOWN, KEY_SHIFT, KEY_ALPHA, KEY_EXIT, KEY_TAN, KEY_F3, KEY_F2, KEY_F1, KEY_F6}; +static PackedFrame pack_frame(void); + void input_init(void) { @@ -15,6 +20,8 @@ input_init(void) input.keys[i] = default_map[i]; input.states[i] = KS_UP; } + i = REPLAY_SIZE; + input.replay_cursor = 0; } void @@ -28,6 +35,20 @@ input_update(void) ? (kdown ? KS_PRESS : KS_UP) : (kdown ? KS_DOWN : KS_UP); } + input.replay[input.replay_cursor++] = pack_frame(); + if (input.replay_cursor >= REPLAY_SIZE) + input.replay_cursor = REPLAY_SIZE - 1; +} + +void +input_write_replay(void) +{ + const int fd = open("jtmm2.rep", O_WRONLY | O_CREAT | O_TRUNC); + if (fd != -1) { + write(fd, input.replay, + input.replay_cursor * sizeof(PackedFrame)); + close(fd); + } } int @@ -47,3 +68,13 @@ input_up(enum Key k) { return input.states[k] == KS_UP; } + +static PackedFrame +pack_frame(void) +{ + PackedFrame r = 0; + int i = K_EXIT; + while (i-- > 0) + r |= input_down(i) << i; + return r; +} diff --git a/src/main.c b/src/main.c index d912f04..4937703 100644 --- a/src/main.c +++ b/src/main.c @@ -8,6 +8,7 @@ #include "util.h" #include #include +#include #include static struct Player player; @@ -72,6 +73,7 @@ deinit(void) { level_deinit(); missile_manager_free(); + gint_world_switch(GINT_CALL(input_write_replay)); timer_stop(timer); }